sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | #!/bin/sh |
| 2 | # the next line restarts using tclsh \ |
| 3 | exec tclsh "$0" ${1+"$@"} |
| 4 | # |
| 5 | # This program runs performance testing on sqlite3.c. Usage: |
| 6 | set usage {USAGE: |
| 7 | |
| 8 | speedtest.tcl sqlite3.c x1.txt trunk.txt -Os -DSQLITE_ENABLE_STAT4 |
| 9 | | | | `-----------------------' |
| 10 | File to test ----' | | | |
| 11 | | | `- options |
| 12 | Output filename --------' | |
| 13 | `--- optional prior output to diff |
| 14 | |
| 15 | Do a cache-grind performance analysis of the sqlite3.c file named and |
| 16 | write the results into the output file. The ".txt" is appended to the |
| 17 | output file (and diff-file) name if it is not already present. If the |
| 18 | diff-file is specified then show a diff from the diff-file to the new |
| 19 | output. |
| 20 | |
| 21 | Other options include: |
| 22 | CC=... Specify an alternative C compiler. Default is "gcc". |
| 23 | -D... -D and -O options are passed through to the C compiler. |
| 24 | --dryrun Show what would happen but don't do anything. |
| 25 | --help Show this help screen. |
| 26 | --lean "Lean" mode. |
| 27 | --lookaside N SZ Lookahead uses N slots of SZ bytes each. |
| 28 | --osmalloc Use the OS native malloc() instead of MEMSYS5 |
| 29 | --pagesize N Use N as the page size. |
| 30 | --pgo Use GCC-style PGO |
| 31 | --quiet | -q "Quiet". Put results in file but don't pop up editor |
| 32 | --size N Change the test size. 100 means 100%. Default: 5. |
| 33 | --testset TEST Specify the specific testset to use. The default |
| 34 | is "mix1". Other options include: "main", "json", |
| 35 | "cte", "orm", "fp", "rtree". |
| 36 | } |
| 37 | set srcfile {} |
| 38 | set outfile {} |
| 39 | set difffile {} |
| 40 | set cflags {-DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_THREADSAFE=0} |
| 41 | set cc gcc |
| 42 | set testset mix1 |
| 43 | set dryrun 0 |
| 44 | set quiet 0 |
| 45 | set osmalloc 0 |
| 46 | set usePGO 0 |
| 47 | set speedtestflags {--shrink-memory --reprepare --stats} |
| 48 | lappend speedtestflags --journal wal --size 5 |
| 49 | |
| 50 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 51 | set arg [lindex $argv $i] |
| 52 | if {[string index $arg 0]=="-"} { |
| 53 | switch -- $arg { |
| 54 | -pagesize - |
| 55 | --pagesize { |
| 56 | lappend speedtestflags --pagesize |
| 57 | incr i |
| 58 | lappend speedtestflags [lindex $argv $i] |
| 59 | } |
| 60 | -lookaside - |
| 61 | --lookaside { |
| 62 | lappend speedtestflags --lookaside |
| 63 | incr i |
| 64 | lappend speedtestflags [lindex $argv $i] |
| 65 | incr i |
| 66 | lappend speedtestflags [lindex $argv $i] |
| 67 | } |
| 68 | -lean - |
| 69 | --lean { |
| 70 | lappend cflags \ |
| 71 | -DSQLITE_DEFAULT_MEMSTATUS=0 \ |
| 72 | -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \ |
| 73 | -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 \ |
| 74 | -DSQLITE_MAX_EXPR_DEPTH=1 \ |
| 75 | -DSQLITE_OMIT_DECLTYPE \ |
| 76 | -DSQLITE_OMIT_DEPRECATED \ |
| 77 | -DSQLITE_OMIT_PROGRESS_CALLBACK \ |
| 78 | -DSQLITE_OMIT_SHARED_CACHE \ |
| 79 | -DSQLITE_USE_ALLOCA |
| 80 | } |
| 81 | -testset - |
| 82 | --testset { |
| 83 | incr i |
| 84 | set testset [lindex $argv $i] |
| 85 | } |
| 86 | -size - |
| 87 | --size { |
| 88 | incr i |
| 89 | set newsize [lindex $argv $i] |
| 90 | if {$newsize<1} {set newsize 1} |
| 91 | set speedtestflags \ |
| 92 | [regsub {.-size \d+} $speedtestflags "-size $newsize"] |
| 93 | } |
| 94 | -n - |
| 95 | -dryrun - |
| 96 | --dryrun { |
| 97 | set dryrun 1 |
| 98 | } |
| 99 | -osmalloc - |
| 100 | --osmalloc { |
| 101 | set osmalloc 1 |
| 102 | } |
| 103 | -? - |
| 104 | -help - |
| 105 | --help { |
| 106 | puts $usage |
| 107 | exit 0 |
| 108 | } |
| 109 | -q - |
| 110 | -quiet - |
| 111 | --quiet { |
| 112 | set quiet 1 |
| 113 | } |
| 114 | -pgo { |
| 115 | set usePGO 1 |
| 116 | } |
| 117 | default { |
| 118 | lappend cflags $arg |
| 119 | } |
| 120 | } |
| 121 | continue |
| 122 | } |
| 123 | if {[string match CC=* $arg]} { |
| 124 | set cc [string range $arg 3 end] |
| 125 | continue |
| 126 | } |
| 127 | if {[string match *.c $arg]} { |
| 128 | if {$srcfile!=""} { |
| 129 | puts stderr "multiple source files: $srcfile $arg" |
| 130 | exit 1 |
| 131 | } |
| 132 | set srcfile $arg |
| 133 | continue |
| 134 | } |
| 135 | if {[lsearch {main cte rtree orm fp json parsenumber mix1} $arg]>=0} { |
| 136 | set testset $arg |
| 137 | continue |
| 138 | } |
| 139 | if {$outfile==""} { |
| 140 | set outfile $arg |
| 141 | continue |
| 142 | } |
| 143 | if {$difffile==""} { |
| 144 | set difffile $arg |
| 145 | continue |
| 146 | } |
| 147 | puts stderr "unknown option: \"$arg\". Use --help for more info." |
| 148 | exit 1 |
| 149 | } |
| 150 | if {[lsearch -glob $cflags -O*]<0} { |
| 151 | lappend cflags -Os |
| 152 | } |
| 153 | if {!$osmalloc} { |
| 154 | append speedtestflags { --heap 40000000 64} |
| 155 | } |
| 156 | if {!$osmalloc && [lsearch -glob $cflags {-DSQLITE_ENABLE_MEMSYS*}]<0} { |
| 157 | lappend cflags -DSQLITE_ENABLE_MEMSYS5 |
| 158 | } |
| 159 | if {[lsearch -glob $cflags {-DSQLITE_ENABLE_RTREE*}]<0} { |
| 160 | lappend cflags -DSQLITE_ENABLE_RTREE |
| 161 | } |
| 162 | if {$srcfile==""} { |
| 163 | puts stderr "no sqlite3.c source file specified" |
| 164 | exit 1 |
| 165 | } |
| 166 | if {![file readable $srcfile]} { |
| 167 | puts stderr "source file \"$srcfile\" does not exist" |
| 168 | exit 1 |
| 169 | } |
| 170 | if {$outfile==""} { |
| 171 | puts stderr "no output file specified" |
| 172 | exit 1 |
| 173 | } |
| 174 | if {![string match *.* [file tail $outfile]]} { |
| 175 | append outfile .txt |
| 176 | } |
| 177 | if {$difffile!=""} { |
| 178 | if {![file exists $difffile]} { |
| 179 | if {[file exists $difffile.txt]} { |
| 180 | append difffile .txt |
| 181 | } else { |
| 182 | puts stderr "No such file: \"$difffile\"" |
| 183 | exit 1 |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | set cccmd [list $cc -g] |
| 189 | lappend cccmd -I[file dir $srcfile] |
| 190 | lappend cccmd {*}[lsort $cflags] |
| 191 | lappend cccmd [file dir $argv0]/speedtest1.c |
| 192 | lappend cccmd $srcfile |
| 193 | lappend cccmd -o speedtest1 |
| 194 | lappend speedtestflags --testset $testset |
| 195 | if {$usePGO} { |
| 196 | set cccmd [linsert $cccmd 2 -fprofile-generate] |
| 197 | puts $cccmd |
| 198 | if {!$dryrun} { |
| 199 | exec {*}$cccmd |
| 200 | } |
| 201 | foreach gcda [glob -nocomplain *.gcda] { |
| 202 | puts "Delete $gcda" |
| 203 | if {!$dryrun} {file delete $gcda} |
| 204 | } |
| 205 | set stcmd [list ./speedtest1 {*}$speedtestflags] |
| 206 | puts $stcmd |
| 207 | if {!$dryrun} { |
| 208 | exec {*}$stcmd >/dev/null |
| 209 | } |
| 210 | set cccmd [lreplace $cccmd 2 2 -fprofile-use] |
| 211 | } |
| 212 | puts $cccmd |
| 213 | if {!$dryrun} { |
| 214 | exec {*}$cccmd |
| 215 | } |
| 216 | set stcmd [list valgrind --tool=cachegrind ./speedtest1 {*}$speedtestflags] |
| 217 | lappend stcmd speedtest1.db |
| 218 | lappend stcmd >valgrind-out.txt 2>valgrind-err.txt |
| 219 | puts $stcmd |
| 220 | if {!$dryrun} { |
| 221 | foreach file {speedtest1.db speedtest1.db-journal speedtest1.db-wal |
| 222 | speedtest1.db-shm} { |
| 223 | if {[file exists $file]} {file delete $file} |
| 224 | } |
| 225 | exec {*}$stcmd |
| 226 | } |
| 227 | |
| 228 | set maxmtime 0 |
| 229 | set cgfile {} |
| 230 | foreach cgout [glob -nocomplain cachegrind.out.*] { |
| 231 | if {[file mtime $cgout]>$maxmtime} { |
| 232 | set cgfile $cgout |
| 233 | set maxmtime [file mtime $cgfile] |
| 234 | } |
| 235 | } |
| 236 | if {$cgfile==""} { |
| 237 | puts "no cachegrind output" |
| 238 | exit 1 |
| 239 | } |
| 240 | |
| 241 | ############# Process the cachegrind.out.# file ########################## |
| 242 | set fd [open $outfile wb] |
| 243 | set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $cgfile" r] |
| 244 | set dest ! |
| 245 | set out(!) {} |
| 246 | set linenum 0 |
| 247 | set cntlines 0 ;# true to remember cycle counts on each line |
| 248 | set seenSqlite3 0 ;# true if we have seen the sqlite3.c file |
| 249 | while {![eof $in]} { |
| 250 | set line [string map {\t { }} [gets $in]] |
| 251 | if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} { |
| 252 | set dest $name |
| 253 | if {[string match */sqlite3.c $dest]} { |
| 254 | set cntlines 1 |
| 255 | set seenSqlite3 1 |
| 256 | } else { |
| 257 | set cntlines 0 |
| 258 | } |
| 259 | } elseif {[regexp {^-- line (\d+) ------} $line all ln]} { |
| 260 | set line [lreplace $line 2 2 {#}] |
| 261 | set linenum [expr {$ln-1}] |
| 262 | } elseif {[regexp {^The following files chosen for } $line]} { |
| 263 | set dest ! |
| 264 | } |
| 265 | append out($dest) $line\n |
| 266 | if {$cntlines} { |
| 267 | incr linenum |
| 268 | if {[regexp {^ *([0-9,]+) } $line all x]} { |
| 269 | set x [string map {, {}} $x] |
| 270 | set cycles($linenum) $x |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | foreach x [lsort [array names out]] { |
| 275 | puts $fd $out($x) |
| 276 | } |
| 277 | # If the sqlite3.c file has been seen, then output a summary of the |
| 278 | # cycle counts for each file that went into making up sqlite3.c |
| 279 | # |
| 280 | if {$seenSqlite3} { |
| 281 | close $in |
| 282 | set in [open sqlite3.c] |
| 283 | set linenum 0 |
| 284 | set fn sqlite3.c |
| 285 | set pattern1 {^/\*+ Begin file ([^ ]+) \*} |
| 286 | set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*} |
| 287 | while {![eof $in]} { |
| 288 | set line [gets $in] |
| 289 | incr linenum |
| 290 | if {[regexp $pattern1 $line all newfn]} { |
| 291 | set fn $newfn |
| 292 | } elseif {[regexp $pattern2 $line all newfn]} { |
| 293 | set fn $newfn |
| 294 | } elseif {[info exists cycles($linenum)]} { |
| 295 | incr fcycles($fn) $cycles($linenum) |
| 296 | } |
| 297 | } |
| 298 | close $in |
| 299 | puts $fd \ |
| 300 | {**********************************************************************} |
| 301 | set lx {} |
| 302 | set sum 0 |
| 303 | foreach {fn cnt} [array get fcycles] { |
| 304 | lappend lx [list $cnt $fn] |
| 305 | incr sum $cnt |
| 306 | } |
| 307 | puts $fd [format {%20s %14d %8.3f%%} TOTAL $sum 100] |
| 308 | foreach entry [lsort -index 0 -integer -decreasing $lx] { |
| 309 | foreach {cnt fn} $entry break |
| 310 | puts $fd [format {%20s %14d %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]] |
| 311 | } |
| 312 | } |
| 313 | puts $fd "Executable size:" |
| 314 | close $fd |
| 315 | exec size speedtest1 >>$outfile |
| 316 | # |
| 317 | # Processed cachegrind output should now be in the $outfile |
| 318 | ############################################################################# |
| 319 | |
| 320 | if {$quiet} { |
| 321 | # Skip this last part of popping up a GUI viewer |
| 322 | } elseif {$difffile!=""} { |
| 323 | set fossilcmd {fossil xdiff --tk -c 20} |
| 324 | lappend fossilcmd $difffile |
| 325 | lappend fossilcmd $outfile |
| 326 | lappend fossilcmd & |
| 327 | puts $fossilcmd |
| 328 | if {!$dryrun} { |
| 329 | exec {*}$fossilcmd |
| 330 | } |
| 331 | } else { |
| 332 | if {!$dryrun} { |
| 333 | exec open $outfile |
| 334 | } |
| 335 | } |