| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Warming the memory pool</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="mp.html" title="Chapter 18. The Memory Pool Subsystem" /> |
| 11 | <link rel="prev" href="mp_config.html" title="Configuring the memory pool" /> |
| 12 | <link rel="next" href="txn.html" title="Chapter 19. The Transaction Subsystem" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Warming the memory pool</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="mp_config.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 18. |
| 26 | The Memory Pool Subsystem |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="txn.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="mp_warm"></a>Warming the memory pool</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <div class="toc"> |
| 42 | <dl> |
| 43 | <dt> |
| 44 | <span class="sect2"> |
| 45 | <a href="mp_warm.html#warm_cache">The warm_cache() function</a> |
| 46 | </span> |
| 47 | </dt> |
| 48 | </dl> |
| 49 | </div> |
| 50 | <p> |
| 51 | Some applications find it is useful to pre-load the memory pool |
| 52 | upon application startup. This is a strictly optional activity that |
| 53 | provides faster initial access to your data at the expense of |
| 54 | longer application startup times. |
| 55 | </p> |
| 56 | <p> |
| 57 | To warm the cache, you simply have to read the records that your |
| 58 | application will operate on most frequently. You can do this with |
| 59 | normal database reads, and you can also use cursors. But the most |
| 60 | efficient way to warm the cache is to use memory pool APIs to get |
| 61 | the pages that contain your most frequently accessed records. |
| 62 | </p> |
| 63 | <p> |
| 64 | You read pages into the memory pool using the |
| 65 | <code class="methodname">DB_MPOOLFILE->get()</code> method. This method |
| 66 | acquires locks on the page, so immediately upon getting the page |
| 67 | you need to put it so as to release the locks. |
| 68 | </p> |
| 69 | <p> |
| 70 | Also, you obtain a memory pool file handle using a database handle. |
| 71 | This means that if your data is contained in more than one Berkeley |
| 72 | DB database, you must operate on each database handle in turn. |
| 73 | </p> |
| 74 | <p> |
| 75 | The following example code illustrates this. It does the following: |
| 76 | </p> |
| 77 | <div class="itemizedlist"> |
| 78 | <ul type="disc"> |
| 79 | <li> |
| 80 | <p> |
| 81 | Opens an environment and two database handles. |
| 82 | </p> |
| 83 | </li> |
| 84 | <li> |
| 85 | <p> |
| 86 | Determines how many database pages can fit into the memory |
| 87 | pool. |
| 88 | </p> |
| 89 | </li> |
| 90 | <li> |
| 91 | <p> |
| 92 | Uses <code class="methodname">DB_MPOOLFILE->get()</code> and |
| 93 | <code class="methodname">DB_MPOOLFILE->put()</code> |
| 94 | to load that number of pages into the memory pool. |
| 95 | </p> |
| 96 | </li> |
| 97 | </ul> |
| 98 | </div> |
| 99 | <p> |
| 100 | First, we include the libraries that we need, forward declare some |
| 101 | functions, and intialize some variables. |
| 102 | </p> |
| 103 | <a id="prog_mp01-1"></a> |
| 104 | <pre class="programlisting">#include <stdio.h> |
| 105 | #include <stdlib.h> |
| 106 | #include <string.h> |
| 107 | #include <db.h> |
| 108 | |
| 109 | /* Forward declarations */ |
| 110 | int warm_cache(DB *, int *, int); |
| 111 | int open_db(DB_ENV *, DB **, const char *); |
| 112 | |
| 113 | int |
| 114 | main(void) |
| 115 | { |
| 116 | DB *dbp1 = 0, *dbp2 = 0; |
| 117 | DB_ENV *envp = 0; |
| 118 | u_int32_t env_flags, pagesize, gbytes, bytes; |
| 119 | int ret = 0, ret_t = 0, numcachepages, pagecount; </pre> |
| 120 | <p> |
| 121 | Then we open the environment and our databases. The |
| 122 | <code class="methodname">open_db()</code> function that we use here simply |
| 123 | opens a database. We will provide that code at the end of this |
| 124 | example, but it should hold no surprises for you. |
| 125 | We only use the function so as to reuse the code. |
| 126 | </p> |
| 127 | <a id="prog_mp01-2"></a> |
| 128 | <pre class="programlisting"> /* |
| 129 | * Open the environment and the databases |
| 130 | */ |
| 131 | ret = db_env_create(&envp, 0); |
| 132 | if (ret != 0) { |
| 133 | fprintf(stderr, "Error creating environment handle: %s\n", |
| 134 | db_strerror(ret)); |
| 135 | goto err; |
| 136 | } |
| 137 | |
| 138 | env_flags = |
| 139 | DB_CREATE | /* Create the environment if it does |
| 140 | not exist */ |
| 141 | DB_RECOVER | /* Run normal recovery. */ |
| 142 | DB_INIT_LOCK | /* Initialize the locking subsystem */ |
| 143 | DB_INIT_LOG | /* Initialize the logging subsystem */ |
| 144 | DB_INIT_TXN | /* Initialize the transactional subsystem. This |
| 145 | * also turns on logging. */ |
| 146 | DB_INIT_MPOOL; /* Initialize the memory pool */ |
| 147 | |
| 148 | /* Now actually open the environment */ |
| 149 | ret = envp->open(envp, "./env", env_flags, 0); |
| 150 | if (ret != 0) { |
| 151 | fprintf(stderr, "Error opening environment: %s\n", |
| 152 | db_strerror(ret)); |
| 153 | goto err; |
| 154 | } |
| 155 | |
| 156 | ret = open_db(envp, &dbp1, "mydb1.db"); |
| 157 | if (ret != 0) |
| 158 | goto err; |
| 159 | |
| 160 | ret = open_db(envp, &dbp2, "mydb2.db"); |
| 161 | if (ret != 0) |
| 162 | goto err; </pre> |
| 163 | <p> |
| 164 | Next we determine how many database pages we can fit into the |
| 165 | cache. We do this by finding out how large our pages are, and then |
| 166 | finding out how large our cache can be. |
| 167 | </p> |
| 168 | <a id="prog_mp01-3"></a> |
| 169 | <pre class="programlisting"> /* Find out how many pages can fit at most in the cache */ |
| 170 | ret = envp->get_mp_pagesize(envp, &pagesize); |
| 171 | if (ret != 0) { |
| 172 | fprintf(stderr, "Error retrieving the cache pagesize: %s\n", |
| 173 | db_strerror(ret)); |
| 174 | goto err; |
| 175 | } |
| 176 | |
| 177 | ret = envp->get_cache_max(envp, &gbytes, &bytes); |
| 178 | if (ret != 0) { |
| 179 | fprintf(stderr, "Error retrieving maximum cache size: %s\n", |
| 180 | db_strerror(ret)); |
| 181 | goto err; |
| 182 | } |
| 183 | /* Avoid an overflow by first calculating pages per gigabyte. */ |
| 184 | numcachepages = gbytes * ((1024 * 1024 * 1024) / pagesize); |
| 185 | numcachepages += bytes / pagesize; </pre> |
| 186 | <p> |
| 187 | Now we call our <code class="methodname">warm_cache()</code> |
| 188 | function. We will describe this function in a little while, but |
| 189 | note that we call <code class="methodname">warm_cache()</code> |
| 190 | twice. This is because our example uses two databases, and the |
| 191 | memory pool methods operate on a per-handle basis. |
| 192 | </p> |
| 193 | <a id="prog_mp01-4"></a> |
| 194 | <pre class="programlisting"> /* |
| 195 | * Warm the cache by loading pages from each of the databases |
| 196 | * in turn. |
| 197 | */ |
| 198 | pagecount = 0; |
| 199 | ret = warm_cache(dbp1, &pagecount, numcachepages); |
| 200 | if (ret != 0) { |
| 201 | fprintf(stderr, "Error warming the cache: %s\n", |
| 202 | db_strerror(ret)); |
| 203 | goto err; |
| 204 | } |
| 205 | |
| 206 | ret = warm_cache(dbp2, &pagecount, numcachepages); |
| 207 | if (ret != 0) { |
| 208 | fprintf(stderr, "Error warming the cache: %s\n", |
| 209 | db_strerror(ret)); |
| 210 | goto err; |
| 211 | } </pre> |
| 212 | <p> |
| 213 | Now we close all our handles and finish our |
| 214 | <code class="methodname">main()</code> function. Again, this is |
| 215 | straight-forward boilerplate code that we provide simply to be |
| 216 | complete. |
| 217 | </p> |
| 218 | <a id="prog_mp01-5"></a> |
| 219 | <pre class="programlisting">err: |
| 220 | /* Close our database handles, if they were opened. */ |
| 221 | if (dbp1 != NULL) { |
| 222 | ret_t = dbp1->close(dbp1, 0); |
| 223 | if (ret_t != 0) { |
| 224 | fprintf(stderr, "dbp1 close failed: %s\n", |
| 225 | db_strerror(ret_t)); |
| 226 | ret = ret_t; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (dbp2 != NULL) { |
| 231 | ret_t = dbp2->close(dbp2, 0); |
| 232 | if (ret_t != 0) { |
| 233 | fprintf(stderr, "dbp2 close failed: %s\n", |
| 234 | db_strerror(ret_t)); |
| 235 | ret = ret_t; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* Close our environment, if it was opened. */ |
| 240 | if (envp != NULL) { |
| 241 | ret_t = envp->close(envp, 0); |
| 242 | if (ret_t != 0) { |
| 243 | fprintf(stderr, "environment close failed: %s\n", |
| 244 | db_strerror(ret_t)); |
| 245 | ret = ret_t; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* Final status message and return. */ |
| 250 | printf("I'm all done.\n"); |
| 251 | return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 252 | } </pre> |
| 253 | <p> |
| 254 | As noted above, this example uses an <code class="methodname">open_db()</code> |
| 255 | function, which opens a database handle inside the provided |
| 256 | environment. To be complete, this is the implementation of that |
| 257 | function: |
| 258 | </p> |
| 259 | <a id="prog_mp01-6"></a> |
| 260 | <pre class="programlisting">/* Open a database handle */ |
| 261 | int |
| 262 | open_db(DB_ENV *envp, DB **dbpp, const char *file_name) |
| 263 | { |
| 264 | int ret = 0; |
| 265 | u_int32_t db_flags = 0; |
| 266 | DB *dbp; |
| 267 | |
| 268 | /* Open the database */ |
| 269 | ret = db_create(&dbp, envp, 0); |
| 270 | if (ret != 0) { |
| 271 | fprintf(stderr, "Error opening database: %s : %s\n", |
| 272 | file_name, db_strerror(ret)); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | /* Point to the memory malloc'd by db_create() */ |
| 277 | *dbpp = dbp; |
| 278 | |
| 279 | db_flags = DB_CREATE | /* Create the database if it does |
| 280 | not exist */ |
| 281 | DB_AUTO_COMMIT; /* Allow autocommit */ |
| 282 | |
| 283 | ret = dbp->open(dbp, /* Pointer to the database */ |
| 284 | 0, /* Txn pointer */ |
| 285 | file_name, /* File name */ |
| 286 | 0, /* Logical db name */ |
| 287 | DB_BTREE, /* Database type (using btree) */ |
| 288 | db_flags, /* Open flags */ |
| 289 | 0); /* File mode. Using defaults */ |
| 290 | if (ret != 0) { |
| 291 | dbp->err(dbp, ret, "Database open failed: %s : %s\n", |
| 292 | file_name, db_strerror(ret)); |
| 293 | return ret; |
| 294 | } |
| 295 | return 0; |
| 296 | } </pre> |
| 297 | <div class="sect2" lang="en" xml:lang="en"> |
| 298 | <div class="titlepage"> |
| 299 | <div> |
| 300 | <div> |
| 301 | <h3 class="title"><a id="warm_cache"></a>The warm_cache() function</h3> |
| 302 | </div> |
| 303 | </div> |
| 304 | </div> |
| 305 | <p> |
| 306 | In this section we provide the implementation of the |
| 307 | <code class="methodname">warm_cache()</code> function. This example |
| 308 | function simply loads all the database pages that will fit into |
| 309 | the memory pool. It starts from the first database page and |
| 310 | continues until it either runs out of database pages or it runs |
| 311 | out of room in the memory pool. |
| 312 | </p> |
| 313 | <a id="prog_mp01-7"></a> |
| 314 | <pre class="programlisting">/* Warm the cache */ |
| 315 | int |
| 316 | warm_cache(DB *dbp, int *pagecountp, int numcachepages) |
| 317 | { |
| 318 | DB_MPOOLFILE *mpf = 0; |
| 319 | void *page_addrp = 0; |
| 320 | db_pgno_t page_number = 0; |
| 321 | int ret = 0; |
| 322 | int pagecount = *pagecountp; |
| 323 | |
| 324 | /* |
| 325 | * Get the mpool handle |
| 326 | */ |
| 327 | mpf = dbp->get_mpf(dbp); |
| 328 | |
| 329 | /* Load pages until there are no more pages in the database, |
| 330 | * or until we've put as many pages into the cache as will fit. |
| 331 | */ |
| 332 | while (ret != DB_PAGE_NOTFOUND && pagecount < numcachepages) { |
| 333 | /* |
| 334 | * Get the page from the cache. This causes DB to retrieve |
| 335 | * the page from disk if it isn't already in the cache. |
| 336 | */ |
| 337 | ret = mpf->get(mpf, &page_number, 0, 0, &page_addrp); |
| 338 | if (ret && ret != DB_PAGE_NOTFOUND) { |
| 339 | fprintf(stderr, "Error retrieving db page: %i : %s\n", |
| 340 | page_number, db_strerror(ret)); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * If a page was retrieved, put it back into the cache. This |
| 346 | * releases the page latch so that the page can be evicted |
| 347 | * if DB needs more room in the cache at some later time. |
| 348 | */ |
| 349 | if (ret != DB_PAGE_NOTFOUND) { |
| 350 | ret = mpf->put(mpf, page_addrp, DB_PRIORITY_UNCHANGED, 0); |
| 351 | if (ret) { |
| 352 | fprintf(stderr, "Error putting db page: %i : %s\n", |
| 353 | page_number, db_strerror(ret)); |
| 354 | return ret; |
| 355 | } |
| 356 | } |
| 357 | ++page_number; |
| 358 | ++pagecount; |
| 359 | *pagecountp = pagecount; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } </pre> |
| 364 | </div> |
| 365 | </div> |
| 366 | <div class="navfooter"> |
| 367 | <hr /> |
| 368 | <table width="100%" summary="Navigation footer"> |
| 369 | <tr> |
| 370 | <td width="40%" align="left"><a accesskey="p" href="mp_config.html">Prev</a> </td> |
| 371 | <td width="20%" align="center"> |
| 372 | <a accesskey="u" href="mp.html">Up</a> |
| 373 | </td> |
| 374 | <td width="40%" align="right"> <a accesskey="n" href="txn.html">Next</a></td> |
| 375 | </tr> |
| 376 | <tr> |
| 377 | <td width="40%" align="left" valign="top">Configuring the memory pool </td> |
| 378 | <td width="20%" align="center"> |
| 379 | <a accesskey="h" href="index.html">Home</a> |
| 380 | </td> |
| 381 | <td width="40%" align="right" valign="top"> Chapter 19. |
| 382 | The Transaction Subsystem |
| 383 | </td> |
| 384 | </tr> |
| 385 | </table> |
| 386 | </div> |
| 387 | </body> |
| 388 | </html> |