CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Monitoring.cc
Go to the documentation of this file.
3 //
4 #include <cstdlib>
5 #include <fstream>
6 
8  m_start(start),
9  m_stop(boost::posix_time::neg_infin),
10  m_status(false){
11 }
12 
13 void ora::TransactionMonitoringData::stop(bool commit_status){
14  m_stop = boost::posix_time::microsec_clock::universal_time();
15  m_status = commit_status;
16 }
17 
18 ora::SessionMonitoringData::SessionMonitoringData( boost::posix_time::ptime start, const std::string& connectionString ):
19  m_connectionString( connectionString ),
20  m_start(start),
21  m_stop(boost::posix_time::neg_infin),
22  m_currentTransaction(0),
23  m_transactions(),
24  m_ncur(0){
25 }
26 
28  for(std::map<std::string,TransactionMonitoringData*>::const_iterator iT = m_transactions.begin();
29  iT != m_transactions.end();iT++ ){
30  delete iT->second;
31  }
32 }
33 
35  m_currentTransaction = new TransactionMonitoringData( boost::posix_time::microsec_clock::universal_time() );
36  m_transactions.insert(std::make_pair(guidFromTime(),m_currentTransaction));
37  m_ncur = m_transactions.size();
38  return m_ncur;
39 }
40 
41 size_t ora::SessionMonitoringData::stopTransaction( bool commit_status ){
42  size_t ncur = 0;
43  if(m_currentTransaction){
44  m_currentTransaction->stop( commit_status );
45  m_currentTransaction = 0;
46  ncur = m_ncur;
47  m_ncur = 0;
48  }
49  return ncur;
50 }
51 
53  m_stop = boost::posix_time::microsec_clock::universal_time();
54  m_currentTransaction = 0;
55  m_ncur = 0;
56 }
57 
59  return m_transactions.size();
60 }
61 
62 void ora::SessionMonitoringData::report( std::ostream& out ) const {
63  size_t i=1;
64  for(std::map<std::string,TransactionMonitoringData*>::const_iterator iT = m_transactions.begin();
65  iT != m_transactions.end();iT++ ){
66  TransactionMonitoringData& data = *iT->second;
67  boost::posix_time::time_duration duration;
68  if( !data.m_stop.is_neg_infinity() ){
69  duration = data.m_stop-data.m_start;
70  }
71  out <<" -> Transaction #"<<i<<" duration="<<boost::posix_time::to_simple_string(duration)<<" status="<<(data.m_status?std::string("COMMIT"):std::string("ROLLBACK"))<<std::endl;
72  i++;
73  }
74 }
75 
76 bool ora::Monitoring::s_enabled = false;
77 
79  static ora::Monitoring s_mon;
80  return s_mon;
81 }
82 
84  if(! s_enabled ){
85  const char* envVar = ::getenv( "ORA_MONITORING_LEVEL" );
86  if( envVar && ::strcmp(envVar,"SESSION")==0 ) s_enabled = true;
87  }
88  return s_enabled;
89 }
90 
92  s_enabled = true;
93 }
94 
96  static std::string s_outFileName("");
97  if( s_outFileName.empty() ){
98  const char* fileEnvVar = ::getenv( "ORA_MONITORING_FILE" );
99  if( fileEnvVar ){
100  s_outFileName = fileEnvVar;
101  }
102  }
103  return s_outFileName;
104 }
105 
107  if( isEnabled() ){
108  try {
109  if( !outFileName().empty() ){
110  std::ofstream outFile;
111  outFile.open( outFileName().c_str() );
112  if(outFile.good()){
113  report( outFile );
114  outFile.flush();
115  }
116  outFile.close();
117 
118  } else {
119  report( std::cout );
120  }
121  } catch ( const std::exception& e ){
122  std::cout <<"ORA_MONITORING Error: "<<e.what()<<std::endl;
123  }
124  }
125  // clean up memory
126  for(std::map<std::string,SessionMonitoringData*>::const_iterator iS = m_sessions.begin();
127  iS != m_sessions.end();iS++ ){
128  delete iS->second;
129  }
130 }
131 
132 ora::SessionMonitoringData* ora::Monitoring::startSession( const std::string& connectionString ){
133  ora::SessionMonitoringData* ret = new SessionMonitoringData( boost::posix_time::microsec_clock::universal_time(), connectionString );
134  m_sessions.insert(std::make_pair(guidFromTime(),ret));
135  return ret;
136 }
137 
138 void ora::Monitoring::report( std::ostream& out ){
139  out << "### ---------------------------------------------------------------------- "<<std::endl;
140  out << "### ORA Monitoring Summary "<<std::endl;
141  out << "### "<<m_sessions.size()<<" session(s) registered."<<std::endl;
142  size_t j = 1;
143  for( std::map<std::string,SessionMonitoringData*>::const_iterator iS = m_sessions.begin();
144  iS != m_sessions.end(); ++iS ){
145  SessionMonitoringData& data = *iS->second;
146  boost::posix_time::time_duration duration;
147  if( !data.m_stop.is_neg_infinity() ){
148  duration = data.m_stop-data.m_start;
149  }
150  out <<" -> Session #"<<j<<": connection=\""<<data.m_connectionString<<"\" duration="<<boost::posix_time::to_simple_string(duration)<<" transactions="<<(iS->second)->numberOfTransactions()<<std::endl;
151  (iS->second)->report(out);
152  j++;
153  }
154  out << "### ---------------------------------------------------------------------- "<<std::endl;
155 
156 }
157 
159  m_sessions(){
160 }
int i
Definition: DBlmapReader.cc:9
TransactionMonitoringData(boost::posix_time::ptime start)
Definition: Monitoring.cc:7
size_t stopTransaction(bool commit_status=true)
Definition: Monitoring.cc:41
boost::posix_time::ptime m_stop
Definition: Monitoring.h:16
static Monitoring & get()
Definition: Monitoring.cc:78
std::string m_connectionString
Definition: Monitoring.h:29
static bool s_enabled
Definition: Monitoring.h:53
static std::string & outFileName()
Definition: Monitoring.cc:95
SessionMonitoringData * startSession(const std::string &connectionString)
Definition: Monitoring.cc:132
static void enable()
Definition: Monitoring.cc:91
void report(std::ostream &out) const
Definition: Monitoring.cc:62
tuple report
Definition: zeeHLT_cff.py:9
boost::posix_time::ptime m_stop
Definition: Monitoring.h:31
SessionMonitoringData(boost::posix_time::ptime start, const std::string &connectionString)
Definition: Monitoring.cc:18
int j
Definition: DBlmapReader.cc:9
void report(std::ostream &out)
Definition: Monitoring.cc:138
boost::posix_time::ptime m_start
Definition: Monitoring.h:15
void stop(bool commit_status=true)
Definition: Monitoring.cc:13
tuple out
Definition: dbtoconf.py:99
virtual ~Monitoring()
Definition: Monitoring.cc:106
size_t numberOfTransactions() const
Definition: Monitoring.cc:58
static bool isEnabled()
Definition: Monitoring.cc:83
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
tuple cout
Definition: gather_cfg.py:121
boost::posix_time::ptime m_start
Definition: Monitoring.h:30
std::string guidFromTime()
Definition: Guid.cc:37