RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/rusage.h
00001 /* rusage.h                                                        -*- C++ -*-
00002    Jeremy Barnes, 26 November 2012
00003    Copyright (c) 2012 Datacratic Inc.  All rights reserved.
00004 
00005    Small structure to track resource usage.
00006 */
00007 
00008 #pragma once
00009 
00010 #include "soa/jsoncpp/json.h"
00011 #include <sys/time.h>
00012 #include <sys/resource.h>
00013 
00014 namespace Datacratic {
00015 
00017 struct RUsage {
00018 
00019     RUsage()
00020     {
00021         clear();
00022     }
00023 
00024     RUsage(const struct rusage & res)
00025     {
00026         userTime = res.ru_utime.tv_sec + (res.ru_utime.tv_usec / 1000000.0);
00027         systemTime = res.ru_stime.tv_sec + (res.ru_stime.tv_usec / 1000000.0);
00028         minorFaults = res.ru_minflt;
00029         majorFaults = res.ru_majflt;
00030         voluntaryContextSwitches = res.ru_nvcsw;
00031         involuntaryContextSwitches = res.ru_nivcsw;
00032     }
00033 
00034     void clear()
00035     {
00036         userTime = systemTime = minorFaults = majorFaults
00037             = voluntaryContextSwitches = involuntaryContextSwitches
00038             = 0;
00039     }
00040 
00041     double userTime;
00042     double systemTime;
00043     long minorFaults;
00044     long majorFaults;
00045     long voluntaryContextSwitches;
00046     long involuntaryContextSwitches;
00047     
00048     void getForCurrentProcess()
00049     {
00050         struct rusage rusage;
00051         getrusage(RUSAGE_SELF, &rusage);  // TODO: check return code...
00052         *this = RUsage(rusage);
00053     }
00054 
00055     RUsage operator - (const RUsage & other) const
00056     {
00057         RUsage result;
00058         result.userTime = userTime - other.userTime;
00059         result.systemTime = systemTime - other.systemTime;
00060         result.minorFaults = minorFaults - other.minorFaults;
00061         result.majorFaults = majorFaults - other.majorFaults;
00062         result.voluntaryContextSwitches
00063             = voluntaryContextSwitches - other.voluntaryContextSwitches;
00064         result.involuntaryContextSwitches
00065             = involuntaryContextSwitches - other.involuntaryContextSwitches;
00066 
00067         return result;
00068     }
00069 
00070     Json::Value toJson() const
00071     {
00072         Json::Value result;
00073 
00074         result["userTime"] = userTime;
00075         result["systemTime"] = systemTime;
00076         result["totalTime"] = userTime + systemTime;
00077         result["minorFaults"] = (int)minorFaults;
00078         result["majorFaults"] = (int)majorFaults;
00079         result["totalFaults"] = int(minorFaults + majorFaults);
00080         result["voluntaryContextSwitches"] = (int)voluntaryContextSwitches;
00081         result["involuntaryContextSwitches"] = (int)involuntaryContextSwitches;
00082         result["contextSwitches"]
00083             = int(voluntaryContextSwitches + involuntaryContextSwitches);
00084 
00085         return result;
00086     }
00087 };
00088 
00089 } // namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator