00001 // counters.h 00002 /* 00003 * Copyright (C) 2010 10gen Inc. 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU Affero General Public License, version 3, 00007 * as published by the Free Software Foundation. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Affero General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Affero General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #pragma once 00019 00020 #include "../../pch.h" 00021 #include "../jsobj.h" 00022 #include "../../util/message.h" 00023 #include "../../util/processinfo.h" 00024 #include "../../util/concurrency/spin_lock.h" 00025 00026 namespace mongo { 00027 00032 class OpCounters { 00033 public: 00034 00035 OpCounters(); 00036 00037 AtomicUInt * getInsert() { return _insert; } 00038 AtomicUInt * getQuery() { return _query; } 00039 AtomicUInt * getUpdate() { return _update; } 00040 AtomicUInt * getDelete() { return _delete; } 00041 AtomicUInt * getGetMore() { return _getmore; } 00042 AtomicUInt * getCommand() { return _command; } 00043 00044 void incInsertInWriteLock(int n) { _insert->x += n; } 00045 void gotInsert() { _insert[0]++; } 00046 void gotQuery() { _query[0]++; } 00047 void gotUpdate() { _update[0]++; } 00048 void gotDelete() { _delete[0]++; } 00049 void gotGetMore() { _getmore[0]++; } 00050 void gotCommand() { _command[0]++; } 00051 00052 void gotOp( int op , bool isCommand ); 00053 00054 BSONObj& getObj(); 00055 00056 private: 00057 BSONObj _obj; 00058 00059 // todo: there will be a lot of cache line contention on these. need to do something 00060 // else eventually. 00061 AtomicUInt * _insert; 00062 AtomicUInt * _query; 00063 AtomicUInt * _update; 00064 AtomicUInt * _delete; 00065 AtomicUInt * _getmore; 00066 AtomicUInt * _command; 00067 }; 00068 00069 extern OpCounters globalOpCounters; 00070 extern OpCounters replOpCounters; 00071 00072 00073 class IndexCounters { 00074 public: 00075 IndexCounters(); 00076 00077 void btree( char * node ) { 00078 if ( ! _memSupported ) 00079 return; 00080 if ( _sampling++ % _samplingrate ) 00081 return; 00082 btree( _pi.blockInMemory( node ) ); 00083 } 00084 00085 void btree( bool memHit ) { 00086 if ( memHit ) 00087 _btreeMemHits++; 00088 else 00089 _btreeMemMisses++; 00090 _btreeAccesses++; 00091 } 00092 void btreeHit() { _btreeMemHits++; _btreeAccesses++; } 00093 void btreeMiss() { _btreeMemMisses++; _btreeAccesses++; } 00094 00095 void append( BSONObjBuilder& b ); 00096 00097 private: 00098 ProcessInfo _pi; 00099 bool _memSupported; 00100 00101 int _sampling; 00102 int _samplingrate; 00103 00104 int _resets; 00105 long long _maxAllowed; 00106 00107 long long _btreeMemMisses; 00108 long long _btreeMemHits; 00109 long long _btreeAccesses; 00110 }; 00111 00112 extern IndexCounters globalIndexCounters; 00113 00114 class FlushCounters { 00115 public: 00116 FlushCounters(); 00117 00118 void flushed(int ms); 00119 00120 void append( BSONObjBuilder& b ); 00121 00122 private: 00123 long long _total_time; 00124 long long _flushes; 00125 int _last_time; 00126 Date_t _last; 00127 }; 00128 00129 extern FlushCounters globalFlushCounters; 00130 00131 00132 class GenericCounter { 00133 public: 00134 GenericCounter() : _mutex("GenericCounter") { } 00135 void hit( const string& name , int count=0 ); 00136 BSONObj getObj(); 00137 private: 00138 map<string,long long> _counts; // TODO: replace with thread safe map 00139 mongo::mutex _mutex; 00140 }; 00141 00142 class NetworkCounter { 00143 public: 00144 NetworkCounter() : _bytesIn(0), _bytesOut(0), _requests(0), _overflows(0) {} 00145 void hit( long long bytesIn , long long bytesOut ); 00146 void append( BSONObjBuilder& b ); 00147 private: 00148 long long _bytesIn; 00149 long long _bytesOut; 00150 long long _requests; 00151 00152 long long _overflows; 00153 00154 SpinLock _lock; 00155 }; 00156 00157 extern NetworkCounter networkCounter; 00158 }