Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-08-23 02:46:13

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 #include <ctime>
0022 
0023 // Possible traces
0024 //#include <iostream>
0025 //#define ELcountTRACE
0026 // #define ELmapDumpTRACE
0027 
0028 namespace edm {
0029 
0030   // ----------------------------------------------------------------------
0031   // LimitAndTimespan:
0032   // ----------------------------------------------------------------------
0033 
0034   LimitAndTimespan::LimitAndTimespan(int lim, int ts, int ivl) : limit(lim), timespan(ts), interval(ivl) {}
0035 
0036   // ----------------------------------------------------------------------
0037   // CountAndLimit:
0038   // ----------------------------------------------------------------------
0039 
0040   CountAndLimit::CountAndLimit(int lim, int ts, int ivl)
0041       : n(0),
0042         aggregateN(0),
0043         lastTime(time(nullptr)),
0044         limit(lim),
0045         timespan(ts),
0046         interval(ivl),
0047         skipped(ivl - 1)  // So that the FIRST of the prescaled messages emerges
0048   {}
0049 
0050   bool CountAndLimit::add() {
0051     time_t now = time(nullptr);
0052 
0053 #ifdef ELcountTRACE
0054     std::cerr << "&&&--- CountAndLimit::add \n";
0055     std::cerr << "&&&    Time now  is " << now << "\n";
0056     std::cerr << "&&&    Last time is " << lastTime << "\n";
0057     std::cerr << "&&&    timespan  is " << timespan << "\n";
0058     std::cerr << "&&&    difftime  is " << difftime(now, lastTime) << "\n" << std::flush;
0059 #endif
0060 
0061     // Has it been so long that we should restart counting toward the limit?
0062     if ((timespan >= 0) && (difftime(now, lastTime) >= timespan)) {
0063       n = 0;
0064       if (interval > 0) {
0065         skipped = interval - 1;  // So this message will be reacted to
0066       } else {
0067         skipped = 0;
0068       }
0069     }
0070 
0071     lastTime = now;
0072 
0073     ++n;
0074     ++aggregateN;
0075     ++skipped;
0076 
0077 #ifdef ELcountTRACE
0078     std::cerr << "&&&       n is " << n << "-- limit is    " << limit << "\n";
0079     std::cerr << "&&& skipped is " << skipped << "-- interval is " << interval << "\n";
0080 #endif
0081 
0082     if (skipped < interval)
0083       return false;
0084 
0085     if (limit == 0)
0086       return false;  // Zero limit - never react to this
0087     if ((limit < 0) || (n <= limit)) {
0088       skipped = 0;
0089       return true;
0090     }
0091 
0092     // Now we are over the limit - have we exceeded limit by 2^N * limit?
0093     long diff = n - limit;
0094     long r = diff / limit;
0095     if (r * limit != diff) {  // Not a multiple of limit - don't react
0096       return false;
0097     }
0098     if (r == 1) {  // Exactly twice limit - react
0099       skipped = 0;
0100       return true;
0101     }
0102 
0103     while (r > 1) {
0104       if ((r & 1) != 0)
0105         return false;  // Not 2**n times limit - don't react
0106       r >>= 1;
0107     }
0108     // If you never get an odd number till one, r is 2**n so react
0109 
0110     skipped = 0;
0111     return true;
0112 
0113   }  // add()
0114 
0115   // ----------------------------------------------------------------------
0116   // StatsCount:
0117   // ----------------------------------------------------------------------
0118 
0119   StatsCount::StatsCount() : n(0), aggregateN(0), ignoredFlag(false), context1(""), context2(""), contextLast("") {}
0120 
0121   void StatsCount::add(std::string_view context, bool reactedTo) {
0122     ++n;
0123     ++aggregateN;
0124 
0125     ((1 == n) ? context1 : (2 == n) ? context2 : contextLast) = std::string(context, 0, 16);
0126 
0127     if (!reactedTo)
0128       ignoredFlag = true;
0129 
0130   }  // add()
0131 
0132 }  // end of namespace edm  */