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
|
// stat_util.cpp
/**
* Copyright (C) 2008 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/>.
*/
#include "stat_util.h"
#include "../util/mongoutils/str.h"
using namespace mongoutils;
namespace mongo {
StatUtil::StatUtil( double seconds , bool all ) :
_seconds( seconds ) ,
_all( all )
{
}
bool StatUtil::_in( const BSONElement& me , const BSONElement& arr ) {
if ( me.type() != String || arr.type() != Array )
return false;
string s = me.String();
BSONForEach(e, arr.Obj()) {
if ( e.type() == String && s == e.String() )
return true;
}
return false;
}
BSONObj StatUtil::doRow( const BSONObj& a , const BSONObj& b ) {
BSONObjBuilder result;
bool isMongos = b["shardCursorType"].type() == Object; // TODO: should have a better check
if ( a["opcounters"].isABSONObj() && b["opcounters"].isABSONObj() ) {
BSONObj ax = a["opcounters"].embeddedObject();
BSONObj bx = b["opcounters"].embeddedObject();
BSONObj ar = a["opcountersRepl"].isABSONObj() ? a["opcountersRepl"].embeddedObject() : BSONObj();
BSONObj br = b["opcountersRepl"].isABSONObj() ? b["opcountersRepl"].embeddedObject() : BSONObj();
BSONObjIterator i( bx );
while ( i.more() ) {
BSONElement e = i.next();
if ( ar.isEmpty() || br.isEmpty() ) {
_append( result , e.fieldName() , 6 , (int)diff( e.fieldName() , ax , bx ) );
}
else {
string f = e.fieldName();
int m = (int)diff( f , ax , bx );
int r = (int)diff( f , ar , br );
string myout;
if ( f == "command" ) {
myout = str::stream() << m << "|" << r;
}
else if ( f == "getmore" ) {
myout = str::stream() << m;
}
else if ( m && r ) {
// this is weird...
myout = str::stream() << m << "|" << r;
}
else if ( m ) {
myout = str::stream() << m;
}
else if ( r ) {
myout = str::stream() << "*" << r;
}
else {
myout = "*0";
}
_append( result , f , 6 , myout );
}
}
}
if ( b["backgroundFlushing"].type() == Object ) {
BSONObj ax = a["backgroundFlushing"].embeddedObject();
BSONObj bx = b["backgroundFlushing"].embeddedObject();
_append( result , "flushes" , 6 , (int)diff( "flushes" , ax , bx ) );
}
if ( b.getFieldDotted("mem.supported").trueValue() ) {
BSONObj bx = b["mem"].embeddedObject();
BSONObjIterator i( bx );
if (!isMongos)
_appendMem( result , "mapped" , 6 , bx["mapped"].numberInt() );
_appendMem( result , "vsize" , 6 , bx["virtual"].numberInt() );
_appendMem( result , "res" , 6 , bx["resident"].numberInt() );
if ( !isMongos && _all )
_appendMem( result , "non-mapped" , 6 , bx["virtual"].numberInt() - bx["mapped"].numberInt() );
}
if ( b["extra_info"].type() == Object ) {
BSONObj ax = a["extra_info"].embeddedObject();
BSONObj bx = b["extra_info"].embeddedObject();
if ( ax["page_faults"].type() || ax["page_faults"].type() )
_append( result , "faults" , 6 , (int)diff( "page_faults" , ax , bx ) );
}
if (!isMongos) {
_append( result , "locked %" , 8 , percent( "globalLock.totalTime" , "globalLock.lockTime" , a , b ) );
_append( result , "idx miss %" , 8 , percent( "indexCounters.btree.accesses" , "indexCounters.btree.misses" , a , b ) );
}
if ( b.getFieldDotted( "globalLock.currentQueue" ).type() == Object ) {
int r = b.getFieldDotted( "globalLock.currentQueue.readers" ).numberInt();
int w = b.getFieldDotted( "globalLock.currentQueue.writers" ).numberInt();
stringstream temp;
temp << r << "|" << w;
_append( result , "qr|qw" , 9 , temp.str() );
}
if ( b.getFieldDotted( "globalLock.activeClients" ).type() == Object ) {
int r = b.getFieldDotted( "globalLock.activeClients.readers" ).numberInt();
int w = b.getFieldDotted( "globalLock.activeClients.writers" ).numberInt();
stringstream temp;
temp << r << "|" << w;
_append( result , "ar|aw" , 7 , temp.str() );
}
if ( a["network"].isABSONObj() && b["network"].isABSONObj() ) {
BSONObj ax = a["network"].embeddedObject();
BSONObj bx = b["network"].embeddedObject();
_appendNet( result , "netIn" , diff( "bytesIn" , ax , bx ) );
_appendNet( result , "netOut" , diff( "bytesOut" , ax , bx ) );
}
_append( result , "conn" , 5 , b.getFieldDotted( "connections.current" ).numberInt() );
if ( b["repl"].type() == Object ) {
BSONObj x = b["repl"].embeddedObject();
bool isReplSet = x["setName"].type() == String;
stringstream ss;
if ( isReplSet ) {
string setName = x["setName"].String();
_append( result , "set" , setName.size() , setName );
}
if ( x["ismaster"].trueValue() )
ss << "PRI";
else if ( x["secondary"].trueValue() )
ss << "SEC";
else if ( x["isreplicaset"].trueValue() )
ss << "REC";
else if ( x["arbiterOnly"].trueValue() )
ss << "ARB";
else if ( _in( x["me"] , x["passives"] ) )
ss << "PSV";
else if ( isReplSet )
ss << "UNK";
else
ss << "SLV";
_append( result , "repl" , 4 , ss.str() );
}
else if ( isMongos ) {
_append( result , "repl" , 4 , "RTR" );
}
{
struct tm t;
time_t_to_Struct( time(0), &t , true );
stringstream temp;
temp << setfill('0') << setw(2) << t.tm_hour
<< ":"
<< setfill('0') << setw(2) << t.tm_min
<< ":"
<< setfill('0') << setw(2) << t.tm_sec;
_append( result , "time" , 10 , temp.str() );
}
return result.obj();
}
double StatUtil::percent( const char * outof , const char * val , const BSONObj& a , const BSONObj& b ) {
double x = ( b.getFieldDotted( val ).number() - a.getFieldDotted( val ).number() );
double y = ( b.getFieldDotted( outof ).number() - a.getFieldDotted( outof ).number() );
if ( y == 0 )
return 0;
double p = x / y;
p = (double)((int)(p * 1000)) / 10;
return p;
}
double StatUtil::diff( const string& name , const BSONObj& a , const BSONObj& b ) {
BSONElement x = a.getFieldDotted( name.c_str() );
BSONElement y = b.getFieldDotted( name.c_str() );
if ( ! x.isNumber() || ! y.isNumber() )
return -1;
return ( y.number() - x.number() ) / _seconds;
}
void StatUtil::_appendNet( BSONObjBuilder& result , const string& name , double diff ) {
// I think 1000 is correct for megabit, but I've seen conflicting things (ERH 11/2010)
const double div = 1000;
string unit = "b";
if ( diff >= div ) {
unit = "k";
diff /= div;
}
if ( diff >= div ) {
unit = "m";
diff /= div;
}
if ( diff >= div ) {
unit = "g";
diff /= div;
}
string out = str::stream() << (int)diff << unit;
_append( result , name , 6 , out );
}
void StatUtil::_appendMem( BSONObjBuilder& result , const string& name , unsigned width , double sz ) {
string unit = "m";
if ( sz > 1024 ) {
unit = "g";
sz /= 1024;
}
if ( sz >= 1000 ) {
string s = str::stream() << (int)sz << unit;
_append( result , name , width , s );
return;
}
stringstream ss;
ss << setprecision(3) << sz << unit;
_append( result , name , width , ss.str() );
}
}
|