RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/service/process_stats.h
00001 /* proc_stats.h                                                   -*- C++ -*-
00002    RĂ©mi Attab, 19 January 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Gathers process and system stats from the proc files.
00006 */
00007 
00008 
00009 #ifndef __logger__process_stats_h__
00010 #define __logger__process_stats_h__
00011 
00012 #include "soa/jsoncpp/json.h"
00013 
00014 #include <boost/function.hpp>
00015 #include <string>
00016 
00017 
00018 namespace Datacratic {
00019 
00020 /*
00021 Reccords statistics related to a process and the system.
00022 The stats should preferably be formatted and dumped via the logToCallback()
00023 method.
00024 */
00025 struct ProcessStats {
00026     ProcessStats (bool doLoadAverage = false) :
00027         doLoadAverage(doLoadAverage)
00028     {
00029         sample();
00030     }
00031 
00032     void sample () {
00033         sampleLoadAverage();
00034         sampleStatm();
00035         sampleRUsage();
00036     }
00037 
00038 
00039     typedef std::function<void(std::string, double)> LogCallback;
00040     static void logToCallback (
00041             LogCallback cb,
00042             const ProcessStats& lastStats,
00043             const ProcessStats& curStats,
00044             const std::string& prefix = "");
00045     static Json::Value toJson (
00046             const ProcessStats& lastStats,
00047             const ProcessStats& curStats,
00048             const std::string& prefix = "");
00049 
00050     uint64_t majorFaults;
00051     uint64_t minorFaults;
00052     uint64_t totalFaults() const { return majorFaults + minorFaults; }
00053 
00054     double userTime;
00055     double systemTime;
00056     double totalTime() const { return userTime + systemTime; } 
00057 
00058     uint64_t virtualMem;
00059     uint64_t residentMem;
00060     uint64_t sharedMem;
00061 
00062     bool doLoadAverage;
00063     float loadAverage1;
00064     float loadAverage5;
00065     float loadAverage15;
00066 
00067     uint64_t voluntaryContextSwitches;
00068     uint64_t involuntaryContextSwitches;
00069     uint64_t totalContextSwitches() const {
00070         return voluntaryContextSwitches + involuntaryContextSwitches;
00071     }
00072 
00073 private:
00074     void sampleLoadAverage ();
00075     void sampleStat ();
00076     void sampleStatm ();
00077     void sampleRUsage ();
00078 };
00079 
00080 
00081 
00082 } // namespace Datacratic
00083 
00084 #endif // __logger__process_stats_h__
00085 
00086 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator