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
|
// 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 "namespace.h"
#include "client.h"
#include "../util/atomic_int.h"
#include "db.h"
namespace mongo {
class OpDebug {
public:
StringBuilder str;
void reset(){
str.reset();
}
};
/* Current operation (for the current Client).
an embedded member of Client class, and typically used from within the mutex there. */
class CurOp : boost::noncopyable {
static AtomicUInt _nextOpNum;
static BSONObj _tooBig; // { $msg : "query not recording (too large)" }
Client * _client;
CurOp * _wrapped;
unsigned long long _start;
unsigned long long _checkpoint;
unsigned long long _end;
bool _active;
int _op;
bool _command;
int _lockType; // see concurrency.h for values
bool _waitingForLock;
int _dbprofile; // 0=off, 1=slow, 2=all
AtomicUInt _opNum;
char _ns[Namespace::MaxNsLen+2];
struct sockaddr_in _remote;
char _queryBuf[256];
void resetQuery(int x=0) { *((int *)_queryBuf) = x; }
OpDebug _debug;
ThreadSafeString _message;
ProgressMeter _progressMeter;
void _reset(){
_command = false;
_lockType = 0;
_dbprofile = 0;
_end = 0;
_waitingForLock = false;
_message = "";
_progressMeter.finished();
}
void setNS(const char *ns) {
strncpy(_ns, ns, Namespace::MaxNsLen);
}
public:
bool haveQuery() const { return *((int *) _queryBuf) != 0; }
BSONObj query() {
if( *((int *) _queryBuf) == 1 ) {
return _tooBig;
}
BSONObj o(_queryBuf);
return o;
}
void ensureStarted(){
if ( _start == 0 )
_start = _checkpoint = curTimeMicros64();
}
void enter( Client::Context * context ){
ensureStarted();
setNS( context->ns() );
if ( context->_db && context->_db->profile > _dbprofile )
_dbprofile = context->_db->profile;
}
void leave( Client::Context * context ){
unsigned long long now = curTimeMicros64();
Top::global.record( _ns , _op , _lockType , now - _checkpoint , _command );
_checkpoint = now;
}
void reset(){
_reset();
_start = _checkpoint = 0;
_active = true;
_opNum = _nextOpNum++;
_ns[0] = '?'; // just in case not set later
_debug.reset();
resetQuery();
}
void reset( const sockaddr_in & remote, int op ) {
reset();
_remote = remote;
_op = op;
}
void markCommand(){
_command = true;
}
void waitingForLock( int type ){
_waitingForLock = true;
if ( type > 0 )
_lockType = 1;
else
_lockType = -1;
}
void gotLock(){
_waitingForLock = false;
}
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; }
int getLockType() const { return _lockType; }
bool isWaitingForLock() const { return _waitingForLock; }
int getOp() const { return _op; }
/** micros */
unsigned long long startTime() {
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);
}
int elapsedMillis() {
unsigned long long total = curTimeMicros64() - startTime();
return (int) (total / 1000);
}
int elapsedSeconds() {
return elapsedMillis() / 1000;
}
void setQuery(const BSONObj& query) {
if( query.objsize() > (int) sizeof(_queryBuf) ) {
resetQuery(1); // flag as too big and return
return;
}
memcpy(_queryBuf, query.objdata(), query.objsize());
}
CurOp( Client * client , CurOp * wrapped = 0 ) {
_client = client;
_wrapped = wrapped;
if ( _wrapped ){
_client->_curOp = this;
}
_start = _checkpoint = 0;
_active = false;
_reset();
_op = 0;
// These addresses should never be written to again. The zeroes are
// placed here as a precaution because currentOp may be accessed
// without the db mutex.
memset(_ns, 0, sizeof(_ns));
memset(_queryBuf, 0, sizeof(_queryBuf));
}
~CurOp(){
if ( _wrapped )
_client->_curOp = _wrapped;
}
BSONObj info() {
if( ! cc().getAuthenticationInfo()->isAuthorized("admin") ) {
BSONObjBuilder b;
b.append("err", "unauthorized");
return b.obj();
}
return infoNoauth();
}
BSONObj infoNoauth();
string getRemoteString(){
stringstream ss;
ss << inet_ntoa( _remote.sin_addr ) << ":" << ntohs( _remote.sin_port );
return ss.str();
}
ProgressMeter& setMessage( const char * msg , long long progressMeterTotal = 0 , int secondsBetween = 3 ){
_message = msg;
if ( progressMeterTotal ){
assert( ! _progressMeter.isActive() );
_progressMeter.reset( progressMeterTotal , secondsBetween );
}
else {
_progressMeter.finished();
}
return _progressMeter;
}
string getMessage() const { return _message; }
ProgressMeter getProgressMeter() { return _progressMeter; }
friend class Client;
};
/* 0 = ok
1 = kill current operation and reset this to 0
future: maybe use this as a "going away" thing on process termination with a higher flag value
*/
extern class KillCurrentOp {
enum { Off, On, All } state;
AtomicUInt toKill;
public:
void killAll() { state = All; }
void kill(AtomicUInt i) { toKill = i; state = On; }
void checkForInterrupt() {
if( state != Off ) {
if( state == All )
uasserted(11600,"interrupted at shutdown");
if( cc().curop()->opNum() == toKill ) {
state = Off;
uasserted(11601,"interrupted");
}
}
}
} killCurrentOp;
}
|