0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/db/curop.h

265 lines
7.4 KiB
C
Raw Normal View History

// 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 {
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 {
static AtomicUInt _nextOpNum;
2009-12-21 19:19:20 +01:00
static BSONObj _tooBig; // { $msg : "query not recording (too large)" }
2010-02-04 21:56:02 +01:00
Client * _client;
CurOp * _wrapped;
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-11 18:52:26 +01:00
bool _command;
int _lockType; // see concurrency.h for values
2010-02-09 16:25:05 +01:00
bool _waitingForLock;
int _dbprofile; // 0=off, 1=slow, 2=all
AtomicUInt _opNum;
2009-12-21 19:19:20 +01:00
char _ns[Namespace::MaxNsLen+2];
2010-02-04 21:56:02 +01:00
struct sockaddr_in _remote;
2009-12-21 19:19:20 +01:00
char _queryBuf[256];
2010-02-09 16:35:31 +01:00
2009-12-21 19:19:20 +01:00
void resetQuery(int x=0) { *((int *)_queryBuf) = x; }
2009-12-30 05:30:29 +01:00
OpDebug _debug;
void _reset(){
2010-02-11 18:52:26 +01:00
_command = false;
_lockType = 0;
_dbprofile = 0;
_end = 0;
2010-02-09 16:25:05 +01:00
_waitingForLock = false;
}
void setNS(const char *ns) {
strncpy(_ns, ns, Namespace::MaxNsLen);
}
2010-02-03 01:49:43 +01:00
public:
2010-02-09 16:35:31 +01:00
bool haveQuery() const { return *((int *) _queryBuf) != 0; }
BSONObj query() {
if( *((int *) _queryBuf) == 1 ) {
return _tooBig;
}
BSONObj o(_queryBuf);
return o;
}
2010-02-04 21:56:02 +01:00
void ensureStarted(){
if ( _start == 0 )
_start = _checkpoint = curTimeMicros64();
}
void enter( Client::Context * context ){
2010-02-04 21:56:02 +01:00
ensureStarted();
setNS( context->ns() );
if ( context->_db && context->_db->profile > _dbprofile )
_dbprofile = context->_db->profile;
}
2010-02-03 01:42:32 +01:00
void leave( Client::Context * context ){
unsigned long long now = curTimeMicros64();
2010-02-11 18:52:26 +01:00
Top::global.record( _ns , _op , _lockType , now - _checkpoint , _command );
_checkpoint = now;
}
2010-02-04 21:56:02 +01:00
void reset( const sockaddr_in & remote, int op ) {
_reset();
2010-02-04 21:56:02 +01:00
_start = _checkpoint = 0;
2009-12-21 19:19:20 +01:00
_active = true;
_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-04 21:56:02 +01:00
_remote = remote;
_op = op;
}
2010-02-11 18:52:26 +01:00
void markCommand(){
_command = true;
}
2010-02-10 21:16:00 +01:00
void waitingForLock( int type ){
2010-02-09 16:25:05 +01:00
_waitingForLock = true;
2010-02-10 21:16:00 +01:00
if ( type > 0 )
_lockType = 1;
else
_lockType = -1;
2010-02-09 16:25:05 +01:00
}
void gotLock(){
_waitingForLock = false;
}
2009-12-30 05:30:29 +01:00
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; }
2010-02-09 22:58:03 +01:00
/** if this op is running */
2009-12-21 19:19:20 +01:00
bool active() const { return _active; }
2010-02-09 22:58:03 +01:00
2010-02-09 16:35:31 +01:00
int getLockType() const { return _lockType; }
bool isWaitingForLock() const { return _waitingForLock; }
int getOp() const { return _op; }
2010-02-09 22:58:03 +01:00
2009-12-27 06:41:53 +01:00
/** micros */
2010-02-07 21:22:59 +01:00
unsigned long long startTime() {
ensureStarted();
return _start;
2009-12-27 06:41:53 +01:00
}
void done() {
_active = false;
_end = curTimeMicros64();
}
2010-02-07 21:22:59 +01:00
unsigned long long totalTimeMicros() {
2010-02-02 23:16:19 +01:00
massert( 12601 , "CurOp not marked done yet" , ! _active );
2010-02-04 21:56:02 +01:00
return _end - startTime();
}
2010-02-07 21:22:59 +01:00
int totalTimeMillis() {
2010-02-03 21:31:48 +01:00
return (int) (totalTimeMicros() / 1000);
2009-12-21 19:19:20 +01:00
}
2010-02-07 21:22:59 +01:00
int elapsedMillis() {
2010-02-04 21:56:02 +01:00
unsigned long long total = curTimeMicros64() - startTime();
return (int) (total / 1000);
}
2010-02-07 21:22:59 +01:00
int elapsedSeconds() {
2010-02-04 21:56:02 +01:00
return elapsedMillis() / 1000;
}
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());
}
2010-02-04 21:56:02 +01:00
CurOp( Client * client , CurOp * wrapped = 0 ) {
_client = client;
_wrapped = wrapped;
if ( _wrapped ){
_client->_curOp = this;
}
_start = _checkpoint = 0;
2009-12-21 19:19:20 +01:00
_active = false;
_reset();
2010-02-03 04:34:15 +01:00
_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.
2009-12-21 19:19:20 +01:00
memset(_ns, 0, sizeof(_ns));
memset(_queryBuf, 0, sizeof(_queryBuf));
}
2010-02-04 21:56:02 +01:00
~CurOp(){
if ( _wrapped )
_client->_curOp = _wrapped;
}
BSONObj info() {
if( ! cc().getAuthenticationInfo()->isAuthorized("admin") ) {
BSONObjBuilder b;
b.append("err", "unauthorized");
return b.obj();
}
return infoNoauth();
}
2010-02-09 16:25:05 +01:00
BSONObj infoNoauth();
2010-02-09 16:35:31 +01:00
string getRemoteString(){
stringstream ss;
ss << inet_ntoa( _remote.sin_addr ) << ":" << ntohs( _remote.sin_port );
return ss.str();
}
friend class Client;
};
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;
AtomicUInt toKill;
2009-12-22 21:22:37 +01:00
public:
void killAll() { state = All; }
void kill(AtomicUInt i) { toKill = i; state = On; }
2009-12-22 21:22:37 +01:00
void checkForInterrupt() {
if( state != Off ) {
if( state == All )
uasserted(11600,"interrupted at shutdown");
2009-12-22 21:22:37 +01:00
if( cc().curop()->opNum() == toKill ) {
state = Off;
uasserted(11601,"interrupted");
2009-12-22 21:22:37 +01:00
}
}
}
} killCurrentOp;
}