1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
// @file curop.h
/*
* Copyright (C) 2010 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include "mongo/bson/util/atomic_int.h"
#include "mongo/db/client.h"
#include "mongo/db/storage/namespace.h"
#include "mongo/util/concurrency/spin_lock.h"
#include "mongo/util/net/hostandport.h"
#include "mongo/util/progress_meter.h"
#include "mongo/util/time_support.h"
namespace mongo {
class CurOp;
/* lifespan is different than CurOp because of recursives with DBDirectClient */
class OpDebug {
public:
OpDebug() : ns(""){ reset(); }
void reset();
void recordStats();
string report( const CurOp& curop ) const;
/**
* Appends stored data and information from curop to the builder.
*
* @param curop information about the current operation which will be
* use to append data to the builder.
* @param builder the BSON builder to use for appending data. Data can
* still be appended even if this method returns false.
* @param maxSize the maximum allowed combined size for the query object
* and update object
*
* @return false if the sum of the sizes for the query object and update
* object exceeded maxSize
*/
bool append(const CurOp& curop, BSONObjBuilder& builder, size_t maxSize) const;
// -------------------
StringBuilder extra; // weird things we need to fix later
// basic options
int op;
bool iscommand;
Namespace ns;
BSONObj query;
BSONObj updateobj;
// detailed options
long long cursorid;
int ntoreturn;
int ntoskip;
bool exhaust;
// debugging/profile info
long long nscanned;
bool idhack; // indicates short circuited code path on an update to make the update faster
bool scanAndOrder; // scanandorder query plan aspect was used
long long nupdated; // number of records updated (including no-ops)
long long nupdateNoops; // number of records updated which were noops
long long nmoved; // updates resulted in a move (moves are expensive)
long long ninserted;
long long ndeleted;
bool fastmod;
bool fastmodinsert; // upsert of an $operation. builds a default object
bool upsert; // true if the update actually did an insert
int keyUpdates;
// error handling
ExceptionInfo exceptionInfo;
// response info
int executionTime;
int nreturned;
int responseLength;
};
/**
* stores a copy of a bson obj in a fixed size buffer
* if its too big for the buffer, says "too big"
* useful for keeping a copy around indefinitely without wasting a lot of space or doing malloc
*/
class CachedBSONObj {
public:
enum { TOO_BIG_SENTINEL = 1 } ;
static BSONObj _tooBig; // { $msg : "query not recording (too large)" }
CachedBSONObj() {
_size = (int*)_buf;
reset();
}
void reset( int sz = 0 ) {
_lock.lock();
_reset( sz );
_lock.unlock();
}
void set( const BSONObj& o ) {
scoped_spinlock lk(_lock);
size_t sz = o.objsize();
if ( sz > sizeof(_buf) ) {
_reset(TOO_BIG_SENTINEL);
}
else {
memcpy(_buf, o.objdata(), sz );
}
}
int size() const { return *_size; }
bool have() const { return size() > 0; }
BSONObj get() const {
scoped_spinlock lk(_lock);
return _get();
}
void append( BSONObjBuilder& b , const StringData& name ) const {
scoped_spinlock lk(_lock);
BSONObj temp = _get();
b.append( name , temp );
}
private:
/** you have to be locked when you call this */
BSONObj _get() const {
int sz = size();
if ( sz == 0 )
return BSONObj();
if ( sz == TOO_BIG_SENTINEL )
return _tooBig;
return BSONObj( _buf ).copy();
}
/** you have to be locked when you call this */
void _reset( int sz ) { _size[0] = sz; }
mutable SpinLock _lock;
int * _size;
char _buf[512];
};
/* Current operation (for the current Client).
an embedded member of Client class, and typically used from within the mutex there.
*/
class CurOp : boost::noncopyable {
public:
CurOp( Client * client , CurOp * wrapped = 0 );
~CurOp();
bool haveQuery() const { return _query.have(); }
BSONObj query() const { return _query.get(); }
void appendQuery( BSONObjBuilder& b , const StringData& name ) const { _query.append( b , name ); }
void enter( Client::Context * context );
void leave( Client::Context * context );
void reset();
void reset( const HostAndPort& remote, int op );
void markCommand() { _command = true; }
OpDebug& debug() { return _debug; }
int profileLevel() const { return _dbprofile; }
const char * getNS() const { return _ns; }
bool shouldDBProfile( int ms ) const {
if ( _dbprofile <= 0 )
return false;
return _dbprofile >= 2 || ms >= cmdLine.slowMS;
}
AtomicUInt opNum() const { return _opNum; }
/** if this op is running */
bool active() const { return _active; }
bool displayInCurop() const { return _active && ! _suppressFromCurop; }
int getOp() const { return _op; }
//
// Methods for controlling CurOp "max time".
//
/**
* Sets the amount of time operation this should be allowed to run, units of microseconds.
* The special value 0 is "allow to run indefinitely".
*/
void setMaxTimeMicros(uint64_t maxTimeMicros);
/**
* Checks whether this operation has been running longer than its time limit. Returns
* false if not, or if the operation has no time limit.
*
* Note that KillCurrentOp objects are responsible for interrupting CurOp objects that
* have exceeded their allotted time; CurOp objects do not interrupt themselves.
*/
bool maxTimeHasExpired();
/**
* Returns the number of microseconds remaining for this operation's time limit, or the
* special value 0 if the operation has no time limit.
*
* Calling this method is more expensive than calling its sibling "maxTimeHasExpired()",
* since an accurate measure of remaining time needs to be calculated.
*/
uint64_t getRemainingMaxTimeMicros() const;
//
// Methods for getting/setting elapsed time.
//
void ensureStarted();
bool isStarted() const { return _start > 0; }
unsigned long long startTime() { // micros
ensureStarted();
return _start;
}
void done() {
_active = false;
_end = curTimeMicros64();
}
unsigned long long totalTimeMicros() {
massert( 12601 , "CurOp not marked done yet" , ! _active );
return _end - startTime();
}
int totalTimeMillis() { return (int) (totalTimeMicros() / 1000); }
unsigned long long elapsedMicros() {
return curTimeMicros64() - startTime();
}
int elapsedMillis() {
return (int) (elapsedMicros() / 1000);
}
int elapsedSeconds() { return elapsedMillis() / 1000; }
void setQuery(const BSONObj& query) { _query.set( query ); }
Client * getClient() const { return _client; }
BSONObj info();
// Fetches less information than "info()"; used to search for ops with certain criteria
BSONObj description();
string getRemoteString( bool includePort = true ) { return _remote.toString(includePort); }
ProgressMeter& setMessage(const char * msg,
std::string name = "Progress",
unsigned long long progressMeterTotal = 0,
int secondsBetween = 3);
string getMessage() const { return _message.toString(); }
ProgressMeter& getProgressMeter() { return _progressMeter; }
CurOp *parent() const { return _wrapped; }
void kill(bool* pNotifyFlag = NULL);
bool killPendingStrict() const { return _killPending.load(); }
bool killPending() const { return _killPending.loadRelaxed(); }
void yielded() { _numYields++; }
int numYields() const { return _numYields; }
void suppressFromCurop() { _suppressFromCurop = true; }
long long getExpectedLatencyMs() const { return _expectedLatencyMs; }
void setExpectedLatencyMs( long long latency ) { _expectedLatencyMs = latency; }
void recordGlobalTime( long long micros ) const;
const LockStat& lockStat() const { return _lockStat; }
LockStat& lockStat() { return _lockStat; }
void setKillWaiterFlags();
/**
* Find a currently running operation matching the given criteria. This assumes that you're
* going to kill the operation, so it must be called multiple times to get multiple matching
* operations.
* @param criteria the search to do against the infoNoauth() BSONObj
* @return a pointer to a matching op or NULL if no ops match
*/
static CurOp* getOp(const BSONObj& criteria);
private:
friend class Client;
void _reset();
static AtomicUInt _nextOpNum;
Client * _client;
CurOp * _wrapped;
unsigned long long _start;
unsigned long long _end;
bool _active;
bool _suppressFromCurop; // unless $all is set
int _op;
bool _command;
int _dbprofile; // 0=off, 1=slow, 2=all
AtomicUInt _opNum; // todo: simple being "unsigned" may make more sense here
char _ns[Namespace::MaxNsLen+2];
HostAndPort _remote; // CAREFUL here with thread safety
CachedBSONObj _query; // CachedBSONObj is thread safe
OpDebug _debug;
ThreadSafeString _message;
ProgressMeter _progressMeter;
AtomicInt32 _killPending;
int _numYields;
LockStat _lockStat;
// _notifyList is protected by the global killCurrentOp's mtx.
std::vector<bool*> _notifyList;
// this is how much "extra" time a query might take
// a writebacklisten for example will block for 30s
// so this should be 30000 in that case
long long _expectedLatencyMs;
/** Nested class that implements a time limit ($maxTimeMS) for a CurOp object. */
class MaxTimeTracker {
MONGO_DISALLOW_COPYING(MaxTimeTracker);
public:
/** Newly-constructed MaxTimeTracker objects have the time limit disabled. */
MaxTimeTracker();
/** Disables the time limit. */
void reset();
/**
* Enables the time limit to be "durationMicros" microseconds from "startEpochMicros"
* (units of microseconds since the epoch).
*
* "durationMicros" must be nonzero.
*/
void setTimeLimit(uint64_t startEpochMicros, uint64_t durationMicros);
/**
* Checks whether the time limit has been hit. Returns false if not, or if the time
* limit is disabled.
*/
bool checkTimeLimit();
/**
* Returns the number of microseconds remaining for the time limit, or the special
* value 0 if the time limit is disabled.
*
* Calling this method is more expensive than calling its sibling "checkInterval()",
* since an accurate measure of remaining time needs to be calculated.
*/
uint64_t getRemainingMicros() const;
private:
// Whether or not this operation is subject to a time limit.
bool _enabled;
// Point in time at which the time limit is hit. Units of microseconds since the
// epoch.
uint64_t _targetEpochMicros;
// Approximate point in time at which the time limit is hit. Units of milliseconds
// since the server process was started.
int64_t _approxTargetServerMillis;
} _maxTimeTracker;
};
}
|