CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
19 
21 
22 // Possible traces
23 //#include <iostream>
24 //#define ELcountTRACE
25 // #define ELmapDumpTRACE
26 
27 
28 namespace edm
29 {
30 
31 
32 // ----------------------------------------------------------------------
33 // LimitAndTimespan:
34 // ----------------------------------------------------------------------
35 
36 LimitAndTimespan::LimitAndTimespan( int lim, int ts, int ivl )
37 : limit ( lim )
38 , timespan( ts )
39 , interval( ivl )
40 { }
41 
42 
43 // ----------------------------------------------------------------------
44 // CountAndLimit:
45 // ----------------------------------------------------------------------
46 
47 CountAndLimit::CountAndLimit( int lim, int ts, int ivl )
48 : n ( 0 )
49 , aggregateN( 0 )
50 , lastTime ( time(0) )
51 , limit ( lim )
52 , timespan ( ts )
53 , interval ( ivl )
54 , skipped ( ivl-1 ) // So that the FIRST of the prescaled messages emerges
55 { }
56 
57 
59 
60  time_t now = time(0);
61 
62 #ifdef ELcountTRACE
63  std::cerr << "&&&--- CountAndLimit::add \n";
64  std::cerr << "&&& Time now is " << now << "\n";
65  std::cerr << "&&& Last time is " << lastTime << "\n";
66  std::cerr << "&&& timespan is " << timespan << "\n";
67  std::cerr << "&&& difftime is " << difftime( now, lastTime ) << "\n"
68  << std::flush;
69 #endif
70 
71  // Has it been so long that we should restart counting toward the limit?
72  if ( (timespan >= 0)
73  &&
74  (difftime(now, lastTime) >= timespan) ) {
75  n = 0;
76  if ( interval > 0 ) {
77  skipped = interval - 1; // So this message will be reacted to
78  } else {
79  skipped = 0;
80  }
81  }
82 
83  lastTime = now;
84 
85  ++n;
86  ++aggregateN;
87  ++skipped;
88 
89 #ifdef ELcountTRACE
90  std::cerr << "&&& n is " << n << "-- limit is " << limit << "\n";
91  std::cerr << "&&& skipped is " << skipped
92  << "-- interval is " << interval << "\n";
93 #endif
94 
95  if (skipped < interval) return false;
96 
97  if ( limit == 0 ) return false; // Zero limit - never react to this
98  if ( (limit < 0) || ( n <= limit )) {
99  skipped = 0;
100  return true;
101  }
102 
103  // Now we are over the limit - have we exceeded limit by 2^N * limit?
104  long diff = n - limit;
105  long r = diff/limit;
106  if ( r*limit != diff ) { // Not a multiple of limit - don't react
107  return false;
108  }
109  if ( r == 1 ) { // Exactly twice limit - react
110  skipped = 0;
111  return true;
112  }
113 
114  while ( r > 1 ) {
115  if ( (r & 1) != 0 ) return false; // Not 2**n times limit - don't react
116  r >>= 1;
117  }
118  // If you never get an odd number till one, r is 2**n so react
119 
120  skipped = 0;
121  return true;
122 
123 } // add()
124 
125 
126 // ----------------------------------------------------------------------
127 // StatsCount:
128 // ----------------------------------------------------------------------
129 
131 : n ( 0 )
132 , aggregateN ( 0 )
133 , ignoredFlag( false )
134 , context1 ( "" )
135 , context2 ( "" )
136 , contextLast( "" )
137 { }
138 
139 
140 void StatsCount::add( const ELstring & context, bool reactedTo ) {
141 
142  ++n; ++aggregateN;
143 
144  ( (1 == n) ? context1
145  : (2 == n) ? context2
146  : contextLast
147  ) = ELstring( context, 0, 16 );
148 
149  if ( ! reactedTo )
150  ignoredFlag = true;
151 
152 } // add()
153 
154 
155 // ----------------------------------------------------------------------
156 
157 #ifdef ELmapDumpTRACE
158 // ----------------------------------------------------------------------
159 // Global Dump methods (useful for debugging)
160 // ----------------------------------------------------------------------
161 
162 #include <sstream>
163 #include <string.h>
164 
165 boost::shared_array<char> ELmapDump ( ELmap_limits m ) {
166 
167  std::ostringstream s;
168  s << "**** ELmap_limits Dump **** \n";
169 
170  ELmap_limits::const_iterator i;
171  for ( i = m.begin(); i != m.end(); ++i ) {
172  LimitAndTimespan lt = (*i).second;
173  s << " " << (*i).first << ": " << lt.limit << ", " <<
174  lt.timespan << "\n";
175  }
176  s << "--------------------------------------------\n";
177 
178  boost::shared_array<char> dump(new char[s.str().size()+1]);
179  strcpy( dump.get(), s.str().c_str() );
180 
181  return dump;
182 
183 }
184 #endif
185 
186 } // end of namespace edm */
int i
Definition: DBlmapReader.cc:9
LimitAndTimespan(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:36
ELstring context2
Definition: ELmap.h:87
tuple interval
Definition: MergeJob_cfg.py:20
CountAndLimit(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:47
void add(const ELstring &context, bool reactedTo)
Definition: ELmap.cc:140
int aggregateN
Definition: ELmap.h:84
ELstring context1
Definition: ELmap.h:86
std::map< ELstring, LimitAndTimespan > ELmap_limits
Definition: ELmap.h:99
time_t lastTime
Definition: ELmap.h:67
ELstring contextLast
Definition: ELmap.h:88
< trclass="colgroup">< tdclass="colgroup"colspan=5 > DT local reconstruction</td ></tr >< tr >< td >< ahref="classDTRecHit1DPair.html"> DTRecHit1DPair</a ></td >< td >< ahref="DataFormats_DTRecHit.html"> edm::RangeMap & lt
bool ignoredFlag
Definition: ELmap.h:85
volatile std::atomic< bool > shutdown_flag false
std::string ELstring
Definition: ELstring.h:26