CMS 3D CMS Logo

StorageAccount.cc
Go to the documentation of this file.
2 #include <cassert>
3 #include <mutex>
4 #include <sstream>
5 #include <unistd.h>
6 #include <sys/time.h>
7 using namespace edm::storage;
8 
9 namespace {
10  char const* const kOperationNames[] = {
11  "check", "close", "construct", "destruct", "flush", "open",
12  "position", "prefetch", "read", "readActual", "readAsync", "readPrefetchToCache",
13  "readViaCache", "readv", "resize", "seek", "stagein", "stat",
14  "write", "writeActual", "writeViaCache", "writev"};
15 
16  //Storage class names to the value of the token to which they are assigned
17  oneapi::tbb::concurrent_unordered_map<std::string, int> s_nameToToken;
18  std::atomic<int> s_nextTokenValue{0};
19 } // namespace
20 
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 
35 inline char const* StorageAccount::operationName(Operation operation) {
36  return kOperationNames[static_cast<int>(operation)];
37 }
38 
40  auto itFound = s_nameToToken.find(iName);
41  if (itFound != s_nameToToken.end()) {
42  return StorageClassToken(itFound->second);
43  }
44  int value = s_nextTokenValue++;
45 
46  s_nameToToken.insert(std::make_pair(iName, value));
47 
48  return StorageClassToken(value);
49 }
50 
52  for (auto it = s_nameToToken.begin(), itEnd = s_nameToToken.end(); it != itEnd; ++it) {
53  if (it->second == iToken.value()) {
54  return it->first;
55  }
56  }
57  assert(false);
58 }
59 
60 std::string StorageAccount::summaryText(bool banner /*=false*/) {
61  bool first = true;
62  std::ostringstream os;
63  if (banner)
64  os << "stats: class/operation/attempts/successes/amount/time-total/time-min/time-max\n";
65  for (auto i = s_nameToToken.begin(); i != s_nameToToken.end(); ++i) {
66  auto const& opStats = m_stats[i->second];
67  for (auto j = opStats.begin(); j != opStats.end(); ++j, first = false)
68  os << (first ? "" : "; ") << (i->first) << '/' << kOperationNames[j->first] << '=' << j->second.attempts << '/'
69  << j->second.successes << '/' << (static_cast<double>(j->second.amount) / 1024 / 1024) << "MB/"
70  << (static_cast<double>(j->second.timeTotal) / 1000 / 1000) << "ms/"
71  << (static_cast<double>(j->second.timeMin) / 1000 / 1000) << "ms/"
72  << (static_cast<double>(j->second.timeMax) / 1000 / 1000) << "ms";
73  }
74  return os.str();
75 }
76 
77 void StorageAccount::fillSummary(std::map<std::string, std::string>& summary) {
78  int const oneM = 1000 * 1000;
79  int const oneMeg = 1024 * 1024;
80  for (auto i = s_nameToToken.begin(); i != s_nameToToken.end(); ++i) {
81  auto const& opStats = m_stats[i->second];
82  for (auto j = opStats.begin(); j != opStats.end(); ++j) {
83  std::ostringstream os;
84  os << "Timing-" << i->first << "-" << kOperationNames[j->first] << "-";
85  summary.insert(std::make_pair(os.str() + "numOperations", i2str(j->second.attempts)));
86  summary.insert(std::make_pair(os.str() + "numSuccessfulOperations", i2str(j->second.successes)));
87  summary.insert(
88  std::make_pair(os.str() + "totalMegabytes", d2str(static_cast<double>(j->second.amount) / oneMeg)));
89  summary.insert(std::make_pair(os.str() + "totalMsecs", d2str(static_cast<double>(j->second.timeTotal) / oneM)));
90  summary.insert(std::make_pair(os.str() + "minMsecs", d2str(static_cast<double>(j->second.timeMin) / oneM)));
91  summary.insert(std::make_pair(os.str() + "maxMsecs", d2str(static_cast<double>(j->second.timeMax) / oneM)));
92  }
93  }
94 }
95 
97 
99  auto& opstats = m_stats[token.value()];
100 
101  return opstats[static_cast<int>(operation)];
102 }
103 
105  : m_counter(counter), m_start(std::chrono::high_resolution_clock::now()) {
107 }
108 
109 void StorageAccount::Stamp::tick(uint64_t amount, int64_t count) const {
111  uint64_t elapsed = elapsed_ns.count();
112  m_counter.successes++;
113 
114  m_counter.vector_count += count;
115  m_counter.vector_square += count * count;
116  m_counter.amount += amount;
117  Counter::addTo(m_counter.amount_square, amount * amount);
118 
119  Counter::addTo(m_counter.timeTotal, elapsed);
120  if (elapsed < m_counter.timeMin || m_counter.successes == 1)
121  m_counter.timeMin = elapsed;
122  if (elapsed > m_counter.timeMax)
123  m_counter.timeMax = elapsed;
124 }
static std::string summaryText(bool banner=false)
boost::date_time::subsecond_duration< boost::posix_time::time_duration, 1000000000 > nanoseconds
static StorageClassToken tokenForStorageClassName(std::string const &iName)
oneapi::tbb::concurrent_unordered_map< int, OperationStats > StorageStats
static Counter & counter(StorageClassToken token, Operation operation)
void tick(uint64_t amount=0, int64_t tick=0) const
assert(be >=bs)
static void fillSummary(std::map< std::string, std::string > &summary)
static std::string d2str(double d)
Definition: value.py:1
static const std::string & nameForToken(StorageClassToken)
d
Definition: ztail.py:151
static std::string i2str(int i)
std::atomic< uint64_t > attempts
unsigned long long uint64_t
Definition: Time.h:13
static const StorageStats & summary(void)
static StorageStats m_stats
static char const * operationName(Operation operation)
static void addTo(std::atomic< double > &iAtomic, double iToAdd)
T first(std::pair< T, U > const &p)