Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:40

0001 // ----------------------------------------------------------------------
0002 //
0003 // ELmap.cc
0004 //
0005 // Change History:
0006 //   99-06-10   mf      correction in sense of comparison between timespan
0007 //                      and diff (now, lastTime)
0008 //              mf      ELcountTRACE made available
0009 //   99-06-11   mf      Corrected logic for suppressing output when n > limit
0010 //                      but not but a factor of 2**K
0011 //   06-05-16   mf      Added code to establish interval and to use skipped
0012 //          and interval when determinine in add() whehter to react
0013 //   06-05-19   wmtan   Bug fix.  skipped = 0, not skipped == 0.
0014 //          and interval when determinine in add() whehter to react
0015 //   09-04-15   wmtan   Use smart pointers with new, not bare pointers
0016 //
0017 // ----------------------------------------------------------------------
0018 
0019 #include "FWCore/MessageLogger/interface/ELmap.h"
0020 
0021 // Possible traces
0022 //#include <iostream>
0023 //#define ELcountTRACE
0024 // #define ELmapDumpTRACE
0025 
0026 namespace edm {
0027 
0028   // ----------------------------------------------------------------------
0029   // LimitAndTimespan:
0030   // ----------------------------------------------------------------------
0031 
0032   LimitAndTimespan::LimitAndTimespan(int lim, int ts, int ivl) : limit(lim), timespan(ts), interval(ivl) {}
0033 
0034   // ----------------------------------------------------------------------
0035   // CountAndLimit:
0036   // ----------------------------------------------------------------------
0037 
0038   CountAndLimit::CountAndLimit(int lim, int ts, int ivl)
0039       : n(0),
0040         aggregateN(0),
0041         lastTime(time(nullptr)),
0042         limit(lim),
0043         timespan(ts),
0044         interval(ivl),
0045         skipped(ivl - 1)  // So that the FIRST of the prescaled messages emerges
0046   {}
0047 
0048   bool CountAndLimit::add() {
0049     time_t now = time(nullptr);
0050 
0051 #ifdef ELcountTRACE
0052     std::cerr << "&&&--- CountAndLimit::add \n";
0053     std::cerr << "&&&    Time now  is " << now << "\n";
0054     std::cerr << "&&&    Last time is " << lastTime << "\n";
0055     std::cerr << "&&&    timespan  is " << timespan << "\n";
0056     std::cerr << "&&&    difftime  is " << difftime(now, lastTime) << "\n" << std::flush;
0057 #endif
0058 
0059     // Has it been so long that we should restart counting toward the limit?
0060     if ((timespan >= 0) && (difftime(now, lastTime) >= timespan)) {
0061       n = 0;
0062       if (interval > 0) {
0063         skipped = interval - 1;  // So this message will be reacted to
0064       } else {
0065         skipped = 0;
0066       }
0067     }
0068 
0069     lastTime = now;
0070 
0071     ++n;
0072     ++aggregateN;
0073     ++skipped;
0074 
0075 #ifdef ELcountTRACE
0076     std::cerr << "&&&       n is " << n << "-- limit is    " << limit << "\n";
0077     std::cerr << "&&& skipped is " << skipped << "-- interval is " << interval << "\n";
0078 #endif
0079 
0080     if (skipped < interval)
0081       return false;
0082 
0083     if (limit == 0)
0084       return false;  // Zero limit - never react to this
0085     if ((limit < 0) || (n <= limit)) {
0086       skipped = 0;
0087       return true;
0088     }
0089 
0090     // Now we are over the limit - have we exceeded limit by 2^N * limit?
0091     long diff = n - limit;
0092     long r = diff / limit;
0093     if (r * limit != diff) {  // Not a multiple of limit - don't react
0094       return false;
0095     }
0096     if (r == 1) {  // Exactly twice limit - react
0097       skipped = 0;
0098       return true;
0099     }
0100 
0101     while (r > 1) {
0102       if ((r & 1) != 0)
0103         return false;  // Not 2**n times limit - don't react
0104       r >>= 1;
0105     }
0106     // If you never get an odd number till one, r is 2**n so react
0107 
0108     skipped = 0;
0109     return true;
0110 
0111   }  // add()
0112 
0113   // ----------------------------------------------------------------------
0114   // StatsCount:
0115   // ----------------------------------------------------------------------
0116 
0117   StatsCount::StatsCount() : n(0), aggregateN(0), ignoredFlag(false), context1(""), context2(""), contextLast("") {}
0118 
0119   void StatsCount::add(std::string_view context, bool reactedTo) {
0120     ++n;
0121     ++aggregateN;
0122 
0123     ((1 == n) ? context1 : (2 == n) ? context2 : contextLast) = std::string(context, 0, 16);
0124 
0125     if (!reactedTo)
0126       ignoredFlag = true;
0127 
0128   }  // add()
0129 
0130 }  // end of namespace edm  */