CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/CondCore/ORA/src/Monitoring.cc

Go to the documentation of this file.
00001 #include "CondCore/ORA/interface/Monitoring.h"
00002 #include "CondCore/ORA/interface/Guid.h"
00003 //
00004 #include <cstdlib>
00005 #include <fstream>
00006 
00007 ora::TransactionMonitoringData::TransactionMonitoringData( boost::posix_time::ptime start ):
00008   m_start(start),
00009   m_stop(boost::posix_time::neg_infin),
00010   m_status(false){
00011 }
00012 
00013 void ora::TransactionMonitoringData::stop(bool commit_status){
00014   m_stop = boost::posix_time::microsec_clock::universal_time();
00015   m_status = commit_status;
00016 }
00017 
00018 ora::SessionMonitoringData::SessionMonitoringData( boost::posix_time::ptime start, const std::string& connectionString ):
00019   m_connectionString( connectionString ),
00020   m_start(start),
00021   m_stop(boost::posix_time::neg_infin),
00022   m_currentTransaction(0),
00023   m_transactions(),
00024   m_ncur(0){
00025 }
00026 
00027 ora::SessionMonitoringData::~SessionMonitoringData(){
00028   for(std::map<std::string,TransactionMonitoringData*>::const_iterator iT = m_transactions.begin();
00029       iT != m_transactions.end();iT++ ){
00030     delete iT->second;
00031   }
00032 }
00033 
00034 size_t ora::SessionMonitoringData::newTransaction(){
00035   m_currentTransaction = new TransactionMonitoringData( boost::posix_time::microsec_clock::universal_time() );
00036   m_transactions.insert(std::make_pair(guidFromTime(),m_currentTransaction));
00037   m_ncur = m_transactions.size();
00038   return m_ncur;
00039 }
00040 
00041 size_t ora::SessionMonitoringData::stopTransaction( bool commit_status ){
00042   size_t ncur = 0;
00043   if(m_currentTransaction){
00044     m_currentTransaction->stop( commit_status );
00045     m_currentTransaction = 0;
00046     ncur = m_ncur;
00047     m_ncur = 0;
00048   }
00049   return ncur;
00050 }
00051 
00052 void ora::SessionMonitoringData::stop(){
00053   m_stop = boost::posix_time::microsec_clock::universal_time();
00054   m_currentTransaction = 0;
00055   m_ncur = 0;
00056 }
00057 
00058 size_t ora::SessionMonitoringData::numberOfTransactions() const{
00059   return m_transactions.size();
00060 }
00061 
00062 void ora::SessionMonitoringData::report( std::ostream& out ) const {
00063   size_t i=1;
00064   for(std::map<std::string,TransactionMonitoringData*>::const_iterator iT = m_transactions.begin();
00065       iT != m_transactions.end();iT++ ){
00066     TransactionMonitoringData& data = *iT->second;
00067     boost::posix_time::time_duration duration;
00068     if( !data.m_stop.is_neg_infinity() ){
00069       duration = data.m_stop-data.m_start;
00070     }
00071     out <<"   -> Transaction #"<<i<<" duration="<<boost::posix_time::to_simple_string(duration)<<" status="<<(data.m_status?std::string("COMMIT"):std::string("ROLLBACK"))<<std::endl;
00072     i++;
00073   }  
00074 }
00075 
00076 bool ora::Monitoring::s_enabled = false;
00077 
00078 ora::Monitoring& ora::Monitoring::get(){
00079   static ora::Monitoring s_mon;
00080   return s_mon;
00081 }
00082 
00083 bool ora::Monitoring::isEnabled(){
00084   if(! s_enabled ){
00085     const char* envVar = ::getenv( "ORA_MONITORING_LEVEL" );
00086     if( envVar && ::strcmp(envVar,"SESSION")==0 ) s_enabled = true;
00087   }
00088   return s_enabled;
00089 }
00090 
00091 void ora::Monitoring::enable(){
00092   s_enabled = true;
00093 }
00094 
00095 std::string& ora::Monitoring::outFileName(){
00096   static std::string s_outFileName("");
00097   if( s_outFileName.empty() ){
00098     const char* fileEnvVar = ::getenv( "ORA_MONITORING_FILE" );
00099     if( fileEnvVar ){
00100       s_outFileName = fileEnvVar;
00101     }
00102   }
00103   return s_outFileName;
00104 }
00105 
00106 ora::Monitoring::~Monitoring() throw(){
00107   if( isEnabled() ){
00108     try {
00109       if( !outFileName().empty() ){
00110         std::ofstream outFile;
00111         outFile.open(  outFileName().c_str() );
00112         if(outFile.good()){
00113           report( outFile );
00114           outFile.flush();
00115         }
00116         outFile.close();
00117         
00118       } else {
00119         report( std::cout );
00120       }
00121     } catch ( const std::exception& e ){
00122       std::cout <<"ORA_MONITORING Error: "<<e.what()<<std::endl;
00123     }
00124   }
00125   // clean up memory
00126   for(std::map<std::string,SessionMonitoringData*>::const_iterator iS = m_sessions.begin();
00127       iS != m_sessions.end();iS++ ){
00128     delete iS->second;
00129   }
00130 }
00131 
00132 ora::SessionMonitoringData* ora::Monitoring::startSession( const std::string& connectionString ){
00133   ora::SessionMonitoringData* ret = new SessionMonitoringData( boost::posix_time::microsec_clock::universal_time(), connectionString );
00134   m_sessions.insert(std::make_pair(guidFromTime(),ret));
00135   return ret;
00136 }
00137 
00138 void ora::Monitoring::report( std::ostream& out ){
00139   out << "### ---------------------------------------------------------------------- "<<std::endl;
00140   out << "### ORA Monitoring Summary "<<std::endl;
00141   out << "### "<<m_sessions.size()<<" session(s) registered."<<std::endl;
00142   size_t j = 1;
00143   for( std::map<std::string,SessionMonitoringData*>::const_iterator iS = m_sessions.begin();
00144        iS != m_sessions.end(); ++iS ){
00145     SessionMonitoringData& data = *iS->second;
00146     boost::posix_time::time_duration duration;
00147     if( !data.m_stop.is_neg_infinity() ){
00148       duration = data.m_stop-data.m_start;
00149     }
00150     out <<" -> Session #"<<j<<": connection=\""<<data.m_connectionString<<"\" duration="<<boost::posix_time::to_simple_string(duration)<<" transactions="<<(iS->second)->numberOfTransactions()<<std::endl;
00151     (iS->second)->report(out);
00152     j++;
00153   }
00154   out << "### ---------------------------------------------------------------------- "<<std::endl;
00155 
00156 }
00157  
00158 ora::Monitoring::Monitoring():
00159   m_sessions(){
00160 }