1<!--$Id: max.so,v 10.13 2008/04/29 22:46:51 mbrey Exp $--> 2<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 3<!--See the file LICENSE for redistribution information.--> 4<html> 5<head> 6<title>Berkeley DB Reference Guide: Configuring locking: sizing the system</title> 7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> 8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 9</head> 10<body bgcolor=white> 11<a name="2"><!--meow--></a> 12<table width="100%"><tr valign=top> 13<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Locking Subsystem</dl></b></td> 14<td align=right><a href="../lock/config.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../lock/stdmode.html"><img src="../../images/next.gif" alt="Next"></a> 15</td></tr></table> 16<p align=center><b>Configuring locking: sizing the system</b></p> 17<p>The lock system is sized using the following four methods:</p> 18<blockquote><pre><a href="../../api_c/env_set_lk_max_locks.html">DB_ENV->set_lk_max_locks</a> 19<a href="../../api_c/env_set_lk_max_lockers.html">DB_ENV->set_lk_max_lockers</a> 20<a href="../../api_c/env_set_lk_max_objects.html">DB_ENV->set_lk_max_objects</a> 21<a href="../../api_c/env_set_lk_partitions.html">DB_ENV->set_lk_partitions</a></pre></blockquote> 22<p>The <a href="../../api_c/env_set_lk_max_locks.html">DB_ENV->set_lk_max_locks</a>, <a href="../../api_c/env_set_lk_max_lockers.html">DB_ENV->set_lk_max_lockers</a>, 23and <a href="../../api_c/env_set_lk_max_objects.html">DB_ENV->set_lk_max_objects</a> methods specify the maximum number of 24locks, lockers, and locked objects supported by the lock subsystem, 25respectively. The maximum number of locks is the number of locks that 26can be simultaneously requested in the system. The maximum number of 27lockers is the number of lockers that can simultaneously request locks 28in the system. The maximum number of lock objects is the number of 29objects that can simultaneously be locked in the system. Selecting 30appropriate values requires an understanding of your application and its 31databases. If the values are too small, requests for locks in an 32application will fail. If the values are too large, the locking 33subsystem will consume more resources than is necessary. It is better 34to err in the direction of allocating too many locks, lockers, and 35objects because increasing the number of locks does not require large 36amounts of additional resources. The default values are 1000 of 37each type of object.</p> 38<p>The <a href="../../api_c/env_set_lk_partitions.html">DB_ENV->set_lk_partitions</a> method specifies the number of lock 39table partitions. Each partition may be accessed independently by 40a thread and more partitions can lead to higher levels of concurrency. 41The default is to set the number of partitions to be 10 times the number 42of cpus that the operating system reports at the time the environment is 43created. 44Having more than one partition when there is only one cpu is not beneficial and 45the locking system is more efficient when there is a single partition. 46Operating systems (Linux, Solaris) may report thread contexts as cpus and it 47may be necessary to override the default to force a single partition on 48a single hyperthreaded cpu system. 49Objects and locks are divided among the partitions so it best to allocate 50several locks and objects per partition. The system will force there 51to be at least one per partition. 52If a partition runs out of locks or objects it will steal what is needed 53from the other partitions. This operation could impact performance if 54it occurs too often.</p> 55<p>When configuring a Berkeley DB Concurrent Data Store application, the number of lock objects needed 56is two per open database (one for the database lock, and one for the 57cursor lock when the <a href="../../api_c/env_set_flags.html#DB_CDB_ALLDB">DB_CDB_ALLDB</a> option is not specified). The 58number of locks needed is one per open database handle plus one per 59simultaneous cursor or non-cursor operation.</p> 60<p>Configuring a Berkeley DB Transactional Data Store application is more complicated. The recommended 61algorithm for selecting the maximum number of locks, lockers, and lock 62objects is to run the application under stressful conditions and then 63review the lock system's statistics to determine the maximum number of 64locks, lockers, and lock objects that were used. Then, double these 65values for safety. However, in some large applications, finer 66granularity of control is necessary in order to minimize the size of the 67Lock subsystem.</p> 68<p>The maximum number of lockers can be estimated as follows:</p> 69<p><ul type=disc> 70<li>If the database environment is using transactions, the maximum number 71of lockers can be estimated by adding the number of simultaneously 72active non-transactional cursors open database handles to the number of 73simultaneously active transactions and child transactions (where a child 74transaction is active until it commits or aborts, not until its parent 75commits or aborts). 76<li>If the database environment is not using transactions, the maximum 77number of lockers can be estimated by adding the number of 78simultaneously active non-transactional cursors and open database 79handles to the number of simultaneous non-cursor operations. 80</ul> 81<p>The maximum number of lock objects needed for a single database 82operation can be estimated as follows:</p> 83<p><ul type=disc> 84<li>For Btree and Recno access methods, you will need one lock object per 85level of the database tree, at a minimum. (Unless keys are quite large 86with respect to the page size, neither Recno nor Btree database trees 87should ever be deeper than five levels.) Then, you will need one lock 88object for each leaf page of the database tree that will be 89simultaneously accessed. 90<li>For the Queue access method, you will need one lock object per record 91that is simultaneously accessed. To this, add one lock object per page 92that will be simultaneously accessed. (Because the Queue access method 93uses fixed-length records and the database page size is known, it is 94possible to calculate the number of pages -- and, therefore, the lock 95objects -- required.) Deleted records skipped by a <a href="../../api_c/dbc_get.html#DB_NEXT">DB_NEXT</a> or 96<a href="../../api_c/dbc_get.html#DB_PREV">DB_PREV</a> operation do not require a separate lock object. 97Further, if your application is using transactions, no database 98operation will ever use more than three lock objects at any time. 99<li>For the Hash access method, you only need a single lock object. 100</ul> 101<p>For all access methods, you should then add an additional lock object 102per database for the database's metadata page.</p> 103<p>Note that transactions accumulate locks over the transaction lifetime, 104and the lock objects required by a single transaction is the total lock 105objects required by all of the database operations in the transaction. 106However, a database page (or record, in the case of the Queue access 107method), that is accessed multiple times within a transaction only 108requires a single lock object for the entire transaction.</p> 109<p>The maximum number of locks required by an application cannot be easily 110estimated. It is possible to calculate a maximum number of locks by 111multiplying the maximum number of lockers, times the maximum number of 112lock objects, times two (two for the two possible lock modes for each 113object, read and write). However, this is a pessimal value, and real 114applications are unlikely to actually need that many locks. Reviewing 115the Lock subsystem statistics is the best way to determine this value.</p> 116<table width="100%"><tr><td><br></td><td align=right><a href="../lock/config.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../lock/stdmode.html"><img src="../../images/next.gif" alt="Next"></a> 117</td></tr></table> 118<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 119</body> 120</html> 121