4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This file implements a program used to measure the start-up performance
19 ** valgrind --tool=cachegrind ./startup run
22 ** The "./startup init" command creates the test database file named
23 ** "startup.db". The performance test is run by the "./startup run"
24 ** command. That command does nothing but open the database file and
25 ** parse the entire schema.
34 static const char zHelp
[] =
37 " init Initialized the startup.db database file\n"
38 " run Run the startup performance test\n"
40 " --dbname NAME Set the name of the test database file\n"
41 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
42 " --stats Show statistics at the end\n"
44 " --journal M Set the journal_mode to M\n"
45 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
46 " --mmap SZ MMAP the first SZ bytes of the database file\n"
47 " --multithread Set multithreaded mode\n"
48 " --nomemstat Disable memory statistics\n"
49 " --pagesize N Set the page size to N\n"
50 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
51 " --serialized Set serialized threading mode\n"
52 " --singlethread Set single-threaded mode - disables all mutexing\n"
53 " --utf16be Set text encoding to UTF-16BE\n"
54 " --utf16le Set text encoding to UTF-16LE\n"
55 " --utf8 Set text encoding to UTF-8\n"
59 static void usage(const char *argv0
){
65 ** The test schema is derived from the Fossil repository for SQLite itself.
66 ** The schema covers the repository, the local checkout database, and
67 ** the global configuration database.
69 static const char zTestSchema
[] =
70 "CREATE TABLE repo_blob(\n"
71 " rid INTEGER PRIMARY KEY,\n"
74 " uuid TEXT UNIQUE NOT NULL,\n"
76 " CHECK( length(uuid)>=40 AND rid>0 )\n"
78 "CREATE TABLE repo_delta(\n"
79 " rid INTEGER PRIMARY KEY,\n"
80 " srcid INTEGER NOT NULL REFERENCES blob\n"
82 "CREATE TABLE repo_rcvfrom(\n"
83 " rcvid INTEGER PRIMARY KEY,\n"
84 " uid INTEGER REFERENCES user,\n"
86 " nonce TEXT UNIQUE,\n"
89 "CREATE TABLE repo_private(rid INTEGER PRIMARY KEY);\n"
90 "CREATE TABLE repo_accesslog(\n"
94 " mtime TIMESTAMP);\n"
95 "CREATE TABLE repo_user(\n"
96 " uid INTEGER PRIMARY KEY,\n"
97 " login TEXT UNIQUE,\n"
102 " cexpire DATETIME,\n"
107 "CREATE TABLE repo_reportfmt(\n"
108 " rn INTEGER PRIMARY KEY,\n"
110 " title TEXT UNIQUE,\n"
115 "CREATE TABLE repo_sqlite_stat2(tbl,idx,sampleno,sample);\n"
116 "CREATE TABLE repo_sqlite_stat1(tbl,idx,stat);\n"
117 "CREATE TABLE repo_sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample);\n"
118 "CREATE TABLE repo_config(\n"
119 " name TEXT PRIMARY KEY NOT NULL,\n"
120 " value CLOB, mtime INTEGER,\n"
121 " CHECK( typeof(name)='text' AND length(name)>=1 )\n"
123 "CREATE TABLE repo_shun(uuid PRIMARY KEY,\n"
125 " scom TEXT) WITHOUT ROWID;\n"
126 "CREATE TABLE repo_concealed(\n"
127 " hash TEXT PRIMARY KEY,\n"
129 ", mtime INTEGER) WITHOUT ROWID;\n"
130 "CREATE TABLE repo_admin_log(\n"
131 " id INTEGER PRIMARY KEY,\n"
132 " time INTEGER, -- Seconds since 1970\n"
133 " page TEXT, -- path of page\n"
134 " who TEXT, -- User who made the change\n"
135 " what TEXT -- What changed\n"
137 "CREATE TABLE repo_unversioned(\n"
138 " name TEXT PRIMARY KEY,\n"
146 "CREATE TABLE repo_subscriber(\n"
147 " subscriberId INTEGER PRIMARY KEY,\n"
148 " subscriberCode BLOB DEFAULT (randomblob(32)) UNIQUE,\n"
149 " semail TEXT UNIQUE COLLATE nocase,\n"
151 " sverified BOOLEAN DEFAULT true,\n"
152 " sdonotcall BOOLEAN,\n"
153 " sdigest BOOLEAN,\n"
159 "CREATE TABLE repo_pending_alert(\n"
160 " eventid TEXT PRIMARY KEY,\n"
161 " sentSep BOOLEAN DEFAULT false,\n"
162 " sentDigest BOOLEAN DEFAULT false\n"
163 ", sentMod BOOLEAN DEFAULT false) WITHOUT ROWID;\n"
164 "CREATE INDEX repo_delta_i1 ON repo_delta(srcid);\n"
165 "CREATE INDEX repo_blob_rcvid ON repo_blob(rcvid);\n"
166 "CREATE INDEX repo_subscriberUname\n"
167 " ON repo_subscriber(suname) WHERE suname IS NOT NULL;\n"
168 "CREATE VIEW repo_artifact(rid,rcvid,size,atype,srcid,hash,content) AS\n"
169 " SELECT blob.rid,rcvid,size,1,srcid,uuid,content\n"
170 " FROM repo_blob LEFT JOIN repo_delta ON (blob.rid=delta.rid);\n"
171 "CREATE TABLE repo_filename(\n"
172 " fnid INTEGER PRIMARY KEY,\n"
173 " name TEXT UNIQUE\n"
175 "CREATE TABLE repo_mlink(\n"
180 " fnid INTEGER REFERENCES filename,\n"
183 " isaux BOOLEAN DEFAULT 0\n"
185 "CREATE INDEX repo_mlink_i1 ON repo_mlink(mid);\n"
186 "CREATE INDEX repo_mlink_i2 ON repo_mlink(fnid);\n"
187 "CREATE INDEX repo_mlink_i3 ON repo_mlink(fid);\n"
188 "CREATE INDEX repo_mlink_i4 ON repo_mlink(pid);\n"
189 "CREATE TABLE repo_plink(\n"
190 " pid INTEGER REFERENCES blob,\n"
191 " cid INTEGER REFERENCES blob,\n"
194 " baseid INTEGER REFERENCES blob,\n"
195 " UNIQUE(pid, cid)\n"
197 "CREATE INDEX repo_plink_i2 ON repo_plink(cid,pid);\n"
198 "CREATE TABLE repo_leaf(rid INTEGER PRIMARY KEY);\n"
199 "CREATE TABLE repo_event(\n"
202 " objid INTEGER PRIMARY KEY,\n"
204 " uid INTEGER REFERENCES user,\n"
213 "CREATE INDEX repo_event_i1 ON repo_event(mtime);\n"
214 "CREATE TABLE repo_phantom(\n"
215 " rid INTEGER PRIMARY KEY\n"
217 "CREATE TABLE repo_orphan(\n"
218 " rid INTEGER PRIMARY KEY,\n"
219 " baseline INTEGER\n"
221 "CREATE INDEX repo_orphan_baseline ON repo_orphan(baseline);\n"
222 "CREATE TABLE repo_unclustered(\n"
223 " rid INTEGER PRIMARY KEY\n"
225 "CREATE TABLE repo_unsent(\n"
226 " rid INTEGER PRIMARY KEY\n"
228 "CREATE TABLE repo_tag(\n"
229 " tagid INTEGER PRIMARY KEY,\n"
230 " tagname TEXT UNIQUE\n"
232 "CREATE TABLE repo_tagxref(\n"
233 " tagid INTEGER REFERENCES tag,\n"
234 " tagtype INTEGER,\n"
235 " srcid INTEGER REFERENCES blob,\n"
236 " origid INTEGER REFERENCES blob,\n"
238 " mtime TIMESTAMP,\n"
239 " rid INTEGER REFERENCE blob,\n"
240 " UNIQUE(rid, tagid)\n"
242 "CREATE INDEX repo_tagxref_i1 ON repo_tagxref(tagid, mtime);\n"
243 "CREATE TABLE repo_backlink(\n"
247 " mtime TIMESTAMP,\n"
248 " UNIQUE(target, srctype, srcid)\n"
250 "CREATE INDEX repo_backlink_src ON repo_backlink(srcid, srctype);\n"
251 "CREATE TABLE repo_attachment(\n"
252 " attachid INTEGER PRIMARY KEY,\n"
253 " isLatest BOOLEAN DEFAULT 0,\n"
254 " mtime TIMESTAMP,\n"
261 "CREATE INDEX repo_attachment_idx1\n"
262 " ON repo_attachment(target, filename, mtime);\n"
263 "CREATE INDEX repo_attachment_idx2 ON repo_attachment(src);\n"
264 "CREATE TABLE repo_cherrypick(\n"
267 " isExclude BOOLEAN DEFAULT false,\n"
268 " PRIMARY KEY(parentid, childid)\n"
270 "CREATE INDEX repo_cherrypick_cid ON repo_cherrypick(childid);\n"
271 "CREATE TABLE repo_ticket(\n"
272 " -- Do not change any column that begins with tkt_\n"
273 " tkt_id INTEGER PRIMARY KEY,\n"
274 " tkt_uuid TEXT UNIQUE,\n"
277 " -- Add as many fields as required below this line\n"
284 " private_contact TEXT,\n"
285 " resolution TEXT,\n"
289 "CREATE TABLE repo_ticketchng(\n"
290 " -- Do not change any column that begins with tkt_\n"
291 " tkt_id INTEGER REFERENCES ticket,\n"
292 " tkt_rid INTEGER REFERENCES blob,\n"
294 " -- Add as many fields as required below this line\n"
300 "CREATE INDEX repo_ticketchng_idx1 ON repo_ticketchng(tkt_id, tkt_mtime);\n"
301 "CREATE TRIGGER repo_alert_trigger1\n"
302 "AFTER INSERT ON repo_event BEGIN\n"
303 " INSERT INTO repo_pending_alert(eventid)\n"
304 " SELECT printf('%.1c%d',new.type,new.objid) WHERE true\n"
305 " ON CONFLICT(eventId) DO NOTHING;\n"
307 "CREATE TABLE repo_vcache(\n"
308 " vid INTEGER, -- check-in ID\n"
309 " fname TEXT, -- filename\n"
310 " rid INTEGER, -- artifact ID\n"
311 " PRIMARY KEY(vid,fname)\n"
313 "CREATE TABLE localdb_vvar(\n"
314 " name TEXT PRIMARY KEY NOT NULL,\n"
316 " CHECK( typeof(name)='text' AND length(name)>=1 )\n"
318 "CREATE TABLE localdb_vfile(\n"
319 " id INTEGER PRIMARY KEY,\n"
320 " vid INTEGER REFERENCES blob,\n"
321 " chnged INT DEFAULT 0,\n"
322 " deleted BOOLEAN DEFAULT 0,\n"
329 " origname TEXT, mhash,\n"
330 " UNIQUE(pathname,vid)\n"
332 "CREATE TABLE localdb_sqlite_stat1(tbl,idx,stat);\n"
333 "CREATE TABLE localdb_vcache(\n"
334 " vid INTEGER, -- check-in ID\n"
335 " fname TEXT, -- filename\n"
336 " rid INTEGER, -- artifact ID\n"
337 " PRIMARY KEY(vid,fname)\n"
339 "CREATE TABLE localdb_stash(\n"
340 " stashid INTEGER PRIMARY KEY,\n"
346 "CREATE TABLE localdb_stashfile(\n"
347 " stashid INTEGER REFERENCES stash,\n"
348 " isAdded BOOLEAN,\n"
349 " isRemoved BOOLEAN,\n"
357 " PRIMARY KEY(newname, stashid)\n"
359 "CREATE TABLE localdb_vmerge(\n"
360 " id INTEGER REFERENCES vfile,\n"
364 "CREATE UNIQUE INDEX localdb_vmergex1 ON localdb_vmerge(id,mhash);\n"
365 "CREATE TRIGGER localdb_vmerge_ck1 AFTER INSERT ON localdb_vmerge\n"
366 "WHEN new.mhash IS NULL BEGIN\n"
367 " SELECT raise(FAIL,\n"
368 " 'trying to update a newer checkout with an older version of Fossil');\n"
370 "CREATE TABLE configdb_global_config(\n"
371 " name TEXT PRIMARY KEY,\n"
374 "CREATE TABLE configdb_sqlite_stat1(tbl,idx,stat);\n"
378 #include <sys/types.h>
382 ** Attempt to display I/O stats on Linux using /proc/PID/io
384 static void displayLinuxIoStats(FILE *out
){
387 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
390 while( fgets(z
, sizeof(z
), in
)!=0 ){
391 static const struct {
392 const char *zPattern
;
395 { "rchar: ", "Bytes received by read():" },
396 { "wchar: ", "Bytes sent to write():" },
397 { "syscr: ", "Read() system calls:" },
398 { "syscw: ", "Write() system calls:" },
399 { "read_bytes: ", "Bytes rcvd from storage:" },
400 { "write_bytes: ", "Bytes sent to storage:" },
401 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
404 for(i
=0; i
<sizeof(aTrans
)/sizeof(aTrans
[0]); i
++){
405 int n
= (int)strlen(aTrans
[i
].zPattern
);
406 if( strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
407 fprintf(out
, "-- %-28s %s", aTrans
[i
].zDesc
, &z
[n
]);
417 ** Return the value of a hexadecimal digit. Return -1 if the input
418 ** is not a hex digit.
420 static int hexDigitValue(char c
){
421 if( c
>='0' && c
<='9' ) return c
- '0';
422 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
423 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
428 ** Interpret zArg as an integer value, possibly with suffixes.
430 static int integerValue(const char *zArg
){
432 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
434 { "MiB", 1024*1024 },
435 { "GiB", 1024*1024*1024 },
438 { "GB", 1000000000 },
448 }else if( zArg
[0]=='+' ){
451 if( zArg
[0]=='0' && zArg
[1]=='x' ){
454 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
459 while( isdigit(zArg
[0]) ){
460 v
= v
*10 + zArg
[0] - '0';
464 for(i
=0; i
<sizeof(aMult
)/sizeof(aMult
[0]); i
++){
465 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
471 printf("ERROR: parameter too large - max 2147483648\n");
474 return (int)(isNeg
? -v
: v
);
478 int main(int argc
, char **argv
){
479 const char *zCmd
= 0;
483 const char *zDbName
= "./startup.db";
487 for(i
=1; i
<argc
; i
++){
488 const char *z
= argv
[i
];
497 if( strcmp(z
, "-autovacuum")==0 ){
500 if( strcmp(z
, "-dbname")==0 ){
502 printf("ERROR: missing argument on \"%s\"\n", argv
[0]);
507 if( strcmp(z
,"-heap")==0 ){
509 printf("ERROR: missing arguments on %s\n", argv
[i
]);
512 nHeap
= integerValue(argv
[i
+1]);
513 mnHeap
= integerValue(argv
[i
+2]);
516 if( strcmp(z
,"-stats")==0 ){
520 printf("ERROR: unknown option \"%s\"\n", argv
[i
]);
525 printf("ERROR: no COMMAND specified\n");
528 if( strcmp(zCmd
, "run")==0 ){
534 pHeap
= malloc( nHeap
);
536 printf("ERROR: cannot allocate %d-byte heap\n", nHeap
);
539 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, pHeap
, nHeap
, mnHeap
);
541 printf("ERROR: heap configuration failed: %d\n", rc
);
545 rc
= sqlite3_open(zDbName
, &db
);
547 printf("SQLite error: %s\n", sqlite3_errmsg(db
));
549 sqlite3_exec(db
, "PRAGMA synchronous", 0, 0, &zErr
);
552 printf("ERROR: %s\n", zErr
);
557 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_USED
, &iCur
, &iHi
, 0);
558 printf("-- Lookaside Slots Used: %d (max %d)\n", iCur
,iHi
);
559 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
, &iCur
, &iHi
, 0);
560 printf("-- Successful lookasides: %d\n", iHi
);
561 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
, &iCur
,&iHi
,0);
562 printf("-- Lookaside size faults: %d\n", iHi
);
563 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
, &iCur
,&iHi
,0);
564 printf("-- Lookaside OOM faults: %d\n", iHi
);
565 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHi
, 0);
566 printf("-- Pager Heap Usage: %d bytes\n", iCur
);
567 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHi
, 1);
568 printf("-- Page cache hits: %d\n", iCur
);
569 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHi
, 1);
570 printf("-- Page cache misses: %d\n", iCur
);
571 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHi
, 1);
572 printf("-- Page cache writes: %d\n", iCur
);
573 sqlite3_db_status(db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHi
, 0);
574 printf("-- Schema Heap Usage: %d bytes\n", iCur
);
575 sqlite3_db_status(db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHi
, 0);
576 printf("-- Statement Heap Usage: %d bytes\n", iCur
);
580 /* Global memory usage statistics printed after the database connection
581 ** has closed. Memory usage should be zero at this point. */
584 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &iCur
, &iHi
, 0);
585 printf("-- Memory Used (bytes): %d (max %d)\n", iCur
,iHi
);
586 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT
, &iCur
, &iHi
, 0);
587 printf("-- Outstanding Allocations: %d (max %d)\n", iCur
,iHi
);
588 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW
, &iCur
, &iHi
, 0);
589 printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur
,iHi
);
590 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE
, &iCur
, &iHi
, 0);
591 printf("-- Largest Allocation: %d bytes\n",iHi
);
592 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE
, &iCur
, &iHi
, 0);
593 printf("-- Largest Pcache Allocation: %d bytes\n",iHi
);
595 displayLinuxIoStats(stdout
);
600 if( strcmp(zCmd
, "init")==0 ){
606 zAux
= sqlite3_mprintf("%s-journal", zDbName
);
609 zAux
= sqlite3_mprintf("%s-wal", zDbName
);
612 rc
= sqlite3_open(zDbName
, &db
);
614 printf("SQLite error: %s\n", sqlite3_errmsg(db
));
616 sqlite3_exec(db
, "BEGIN", 0, 0, 0);
617 sqlite3_exec(db
, zTestSchema
, 0, 0, &zErr
);
618 sqlite3_exec(db
, "COMMIT", 0, 0, 0);
621 printf("ERROR: %s\n", zErr
);