gfs2-glocks.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Glock internal locking rules
  2. ------------------------------
  3. This documents the basic principles of the glock state machine
  4. internals. Each glock (struct gfs2_glock in fs/gfs2/incore.h)
  5. has two main (internal) locks:
  6. 1. A spinlock (gl_lockref.lock) which protects the internal state such
  7. as gl_state, gl_target and the list of holders (gl_holders)
  8. 2. A non-blocking bit lock, GLF_LOCK, which is used to prevent other
  9. threads from making calls to the DLM, etc. at the same time. If a
  10. thread takes this lock, it must then call run_queue (usually via the
  11. workqueue) when it releases it in order to ensure any pending tasks
  12. are completed.
  13. The gl_holders list contains all the queued lock requests (not
  14. just the holders) associated with the glock. If there are any
  15. held locks, then they will be contiguous entries at the head
  16. of the list. Locks are granted in strictly the order that they
  17. are queued, except for those marked LM_FLAG_PRIORITY which are
  18. used only during recovery, and even then only for journal locks.
  19. There are three lock states that users of the glock layer can request,
  20. namely shared (SH), deferred (DF) and exclusive (EX). Those translate
  21. to the following DLM lock modes:
  22. Glock mode | DLM lock mode
  23. ------------------------------
  24. UN | IV/NL Unlocked (no DLM lock associated with glock) or NL
  25. SH | PR (Protected read)
  26. DF | CW (Concurrent write)
  27. EX | EX (Exclusive)
  28. Thus DF is basically a shared mode which is incompatible with the "normal"
  29. shared lock mode, SH. In GFS2 the DF mode is used exclusively for direct I/O
  30. operations. The glocks are basically a lock plus some routines which deal
  31. with cache management. The following rules apply for the cache:
  32. Glock mode | Cache data | Cache Metadata | Dirty Data | Dirty Metadata
  33. --------------------------------------------------------------------------
  34. UN | No | No | No | No
  35. SH | Yes | Yes | No | No
  36. DF | No | Yes | No | No
  37. EX | Yes | Yes | Yes | Yes
  38. These rules are implemented using the various glock operations which
  39. are defined for each type of glock. Not all types of glocks use
  40. all the modes. Only inode glocks use the DF mode for example.
  41. Table of glock operations and per type constants:
  42. Field | Purpose
  43. ----------------------------------------------------------------------------
  44. go_xmote_th | Called before remote state change (e.g. to sync dirty data)
  45. go_xmote_bh | Called after remote state change (e.g. to refill cache)
  46. go_inval | Called if remote state change requires invalidating the cache
  47. go_demote_ok | Returns boolean value of whether its ok to demote a glock
  48. | (e.g. checks timeout, and that there is no cached data)
  49. go_lock | Called for the first local holder of a lock
  50. go_unlock | Called on the final local unlock of a lock
  51. go_dump | Called to print content of object for debugfs file, or on
  52. | error to dump glock to the log.
  53. go_type | The type of the glock, LM_TYPE_.....
  54. go_callback | Called if the DLM sends a callback to drop this lock
  55. go_flags | GLOF_ASPACE is set, if the glock has an address space
  56. | associated with it
  57. The minimum hold time for each lock is the time after a remote lock
  58. grant for which we ignore remote demote requests. This is in order to
  59. prevent a situation where locks are being bounced around the cluster
  60. from node to node with none of the nodes making any progress. This
  61. tends to show up most with shared mmaped files which are being written
  62. to by multiple nodes. By delaying the demotion in response to a
  63. remote callback, that gives the userspace program time to make
  64. some progress before the pages are unmapped.
  65. There is a plan to try and remove the go_lock and go_unlock callbacks
  66. if possible, in order to try and speed up the fast path though the locking.
  67. Also, eventually we hope to make the glock "EX" mode locally shared
  68. such that any local locking will be done with the i_mutex as required
  69. rather than via the glock.
  70. Locking rules for glock operations:
  71. Operation | GLF_LOCK bit lock held | gl_lockref.lock spinlock held
  72. -------------------------------------------------------------------------
  73. go_xmote_th | Yes | No
  74. go_xmote_bh | Yes | No
  75. go_inval | Yes | No
  76. go_demote_ok | Sometimes | Yes
  77. go_lock | Yes | No
  78. go_unlock | Yes | No
  79. go_dump | Sometimes | Yes
  80. go_callback | Sometimes (N/A) | Yes
  81. N.B. Operations must not drop either the bit lock or the spinlock
  82. if its held on entry. go_dump and do_demote_ok must never block.
  83. Note that go_dump will only be called if the glock's state
  84. indicates that it is caching uptodate data.
  85. Glock locking order within GFS2:
  86. 1. i_mutex (if required)
  87. 2. Rename glock (for rename only)
  88. 3. Inode glock(s)
  89. (Parents before children, inodes at "same level" with same parent in
  90. lock number order)
  91. 4. Rgrp glock(s) (for (de)allocation operations)
  92. 5. Transaction glock (via gfs2_trans_begin) for non-read operations
  93. 6. Page lock (always last, very important!)
  94. There are two glocks per inode. One deals with access to the inode
  95. itself (locking order as above), and the other, known as the iopen
  96. glock is used in conjunction with the i_nlink field in the inode to
  97. determine the lifetime of the inode in question. Locking of inodes
  98. is on a per-inode basis. Locking of rgrps is on a per rgrp basis.
  99. In general we prefer to lock local locks prior to cluster locks.
  100. Glock Statistics
  101. ------------------
  102. The stats are divided into two sets: those relating to the
  103. super block and those relating to an individual glock. The
  104. super block stats are done on a per cpu basis in order to
  105. try and reduce the overhead of gathering them. They are also
  106. further divided by glock type. All timings are in nanoseconds.
  107. In the case of both the super block and glock statistics,
  108. the same information is gathered in each case. The super
  109. block timing statistics are used to provide default values for
  110. the glock timing statistics, so that newly created glocks
  111. should have, as far as possible, a sensible starting point.
  112. The per-glock counters are initialised to zero when the
  113. glock is created. The per-glock statistics are lost when
  114. the glock is ejected from memory.
  115. The statistics are divided into three pairs of mean and
  116. variance, plus two counters. The mean/variance pairs are
  117. smoothed exponential estimates and the algorithm used is
  118. one which will be very familiar to those used to calculation
  119. of round trip times in network code. See "TCP/IP Illustrated,
  120. Volume 1", W. Richard Stevens, sect 21.3, "Round-Trip Time Measurement",
  121. p. 299 and onwards. Also, Volume 2, Sect. 25.10, p. 838 and onwards.
  122. Unlike the TCP/IP Illustrated case, the mean and variance are
  123. not scaled, but are in units of integer nanoseconds.
  124. The three pairs of mean/variance measure the following
  125. things:
  126. 1. DLM lock time (non-blocking requests)
  127. 2. DLM lock time (blocking requests)
  128. 3. Inter-request time (again to the DLM)
  129. A non-blocking request is one which will complete right
  130. away, whatever the state of the DLM lock in question. That
  131. currently means any requests when (a) the current state of
  132. the lock is exclusive, i.e. a lock demotion (b) the requested
  133. state is either null or unlocked (again, a demotion) or (c) the
  134. "try lock" flag is set. A blocking request covers all the other
  135. lock requests.
  136. There are two counters. The first is there primarily to show
  137. how many lock requests have been made, and thus how much data
  138. has gone into the mean/variance calculations. The other counter
  139. is counting queuing of holders at the top layer of the glock
  140. code. Hopefully that number will be a lot larger than the number
  141. of dlm lock requests issued.
  142. So why gather these statistics? There are several reasons
  143. we'd like to get a better idea of these timings:
  144. 1. To be able to better set the glock "min hold time"
  145. 2. To spot performance issues more easily
  146. 3. To improve the algorithm for selecting resource groups for
  147. allocation (to base it on lock wait time, rather than blindly
  148. using a "try lock")
  149. Due to the smoothing action of the updates, a step change in
  150. some input quantity being sampled will only fully be taken
  151. into account after 8 samples (or 4 for the variance) and this
  152. needs to be carefully considered when interpreting the
  153. results.
  154. Knowing both the time it takes a lock request to complete and
  155. the average time between lock requests for a glock means we
  156. can compute the total percentage of the time for which the
  157. node is able to use a glock vs. time that the rest of the
  158. cluster has its share. That will be very useful when setting
  159. the lock min hold time.
  160. Great care has been taken to ensure that we
  161. measure exactly the quantities that we want, as accurately
  162. as possible. There are always inaccuracies in any
  163. measuring system, but I hope this is as accurate as we
  164. can reasonably make it.
  165. Per sb stats can be found here:
  166. /sys/kernel/debug/gfs2/<fsname>/sbstats
  167. Per glock stats can be found here:
  168. /sys/kernel/debug/gfs2/<fsname>/glstats
  169. Assuming that debugfs is mounted on /sys/kernel/debug and also
  170. that <fsname> is replaced with the name of the gfs2 filesystem
  171. in question.
  172. The abbreviations used in the output as are follows:
  173. srtt - Smoothed round trip time for non-blocking dlm requests
  174. srttvar - Variance estimate for srtt
  175. srttb - Smoothed round trip time for (potentially) blocking dlm requests
  176. srttvarb - Variance estimate for srttb
  177. sirt - Smoothed inter-request time (for dlm requests)
  178. sirtvar - Variance estimate for sirt
  179. dlm - Number of dlm requests made (dcnt in glstats file)
  180. queue - Number of glock requests queued (qcnt in glstats file)
  181. The sbstats file contains a set of these stats for each glock type (so 8 lines
  182. for each type) and for each cpu (one column per cpu). The glstats file contains
  183. a set of these stats for each glock in a similar format to the glocks file, but
  184. using the format mean/variance for each of the timing stats.
  185. The gfs2_glock_lock_time tracepoint prints out the current values of the stats
  186. for the glock in question, along with some addition information on each dlm
  187. reply that is received:
  188. status - The status of the dlm request
  189. flags - The dlm request flags
  190. tdiff - The time taken by this specific request
  191. (remaining fields as per above list)