1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
** 2023-06-21
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2
** mechanism to add a tracing layer on top of pluggable page cache of
** SQLite. If this extension is registered prior to sqlite3_initialize(),
** it will cause all page cache activities to be logged on standard output,
** or to some other FILE specified by the initializer.
**
** This file needs to be compiled into the application that uses it.
**
** This extension is used to implement the --pcachetrace option of the
** command-line shell.
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
/* The original page cache routines */
static sqlite3_pcache_methods2 pcacheBase;
static FILE *pcachetraceOut;
/* Methods that trace pcache activity */
static int pcachetraceInit(void *pArg){
int nRes;
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg);
}
nRes = pcacheBase.xInit(pArg);
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes);
}
return nRes;
}
static void pcachetraceShutdown(void *pArg){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg);
}
pcacheBase.xShutdown(pArg);
}
static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){
sqlite3_pcache *pRes;
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n",
szPage, szExtra, bPurge);
}
pRes = pcacheBase.xCreate(szPage, szExtra, bPurge);
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n",
szPage, szExtra, bPurge, pRes);
}
return pRes;
}
static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize);
}
pcacheBase.xCachesize(p, nCachesize);
}
static int pcachetracePagecount(sqlite3_pcache *p){
int nRes;
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p);
}
nRes = pcacheBase.xPagecount(p);
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes);
}
return nRes;
}
static sqlite3_pcache_page *pcachetraceFetch(
sqlite3_pcache *p,
unsigned key,
int crFg
){
sqlite3_pcache_page *pRes;
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg);
}
pRes = pcacheBase.xFetch(p, key, crFg);
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n",
p, key, crFg, pRes);
}
return pRes;
}
static void pcachetraceUnpin(
sqlite3_pcache *p,
sqlite3_pcache_page *pPg,
int bDiscard
){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n",
p, pPg, bDiscard);
}
pcacheBase.xUnpin(p, pPg, bDiscard);
}
static void pcachetraceRekey(
sqlite3_pcache *p,
sqlite3_pcache_page *pPg,
unsigned oldKey,
unsigned newKey
){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n",
p, pPg, oldKey, newKey);
}
pcacheBase.xRekey(p, pPg, oldKey, newKey);
}
static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n);
}
pcacheBase.xTruncate(p, n);
}
static void pcachetraceDestroy(sqlite3_pcache *p){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p);
}
pcacheBase.xDestroy(p);
}
static void pcachetraceShrink(sqlite3_pcache *p){
if( pcachetraceOut ){
fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p);
}
pcacheBase.xShrink(p);
}
/* The substitute pcache methods */
static sqlite3_pcache_methods2 ersaztPcacheMethods = {
0,
0,
pcachetraceInit,
pcachetraceShutdown,
pcachetraceCreate,
pcachetraceCachesize,
pcachetracePagecount,
pcachetraceFetch,
pcachetraceUnpin,
pcachetraceRekey,
pcachetraceTruncate,
pcachetraceDestroy,
pcachetraceShrink
};
/* Begin tracing memory allocations to out. */
int sqlite3PcacheTraceActivate(FILE *out){
int rc = SQLITE_OK;
if( pcacheBase.xFetch==0 ){
rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase);
if( rc==SQLITE_OK ){
rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods);
}
}
pcachetraceOut = out;
return rc;
}
/* Deactivate memory tracing */
int sqlite3PcacheTraceDeactivate(void){
int rc = SQLITE_OK;
if( pcacheBase.xFetch!=0 ){
rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase);
if( rc==SQLITE_OK ){
memset(&pcacheBase, 0, sizeof(pcacheBase));
}
}
pcachetraceOut = 0;
return rc;
}
|