2009-05-26 23:18:34 +02:00
|
|
|
// curop.h
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "namespace.h"
|
|
|
|
#include "security.h"
|
2009-10-12 21:12:16 +02:00
|
|
|
#include "client.h"
|
2010-01-26 01:32:00 +01:00
|
|
|
#include "../util/atomic_int.h"
|
2009-05-26 23:18:34 +02:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
2009-12-30 17:59:54 +01:00
|
|
|
class OpDebug {
|
|
|
|
public:
|
2009-12-30 05:30:29 +01:00
|
|
|
StringBuilder str;
|
|
|
|
|
|
|
|
void reset(){
|
|
|
|
str.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-12-21 19:19:20 +01:00
|
|
|
/* Current operation (for the current Client).
|
|
|
|
an embedded member of Client class, and typically used from within the mutex there. */
|
2009-12-30 05:30:29 +01:00
|
|
|
class CurOp : boost::noncopyable {
|
2010-01-26 01:32:00 +01:00
|
|
|
static AtomicUInt _nextOpNum;
|
2009-12-21 19:19:20 +01:00
|
|
|
static BSONObj _tooBig; // { $msg : "query not recording (too large)" }
|
2010-02-01 19:27:05 +01:00
|
|
|
|
|
|
|
unsigned long long _start;
|
|
|
|
unsigned long long _checkpoint;
|
|
|
|
unsigned long long _end;
|
2009-12-21 19:19:20 +01:00
|
|
|
|
|
|
|
bool _active;
|
|
|
|
int _op;
|
2010-02-01 19:27:05 +01:00
|
|
|
int _lockType; // see concurrency.h for values
|
|
|
|
int _dbprofile; // 0=off, 1=slow, 2=all
|
2010-01-26 01:32:00 +01:00
|
|
|
AtomicUInt _opNum;
|
2009-12-21 19:19:20 +01:00
|
|
|
char _ns[Namespace::MaxNsLen+2];
|
2010-02-01 19:27:05 +01:00
|
|
|
struct sockaddr_in _client;
|
|
|
|
|
2009-12-21 19:19:20 +01:00
|
|
|
char _queryBuf[256];
|
|
|
|
bool haveQuery() const { return *((int *) _queryBuf) != 0; }
|
|
|
|
void resetQuery(int x=0) { *((int *)_queryBuf) = x; }
|
|
|
|
BSONObj query() {
|
|
|
|
if( *((int *) _queryBuf) == 1 ) {
|
|
|
|
return _tooBig;
|
|
|
|
}
|
|
|
|
BSONObj o(_queryBuf);
|
|
|
|
return o;
|
|
|
|
}
|
2010-02-01 19:27:05 +01:00
|
|
|
|
2009-12-30 05:30:29 +01:00
|
|
|
OpDebug _debug;
|
2010-02-01 19:27:05 +01:00
|
|
|
|
|
|
|
void _reset(){
|
|
|
|
_lockType = 0;
|
|
|
|
_dbprofile = 0;
|
|
|
|
_end = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setNS(const char *ns) {
|
|
|
|
strncpy(_ns, ns, Namespace::MaxNsLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void enter( Client::Context * context ){
|
|
|
|
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 );
|
|
|
|
_checkpoint = now;
|
|
|
|
}
|
|
|
|
|
2009-12-21 19:19:20 +01:00
|
|
|
public:
|
2010-02-01 19:27:05 +01:00
|
|
|
void reset( const sockaddr_in & client, int op ) {
|
|
|
|
_reset();
|
|
|
|
_start = curTimeMicros64();
|
|
|
|
_checkpoint = _start;
|
2009-12-21 19:19:20 +01:00
|
|
|
_active = true;
|
2010-01-26 01:32:00 +01:00
|
|
|
_opNum = _nextOpNum++;
|
2009-12-21 19:19:20 +01:00
|
|
|
_ns[0] = '?'; // just in case not set later
|
2009-12-30 05:30:29 +01:00
|
|
|
_debug.reset();
|
2009-12-21 19:19:20 +01:00
|
|
|
resetQuery();
|
2010-02-01 19:27:05 +01:00
|
|
|
_client = client;
|
|
|
|
_op = op;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRead(){
|
|
|
|
_lockType = -1;
|
|
|
|
}
|
|
|
|
void setWrite(){
|
|
|
|
_lockType = 1;
|
2009-05-26 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
2009-12-30 05:30:29 +01:00
|
|
|
OpDebug& debug(){
|
2009-12-29 23:32:31 +01:00
|
|
|
return _debug;
|
|
|
|
}
|
2010-02-01 19:27:05 +01:00
|
|
|
|
|
|
|
int profileLevel() const {
|
|
|
|
return _dbprofile;
|
|
|
|
}
|
2009-12-29 23:32:31 +01:00
|
|
|
|
2010-02-01 19:27:05 +01:00
|
|
|
const char * getNS() const {
|
|
|
|
return _ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldDBProfile( int ms ) const {
|
|
|
|
if ( _dbprofile <= 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return _dbprofile >= 2 || ms >= cmdLine.slowMS;
|
|
|
|
}
|
|
|
|
|
2010-01-26 01:32:00 +01:00
|
|
|
AtomicUInt opNum() const { return _opNum; }
|
2009-12-21 19:19:20 +01:00
|
|
|
bool active() const { return _active; }
|
|
|
|
|
2009-12-27 06:41:53 +01:00
|
|
|
/** micros */
|
|
|
|
unsigned long long startTime(){
|
2010-02-01 19:27:05 +01:00
|
|
|
return _start;
|
2009-12-27 06:41:53 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 19:27:05 +01:00
|
|
|
void done() {
|
|
|
|
_active = false;
|
|
|
|
_end = curTimeMicros64();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long totalTimeMicros() const {
|
|
|
|
massert( 12596 , "CurOp not marked done yet" , ! _active );
|
|
|
|
return _end - _start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int totalTimeMillis() const {
|
|
|
|
return totalTimeMicros() / 1000;
|
2009-12-21 19:19:20 +01:00
|
|
|
}
|
2010-02-01 19:27:05 +01:00
|
|
|
|
|
|
|
int elapsedSeconds() const {
|
|
|
|
unsigned long long total = curTimeMicros64() - _start;
|
|
|
|
return total / 1000000;
|
|
|
|
}
|
|
|
|
|
2009-12-21 19:19:20 +01:00
|
|
|
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());
|
|
|
|
}
|
2009-05-26 23:18:34 +02:00
|
|
|
|
|
|
|
CurOp() {
|
2010-02-01 19:27:05 +01:00
|
|
|
_start = _checkpoint = curTimeMicros64();
|
2009-12-21 19:19:20 +01:00
|
|
|
_active = false;
|
2010-02-01 19:27:05 +01:00
|
|
|
_reset();
|
2009-05-26 23:18:34 +02:00
|
|
|
// 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.
|
2009-12-21 19:19:20 +01:00
|
|
|
memset(_ns, 0, sizeof(_ns));
|
|
|
|
memset(_queryBuf, 0, sizeof(_queryBuf));
|
2009-05-26 23:18:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BSONObj info() {
|
2009-10-12 21:12:16 +02:00
|
|
|
AuthenticationInfo *ai = currentClient.get()->ai;
|
2009-10-09 20:59:44 +02:00
|
|
|
if( !ai->isAuthorized("admin") ) {
|
2009-05-26 23:18:34 +02:00
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append("err", "unauthorized");
|
|
|
|
return b.obj();
|
|
|
|
}
|
|
|
|
return infoNoauth();
|
|
|
|
}
|
|
|
|
|
|
|
|
BSONObj infoNoauth() {
|
|
|
|
BSONObjBuilder b;
|
2009-12-22 21:22:37 +01:00
|
|
|
b.append("opid", _opNum);
|
2009-12-21 19:19:20 +01:00
|
|
|
b.append("active", _active);
|
|
|
|
if( _active )
|
2010-02-01 19:27:05 +01:00
|
|
|
b.append("secs_running", elapsedSeconds() );
|
2009-12-21 19:19:20 +01:00
|
|
|
if( _op == 2004 )
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("op", "query");
|
2009-12-21 19:19:20 +01:00
|
|
|
else if( _op == 2005 )
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("op", "getMore");
|
2009-12-21 19:19:20 +01:00
|
|
|
else if( _op == 2001 )
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("op", "update");
|
2009-12-21 19:19:20 +01:00
|
|
|
else if( _op == 2002 )
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("op", "insert");
|
2009-12-21 19:19:20 +01:00
|
|
|
else if( _op == 2006 )
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("op", "delete");
|
|
|
|
else
|
2009-12-21 19:19:20 +01:00
|
|
|
b.append("op", _op);
|
|
|
|
b.append("ns", _ns);
|
|
|
|
|
|
|
|
if( haveQuery() ) {
|
|
|
|
b.append("query", query());
|
|
|
|
}
|
2009-12-07 21:42:26 +01:00
|
|
|
// b.append("inLock", ??
|
2009-05-26 23:18:34 +02:00
|
|
|
stringstream clientStr;
|
2010-02-01 19:27:05 +01:00
|
|
|
clientStr << inet_ntoa( _client.sin_addr ) << ":" << ntohs( _client.sin_port );
|
2009-05-26 23:18:34 +02:00
|
|
|
b.append("client", clientStr.str());
|
|
|
|
return b.obj();
|
|
|
|
}
|
2010-02-01 19:27:05 +01:00
|
|
|
|
|
|
|
friend class Client;
|
2009-10-16 21:36:34 +02:00
|
|
|
};
|
2009-05-26 23:18:34 +02:00
|
|
|
|
2009-12-22 21:22:37 +01:00
|
|
|
/* 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;
|
2010-01-26 01:32:00 +01:00
|
|
|
AtomicUInt toKill;
|
2009-12-22 21:22:37 +01:00
|
|
|
public:
|
|
|
|
void killAll() { state = All; }
|
2010-01-26 01:32:00 +01:00
|
|
|
void kill(AtomicUInt i) { toKill = i; state = On; }
|
2009-12-22 21:22:37 +01:00
|
|
|
|
|
|
|
void checkForInterrupt() {
|
|
|
|
if( state != Off ) {
|
|
|
|
if( state == All )
|
2009-12-28 22:43:43 +01:00
|
|
|
uasserted(11600,"interrupted at shutdown");
|
2009-12-22 21:22:37 +01:00
|
|
|
if( cc().curop()->opNum() == toKill ) {
|
|
|
|
state = Off;
|
2009-12-28 22:43:43 +01:00
|
|
|
uasserted(11601,"interrupted");
|
2009-12-22 21:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} killCurrentOp;
|
2009-05-26 23:18:34 +02:00
|
|
|
}
|