CMS 3D CMS Logo

StorageAccount.h
Go to the documentation of this file.
1 #ifndef STORAGE_FACTORY_STORAGE_ACCOUNT_H
2 #define STORAGE_FACTORY_STORAGE_ACCOUNT_H
3 
4 #include <cstdint>
5 #include <string>
6 #include <chrono>
7 #include <atomic>
8 #include <map>
9 #include <memory>
10 #include "tbb/concurrent_unordered_map.h"
11 
13 public:
14  enum class Operation {
15  check,
16  close,
17  construct,
18  destruct,
19  flush,
20  open,
21  position,
22  prefetch,
23  read,
24  readActual,
25  readAsync,
28  readv,
29  resize,
30  seek,
31  stagein,
32  stat,
33  write,
36  writev
37  };
38 
39  struct Counter {
41  : attempts{0},
42  successes{0},
43  amount{0},
44  amount_square{0.},
45  vector_count{0},
46  vector_square{0},
47  timeTotal{0.},
48  timeMin{0.},
49  timeMax{0.} {}
50 
51  //NOTE: This is needed by tbb::concurrent_unordered_map when it
52  // is constructing a new one. This would not give correct results
53  // if the object being passed were being updated, but that is not
54  // the case for operator[]
55  Counter(Counter const& iOther)
56  : attempts{iOther.attempts.load()},
57  successes{iOther.successes.load()},
58  amount{iOther.amount.load()},
59  amount_square{iOther.amount_square.load()},
60  vector_count{iOther.vector_count.load()},
61  vector_square{iOther.vector_square.load()},
62  timeTotal{iOther.timeTotal.load()},
63  timeMin{iOther.timeMin.load()},
64  timeMax{iOther.timeMax.load()} {}
65 
66  //Use atomics to allow concurrent read/write for intermediate
67  // output of the statics while running. The values obtained
68  // won't be completely consistent but should be good enough for
69  // monitoring. The values obtained once the program is shutting
70  // down should be completely consistent.
71 
72  std::atomic<uint64_t> attempts;
73  std::atomic<uint64_t> successes;
74  std::atomic<uint64_t> amount;
75  // NOTE: Significant risk exists for underflow in this value.
76  // However, the use cases are marginal so I let it pass.
77  std::atomic<double> amount_square;
78  std::atomic<int64_t> vector_count;
79  std::atomic<int64_t> vector_square;
80  std::atomic<double> timeTotal;
81  std::atomic<double> timeMin;
82  std::atomic<double> timeMax;
83 
84  static void addTo(std::atomic<double>& iAtomic, double iToAdd) {
85  double oldValue = iAtomic.load();
86  double newValue = oldValue + iToAdd;
87  while (not iAtomic.compare_exchange_weak(oldValue, newValue)) {
88  newValue = oldValue + iToAdd;
89  }
90  }
91  };
92 
93  class Stamp {
94  public:
96 
97  void tick(uint64_t amount = 0, int64_t tick = 0) const;
98 
99  protected:
101  std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
102  };
103 
105  public:
106  StorageClassToken(StorageClassToken const&) = default;
107  int value() const { return m_value; }
108 
109  friend class StorageAccount;
110 
111  private:
112  StorageClassToken() = delete;
113  explicit StorageClassToken(int iValue) : m_value{iValue} {}
114 
115  int m_value;
116  };
117 
118  typedef tbb::concurrent_unordered_map<int, Counter> OperationStats;
119  typedef tbb::concurrent_unordered_map<int, OperationStats> StorageStats;
120 
121  static char const* operationName(Operation operation);
124 
125  static const StorageStats& summary(void);
126  static std::string summaryText(bool banner = false);
127  static void fillSummary(std::map<std::string, std::string>& summary);
128  static Counter& counter(StorageClassToken token, Operation operation);
129 
130 private:
132 };
133 
134 #endif // STORAGE_FACTORY_STORAGE_ACCOUNT_H
StorageAccount::Operation::readAsync
StorageAccount::Operation::flush
counter
Definition: counter.py:1
StorageAccount::StorageStats
tbb::concurrent_unordered_map< int, OperationStats > StorageStats
Definition: StorageAccount.h:119
StorageAccount::Operation::seek
StorageAccount::Operation::stat
StorageAccount::Counter::timeTotal
std::atomic< double > timeTotal
Definition: StorageAccount.h:80
StorageAccount::Operation::prefetch
StorageAccount::m_stats
static StorageStats m_stats
Definition: StorageAccount.h:131
StorageAccount::Counter
Definition: StorageAccount.h:39
StorageAccount::Counter::amount
std::atomic< uint64_t > amount
Definition: StorageAccount.h:74
StorageAccount::Operation::readPrefetchToCache
StorageAccount::Operation::position
StorageAccount::Counter::timeMax
std::atomic< double > timeMax
Definition: StorageAccount.h:82
StorageAccount::Counter::vector_square
std::atomic< int64_t > vector_square
Definition: StorageAccount.h:79
StorageAccount::StorageClassToken::StorageClassToken
StorageClassToken()=delete
StorageAccount::Operation::readv
StorageAccount::StorageClassToken::m_value
int m_value
Definition: StorageAccount.h:115
StorageAccount::OperationStats
tbb::concurrent_unordered_map< int, Counter > OperationStats
Definition: StorageAccount.h:118
StorageAccount::Operation::writeActual
StorageAccount::Counter::successes
std::atomic< uint64_t > successes
Definition: StorageAccount.h:73
StorageAccount::Operation::close
StorageAccount::Operation::readActual
StorageAccount::nameForToken
static const std::string & nameForToken(StorageClassToken)
Definition: StorageAccount.cc:50
StorageAccount::Operation
Operation
Definition: StorageAccount.h:14
StorageAccount::Operation::check
StorageAccount::Stamp::m_start
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
Definition: StorageAccount.h:101
StorageAccount::StorageClassToken::value
int value() const
Definition: StorageAccount.h:107
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
StorageAccount::Counter::vector_count
std::atomic< int64_t > vector_count
Definition: StorageAccount.h:78
StorageAccount::Operation::resize
StorageAccount::Stamp
Definition: StorageAccount.h:93
StorageAccount::Counter::attempts
std::atomic< uint64_t > attempts
Definition: StorageAccount.h:72
StorageAccount::Operation::open
StorageAccount
Definition: StorageAccount.h:12
StorageAccount::operationName
static char const * operationName(Operation operation)
Definition: StorageAccount.cc:34
StorageAccount::StorageClassToken::StorageClassToken
StorageClassToken(int iValue)
Definition: StorageAccount.h:113
StorageAccount::tokenForStorageClassName
static StorageClassToken tokenForStorageClassName(std::string const &iName)
Definition: StorageAccount.cc:38
StorageAccount::Operation::writeViaCache
StorageAccount::summary
static const StorageStats & summary(void)
Definition: StorageAccount.cc:95
StorageAccount::Stamp::Stamp
Stamp(Counter &counter)
Definition: StorageAccount.cc:103
StorageAccount::Counter::addTo
static void addTo(std::atomic< double > &iAtomic, double iToAdd)
Definition: StorageAccount.h:84
StorageAccount::Operation::stagein
StorageAccount::Stamp::tick
void tick(uint64_t amount=0, int64_t tick=0) const
Definition: StorageAccount.cc:108
StorageAccount::Operation::write
StorageAccount::Operation::read
StorageAccount::Counter::amount_square
std::atomic< double > amount_square
Definition: StorageAccount.h:77
StorageAccount::Counter::timeMin
std::atomic< double > timeMin
Definition: StorageAccount.h:81
StorageAccount::summaryText
static std::string summaryText(bool banner=false)
Definition: StorageAccount.cc:59
StorageAccount::counter
static Counter & counter(StorageClassToken token, Operation operation)
Definition: StorageAccount.cc:97
StorageAccount::Operation::destruct
StorageAccount::Operation::readViaCache
StorageAccount::Counter::Counter
Counter(Counter const &iOther)
Definition: StorageAccount.h:55
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
StorageAccount::Counter::Counter
Counter()
Definition: StorageAccount.h:40
StorageAccount::fillSummary
static void fillSummary(std::map< std::string, std::string > &summary)
Definition: StorageAccount.cc:76
StorageAccount::Stamp::m_counter
Counter & m_counter
Definition: StorageAccount.h:100
StorageAccount::StorageClassToken
Definition: StorageAccount.h:104
StorageAccount::Operation::writev
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:318
StorageAccount::Operation::construct