| 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>Deadlock debugging</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 16. The Locking Subsystem" /> |
| 11 | <link rel="prev" href="lock_timeout.html" title="Deadlock detection using timers" /> |
| 12 | <link rel="next" href="lock_page.html" title="Locking granularity" /> |
| 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">Deadlock debugging</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="lock_timeout.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 16. |
| 26 | The Locking Subsystem |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="lock_page.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="lock_deaddbg"></a>Deadlock debugging</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>An occasional debugging problem in Berkeley DB applications is unresolvable |
| 42 | deadlock. The output of the <span class="bold"><strong>-Co</strong></span> flags of the <a href="../api_reference/C/db_stat.html" class="olink">db_stat</a> utility can be used to detect and debug these problems. The following |
| 43 | is a typical example of the output of this utility:</p> |
| 44 | <pre class="programlisting">Locks grouped by object |
| 45 | Locker Mode Count Status ----------- Object ---------- |
| 46 | 1 READ 1 HELD a.db handle 0 |
| 47 | 80000004 WRITE 1 HELD a.db page 3</pre> |
| 48 | <p>In this example, we have opened a database and stored a single key/data |
| 49 | pair in it. Because we have a database handle open, we have a read lock |
| 50 | on that database handle. The database handle lock is the read lock |
| 51 | labeled <span class="emphasis"><em>handle</em></span>. (We can normally ignore handle locks for |
| 52 | the purposes of database debugging, as they will only conflict with |
| 53 | other handle operations, for example, an attempt to remove the database |
| 54 | will block because we are holding the handle locked, but reading and |
| 55 | writing the database will not conflict with the handle lock.)</p> |
| 56 | <p>It is important to note that locker IDs are 32-bit unsigned integers, |
| 57 | and are divided into two name spaces. Locker IDs with the high bit set |
| 58 | (that is, values 80000000 or higher), are locker IDs associated with |
| 59 | transactions. Locker IDs without the high bit set are locker IDs that |
| 60 | are not associated with a transaction. Locker IDs associated with |
| 61 | transactions map one-to-one with the transaction, that is, a transaction |
| 62 | never has more than a single locker ID, and all of the locks acquired |
| 63 | by the transaction will be acquired on behalf of the same locker ID.</p> |
| 64 | <p>We also hold a write lock on the database page where we stored the new |
| 65 | key/data pair. The page lock is labeled <span class="emphasis"><em>page</em></span> and is on page |
| 66 | number 3. If we were to put an additional key/data pair in the |
| 67 | database, we would see the following output:</p> |
| 68 | <pre class="programlisting">Locks grouped by object |
| 69 | Locker Mode Count Status ----------- Object ---------- |
| 70 | 80000004 WRITE 2 HELD a.db page 3 |
| 71 | 1 READ 1 HELD a.db handle 0</pre> |
| 72 | <p>That is, we have acquired a second reference count to page number 3, but |
| 73 | have not acquired any new locks. If we add an entry to a different page |
| 74 | in the database, we would acquire additional locks:</p> |
| 75 | <pre class="programlisting">Locks grouped by object |
| 76 | Locker Mode Count Status ----------- Object ---------- |
| 77 | 1 READ 1 HELD a.db handle 0 |
| 78 | 80000004 WRITE 2 HELD a.db page 3 |
| 79 | 80000004 WRITE 1 HELD a.db page 2</pre> |
| 80 | <p>Here's a simple example of one lock blocking another one:</p> |
| 81 | <pre class="programlisting">Locks grouped by object |
| 82 | Locker Mode Count Status ----------- Object ---------- |
| 83 | 80000004 WRITE 1 HELD a.db page 2 |
| 84 | 80000005 WRITE 1 WAIT a.db page 2 |
| 85 | 1 READ 1 HELD a.db handle 0 |
| 86 | 80000004 READ 1 HELD a.db page 1</pre> |
| 87 | <p>In this example, there are two different transactional lockers (80000004 and |
| 88 | 80000005). Locker 80000004 is holding a write lock on page 2, and |
| 89 | locker 80000005 is waiting for a write lock on page 2. This is not a |
| 90 | deadlock, because locker 80000004 is not blocked on anything. |
| 91 | Presumably, the thread of control using locker 80000004 will proceed, |
| 92 | eventually release its write lock on page 2, at which point the thread |
| 93 | of control using locker 80000005 can also proceed, acquiring a write |
| 94 | lock on page 2.</p> |
| 95 | <p>If lockers 80000004 and 80000005 are not in different threads of |
| 96 | control, the result would be <span class="emphasis"><em>self deadlock</em></span>. Self deadlock |
| 97 | is not a true deadlock, and won't be detected by the Berkeley DB deadlock |
| 98 | detector. It's not a true deadlock because, if work could continue to |
| 99 | be done on behalf of locker 80000004, then the lock would eventually be |
| 100 | released, and locker 80000005 could acquire the lock and itself proceed. |
| 101 | So, the key element is that the thread of control holding the lock |
| 102 | cannot proceed because it is the same thread as is blocked waiting on the |
| 103 | lock.</p> |
| 104 | <p>Here's an example of three transactions reaching true deadlock. First, |
| 105 | three different threads of control opened the database, acquiring three |
| 106 | database handle read locks.</p> |
| 107 | <pre class="programlisting">Locks grouped by object |
| 108 | Locker Mode Count Status ----------- Object ---------- |
| 109 | 1 READ 1 HELD a.db handle 0 |
| 110 | 3 READ 1 HELD a.db handle 0 |
| 111 | 5 READ 1 HELD a.db handle 0</pre> |
| 112 | <p>The three threads then each began a transaction, and put a key/data pair |
| 113 | on a different page:</p> |
| 114 | <pre class="programlisting">Locks grouped by object |
| 115 | Locker Mode Count Status ----------- Object ---------- |
| 116 | 80000008 WRITE 1 HELD a.db page 4 |
| 117 | 1 READ 1 HELD a.db handle 0 |
| 118 | 3 READ 1 HELD a.db handle 0 |
| 119 | 5 READ 1 HELD a.db handle 0 |
| 120 | 80000006 READ 1 HELD a.db page 1 |
| 121 | 80000007 READ 1 HELD a.db page 1 |
| 122 | 80000008 READ 1 HELD a.db page 1 |
| 123 | 80000006 WRITE 1 HELD a.db page 2 |
| 124 | 80000007 WRITE 1 HELD a.db page 3</pre> |
| 125 | <p>The thread using locker 80000006 put a new key/data pair on page 2, the |
| 126 | thread using locker 80000007, on page 3, and the thread using locker |
| 127 | 80000008 on page 4. Because the database is a 2-level Btree, the tree |
| 128 | was searched, and so each transaction acquired a read lock on the Btree |
| 129 | root page (page 1) as part of this operation.</p> |
| 130 | <p>The three threads then each attempted to put a second key/data pair on |
| 131 | a page currently locked by another thread. The thread using locker |
| 132 | 80000006 tried to put a key/data pair on page 3, the thread using locker |
| 133 | 80000007 on page 4, and the thread using locker 80000008 on page 2:</p> |
| 134 | <pre class="programlisting">Locks grouped by object |
| 135 | Locker Mode Count Status ----------- Object ---------- |
| 136 | 80000008 WRITE 1 HELD a.db page 4 |
| 137 | 80000007 WRITE 1 WAIT a.db page 4 |
| 138 | 1 READ 1 HELD a.db handle 0 |
| 139 | 3 READ 1 HELD a.db handle 0 |
| 140 | 5 READ 1 HELD a.db handle 0 |
| 141 | 80000006 READ 2 HELD a.db page 1 |
| 142 | 80000007 READ 2 HELD a.db page 1 |
| 143 | 80000008 READ 2 HELD a.db page 1 |
| 144 | 80000006 WRITE 1 HELD a.db page 2 |
| 145 | 80000008 WRITE 1 WAIT a.db page 2 |
| 146 | 80000007 WRITE 1 HELD a.db page 3 |
| 147 | 80000006 WRITE 1 WAIT a.db page 3</pre> |
| 148 | <p>Now, each of the threads of control is blocked, waiting on a different |
| 149 | thread of control. |
| 150 | The thread using locker 80000007 is blocked by |
| 151 | the thread using locker 80000008, due to the lock on page 4. |
| 152 | The thread using locker 80000008 is blocked by |
| 153 | the thread using locker 80000006, due to the lock on page 2. |
| 154 | And the thread using locker 80000006 is blocked by |
| 155 | the thread using locker 80000007, due to the lock on page 3. |
| 156 | Since none of the threads of control can make |
| 157 | progress, one of them will have to be killed in order to resolve the |
| 158 | deadlock.</p> |
| 159 | </div> |
| 160 | <div class="navfooter"> |
| 161 | <hr /> |
| 162 | <table width="100%" summary="Navigation footer"> |
| 163 | <tr> |
| 164 | <td width="40%" align="left"><a accesskey="p" href="lock_timeout.html">Prev</a> </td> |
| 165 | <td width="20%" align="center"> |
| 166 | <a accesskey="u" href="lock.html">Up</a> |
| 167 | </td> |
| 168 | <td width="40%" align="right"> <a accesskey="n" href="lock_page.html">Next</a></td> |
| 169 | </tr> |
| 170 | <tr> |
| 171 | <td width="40%" align="left" valign="top">Deadlock detection using timers </td> |
| 172 | <td width="20%" align="center"> |
| 173 | <a accesskey="h" href="index.html">Home</a> |
| 174 | </td> |
| 175 | <td width="40%" align="right" valign="top"> Locking granularity</td> |
| 176 | </tr> |
| 177 | </table> |
| 178 | </div> |
| 179 | </body> |
| 180 | </html> |