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