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>Configuring locking: sizing the system</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="lock.html" title="Chapter 15. The Locking Subsystem" /> 11 <link rel="prev" href="lock_config.html" title="Configuring locking" /> 12 <link rel="next" href="lock_stdmode.html" title="Standard lock modes" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Configuring locking: sizing the system</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="lock_config.html">Prev</a> </td> 22 <th width="60%" align="center">Chapter 15. 23 The Locking Subsystem 24 </th> 25 <td width="20%" align="right"> <a accesskey="n" href="lock_stdmode.html">Next</a></td> 26 </tr> 27 </table> 28 <hr /> 29 </div> 30 <div class="sect1" lang="en" xml:lang="en"> 31 <div class="titlepage"> 32 <div> 33 <div> 34 <h2 class="title" style="clear: both"><a id="lock_max"></a>Configuring locking: sizing the system</h2> 35 </div> 36 </div> 37 </div> 38 <p>The lock system is sized using the following four methods:</p> 39 <div class="orderedlist"> 40 <ol type="1"> 41 <li> 42 <p> 43 <a href="../api_reference/C/envset_lk_max_locks.html" class="olink">DB_ENV->set_lk_max_locks()</a> 44 </p> 45 </li> 46 <li> 47 <p> 48 <a href="../api_reference/C/envset_lk_max_lockers.html" class="olink">DB_ENV->set_lk_max_lockers()</a> 49 </p> 50 </li> 51 <li> 52 <p> 53 <a href="../api_reference/C/envset_lk_max_objects.html" class="olink">DB_ENV->set_lk_max_objects()</a> 54 </p> 55 </li> 56 <li> 57 <p> 58 <a href="../api_reference/C/envset_lk_partitions.html" class="olink">DB_ENV->set_lk_partitions()</a> 59 </p> 60 </li> 61 </ol> 62 </div> 63 <p>The <a href="../api_reference/C/envset_lk_max_locks.html" class="olink">DB_ENV->set_lk_max_locks()</a>, <a href="../api_reference/C/envset_lk_max_lockers.html" class="olink">DB_ENV->set_lk_max_lockers()</a>, and 64and <a href="../api_reference/C/envset_lk_max_objects.html" class="olink">DB_ENV->set_lk_max_objects()</a> methods specify the maximum number of 65locks, lockers, and locked objects supported by the lock subsystem, 66respectively. The maximum number of locks is the number of locks that 67can be simultaneously requested in the system. The maximum number of 68lockers is the number of lockers that can simultaneously request locks 69in the system. The maximum number of lock objects is the number of 70objects that can simultaneously be locked in the system. Selecting 71appropriate values requires an understanding of your application and its 72databases. If the values are too small, requests for locks in an 73application will fail. If the values are too large, the locking 74subsystem will consume more resources than is necessary. It is better 75to err in the direction of allocating too many locks, lockers, and 76objects because increasing the number of locks does not require large 77amounts of additional resources. The default values are 1000 of 78each type of object.</p> 79 <p>The <a href="../api_reference/C/envset_lk_partitions.html" class="olink">DB_ENV->set_lk_partitions()</a> method specifies the number of lock 80table partitions. Each partition may be accessed independently by 81a thread and more partitions can lead to higher levels of concurrency. 82The default is to set the number of partitions to be 10 times the number 83of cpus that the operating system reports at the time the environment is 84created. Having more than one partition when there is only one cpu is not beneficial and 85the locking system is more efficient when there is a single partition. 86Operating systems (Linux, Solaris) may report thread contexts as cpus and it 87may be necessary to override the default to force a single partition on 88a single hyperthreaded cpu system. 89Objects and locks are divided among the partitions so it best to allocate 90several locks and objects per partition. The system will force there 91to be at least one per partition. 92If a partition runs out of locks or objects it will steal what is needed 93from the other partitions. This operation could impact performance if 94it occurs too often.</p> 95 <p>When configuring a Berkeley DB Concurrent Data Store application, the number of lock objects needed 96is two per open database (one for the database lock, and one for the 97cursor lock when the <a href="../api_reference/C/envset_flags.html#set_flags_DB_CDB_ALLDB" class="olink">DB_CDB_ALLDB</a> option is not specified). The 98number of locks needed is one per open database handle plus one per 99simultaneous cursor or non-cursor operation.</p> 100 <p>Configuring a Berkeley DB Transactional Data Store application is more complicated. The recommended 101algorithm for selecting the maximum number of locks, lockers, and lock 102objects is to run the application under stressful conditions and then 103review the lock system's statistics to determine the maximum number of 104locks, lockers, and lock objects that were used. Then, double these 105values for safety. However, in some large applications, finer 106granularity of control is necessary in order to minimize the size of the 107Lock subsystem.</p> 108 <p>The maximum number of lockers can be estimated as follows:</p> 109 <div class="itemizedlist"> 110 <ul type="disc"> 111 <li>If the database environment is using transactions, the maximum number 112of lockers can be estimated by adding the number of simultaneously 113active non-transactional cursors open database handles to the number of 114simultaneously active transactions and child transactions (where a child 115transaction is active until it commits or aborts, not until its parent 116commits or aborts).</li> 117 <li>If the database environment is not using transactions, the maximum 118number of lockers can be estimated by adding the number of 119simultaneously active non-transactional cursors and open database 120handles to the number of simultaneous non-cursor operations.</li> 121 </ul> 122 </div> 123 <p>The maximum number of lock objects needed for a single database 124operation can be estimated as follows:</p> 125 <div class="itemizedlist"> 126 <ul type="disc"> 127 <li>For Btree and Recno access methods, you will need one lock object per 128level of the database tree, at a minimum. (Unless keys are quite large 129with respect to the page size, neither Recno nor Btree database trees 130should ever be deeper than five levels.) Then, you will need one lock 131object for each leaf page of the database tree that will be 132simultaneously accessed.</li> 133 <li>For the Queue access method, you will need one lock object per record 134that is simultaneously accessed. To this, add one lock object per page 135that will be simultaneously accessed. (Because the Queue access method 136uses fixed-length records and the database page size is known, it is 137possible to calculate the number of pages -- and, therefore, the lock 138objects -- required.) Deleted records skipped by a <a href="../api_reference/C/dbcget.html#dbcget_DB_NEXT" class="olink">DB_NEXT</a> or 139<a href="../api_reference/C/dbcget.html#dbcget_DB_PREV" class="olink">DB_PREV</a> operation do not require a separate lock object. 140Further, if your application is using transactions, no database 141operation will ever use more than three lock objects at any time.</li> 142 <li>For the Hash access method, you only need a single lock object.</li> 143 </ul> 144 </div> 145 <p>For all access methods, you should then add an additional lock object 146per database for the database's metadata page.</p> 147 <p>Note that transactions accumulate locks over the transaction lifetime, 148and the lock objects required by a single transaction is the total lock 149objects required by all of the database operations in the transaction. 150However, a database page (or record, in the case of the Queue access 151method), that is accessed multiple times within a transaction only 152requires a single lock object for the entire transaction.</p> 153 <p>The maximum number of locks required by an application cannot be easily 154estimated. It is possible to calculate a maximum number of locks by 155multiplying the maximum number of lockers, times the maximum number of 156lock objects, times two (two for the two possible lock modes for each 157object, read and write). However, this is a pessimal value, and real 158applications are unlikely to actually need that many locks. Reviewing 159the Lock subsystem statistics is the best way to determine this value.</p> 160 </div> 161 <div class="navfooter"> 162 <hr /> 163 <table width="100%" summary="Navigation footer"> 164 <tr> 165 <td width="40%" align="left"><a accesskey="p" href="lock_config.html">Prev</a> </td> 166 <td width="20%" align="center"> 167 <a accesskey="u" href="lock.html">Up</a> 168 </td> 169 <td width="40%" align="right"> <a accesskey="n" href="lock_stdmode.html">Next</a></td> 170 </tr> 171 <tr> 172 <td width="40%" align="left" valign="top">Configuring locking </td> 173 <td width="20%" align="center"> 174 <a accesskey="h" href="index.html">Home</a> 175 </td> 176 <td width="40%" align="right" valign="top"> Standard lock modes</td> 177 </tr> 178 </table> 179 </div> 180 </body> 181</html> 182