CMS 3D CMS Logo

ELmap.cc

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------
00002 //
00003 // ELmap.cc
00004 //
00005 // Change History:
00006 //   99-06-10   mf      correction in sense of comparison between timespan
00007 //                      and diff (now, lastTime)
00008 //              mf      ELcountTRACE made available
00009 //   99-06-11   mf      Corrected logic for suppressing output when n > limit
00010 //                      but not but a factor of 2**K
00011 //   06-05-16   mf      Added code to establish interval and to use skipped
00012 //                      and interval when determinine in add() whehter to react
00013 //
00014 // ----------------------------------------------------------------------
00015 
00016 
00017 #include "FWCore/MessageLogger/interface/ELmap.h"
00018 
00019 // Possible traces
00020 //#include <iostream>
00021 //#define ELcountTRACE
00022 // #define ELmapDumpTRACE
00023 
00024 
00025 namespace edm
00026 {
00027 
00028 
00029 // ----------------------------------------------------------------------
00030 // LimitAndTimespan:
00031 // ----------------------------------------------------------------------
00032 
00033 LimitAndTimespan::LimitAndTimespan( int lim, int ts, int ivl )
00034 : limit   ( lim )
00035 , timespan( ts )
00036 , interval( ivl )
00037 { }
00038 
00039 
00040 // ----------------------------------------------------------------------
00041 // CountAndLimit:
00042 // ----------------------------------------------------------------------
00043 
00044 CountAndLimit::CountAndLimit( int lim, int ts, int ivl )
00045 : n         ( 0 )
00046 , aggregateN( 0 )
00047 , lastTime  ( time(0) )
00048 , limit     ( lim )
00049 , timespan  ( ts  )
00050 , interval  ( ivl )
00051 , skipped   ( ivl-1 )  // So that the FIRST of the prescaled messages emerges
00052 { }
00053 
00054 
00055 bool  CountAndLimit::add()  {
00056 
00057   time_t  now = time(0);
00058 
00059 #ifdef ELcountTRACE
00060   std::cerr << "&&&--- CountAndLimit::add \n";
00061   std::cerr << "&&&    Time now  is " << now << "\n";
00062   std::cerr << "&&&    Last time is " << lastTime << "\n";
00063   std::cerr << "&&&    timespan  is " << timespan << "\n";
00064   std::cerr << "&&&    difftime  is " << difftime( now, lastTime ) << "\n"
00065                                 << std::flush;
00066 #endif
00067 
00068   // Has it been so long that we should restart counting toward the limit?
00069   if ( (timespan >= 0)
00070             &&
00071         (difftime(now, lastTime) >= timespan) )  {
00072      n = 0;
00073      if ( interval > 0 ) {
00074        skipped = interval - 1; // So this message will be reacted to
00075      } else {
00076        skipped = 0;
00077      }
00078   }
00079 
00080   lastTime = now;
00081 
00082   ++n;  
00083   ++aggregateN;
00084   ++skipped;  
00085 
00086 #ifdef ELcountTRACE
00087   std::cerr << "&&&       n is " << n << "-- limit is    " << limit    << "\n";
00088   std::cerr << "&&& skipped is " << skipped 
00089                                       << "-- interval is " << interval << "\n";
00090 #endif
00091   
00092   if (skipped < interval) return false;
00093 
00094   if ( limit == 0 ) return false;        // Zero limit - never react to this
00095   if ( (limit < 0)  || ( n <= limit )) {
00096     skipped = 0;
00097     return true;
00098   }
00099   
00100   // Now we are over the limit - have we exceeded limit by 2^N * limit?
00101   long  diff = n - limit;
00102   long  r = diff/limit;
00103   if ( r*limit != diff ) { // Not a multiple of limit - don't react
00104     return false;
00105   }  
00106   if ( r == 1 )   {     // Exactly twice limit - react
00107     skipped = 0;
00108     return true;
00109   }
00110 
00111   while ( r > 1 )  {
00112     if ( (r & 1) != 0 )  return false;  // Not 2**n times limit - don't react
00113     r >>= 1;
00114   }
00115   // If you never get an odd number till one, r is 2**n so react
00116   
00117   skipped = 0;
00118   return true;
00119 
00120 }  // add()
00121 
00122 
00123 // ----------------------------------------------------------------------
00124 // StatsCount:
00125 // ----------------------------------------------------------------------
00126 
00127 StatsCount::StatsCount()
00128 : n          ( 0 )
00129 , aggregateN ( 0 )
00130 , ignoredFlag( false )
00131 , context1   ( "" )
00132 , context2   ( "" )
00133 , contextLast( "" )
00134 { }
00135 
00136 
00137 void  StatsCount::add( const ELstring & context, bool reactedTo )  {
00138 
00139   ++n;  ++aggregateN;
00140 
00141   ( (1 == n) ? context1
00142   : (2 == n) ? context2
00143   :            contextLast
00144   )                        = ELstring( context, 0, 16 );
00145 
00146   if ( ! reactedTo )
00147     ignoredFlag = true;
00148 
00149 }  // add()
00150 
00151 
00152 // ----------------------------------------------------------------------
00153 
00154 #ifdef ELmapDumpTRACE
00155 // ----------------------------------------------------------------------
00156 // Global Dump methods (useful for debugging)
00157 // ----------------------------------------------------------------------
00158 
00159 #include <sstream>
00160 #include <string.h>
00161 
00162 char *  ELmapDump ( ELmap_limits m )  {
00163 
00164   std::ostringstream s;
00165   s << "**** ELmap_limits Dump **** \n";
00166 
00167   ELmap_limits::const_iterator i;
00168   for ( i = m.begin();  i != m.end();  ++i )  {
00169     LimitAndTimespan lt = (*i).second;
00170     s << "     " << (*i).first << ":  " << lt.limit << ", " <<
00171                 lt.timespan << "\n";
00172   }
00173   s << "--------------------------------------------\n";
00174 
00175   char *  dump = new char[s.str().size()+1];
00176   strcpy( dump, s.str().c_str() );
00177 
00178   return dump;
00179 
00180 }
00181 #endif
00182 
00183 } // end of namespace edm  */

Generated on Tue Jun 9 17:36:17 2009 for CMSSW by  doxygen 1.5.4