Storage Engine API
lock_stats.h
Go to the documentation of this file.
1 
29 #pragma once
30 
32 #include "mongo/platform/atomic_word.h"
33 
34 namespace mongo {
35 
36 class BSONObjBuilder;
37 
38 
42 struct CounterOps {
43  static int64_t get(const int64_t& counter) {
44  return counter;
45  }
46 
47  static int64_t get(const AtomicInt64& counter) {
48  return counter.load();
49  }
50 
51  static void set(int64_t& counter, int64_t value) {
52  counter = value;
53  }
54 
55  static void set(AtomicInt64& counter, int64_t value) {
56  counter.store(value);
57  }
58 
59  static void add(int64_t& counter, int64_t value) {
60  counter += value;
61  }
62 
63  static void add(int64_t& counter, const AtomicInt64& value) {
64  counter += value.load();
65  }
66 
67  static void add(AtomicInt64& counter, int64_t value) {
68  counter.addAndFetch(value);
69  }
70 };
71 
72 
76 template <typename CounterType>
78  template <typename OtherType>
79  void append(const LockStatCounters<OtherType>& other) {
80  CounterOps::add(numAcquisitions, other.numAcquisitions);
81  CounterOps::add(numWaits, other.numWaits);
82  CounterOps::add(combinedWaitTimeMicros, other.combinedWaitTimeMicros);
83  CounterOps::add(numDeadlocks, other.numDeadlocks);
84  }
85 
86  void reset() {
87  CounterOps::set(numAcquisitions, 0);
88  CounterOps::set(numWaits, 0);
89  CounterOps::set(combinedWaitTimeMicros, 0);
90  CounterOps::set(numDeadlocks, 0);
91  }
92 
93 
94  CounterType numAcquisitions;
95  CounterType numWaits;
97  CounterType numDeadlocks;
98 };
99 
100 
105 template <typename CounterType>
106 class LockStats {
107 public:
108  // Declare the type for the lock counters bundle
110 
114  LockStats();
115 
117  CounterOps::add(get(resId, mode).numAcquisitions, 1);
118  }
119 
120  void recordWait(ResourceId resId, LockMode mode) {
121  CounterOps::add(get(resId, mode).numWaits, 1);
122  }
123 
124  void recordWaitTime(ResourceId resId, LockMode mode, int64_t waitMicros) {
125  CounterOps::add(get(resId, mode).combinedWaitTimeMicros, waitMicros);
126  }
127 
128  void recordDeadlock(ResourceId resId, LockMode mode) {
129  CounterOps::add(get(resId, mode).numDeadlocks, 1);
130  }
131 
132  LockStatCountersType& get(ResourceId resId, LockMode mode) {
133  if (resId == resourceIdOplog) {
134  return _oplogStats.modeStats[mode];
135  }
136 
137  return _stats[resId.getType()].modeStats[mode];
138  }
139 
140  template <typename OtherType>
141  void append(const LockStats<OtherType>& other) {
142  typedef LockStatCounters<OtherType> OtherLockStatCountersType;
143 
144  // Append all lock stats
145  for (int i = 0; i < ResourceTypesCount; i++) {
146  for (int mode = 0; mode < LockModesCount; mode++) {
147  const OtherLockStatCountersType& otherStats = other._stats[i].modeStats[mode];
148  LockStatCountersType& thisStats = _stats[i].modeStats[mode];
149  thisStats.append(otherStats);
150  }
151  }
152 
153  // Append the oplog stats
154  for (int mode = 0; mode < LockModesCount; mode++) {
155  const OtherLockStatCountersType& otherStats = other._oplogStats.modeStats[mode];
156  LockStatCountersType& thisStats = _oplogStats.modeStats[mode];
157  thisStats.append(otherStats);
158  }
159  }
160 
161  void report(BSONObjBuilder* builder) const;
162  void reset();
163 
164 private:
165  // Necessary for the append call, which accepts argument of type different than our
166  // template parameter.
167  template <typename T>
168  friend class LockStats;
169 
170 
171  // Keep the per-mode lock stats next to each other in case we want to do fancy operations
172  // such as atomic operations on 128-bit values.
174  LockStatCountersType modeStats[LockModesCount];
175  };
176 
177 
178  void _report(BSONObjBuilder* builder,
179  const char* sectionName,
180  const PerModeLockStatCounters& stat) const;
181 
182 
183  // Split the lock stats per resource type and special-case the oplog so we can collect
184  // more detailed stats for it.
185  PerModeLockStatCounters _stats[ResourceTypesCount];
186  PerModeLockStatCounters _oplogStats;
187 };
188 
191 
192 
196 void reportGlobalLockingStats(SingleThreadedLockStats* outStats);
197 
201 void resetGlobalLockStats();
202 
203 } // namespace mongo
Templatized lock statistics management class, which can be specialized with atomic integers for the g...
Definition: lock_stats.h:106
static void add(AtomicInt64 &counter, int64_t value)
Definition: lock_stats.h:67
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
LockStatCounters< CounterType > LockStatCountersType
Definition: lock_stats.h:109
static void set(int64_t &counter, int64_t value)
Definition: lock_stats.h:51
const ResourceId resourceIdOplog
Definition: lock_state.cpp:1108
PerModeLockStatCounters _stats[ResourceTypesCount]
Definition: lock_stats.h:185
Uniquely identifies a lockable resource.
Definition: lock_manager_defs.h:176
Definition: lock_manager_defs.h:165
void resetGlobalLockStats()
Currently used for testing only.
Definition: lock_state.cpp:1096
void append(const LockStatCounters< OtherType > &other)
Definition: lock_stats.h:79
LockMode
Lock modes.
Definition: lock_manager_defs.h:59
Bundle of locking statistics values.
Definition: lock_stats.h:77
PerModeLockStatCounters _oplogStats
Definition: lock_stats.h:186
CounterType numDeadlocks
Definition: lock_stats.h:97
void recordDeadlock(ResourceId resId, LockMode mode)
Definition: lock_stats.h:128
CounterType combinedWaitTimeMicros
Definition: lock_stats.h:96
void recordWaitTime(ResourceId resId, LockMode mode, int64_t waitMicros)
Definition: lock_stats.h:124
Operations for manipulating the lock statistics abstracting whether they are atomic or not...
Definition: lock_stats.h:42
void reportGlobalLockingStats(SingleThreadedLockStats *outStats)
Reports instance-wide locking statistics, which can then be converted to BSON or logged.
Definition: lock_state.cpp:1092
static void add(int64_t &counter, int64_t value)
Definition: lock_stats.h:59
Definition: lock_manager_defs.h:68
void reset()
Definition: lock_stats.h:86
LockStats< int64_t > SingleThreadedLockStats
Definition: lock_stats.h:189
Definition: lock_stats.h:173
void append(const LockStats< OtherType > &other)
Definition: lock_stats.h:141
CounterType numWaits
Definition: lock_stats.h:95
static void add(int64_t &counter, const AtomicInt64 &value)
Definition: lock_stats.h:63
LockStats< AtomicInt64 > AtomicLockStats
Definition: lock_stats.h:190
void recordAcquisition(ResourceId resId, LockMode mode)
Definition: lock_stats.h:116
void recordWait(ResourceId resId, LockMode mode)
Definition: lock_stats.h:120
CounterType numAcquisitions
Definition: lock_stats.h:94