CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/HiggsAnalysis/CombinedLimit/src/ProfilingTools.cc

Go to the documentation of this file.
00001 #include "../interface/ProfilingTools.h"
00002 
00003 // we try to stick to pure C within the signal handlers
00004 #include <stdio.h>
00005 #include <dlfcn.h>
00006 #include <signal.h>
00007 #include <sys/types.h>
00008 #include <unistd.h>
00009 
00010 #include <boost/unordered_map.hpp>
00011 
00012 void (*igProfRequestDump_)(const char *);
00013 int igProfDumpNumber_ = 0;
00014 
00015 void igProfDumpNow(int) {
00016     char buff[50];
00017     igProfDumpNumber_++;
00018     sprintf(buff,"dump.%d.%d.out.gz", getpid(), igProfDumpNumber_);
00019     igProfRequestDump_(buff);
00020     fprintf(stderr, "Dumped to %s\n", buff); fflush(stderr);
00021 }
00022 
00023 bool setupIgProfDumpHook() {
00024     if (void *sym = dlsym(0, "igprof_dump_now")) {
00025         igProfRequestDump_ = __extension__ (void(*)(const char *)) sym;
00026         fprintf(stderr, "IgProf dump hook enabled. Do kill -USR2 %d to request a dump.\n", int(getpid())); fflush(stderr);
00027     } else {
00028         fprintf(stderr, "Not being profiled by IgProf. The command you should use to profile this is:\n"); 
00029         fprintf(stderr, "  igprof -mp -z -t combine combine datacard [options]\n\n");
00030         fflush(stderr);
00031         return false;
00032     }
00033     signal(SIGUSR2,igProfDumpNow);
00034     return true;
00035 }
00036 
00037 
00038 boost::unordered_map<const char *, PerfCounter> perfCounters_;
00039 
00040 PerfCounter & PerfCounter::get(const char *name) 
00041 {
00042     return perfCounters_[name];
00043 }
00044 
00045 void PerfCounter::printAll() 
00046 {
00047     for (boost::unordered_map<const char *, PerfCounter>::const_iterator it = perfCounters_.begin(), ed = perfCounters_.end(); it != ed; ++it) {
00048         fprintf(stderr, "%-40s: %g\n", it->first, it->second.get());
00049     }
00050 }
00051 
00052 // we define them by string value, but we lookup them by const char *
00053 namespace runtimedef {
00054     boost::unordered_map<const char *, std::pair<int,int> > defines_;
00055     boost::unordered_map<std::string,  int>                 definesByString_;
00056     int get(const char *name) {
00057         std::pair<int,int> & ret = defines_[name];
00058         if (ret.second == 0) {
00059             ret.first = definesByString_[name];
00060             ret.second = 1;
00061         }
00062         return ret.first;
00063     }
00064     int get(const std::string & name) {
00065         return definesByString_[name];
00066     }
00067     void set(const std::string & name, int value) {
00068         definesByString_[name] = value;
00069     }
00070 }
00071