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