RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/router/profiler.h
00001 /* profiler.h                                                      -*- C++ -*-
00002    Jeremy Barnes, 30 May 2012
00003    Profiler class for router.
00004 */
00005 
00006 #ifndef __router__profiler_h__
00007 #define __router__profiler_h__
00008 
00009 #include "jml/arch/timers.h"
00010 #include "jml/arch/exception.h"
00011 #include "jml/arch/atomic_ops.h"
00012 
00013 namespace RTBKIT {
00014 
00015 
00016 /*****************************************************************************/
00017 /* ROUTER PROFILER                                                           */
00018 /*****************************************************************************/
00019 
00020 inline double getProfilingTime()
00021 {
00022     return ML::ticks() * ML::seconds_per_tick;
00023 }
00024 
00026 inline int64_t microsecondsBetween(double after, double before)
00027 {
00028     if (after < before) {
00029         // This can happen on a CPU that doesn't have a monotonic
00030         // synchronous TSC over all CPUs
00031         using namespace std;
00032         cerr << "warning: microseconds between problem: after "
00033              << after << " before " << before << " distance "
00034              << after - before << endl;
00035         after = before;
00036 #if 0
00037         throw ML::Exception("microseconds between problem: "
00038                             "after %f vs before %f",
00039                             after, before);
00040 #endif
00041     }
00042     return (after - before) * 1000000;
00043 }
00044 
00045 
00046 struct RouterProfiler {
00047 
00048     RouterProfiler(uint64_t & counter)
00049         : counter(counter)
00050     {
00051         startTime = getProfilingTime();
00052     }
00053 
00054     ~RouterProfiler()
00055     {
00056         ML::atomic_add(counter,
00057                        microsecondsBetween(getProfilingTime(), startTime));
00058     }
00059 
00060     uint64_t & counter;
00061     double startTime;
00062 };
00063 
00064 } // namespace RTBKIT
00065 
00066 
00067 #endif /* __router__profiler_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator