memstat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ** 2018-09-27
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. **
  13. ** This file demonstrates an eponymous virtual table that returns information
  14. ** from sqlite3_status64() and sqlite3_db_status().
  15. **
  16. ** Usage example:
  17. **
  18. ** .load ./memstat
  19. ** .mode quote
  20. ** .header on
  21. ** SELECT * FROM memstat;
  22. */
  23. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_MEMSTATVTAB)
  24. #if !defined(SQLITEINT_H)
  25. #include "sqlite3ext.h"
  26. #endif
  27. SQLITE_EXTENSION_INIT1
  28. #include <assert.h>
  29. #include <string.h>
  30. #ifndef SQLITE_OMIT_VIRTUALTABLE
  31. /* memstat_vtab is a subclass of sqlite3_vtab which will
  32. ** serve as the underlying representation of a memstat virtual table
  33. */
  34. typedef struct memstat_vtab memstat_vtab;
  35. struct memstat_vtab {
  36. sqlite3_vtab base; /* Base class - must be first */
  37. sqlite3 *db; /* Database connection for this memstat vtab */
  38. };
  39. /* memstat_cursor is a subclass of sqlite3_vtab_cursor which will
  40. ** serve as the underlying representation of a cursor that scans
  41. ** over rows of the result
  42. */
  43. typedef struct memstat_cursor memstat_cursor;
  44. struct memstat_cursor {
  45. sqlite3_vtab_cursor base; /* Base class - must be first */
  46. sqlite3 *db; /* Database connection for this cursor */
  47. int iRowid; /* Current row in aMemstatColumn[] */
  48. int iDb; /* Which schema we are looking at */
  49. int nDb; /* Number of schemas */
  50. char **azDb; /* Names of all schemas */
  51. sqlite3_int64 aVal[2]; /* Result values */
  52. };
  53. /*
  54. ** The memstatConnect() method is invoked to create a new
  55. ** memstat_vtab that describes the memstat virtual table.
  56. **
  57. ** Think of this routine as the constructor for memstat_vtab objects.
  58. **
  59. ** All this routine needs to do is:
  60. **
  61. ** (1) Allocate the memstat_vtab object and initialize all fields.
  62. **
  63. ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
  64. ** result set of queries against memstat will look like.
  65. */
  66. static int memstatConnect(
  67. sqlite3 *db,
  68. void *pAux,
  69. int argc, const char *const*argv,
  70. sqlite3_vtab **ppVtab,
  71. char **pzErr
  72. ){
  73. memstat_vtab *pNew;
  74. int rc;
  75. /* Column numbers */
  76. #define MSV_COLUMN_NAME 0 /* Name of quantity being measured */
  77. #define MSV_COLUMN_SCHEMA 1 /* schema name */
  78. #define MSV_COLUMN_VALUE 2 /* Current value */
  79. #define MSV_COLUMN_HIWTR 3 /* Highwater mark */
  80. rc = sqlite3_declare_vtab(db,"CREATE TABLE x(name,schema,value,hiwtr)");
  81. if( rc==SQLITE_OK ){
  82. pNew = sqlite3_malloc( sizeof(*pNew) );
  83. *ppVtab = (sqlite3_vtab*)pNew;
  84. if( pNew==0 ) return SQLITE_NOMEM;
  85. memset(pNew, 0, sizeof(*pNew));
  86. pNew->db = db;
  87. }
  88. return rc;
  89. }
  90. /*
  91. ** This method is the destructor for memstat_cursor objects.
  92. */
  93. static int memstatDisconnect(sqlite3_vtab *pVtab){
  94. sqlite3_free(pVtab);
  95. return SQLITE_OK;
  96. }
  97. /*
  98. ** Constructor for a new memstat_cursor object.
  99. */
  100. static int memstatOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
  101. memstat_cursor *pCur;
  102. pCur = sqlite3_malloc( sizeof(*pCur) );
  103. if( pCur==0 ) return SQLITE_NOMEM;
  104. memset(pCur, 0, sizeof(*pCur));
  105. pCur->db = ((memstat_vtab*)p)->db;
  106. *ppCursor = &pCur->base;
  107. return SQLITE_OK;
  108. }
  109. /*
  110. ** Clear all the schema names from a cursor
  111. */
  112. static void memstatClearSchema(memstat_cursor *pCur){
  113. int i;
  114. if( pCur->azDb==0 ) return;
  115. for(i=0; i<pCur->nDb; i++){
  116. sqlite3_free(pCur->azDb[i]);
  117. }
  118. sqlite3_free(pCur->azDb);
  119. pCur->azDb = 0;
  120. pCur->nDb = 0;
  121. }
  122. /*
  123. ** Fill in the azDb[] array for the cursor.
  124. */
  125. static int memstatFindSchemas(memstat_cursor *pCur){
  126. sqlite3_stmt *pStmt = 0;
  127. int rc;
  128. if( pCur->nDb ) return SQLITE_OK;
  129. rc = sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pStmt, 0);
  130. if( rc ){
  131. sqlite3_finalize(pStmt);
  132. return rc;
  133. }
  134. while( sqlite3_step(pStmt)==SQLITE_ROW ){
  135. char **az, *z;
  136. az = sqlite3_realloc64(pCur->azDb, sizeof(char*)*(pCur->nDb+1));
  137. if( az==0 ){
  138. memstatClearSchema(pCur);
  139. return SQLITE_NOMEM;
  140. }
  141. pCur->azDb = az;
  142. z = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
  143. if( z==0 ){
  144. memstatClearSchema(pCur);
  145. return SQLITE_NOMEM;
  146. }
  147. pCur->azDb[pCur->nDb] = z;
  148. pCur->nDb++;
  149. }
  150. sqlite3_finalize(pStmt);
  151. return SQLITE_OK;
  152. }
  153. /*
  154. ** Destructor for a memstat_cursor.
  155. */
  156. static int memstatClose(sqlite3_vtab_cursor *cur){
  157. memstat_cursor *pCur = (memstat_cursor*)cur;
  158. memstatClearSchema(pCur);
  159. sqlite3_free(cur);
  160. return SQLITE_OK;
  161. }
  162. /*
  163. ** Allowed values for aMemstatColumn[].eType
  164. */
  165. #define MSV_GSTAT 0 /* sqlite3_status64() information */
  166. #define MSV_DB 1 /* sqlite3_db_status() information */
  167. #define MSV_ZIPVFS 2 /* ZIPVFS file-control with 64-bit return */
  168. /*
  169. ** An array of quantities that can be measured and reported by
  170. ** this virtual table
  171. */
  172. static const struct MemstatColumns {
  173. const char *zName; /* Symbolic name */
  174. unsigned char eType; /* Type of interface */
  175. unsigned char mNull; /* Bitmask of which columns are NULL */
  176. /* 2: dbname, 4: current, 8: hiwtr */
  177. int eOp; /* Opcode */
  178. } aMemstatColumn[] = {
  179. {"MEMORY_USED", MSV_GSTAT, 2, SQLITE_STATUS_MEMORY_USED },
  180. {"MALLOC_SIZE", MSV_GSTAT, 6, SQLITE_STATUS_MALLOC_SIZE },
  181. {"MALLOC_COUNT", MSV_GSTAT, 2, SQLITE_STATUS_MALLOC_COUNT },
  182. {"PAGECACHE_USED", MSV_GSTAT, 2, SQLITE_STATUS_PAGECACHE_USED },
  183. {"PAGECACHE_OVERFLOW", MSV_GSTAT, 2, SQLITE_STATUS_PAGECACHE_OVERFLOW },
  184. {"PAGECACHE_SIZE", MSV_GSTAT, 6, SQLITE_STATUS_PAGECACHE_SIZE },
  185. {"PARSER_STACK", MSV_GSTAT, 6, SQLITE_STATUS_PARSER_STACK },
  186. {"DB_LOOKASIDE_USED", MSV_DB, 2, SQLITE_DBSTATUS_LOOKASIDE_USED },
  187. {"DB_LOOKASIDE_HIT", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_HIT },
  188. {"DB_LOOKASIDE_MISS_SIZE", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE},
  189. {"DB_LOOKASIDE_MISS_FULL", MSV_DB, 6, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL},
  190. {"DB_CACHE_USED", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_USED },
  191. #if SQLITE_VERSION_NUMBER >= 3140000
  192. {"DB_CACHE_USED_SHARED", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_USED_SHARED },
  193. #endif
  194. {"DB_SCHEMA_USED", MSV_DB, 10, SQLITE_DBSTATUS_SCHEMA_USED },
  195. {"DB_STMT_USED", MSV_DB, 10, SQLITE_DBSTATUS_STMT_USED },
  196. {"DB_CACHE_HIT", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_HIT },
  197. {"DB_CACHE_MISS", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_MISS },
  198. {"DB_CACHE_WRITE", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_WRITE },
  199. #if SQLITE_VERSION_NUMBER >= 3230000
  200. {"DB_CACHE_SPILL", MSV_DB, 10, SQLITE_DBSTATUS_CACHE_SPILL },
  201. #endif
  202. {"DB_DEFERRED_FKS", MSV_DB, 10, SQLITE_DBSTATUS_DEFERRED_FKS },
  203. #ifdef SQLITE_ENABLE_ZIPVFS
  204. {"ZIPVFS_CACHE_USED", MSV_ZIPVFS, 8, 231454 },
  205. {"ZIPVFS_CACHE_HIT", MSV_ZIPVFS, 8, 231455 },
  206. {"ZIPVFS_CACHE_MISS", MSV_ZIPVFS, 8, 231456 },
  207. {"ZIPVFS_CACHE_WRITE", MSV_ZIPVFS, 8, 231457 },
  208. {"ZIPVFS_DIRECT_READ", MSV_ZIPVFS, 8, 231458 },
  209. {"ZIPVFS_DIRECT_BYTES", MSV_ZIPVFS, 8, 231459 },
  210. #endif /* SQLITE_ENABLE_ZIPVFS */
  211. };
  212. #define MSV_NROW (sizeof(aMemstatColumn)/sizeof(aMemstatColumn[0]))
  213. /*
  214. ** Advance a memstat_cursor to its next row of output.
  215. */
  216. static int memstatNext(sqlite3_vtab_cursor *cur){
  217. memstat_cursor *pCur = (memstat_cursor*)cur;
  218. int i;
  219. assert( pCur->iRowid<=MSV_NROW );
  220. while(1){
  221. i = (int)pCur->iRowid - 1;
  222. if( i<0 || (aMemstatColumn[i].mNull & 2)!=0 || (++pCur->iDb)>=pCur->nDb ){
  223. pCur->iRowid++;
  224. if( pCur->iRowid>MSV_NROW ) return SQLITE_OK; /* End of the table */
  225. pCur->iDb = 0;
  226. i++;
  227. }
  228. pCur->aVal[0] = 0;
  229. pCur->aVal[1] = 0;
  230. switch( aMemstatColumn[i].eType ){
  231. case MSV_GSTAT: {
  232. if( sqlite3_libversion_number()>=3010000 ){
  233. sqlite3_status64(aMemstatColumn[i].eOp,
  234. &pCur->aVal[0], &pCur->aVal[1],0);
  235. }else{
  236. int xCur, xHiwtr;
  237. sqlite3_status(aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0);
  238. pCur->aVal[0] = xCur;
  239. pCur->aVal[1] = xHiwtr;
  240. }
  241. break;
  242. }
  243. case MSV_DB: {
  244. int xCur, xHiwtr;
  245. sqlite3_db_status(pCur->db, aMemstatColumn[i].eOp, &xCur, &xHiwtr, 0);
  246. pCur->aVal[0] = xCur;
  247. pCur->aVal[1] = xHiwtr;
  248. break;
  249. }
  250. case MSV_ZIPVFS: {
  251. int rc;
  252. rc = sqlite3_file_control(pCur->db, pCur->azDb[pCur->iDb],
  253. aMemstatColumn[i].eOp, (void*)&pCur->aVal[0]);
  254. if( rc!=SQLITE_OK ) continue;
  255. break;
  256. }
  257. }
  258. break;
  259. }
  260. return SQLITE_OK;
  261. }
  262. /*
  263. ** Return values of columns for the row at which the memstat_cursor
  264. ** is currently pointing.
  265. */
  266. static int memstatColumn(
  267. sqlite3_vtab_cursor *cur, /* The cursor */
  268. sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
  269. int iCol /* Which column to return */
  270. ){
  271. memstat_cursor *pCur = (memstat_cursor*)cur;
  272. int i;
  273. assert( pCur->iRowid>0 && pCur->iRowid<=MSV_NROW );
  274. i = (int)pCur->iRowid - 1;
  275. if( (aMemstatColumn[i].mNull & (1<<iCol))!=0 ){
  276. return SQLITE_OK;
  277. }
  278. switch( iCol ){
  279. case MSV_COLUMN_NAME: {
  280. sqlite3_result_text(ctx, aMemstatColumn[i].zName, -1, SQLITE_STATIC);
  281. break;
  282. }
  283. case MSV_COLUMN_SCHEMA: {
  284. sqlite3_result_text(ctx, pCur->azDb[pCur->iDb], -1, 0);
  285. break;
  286. }
  287. case MSV_COLUMN_VALUE: {
  288. sqlite3_result_int64(ctx, pCur->aVal[0]);
  289. break;
  290. }
  291. case MSV_COLUMN_HIWTR: {
  292. sqlite3_result_int64(ctx, pCur->aVal[1]);
  293. break;
  294. }
  295. }
  296. return SQLITE_OK;
  297. }
  298. /*
  299. ** Return the rowid for the current row. In this implementation, the
  300. ** rowid is the same as the output value.
  301. */
  302. static int memstatRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  303. memstat_cursor *pCur = (memstat_cursor*)cur;
  304. *pRowid = pCur->iRowid*1000 + pCur->iDb;
  305. return SQLITE_OK;
  306. }
  307. /*
  308. ** Return TRUE if the cursor has been moved off of the last
  309. ** row of output.
  310. */
  311. static int memstatEof(sqlite3_vtab_cursor *cur){
  312. memstat_cursor *pCur = (memstat_cursor*)cur;
  313. return pCur->iRowid>MSV_NROW;
  314. }
  315. /*
  316. ** This method is called to "rewind" the memstat_cursor object back
  317. ** to the first row of output. This method is always called at least
  318. ** once prior to any call to memstatColumn() or memstatRowid() or
  319. ** memstatEof().
  320. */
  321. static int memstatFilter(
  322. sqlite3_vtab_cursor *pVtabCursor,
  323. int idxNum, const char *idxStr,
  324. int argc, sqlite3_value **argv
  325. ){
  326. memstat_cursor *pCur = (memstat_cursor *)pVtabCursor;
  327. int rc = memstatFindSchemas(pCur);
  328. if( rc ) return rc;
  329. pCur->iRowid = 0;
  330. pCur->iDb = 0;
  331. return memstatNext(pVtabCursor);
  332. }
  333. /*
  334. ** SQLite will invoke this method one or more times while planning a query
  335. ** that uses the memstat virtual table. This routine needs to create
  336. ** a query plan for each invocation and compute an estimated cost for that
  337. ** plan.
  338. */
  339. static int memstatBestIndex(
  340. sqlite3_vtab *tab,
  341. sqlite3_index_info *pIdxInfo
  342. ){
  343. pIdxInfo->estimatedCost = (double)500;
  344. pIdxInfo->estimatedRows = 500;
  345. return SQLITE_OK;
  346. }
  347. /*
  348. ** This following structure defines all the methods for the
  349. ** memstat virtual table.
  350. */
  351. static sqlite3_module memstatModule = {
  352. 0, /* iVersion */
  353. 0, /* xCreate */
  354. memstatConnect, /* xConnect */
  355. memstatBestIndex, /* xBestIndex */
  356. memstatDisconnect, /* xDisconnect */
  357. 0, /* xDestroy */
  358. memstatOpen, /* xOpen - open a cursor */
  359. memstatClose, /* xClose - close a cursor */
  360. memstatFilter, /* xFilter - configure scan constraints */
  361. memstatNext, /* xNext - advance a cursor */
  362. memstatEof, /* xEof - check for end of scan */
  363. memstatColumn, /* xColumn - read data */
  364. memstatRowid, /* xRowid - read data */
  365. 0, /* xUpdate */
  366. 0, /* xBegin */
  367. 0, /* xSync */
  368. 0, /* xCommit */
  369. 0, /* xRollback */
  370. 0, /* xFindMethod */
  371. 0, /* xRename */
  372. 0, /* xSavepoint */
  373. 0, /* xRelease */
  374. 0, /* xRollbackTo */
  375. 0, /* xShadowName */
  376. 0 /* xIntegrity */
  377. };
  378. #endif /* SQLITE_OMIT_VIRTUALTABLE */
  379. int sqlite3MemstatVtabInit(sqlite3 *db){
  380. int rc = SQLITE_OK;
  381. #ifndef SQLITE_OMIT_VIRTUALTABLE
  382. rc = sqlite3_create_module(db, "sqlite_memstat", &memstatModule, 0);
  383. #endif
  384. return rc;
  385. }
  386. #ifndef SQLITE_CORE
  387. #ifdef _WIN32
  388. __declspec(dllexport)
  389. #endif
  390. int sqlite3_memstat_init(
  391. sqlite3 *db,
  392. char **pzErrMsg,
  393. const sqlite3_api_routines *pApi
  394. ){
  395. int rc = SQLITE_OK;
  396. SQLITE_EXTENSION_INIT2(pApi);
  397. #ifndef SQLITE_OMIT_VIRTUALTABLE
  398. rc = sqlite3MemstatVtabInit(db);
  399. #endif
  400. return rc;
  401. }
  402. #endif /* SQLITE_CORE */
  403. #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_MEMSTATVTAB) */