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 
7 namespace {
8  char const * const kOperationNames[] = {
9  "check",
10  "close",
11  "construct",
12  "destruct",
13  "flush",
14  "open",
15  "position",
16  "prefetch",
17  "read",
18  "readActual",
19  "readAsync",
20  "readPrefetchToCache",
21  "readViaCache",
22  "readv",
23  "resize",
24  "seek",
25  "stagein",
26  "stat",
27  "write",
28  "writeActual",
29  "writeViaCache",
30  "writev"
31  };
32 
33  //Storage class names to the value of the token to which they are assigned
34  tbb::concurrent_unordered_map<std::string, int> s_nameToToken;
35  std::atomic<int> s_nextTokenValue{0};
36 }
37 
39 
40 static std::string i2str(int i) {
41  std::ostringstream t;
42  t << i;
43  return t.str();
44 }
45 
46 static std::string d2str(double d) {
47  std::ostringstream t;
48  t << d;
49  return t.str();
50 }
51 
52 inline char const* StorageAccount::operationName(Operation operation) {
53  return kOperationNames[static_cast<int>(operation)];
54 }
55 
57  auto itFound = s_nameToToken.find(iName);
58  if( itFound != s_nameToToken.end()) {
59  return StorageClassToken(itFound->second);
60  }
61  int value = s_nextTokenValue++;
62 
63  auto ret = s_nameToToken.insert(std::make_pair(iName, value));
64 
65  //don't use value since another thread may have beaten us to here
66  return StorageClassToken(ret.second);
67 }
68 
70  for( auto it = s_nameToToken.begin(), itEnd = s_nameToToken.end(); it != itEnd; ++it) {
71  if (it->second == iToken.value()) {
72  return it->first;
73  }
74  }
75  assert(false);
76 }
77 
78 
80 StorageAccount::summaryText (bool banner /*=false*/) {
81  bool first = true;
82  std::ostringstream os;
83  if (banner)
84  os << "stats: class/operation/attempts/successes/amount/time-total/time-min/time-max\n";
85  for (auto i = s_nameToToken.begin (); i != s_nameToToken.end(); ++i) {
86  auto const& opStats = m_stats[i->second];
87  for (auto j = opStats.begin (); j != opStats.end (); ++j, first = false)
88  os << (first ? "" : "; ")
89  << (i->first) << '/'
90  << kOperationNames[j->first] << '='
91  << j->second.attempts << '/'
92  << j->second.successes << '/'
93  << (static_cast<double>(j->second.amount) / 1024 / 1024) << "MB/"
94  << (static_cast<double>(j->second.timeTotal) / 1000 / 1000) << "ms/"
95  << (static_cast<double>(j->second.timeMin) / 1000 / 1000) << "ms/"
96  << (static_cast<double>(j->second.timeMax) / 1000 / 1000) << "ms";
97  }
98  return os.str ();
99 }
100 
101 void
102 StorageAccount::fillSummary(std::map<std::string, std::string>& summary) {
103  int const oneM = 1000 * 1000;
104  int const oneMeg = 1024 * 1024;
105  for (auto i = s_nameToToken.begin (); i != s_nameToToken.end(); ++i) {
106  auto const& opStats = m_stats[i->second];
107  for (auto j = opStats.begin(); j != opStats.end(); ++j) {
108  std::ostringstream os;
109  os << "Timing-" << i->first << "-" << kOperationNames[j->first] << "-";
110  summary.insert(std::make_pair(os.str() + "numOperations", i2str(j->second.attempts)));
111  summary.insert(std::make_pair(os.str() + "numSuccessfulOperations", i2str(j->second.successes)));
112  summary.insert(std::make_pair(os.str() + "totalMegabytes", d2str(static_cast<double>(j->second.amount) / oneMeg)));
113  summary.insert(std::make_pair(os.str() + "totalMsecs", d2str(static_cast<double>(j->second.timeTotal) / oneM)));
114  summary.insert(std::make_pair(os.str() + "minMsecs", d2str(static_cast<double>(j->second.timeMin) / oneM)));
115  summary.insert(std::make_pair(os.str() + "maxMsecs", d2str(static_cast<double>(j->second.timeMax) / oneM)));
116  }
117  }
118 }
119 
122 { return m_stats; }
123 
126  auto &opstats = m_stats [token.value()];
127 
128  return opstats[static_cast<int>(operation)];
129 }
130 
132  : m_counter (counter),
133  m_start (std::chrono::high_resolution_clock::now())
134 {
136 }
137 
138 void
140 {
142  uint64_t elapsed = elapsed_ns.count();
143  m_counter.successes++;
144 
145  m_counter.vector_count += count;
146  m_counter.vector_square += count*count;
147  m_counter.amount += amount;
148  Counter::addTo(m_counter.amount_square, amount*amount);
149 
150  Counter::addTo(m_counter.timeTotal, elapsed);
151  if (elapsed < m_counter.timeMin || m_counter.successes == 1)
152  m_counter.timeMin = elapsed;
153  if (elapsed > m_counter.timeMax)
154  m_counter.timeMax = elapsed;
155 }
int i
Definition: DBlmapReader.cc:9
static StorageStats m_stats
static void fillSummary(std::map< std::string, std::string > &summary)
static char const * operationName(Operation operation)
assert(m_qm.get())
static const StorageStats & summary(void)
std::atomic< uint64_t > attempts
static std::string d2str(double d)
tuple d
Definition: ztail.py:151
tbb::concurrent_unordered_map< int, OperationStats > StorageStats
static StorageClassToken tokenForStorageClassName(std::string const &iName)
int j
Definition: DBlmapReader.cc:9
static void addTo(std::atomic< double > &iAtomic, double iToAdd)
static const std::string & nameForToken(StorageClassToken)
static std::string i2str(int i)
static Counter & counter(StorageClassToken token, Operation operation)
unsigned long long uint64_t
Definition: Time.h:15
void tick(uint64_t amount=0, int64_t tick=0) const
Stamp(Counter &counter)
static std::atomic< unsigned int > counter
static std::string summaryText(bool banner=false)
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds