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