int run() { if (isMongos()) { toolError() << "mongotop only works on instances of mongod." << std::endl; return EXIT_FAILURE; } NamespaceStats prev = getData(); while ( true ) { sleepsecs(mongoTopGlobalParams.sleep); NamespaceStats now; try { now = getData(); } catch ( std::exception& e ) { toolError() << "can't get data: " << e.what() << std::endl; continue; } if ( now.size() == 0 ) return -2; try { printDiff( prev , now ); } catch ( AssertionException& e ) { toolError() << "\nerror: " << e.what() << std::endl; } prev = now; } return 0; }
void printDiff( const NamespaceStats& prev , const NamespaceStats& now ) { if ( prev.size() == 0 || now.size() == 0 ) { cout << "." << endl; return; } vector<NamespaceDiff> data = StatUtil::computeDiff( prev , now ); unsigned longest = 30; for ( unsigned i=0; i < data.size(); i++ ) { const string& ns = data[i].ns; if ( ! useLocks() && ns.find( '.' ) == string::npos ) continue; if ( ns.size() > longest ) longest = ns.size(); } int numberWidth = 10; cout << "\n" << setw(longest) << ( useLocks() ? "db" : "ns" ) << setw(numberWidth+2) << "total" << setw(numberWidth+2) << "read" << setw(numberWidth+2) << "write" << "\t\t" << terseCurrentTime() << endl; for ( int i=data.size()-1; i>=0 && data.size() - i < 10 ; i-- ) { if ( ! useLocks() && data[i].ns.find( '.' ) == string::npos ) continue; cout << setw(longest) << data[i].ns << setw(numberWidth) << setprecision(3) << data[i].total() << "ms" << setw(numberWidth) << setprecision(3) << data[i].read << "ms" << setw(numberWidth) << setprecision(3) << data[i].write << "ms" << endl; } }
vector<NamespaceDiff> StatUtil::computeDiff( const NamespaceStats& prev , const NamespaceStats& current ) { vector<NamespaceDiff> data; for ( NamespaceStats::const_iterator i = current.begin() ; i != current.end(); ++i ) { const string& ns = i->first; const NamespaceInfo& b = i->second; if ( prev.find( ns ) == prev.end() ) continue; const NamespaceInfo& a = prev.find( ns )->second; // invalid, data fixed in 1.8.0 if ( ns[0] == '?' ) continue; data.push_back( NamespaceDiff( a , b ) ); } std::sort( data.begin() , data.end() ); return data; }