Debian Codegraph @

www/cpu.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Measuring and Reducing CPU Usage in SQLite</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Measuring and Reducing CPU Usage in SQLite
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#measuring_performance">2. Measuring Performance</a></div>
130 <div class="fancy-toc2"><a href="#compile_options">2.1. Compile Options</a></div>
131 <div class="fancy-toc2"><a href="#workload">2.2. Workload</a></div>
132 <div class="fancy-toc2"><a href="#performance_measurement">2.3. Performance Measurement</a></div>
133 <div class="fancy-toc2"><a href="#microoptimizations">2.4. Microoptimizations</a></div>
134 <div class="fancy-toc1"><a href="#performance_measurement_workflow">3. Performance Measurement Workflow</a></div>
135 <div class="fancy-toc1"><a href="#limitations">4. Limitations</a></div>
136 </div>
137 </div>
138 <script>
139 function toggle_toc(){
140 var sub = document.getElementById("toc_sub")
141 var mk = document.getElementById("toc_mk")
142 if( sub.style.display!="block" ){
143 sub.style.display = "block";
144 mk.innerHTML = "&#x25bc;";
145 } else {
146 sub.style.display = "none";
147 mk.innerHTML = "&#x25ba;";
148 }
149 }
150 </script>
151 </div>
152
153
154
155
156
157 <h1 id="overview"><span>1. </span>Overview</h1>
158
159 <p>The graph below shows the number of CPU cycles used by SQLite on a
160 standard workload, for versions of SQLite going back about 10 years.
161 Recent versions of SQLite use about one third as many the CPU cycles
162 compared to older versions.
163
164 </p><p>
165 This article describes how the SQLite developers measure CPU usage,
166 what those measurements actually mean, and the techniques used by
167 SQLite developers on their continuing quest to further reduce the
168 CPU usage of the SQLite library.
169 </p>
170
171 <center>
172 <hr>
173 <div class="imgcontainer">
174 <img src="./images/cpu-usage.jpg"></div><br>
175 Measured using cachegrind on Ubuntu 16.04 on x64 with gcc 5.4.0 and -Os.<br>
176 <hr>
177 </center>
178
179 <h1 id="measuring_performance"><span>2. </span>Measuring Performance</h1>
180
181 <p>In brief, the CPU performance of SQLite is measured as follows:
182
183 </p><p></p><ol>
184 <li> Compile SQLite in an as-delivered configuration, without any special
185 telemetry or debugging options.
186 </li><li> Link SQLite against a test program that runs approximately 30,000
187 SQL statements representing a typical workload.
188 </li><li> Count the number of CPU cycles consumed using
189 <a href="http://valgrind.org/docs/manual/cg-manual.html">cachegrind</a>.
190 </li></ol>
191
192 <h2 id="compile_options"><span>2.1. </span>Compile Options</h2>
193
194 <p>For performance measurement, SQLite is compiled in approximately the same
195 way as it would be for use in production systems. The compile-time configuration
196 is "approximate" in the sense that every production use of SQLite is
197 different. Compile-time options used by one system are not necessarily
198 the same as those used by others. The key point is that options that
199 significantly impact the generated machine code are avoided. For example,
200 the -DSQLITE_DEBUG option is omitted because that option inserts thousands
201 of assert() statements in the middle of performance critical sections of the
202 SQLite library. The -pg option (on GCC) is omitted because it causes the
203 compiler to emit extra probabilistic performance measuring code which interferes
204 with actual performance measurements.
205
206 </p><p>
207 For performance measurements,
208 the -Os option is used (optimize for size) rather than -O2 because the
209 -O2 option creates so much code movement that it is difficult to associate
210 specific CPU instructions to C source code lines.
211
212 </p><h2 id="workload"><span>2.2. </span>Workload</h2>
213
214 <p>
215 The "typical" workload is generated by the
216 <a href="https://sqlite.org/src/file/test/speedtest1.c">speedtest1.c</a>
217 program in the canonical SQLite source tree. This program strives to
218 exercise the SQLite library in a way that is typical of real-world
219 applications. Of course, every application is different, and so
220 no test program can exactly mirror the behavior of all applications.
221
222 </p><p>
223 The speedtest1.c program is updated from time to time as the SQLite
224 developers' understanding of what constitutes "typical" usage evolves.
225
226 </p><p>
227 The
228 <a href="https://sqlite.org/src/file/tool/speed-check.sh">speed-check.sh</a> shell
229 script, also in the canonical source tree, is used to run the speedtest1.c
230 program. To replicate the performance measurements, collect the following
231 files into a single directory:
232 </p><ul>
233 <li> the "speed-check.sh" script,
234 </li><li> the "speedtest1.c" test program, and
235 </li><li> the <a href="amalgamation.html">SQLite amalgamation</a> source files "sqlite3.c" and
236 "sqlite3.h"
237 </li></ul>
238 <p>
239 Then run "sh speed-check.sh trunk".
240
241
242 </p><h2 id="performance_measurement"><span>2.3. </span>Performance Measurement</h2>
243
244 <p>
245 <a href="http://valgrind.org/docs/manual/cg-manual.html">Cachegrind</a> is used to
246 measure performance because it gives answers that are repeatable to
247 7 or more significant digits. In comparison, actual (wall-clock)
248 run times are scarcely repeatable beyond one significant digit.
249
250 <a name="microopt"></a>
251
252 </p><h2 id="microoptimizations"><span>2.4. </span>Microoptimizations</h2>
253
254 <p>
255 The high repeatability of cachegrind allows the SQLite developers to
256 implement and measure "microoptimizations". A microoptimization is
257 a change to the code that results in a very small performance increase.
258 Typical micro-optimizations reduce the number of CPU cycles by 0.1% or
259 0.05% or even less. Such improvements are impossible to measure with
260 real-world timings. But hundreds or thousands of microoptimizations
261 add up, resulting in measurable real-world performance gains.
262
263 </p><h1 id="performance_measurement_workflow"><span>3. </span>Performance Measurement Workflow</h1>
264
265 <p>
266 As SQLite developers edit the SQLite source code, they run the
267 <a href="https://sqlite.org/src/file/tool/speed-check.sh">speed-check.sh</a>
268 shell script to track the performance impact of changes. This
269 script compiles the speedtest1.c program, runs it under cachegrind,
270 processes the cachegrind output using the
271 <a href="https://sqlite.org/src/file/tool/cg_anno.tcl">cg_anno.tcl</a> TCL
272 script, then saves the results in a series of text files.
273 Typical output from the speed-check.sh script looks like this:
274
275 </p><blockquote><pre>
276 ==8683==
277 ==8683== I refs: <font color="red">1,060,925,768</font>
278 ==8683== I1 misses: 23,731,246
279 ==8683== LLi misses: 5,176
280 ==8683== I1 miss rate: 2.24%
281 ==8683== LLi miss rate: 0.00%
282 ==8683==
283 ==8683== D refs: 557,686,925 (361,828,925 rd + 195,858,000 wr)
284 ==8683== D1 misses: 5,067,063 ( 3,544,278 rd + 1,522,785 wr)
285 ==8683== LLd misses: 57,958 ( 16,067 rd + 41,891 wr)
286 ==8683== D1 miss rate: 0.9% ( 1.0% + 0.8% )
287 ==8683== LLd miss rate: 0.0% ( 0.0% + 0.0% )
288 ==8683==
289 ==8683== LL refs: 28,798,309 ( 27,275,524 rd + 1,522,785 wr)
290 ==8683== LL misses: 63,134 ( 21,243 rd + 41,891 wr)
291 ==8683== LL miss rate: 0.0% ( 0.0% + 0.0% )
292 text data bss dec hex filename
293 523044 8240 1976 <font color="red">533260</font> 8230c sqlite3.o
294 220507 1007870 7769352 sqlite3.c
295 </pre></blockquote>
296
297 <p>The important parts of the output (the parts that the developers pay
298 the most attention to) are shown in red.
299 Basically, the developers want to know the size of the compiled SQLite
300 library and how many CPU cycles were needed to run the performance test.
301
302 </p><p>The output from the
303 <a href="https://sqlite.org/src/file/tool/cg_anno.tcl">cg_anno.tcl</a> script
304 shows the number of CPU cycles spent on each line of code.
305 The report is approximately 80,000 lines long. The following is a brief
306 snippet taken from the middle of the report to show what it looks like:
307
308 </p><blockquote><pre>
309 . SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
310 . MemPage *pPage;
311 . assert( cursorOwnsBtShared(pCur) );
312 . assert( pRes!=0 );
313 . assert( *pRes==0 || *pRes==1 );
314 . assert( pCur-&gt;skipNext==0 || pCur-&gt;eState!=CURSOR_VALID );
315 369,648 pCur-&gt;info.nSize = 0;
316 369,648 pCur-&gt;curFlags &amp;= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
317 369,648 *pRes = 0;
318 739,296 if( pCur-&gt;eState!=CURSOR_VALID ) return btreeNext(pCur, pRes);
319 1,473,580 pPage = pCur-&gt;apPage&#91;pCur-&gt;iPage&#93;;
320 1,841,975 if( (++pCur-&gt;aiIdx&#91;pCur-&gt;iPage&#93;)&gt;=pPage-&gt;nCell ){
321 4,340 pCur-&gt;aiIdx&#91;pCur-&gt;iPage&#93;--;
322 5,593 return btreeNext(pCur, pRes);
323 . }
324 728,110 if( pPage-&gt;leaf ){
325 . return SQLITE_OK;
326 . }else{
327 3,117 return moveToLeftmost(pCur);
328 . }
329 721,876 }
330 </pre></blockquote>
331
332 <p>
333 The numbers on the left are the CPU cycle counts for that line of code,
334 of course.
335
336 </p><p>
337 The cg_anno.tcl script removes extraneous details from the default
338 cachegrind annotation
339 output so that before-and-after reports can be compared using a
340 side-by-side diff to view specific details of how a
341 micro-optimization attempt affected performance.
342
343
344 </p><h1 id="limitations"><span>4. </span>Limitations</h1>
345
346 <p>The use of the standardized speedtest1.c workload and cachegrind has
347 enabled significant performance improvement.
348 However, it is important to recognize the limitations of this approach:
349
350 </p><ul>
351 <li><p>
352 Performance measurements are done with a single compiler (gcc 5.4.0),
353 optimization setting (-Os), and
354 on a single platform (Ubuntu 16.04 LTS on x64). The performance of
355 other compilers and processors may vary.
356
357 </p></li><li><p>
358 The speedtest1.c workload that is being measured tries to be representative
359 of a wide range of typical uses of SQLite. But every application is
360 different. The speedtest1.c workload might not be a good proxy for the
361 kinds of activities performed by some applications. The SQLite developers
362 are constantly working to improve the speedtest1.c program, to make it
363 a better proxy for actual SQLite usage. Community feedback is welcomed.
364
365 </p></li><li><p>
366 The cycle counts provided by cachegrind are a good proxy for actual
367 performance, but they are not 100% accurate.
368
369 </p></li><li><p>
370 Only CPU cycle counts are being measured here.
371 CPU cycle counts are a good proxy for energy consumption,
372 but do not necessary correlate well with real-world timings.
373 Time spent doing I/O is not reflected in the CPU cycle counts,
374 and I/O time predominates in many SQLite usage scenarios.
375 </p></li></ul>
376 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/cpu.in?m=30de04a5bf">2022-01-08 05:02:57</a> UTC </small></i></p>
377