CMS 3D CMS Logo

ELmap.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------
2 //
3 // ELmap.cc
4 //
5 // Change History:
6 // 99-06-10 mf correction in sense of comparison between timespan
7 // and diff (now, lastTime)
8 // mf ELcountTRACE made available
9 // 99-06-11 mf Corrected logic for suppressing output when n > limit
10 // but not but a factor of 2**K
11 // 06-05-16 mf Added code to establish interval and to use skipped
12 // and interval when determinine in add() whehter to react
13 // 06-05-19 wmtan Bug fix. skipped = 0, not skipped == 0.
14 // and interval when determinine in add() whehter to react
15 // 09-04-15 wmtan Use smart pointers with new, not bare pointers
16 //
17 // ----------------------------------------------------------------------
18 
20 
21 // Possible traces
22 //#include <iostream>
23 //#define ELcountTRACE
24 // #define ELmapDumpTRACE
25 
26 namespace edm {
27 
28  // ----------------------------------------------------------------------
29  // LimitAndTimespan:
30  // ----------------------------------------------------------------------
31 
32  LimitAndTimespan::LimitAndTimespan(int lim, int ts, int ivl) : limit(lim), timespan(ts), interval(ivl) {}
33 
34  // ----------------------------------------------------------------------
35  // CountAndLimit:
36  // ----------------------------------------------------------------------
37 
38  CountAndLimit::CountAndLimit(int lim, int ts, int ivl)
39  : n(0),
40  aggregateN(0),
41  lastTime(time(nullptr)),
42  limit(lim),
43  timespan(ts),
44  interval(ivl),
45  skipped(ivl - 1) // So that the FIRST of the prescaled messages emerges
46  {}
47 
49  time_t now = time(nullptr);
50 
51 #ifdef ELcountTRACE
52  std::cerr << "&&&--- CountAndLimit::add \n";
53  std::cerr << "&&& Time now is " << now << "\n";
54  std::cerr << "&&& Last time is " << lastTime << "\n";
55  std::cerr << "&&& timespan is " << timespan << "\n";
56  std::cerr << "&&& difftime is " << difftime(now, lastTime) << "\n" << std::flush;
57 #endif
58 
59  // Has it been so long that we should restart counting toward the limit?
60  if ((timespan >= 0) && (difftime(now, lastTime) >= timespan)) {
61  n = 0;
62  if (interval > 0) {
63  skipped = interval - 1; // So this message will be reacted to
64  } else {
65  skipped = 0;
66  }
67  }
68 
69  lastTime = now;
70 
71  ++n;
72  ++aggregateN;
73  ++skipped;
74 
75 #ifdef ELcountTRACE
76  std::cerr << "&&& n is " << n << "-- limit is " << limit << "\n";
77  std::cerr << "&&& skipped is " << skipped << "-- interval is " << interval << "\n";
78 #endif
79 
80  if (skipped < interval)
81  return false;
82 
83  if (limit == 0)
84  return false; // Zero limit - never react to this
85  if ((limit < 0) || (n <= limit)) {
86  skipped = 0;
87  return true;
88  }
89 
90  // Now we are over the limit - have we exceeded limit by 2^N * limit?
91  long diff = n - limit;
92  long r = diff / limit;
93  if (r * limit != diff) { // Not a multiple of limit - don't react
94  return false;
95  }
96  if (r == 1) { // Exactly twice limit - react
97  skipped = 0;
98  return true;
99  }
100 
101  while (r > 1) {
102  if ((r & 1) != 0)
103  return false; // Not 2**n times limit - don't react
104  r >>= 1;
105  }
106  // If you never get an odd number till one, r is 2**n so react
107 
108  skipped = 0;
109  return true;
110 
111  } // add()
112 
113  // ----------------------------------------------------------------------
114  // StatsCount:
115  // ----------------------------------------------------------------------
116 
117  StatsCount::StatsCount() : n(0), aggregateN(0), ignoredFlag(false), context1(""), context2(""), contextLast("") {}
118 
119  void StatsCount::add(const ELstring& context, bool reactedTo) {
120  ++n;
121  ++aggregateN;
122 
123  ((1 == n) ? context1 : (2 == n) ? context2 : contextLast) = ELstring(context, 0, 16);
124 
125  if (!reactedTo)
126  ignoredFlag = true;
127 
128  } // add()
129 
130  // ----------------------------------------------------------------------
131 
132 #ifdef ELmapDumpTRACE
133  // ----------------------------------------------------------------------
134  // Global Dump methods (useful for debugging)
135  // ----------------------------------------------------------------------
136 
137 #include <sstream>
138 #include <string.h>
139 
140  boost::shared_array<char> ELmapDump(ELmap_limits m) {
141  std::ostringstream s;
142  s << "**** ELmap_limits Dump **** \n";
143 
144  ELmap_limits::const_iterator i;
145  for (i = m.begin(); i != m.end(); ++i) {
146  LimitAndTimespan lt = (*i).second;
147  s << " " << (*i).first << ": " << lt.limit << ", " << lt.timespan << "\n";
148  }
149  s << "--------------------------------------------\n";
150 
151  boost::shared_array<char> dump(new char[s.str().size() + 1]);
152  strcpy(dump.get(), s.str().c_str());
153 
154  return dump;
155  }
156 #endif
157 
158 } // end of namespace edm */
LimitAndTimespan(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:32
ELstring context2
Definition: ELmap.h:74
CountAndLimit(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:38
void add(const ELstring &context, bool reactedTo)
Definition: ELmap.cc:119
#define nullptr
int aggregateN
Definition: ELmap.h:71
ELstring context1
Definition: ELmap.h:73
time_t lastTime
Definition: ELmap.h:57
ELstring contextLast
Definition: ELmap.h:75
HLT enums.
bool ignoredFlag
Definition: ELmap.h:72
std::map< ELstring, LimitAndTimespan > ELmap_limits
Definition: ELmap.h:84
std::string ELstring
Definition: ELstring.h:21