libutil++  1.9.3
 All Classes Functions Variables
Process.h
1 /*
2 ** libutil++
3 ** $Id: Process.h 1659 2016-03-15 22:20:58Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 #ifndef __libutilxx__sella__util__Process_H__
10 #define __libutilxx__sella__util__Process_H__
11 
12 #include "../../common.h"
13 #include "ProcessException.h"
14 
15 #include <sys/vtimes.h>
16 #include <sys/sysinfo.h>
17 #include <pthread.h>
18 
19 #include <string>
20 #include <vector>
21 
22 #ifndef __IOPRIO_H__ /* Missing kernel header */
23 #define __IOPRIO_H__
24 
25 enum {
26  IOPRIO_CLASS_NONE = 0,
27  IOPRIO_CLASS_RT,
28  IOPRIO_CLASS_BE,
29  IOPRIO_CLASS_IDLE
30 };
31 
32 #define IOPRIO_BE_NR (8)
33 
34 enum {
35  IOPRIO_WHO_PROCESS = 1,
36  IOPRIO_WHO_PGRP,
37  IOPRIO_WHO_USER
38 };
39 
40 #define IOPRIO_CLASS_SHIFT (13)
41 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
42 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
43 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
44 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
45 
46 #endif /* __IOPRIO_H__ */
47 
48 namespace sella {
49  namespace util {
50  class Process;
51  }
52 }
53 
55  public:
56  Process() throw (ProcessException);
57  virtual ~Process();
58 
59  void update(void) throw (ProcessException); /* Update process statistics. */
60 
61  uint64_t getUpdateIntervalUsec(void) const throw ();
62  uint64_t getUpdateIntervalSec(void) const throw ();
63 
64  uint64_t getStartTime(void) const throw ();
65  uint64_t getUptime(void) const throw ();
66 
67  int getCPU(bool scale = false) const throw ();
68  int getUserCPU(bool scale = false) const throw ();
69  int getSystemCPU(bool scale = false) const throw ();
70 
71  int getMemory(void) const throw (); /* Percentage */
72 
73  char getState(void) const throw ();
74  uint64_t getFDs(void) const throw ();
75  uint64_t getFDSize(void) const throw ();
76  uint64_t getVMPeak(void) const throw ();
77  uint64_t getVMSize(void) const throw ();
78  uint64_t getLocked(void) const throw ();
79  uint64_t getRSSHighWaterMark(void) const throw ();
80  uint64_t getRSS(void) const throw ();
81  uint64_t getData(void) const throw ();
82  uint64_t getStack(void) const throw ();
83  uint64_t getEXE(void) const throw ();
84  uint64_t getLib(void) const throw ();
85  uint64_t getPTE(void) const throw ();
86  uint64_t getSwap(void) const throw ();
87  uint64_t getThreads(void) const throw ();
88  uint64_t getVoluntaryContextSwitch(void) const throw ();
89  uint64_t getNonVoluntaryContextSwitch(void) const throw ();
90 
91  uint64_t getBytesRead(void) const throw (); /* Read bytes including from cache. */
92  uint64_t getBytesWrite(void) const throw (); /* Written bytes including from cache */
93  uint64_t getReadSyscall(void) const throw (); /* Read calls to kernel. Indirectly related to IOPs. */
94  uint64_t getWriteSyscall(void) const throw (); /* Write calls to kernel. Indirectly related to IOPs. */
95  uint64_t getBytesReadStorage(void) const throw (); /* Read bytes from storage. */
96  uint64_t getBytesWriteStorage(void) const throw (); /* Written bytes to storage. */
97  int64_t getCancelledBytesWrite(void) const throw (); /* Written and removed/changed before commit. May go negative. */
98  uint64_t getBlockIOWait(void) const throw (); /* Milliseconds blocked IO wait. */
99 
100  int getClockTicks(void) const throw ();
101  int getProcessorCount(void) const throw ();
102 
103  uint64_t getSystemUptime(void) const throw ();
104  uint64_t getSystemLoadAverage(int min = 1) const throw (); /* Supports 1, 5 and 15 */
105  uint64_t getSystemMemory(void) const throw ();
106  uint64_t getSystemFree(void) const throw ();
107  uint64_t getSystemShared(void) const throw ();
108  uint64_t getSystemBuffer(void) const throw ();
109  uint64_t getSystemSwap(void) const throw ();
110  uint64_t getSystemProcesses(void) const throw ();
111  uint64_t getSystemTotalHigh(void) const throw ();
112  uint64_t getSystemFreeHigh(void) const throw ();
113  uint64_t getSystemMemUnitSize(void) const throw ();
114 
115  static std::string getBaseName(void) throw ();
116  static std::string getDirName(void) throw ();
117  static std::string getPath(void) throw ();
118  static void setName(const std::string &name) throw (ProcessException); /* Limited to 16 characters */
119  static std::string getName(void) throw (ProcessException);
120  static void setThreadName(pthread_t tid, const std::string &name) throw (ProcessException); /* When not root, must use pthread_self(). */
121  static std::string getThreadName(pthread_t tid) throw (ProcessException);
122  static void setTitle(const std::string &title) throw (ProcessException);
123  static pid_t getPID(void) throw ();
124  static pid_t getParentPID(void) throw ();
125  static bool isRunning(pid_t pid) throw ();
126  static uint64_t getStartTime(pid_t pid) throw (ProcessException);
127  static uint64_t getBlockIOWait(pid_t pid) throw (ProcessException); /* Milliseconds blocked IO wait. */
128 
129  static std::string runCommand(const std::string &command) throw (ProcessException);
130  static std::string stackTrace(const std::string &executableFilename) throw (); /* Must compile app with -rdynamic */
131 
132  static void setNice(int niceness, int which = IOPRIO_WHO_PGRP, int who = 0) throw (ProcessException); /* See man setpriority */
133  static int getNice(int which = IOPRIO_WHO_PGRP, int who = 0) throw (ProcessException);
134 
135  /* Only work when CFQ scheduler is used. Desktops often use CFQ and servers often use Deadline. */
136  static void setIONice(int ioclass = IOPRIO_CLASS_BE, int level = 7, int which = IOPRIO_WHO_PGRP, int who = 0) throw (ProcessException); /* See man ioprio_set */
137  static int getIONice(int &ioclass, int &level, int which = IOPRIO_WHO_PROCESS, int who = 0) throw (ProcessException);
138 
139  protected:
140  std::vector<uint64_t> stats, iostats;
141  int cpu, userCPU, sysCPU, ncpu, jiffies, fds;
142  struct timespec prev, last;
143  clock_t lastCPU, lastUserCPU, lastSysCPU;
144  struct sysinfo info;
145 
146  void updateCPU(void) throw ();
147  void updateFDs(void) throw (ProcessException);
148  void updateStats(void) throw (ProcessException);
149  void updateIOStats(void) throw (ProcessException);
150 };
151 
152 #endif
153 
154 /*
155 ** vim: noet ts=3 sw=3
156 */