CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StorageAccount.cc
Go to the documentation of this file.
2 #include <boost/thread/mutex.hpp>
3 #include <sstream>
4 #include <unistd.h>
5 
8 
9 static double timeRealNanoSecs (void) {
10 #if _POSIX_TIMERS > 0
11  struct timespec tm;
12  if (clock_gettime(CLOCK_REALTIME, &tm) == 0)
13  return tm.tv_sec * 1e9 + tm.tv_nsec;
14 #else
15  struct timeval tm;
16  if (gettimeofday(&tm, 0) == 0)
17  return tm.tv_sec * 1e9 + tm.tv_usec * 1e3;
18 #endif
19  return 0;
20 }
21 
22 static std::string i2str(int i) {
23  std::ostringstream t;
24  t << i;
25  return t.str();
26 }
27 
28 static std::string d2str(double d) {
29  std::ostringstream t;
30  t << d;
31  return t.str();
32 }
33 
35 StorageAccount::summaryText (bool banner /*=false*/) {
36  bool first = true;
37  std::ostringstream os;
38  if (banner)
39  os << "stats: class/operation/attempts/successes/amount/time-total/time-min/time-max\n";
40  for (StorageStats::iterator i = s_stats.begin (); i != s_stats.end(); ++i)
41  for (OperationStats::iterator j = i->second->begin (); j != i->second->end (); ++j, first = false)
42  os << (first ? "" : "; ")
43  << i->first << '/'
44  << j->first << '='
45  << j->second.attempts << '/'
46  << j->second.successes << '/'
47  << (j->second.amount / 1024 / 1024) << "MB/"
48  << (j->second.timeTotal / 1000 / 1000) << "ms/"
49  << (j->second.timeMin / 1000 / 1000) << "ms/"
50  << (j->second.timeMax / 1000 / 1000) << "ms";
51 
52  return os.str ();
53 }
54 
57  std::ostringstream os;
58  os << "<storage-timing-summary>\n";
59  for (StorageStats::iterator i = s_stats.begin (); i != s_stats.end(); ++i)
60  for (OperationStats::iterator j = i->second->begin (); j != i->second->end (); ++j)
61  os << " <counter-value subsystem='" << i->first
62  << "' counter-name='" << j->first
63  << "' num-operations='" << j->second.attempts
64  << "' num-successful-operations='" << j->second.successes
65  << "' total-megabytes='" << (j->second.amount / 1024 / 1024)
66  << "' total-msecs='" << (j->second.timeTotal / 1000 / 1000)
67  << "' min-msecs='" << (j->second.timeMin / 1000 / 1000)
68  << "' max-msecs='" << (j->second.timeMax / 1000 / 1000) << "'/>\n";
69  os << "</storage-timing-summary>";
70  return os.str ();
71 }
72 
73 void
74 StorageAccount::fillSummary(std::map<std::string, std::string>& summary) {
75  int const oneM = 1000 * 1000;
76  int const oneMeg = 1024 * 1024;
77  for (StorageStats::iterator i = s_stats.begin (); i != s_stats.end(); ++i) {
78  for (OperationStats::iterator j = i->second->begin(); j != i->second->end(); ++j) {
79  std::ostringstream os;
80  os << "Timing-" << i->first << "-" << j->first << "-";
81  summary.insert(std::make_pair(os.str() + "numOperations", i2str(j->second.attempts)));
82  summary.insert(std::make_pair(os.str() + "numSuccessfulOperations", i2str(j->second.successes)));
83  summary.insert(std::make_pair(os.str() + "totalMegabytes", d2str(j->second.amount / oneMeg)));
84  summary.insert(std::make_pair(os.str() + "totalMsecs", d2str(j->second.timeTotal / oneM)));
85  summary.insert(std::make_pair(os.str() + "minMsecs", d2str(j->second.timeMin / oneM)));
86  summary.insert(std::make_pair(os.str() + "maxMsecs", d2str(j->second.timeMax / oneM)));
87  }
88  }
89 }
90 
93 { return s_stats; }
94 
96 StorageAccount::counter (const std::string &storageClass, const std::string &operation) {
97  boost::mutex::scoped_lock lock (s_mutex);
98  boost::shared_ptr<OperationStats> &opstats = s_stats [storageClass];
99  if (!opstats) opstats.reset(new OperationStats);
100 
101  OperationStats::iterator pos = opstats->find (operation);
102  if (pos == opstats->end ()) {
103  Counter x = { 0, 0, 0, 0, 0, 0, 0 };
104  pos = opstats->insert (OperationStats::value_type (operation, x)).first;
105  }
106 
107  return pos->second;
108 }
109 
111  : m_counter (counter),
112  m_start (timeRealNanoSecs ())
113 {
114  boost::mutex::scoped_lock lock (s_mutex);
116 }
117 
118 void
119 StorageAccount::Stamp::tick (double amount, int64_t count) const
120 {
121  boost::mutex::scoped_lock lock (s_mutex);
122  double elapsed = timeRealNanoSecs () - m_start;
123  m_counter.successes++;
124 
125  m_counter.vector_count += count;
126  m_counter.vector_square += count*count;
127  m_counter.amount += amount;
128  m_counter.amount_square += amount*amount;
129 
130  m_counter.timeTotal += elapsed;
131  if (elapsed < m_counter.timeMin || m_counter.successes == 1)
132  m_counter.timeMin = elapsed;
133  if (elapsed > m_counter.timeMax)
134  m_counter.timeMax = elapsed;
135 }
int i
Definition: DBlmapReader.cc:9
static double timeRealNanoSecs(void)
void tick(double amount=0., int64_t tick=0) const
static void fillSummary(std::map< std::string, std::string > &summary)
static boost::mutex mutex
Definition: LHEProxy.cc:11
std::map< std::string, Counter > OperationStats
static const StorageStats & summary(void)
StorageAccount::StorageStats s_stats
std::map< std::string, boost::shared_ptr< OperationStats > > StorageStats
static std::string d2str(double d)
int j
Definition: DBlmapReader.cc:9
bool first
Definition: L1TdeRCT.cc:79
Container::value_type value_type
static std::string i2str(int i)
static std::string summaryXML(void)
static Counter & counter(const std::string &storageClass, const std::string &operation)
Stamp(Counter &counter)
static std::atomic< unsigned int > counter
static std::string summaryText(bool banner=false)
Definition: DDAxes.h:10
boost::mutex s_mutex
#define CLOCK_REALTIME
Definition: TimerService.h:32