|
| 1 | +/* |
| 2 | + This example demonstrates how SQLite behaves |
| 3 | + when memory is low. |
| 4 | + It shows how heap defragmentation causes |
| 5 | + out of memory and how to avoid it. |
| 6 | + At first it asks how much memory to occupy |
| 7 | + so as not be available to SQLite. Then |
| 8 | + tries to insert huge number of records |
| 9 | + and shows how much free memory available |
| 10 | + after each insert. |
| 11 | +*/ |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <sqlite3.h> |
| 15 | +#include <SPI.h> |
| 16 | +#include <FS.h> |
| 17 | +#include "SD_MMC.h" |
| 18 | + |
| 19 | +char *dat = NULL; |
| 20 | +void block_heap(int times) { |
| 21 | + |
| 22 | + while (times--) { |
| 23 | + dat = (char *) malloc(4096); |
| 24 | + } |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +const char* data = "Callback function called"; |
| 29 | +static int callback(void *data, int argc, char **argv, char **azColName){ |
| 30 | + int i; |
| 31 | + Serial.printf("%s: ", (const char*)data); |
| 32 | + for (i = 0; i<argc; i++){ |
| 33 | + Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
| 34 | + } |
| 35 | + Serial.printf("\n"); |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +int openDb(const char *filename, sqlite3 **db) { |
| 40 | + int rc = sqlite3_open(filename, db); |
| 41 | + if (rc) { |
| 42 | + Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db)); |
| 43 | + return rc; |
| 44 | + } else { |
| 45 | + Serial.printf("Opened database successfully\n"); |
| 46 | + } |
| 47 | + return rc; |
| 48 | +} |
| 49 | + |
| 50 | +char *zErrMsg = 0; |
| 51 | +int db_exec(sqlite3 *db, const char *sql) { |
| 52 | + Serial.println(sql); |
| 53 | + long start = micros(); |
| 54 | + int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg); |
| 55 | + if (rc != SQLITE_OK) { |
| 56 | + Serial.printf("SQL error: %s\n", zErrMsg); |
| 57 | + sqlite3_free(zErrMsg); |
| 58 | + } else { |
| 59 | + Serial.printf("Operation done successfully\n"); |
| 60 | + } |
| 61 | + Serial.print(F("Time taken:")); |
| 62 | + Serial.println(micros()-start); |
| 63 | + return rc; |
| 64 | +} |
| 65 | + |
| 66 | +int input_string(char *str, int max_len) { |
| 67 | + max_len--; |
| 68 | + int ctr = 0; |
| 69 | + str[ctr] = 0; |
| 70 | + while (str[ctr] != '\n') { |
| 71 | + if (Serial.available()) { |
| 72 | + str[ctr] = Serial.read(); |
| 73 | + if (str[ctr] >= ' ' && str[ctr] <= '~') |
| 74 | + ctr++; |
| 75 | + if (ctr >= max_len) |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + str[ctr] = 0; |
| 80 | + Serial.println(str); |
| 81 | +} |
| 82 | + |
| 83 | +int input_num() { |
| 84 | + char in[20]; |
| 85 | + int ctr = 0; |
| 86 | + in[ctr] = 0; |
| 87 | + while (in[ctr] != '\n') { |
| 88 | + if (Serial.available()) { |
| 89 | + in[ctr] = Serial.read(); |
| 90 | + if (in[ctr] >= '0' && in[ctr] <= '9') |
| 91 | + ctr++; |
| 92 | + if (ctr >= sizeof(in)) |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + in[ctr] = 0; |
| 97 | + int ret = atoi(in); |
| 98 | + Serial.println(ret); |
| 99 | + return ret; |
| 100 | +} |
| 101 | + |
| 102 | +void displayPrompt(const char *title) { |
| 103 | + Serial.print(F("Enter ")); |
| 104 | + Serial.println(title); |
| 105 | +} |
| 106 | + |
| 107 | +void displayFreeHeap() { |
| 108 | + Serial.printf("\nHeap size: %d\n", ESP.getHeapSize()); |
| 109 | + Serial.printf("Free Heap: %d\n", esp_get_free_heap_size()); |
| 110 | + Serial.printf("Min Free Heap: %d\n", esp_get_minimum_free_heap_size()); |
| 111 | + Serial.printf("Max Alloc Heap: %d\n", ESP.getMaxAllocHeap()); |
| 112 | +} |
| 113 | + |
| 114 | +char *random_strings[] = {"Hello world", "Have a nice day", "Testing memory problems", "This should work", "ESP32 has 512k RAM", "ESP8266 has only 36k user RAM", |
| 115 | + "A stitch in time saves nine", "Needle in a haystack", "Too many strings", "I am done"}; |
| 116 | +char sql[1024]; |
| 117 | +sqlite3 *db1; |
| 118 | +sqlite3_stmt *res; |
| 119 | +const char *tail; |
| 120 | +int rc; |
| 121 | + |
| 122 | +void setup() { |
| 123 | + Serial.begin(115200); |
| 124 | + |
| 125 | + randomSeed(analogRead(0)); |
| 126 | + |
| 127 | + SPI.begin(); |
| 128 | + SD_MMC.begin(); |
| 129 | + |
| 130 | + displayFreeHeap(); |
| 131 | + displayPrompt("No. of 4k heap to block:"); |
| 132 | + block_heap(input_num()); |
| 133 | + displayFreeHeap(); |
| 134 | + |
| 135 | + sqlite3_initialize(); |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +void loop() { |
| 140 | + |
| 141 | + // Open database 1 |
| 142 | + if (openDb("/sdcard/test_bulk_insert.db", &db1)) |
| 143 | + return; |
| 144 | + |
| 145 | + displayFreeHeap(); |
| 146 | + |
| 147 | + rc = db_exec(db1, "CREATE TABLE IF NOT EXISTS test (c1 INTEGER, c2, c3, c4, c5 INTEGER, c6 INTEGER, c7, c8, c9 DATETIME, c10 DATETIME, c11 INTEGER )"); |
| 148 | + if (rc != SQLITE_OK) { |
| 149 | + sqlite3_close(db1); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + displayFreeHeap(); |
| 154 | + |
| 155 | + int rec_count; |
| 156 | + displayPrompt("No. of records to insert:"); |
| 157 | + rec_count = input_num(); |
| 158 | + char *sql = "INSERT INTO test VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
| 159 | + rc = sqlite3_prepare_v2(db1, sql, strlen(sql), &res, &tail); |
| 160 | + if (rc != SQLITE_OK) { |
| 161 | + Serial.printf("ERROR preparing sql: %s\n", sqlite3_errmsg(db1)); |
| 162 | + sqlite3_close(db1); |
| 163 | + return; |
| 164 | + } |
| 165 | + char *value; |
| 166 | + while (rec_count--) { |
| 167 | + sqlite3_bind_int(res, 1, random(65535)); |
| 168 | + value = random_strings[random(10)]; |
| 169 | + sqlite3_bind_text(res, 2, value, strlen(value), SQLITE_STATIC); |
| 170 | + value = random_strings[random(10)]; |
| 171 | + sqlite3_bind_text(res, 3, value, strlen(value), SQLITE_STATIC); |
| 172 | + value = random_strings[random(10)]; |
| 173 | + sqlite3_bind_text(res, 4, value, strlen(value), SQLITE_STATIC); |
| 174 | + sqlite3_bind_int(res, 5, random(65535)); |
| 175 | + sqlite3_bind_int(res, 6, random(65535)); |
| 176 | + value = random_strings[random(10)]; |
| 177 | + sqlite3_bind_text(res, 7, value, strlen(value), SQLITE_STATIC); |
| 178 | + value = random_strings[random(10)]; |
| 179 | + sqlite3_bind_text(res, 8, value, strlen(value), SQLITE_STATIC); |
| 180 | + sqlite3_bind_int(res, 9, random(100000000L)); |
| 181 | + sqlite3_bind_int(res, 10, random(100000000L)); |
| 182 | + sqlite3_bind_int(res, 11, random(65535)); |
| 183 | + if (sqlite3_step(res) != SQLITE_DONE) { |
| 184 | + Serial.printf("ERROR executing stmt: %s\n", sqlite3_errmsg(db1)); |
| 185 | + sqlite3_close(db1); |
| 186 | + return; |
| 187 | + } |
| 188 | + sqlite3_clear_bindings(res); |
| 189 | + rc = sqlite3_reset(res); |
| 190 | + if (rc != SQLITE_OK) { |
| 191 | + sqlite3_close(db1); |
| 192 | + return; |
| 193 | + } |
| 194 | + displayFreeHeap(); |
| 195 | + } |
| 196 | + sqlite3_finalize(res); |
| 197 | + Serial.write("\n"); |
| 198 | + |
| 199 | + rc = db_exec(db1, "Select count(*) from test"); |
| 200 | + if (rc != SQLITE_OK) { |
| 201 | + sqlite3_close(db1); |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + sqlite3_close(db1); |
| 206 | + displayFreeHeap(); |
| 207 | + |
| 208 | + displayPrompt("Press enter to continue:"); |
| 209 | + input_num(); |
| 210 | + |
| 211 | +} |
| 212 | + |
0 commit comments