hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCEReadProcessData.hpp
Go to the documentation of this file.
1 #ifndef DRCE_READ_PROCESS_DATA
2 #define DRCE_READ_PROCESS_DATA
3 
4 #include <sys/types.h>
5 #include <dirent.h>
6 #include <unistd.h>
7 
8 #ifdef __cplusplus
9 #define EXTERN_C_BEGIN extern "C" {
10 #define EXTERN_C_END }
11 #define EXTERN_C extern "C"
12 #else
13 #define EXTERN_C_BEGIN
14 #define EXTERN_C_END
15 #define EXTERN_C
16 #endif
17 
19 
20 #define P_G_SZ 20
21 
22 #define restrict __restrict__
23 
24 #define likely(x) __builtin_expect(!!(x),1)
25 #define unlikely(x) __builtin_expect(!!(x),0)
26 #define expected(x,y) __builtin_expect((x),(y))
27 
28 typedef struct proc_t
29 {
30 // 1st 16 bytes
31  int
32  tid, // (special) task id, the POSIX thread ID (see also: tgid)
33  ppid; // stat,status pid of parent process
34  unsigned
35  pcpu; // stat (special) %CPU usage (is not filled in by readproc!!!)
36  char
37  state, // stat,status single-char code for process state (S=sleeping)
38  pad_1, // n/a padding
39  pad_2, // n/a padding
40  pad_3; // n/a padding
41 // 2nd 16 bytes
42  unsigned long long
43  utime, // stat user-mode CPU time accumulated by process
44  stime, // stat kernel-mode CPU time accumulated by process
45 // and so on...
46  cutime, // stat cumulative utime of process and reaped children
47  cstime, // stat cumulative stime of process and reaped children
48  start_time; // stat start time of process -- seconds since 1-1-70
49 
50  char
51 // Linux 2.1.7x and up have 64 signals. Allow 64, plus '\0' and padding.
52  signal[18], // status mask of pending signals, per-task for readtask() but per-proc for readproc()
53  blocked[18], // status mask of blocked signals
54  sigignore[18],// status mask of ignored signals
55  sigcatch[18], // status mask of caught signals
56  _sigpnd[18]; // status mask of PER TASK pending signals
57 
58  unsigned long
59  start_code, // stat address of beginning of code segment
60  end_code, // stat address of end of code segment
61  start_stack,// stat address of the bottom of stack for the process
62  kstk_esp, // stat kernel stack pointer
63  kstk_eip, // stat kernel instruction pointer
64  wchan; // stat (special) address of kernel wait channel proc is sleeping in
65  long
66  priority, // stat kernel scheduling priority
67  nice, // stat standard unix nice level of process
68  rss, // stat resident set size from /proc/#/stat (pages)
69  alarm, // stat ?
70  // the next 7 members come from /proc/#/statm
71  size, // statm total # of pages of memory
72  resident, // statm number of resident set (non-swapped) pages (4k)
73  share, // statm number of pages of shared (mmap'd) memory
74  trs, // statm text resident set size
75  lrs, // statm shared-lib resident set size
76  drs, // statm data resident set size
77  dt; // statm dirty pages
78  unsigned long
79  vm_size, // status same as vsize in kb
80  vm_lock, // status locked pages in kb
81  vm_rss, // status same as rss in kb
82  vm_data, // status data size
83  vm_stack, // status stack size
84  vm_exe, // status executable size
85  vm_lib, // status library size (all pages, not just used ones)
86  rtprio, // stat real-time priority
87  sched, // stat scheduling class
88  vsize, // stat number of pages of virtual memory ...
89  rss_rlim, // stat resident set size limit?
90  flags, // stat kernel flags for the process
91  min_flt, // stat number of minor page faults since process start
92  maj_flt, // stat number of major page faults since process start
93  cmin_flt, // stat cumulative min_flt of process and child processes
94  cmaj_flt; // stat cumulative maj_flt of process and child processes
95  char
96  **environ, // (special) environment string vector (/proc/#/environ)
97  **cmdline; // (special) command line string vector (/proc/#/cmdline)
98  char
99 // Be compatible: Digital allows 16 and NT allows 14 ???
100  euser[P_G_SZ], // stat(),status effective user name
101  ruser[P_G_SZ], // status real user name
102  suser[P_G_SZ], // status saved user name
103  fuser[P_G_SZ], // status filesystem user name
104  rgroup[P_G_SZ], // status real group name
105  egroup[P_G_SZ], // status effective group name
106  sgroup[P_G_SZ], // status saved group name
107  fgroup[P_G_SZ], // status filesystem group name
108  cmd[16]; // stat,status basename of executable file in call to exec(2)
109  struct proc_t
110  *ring, // n/a thread group ring
111  *next; // n/a various library uses
112  int
113  pgrp, // stat process group id
114  session, // stat session id
115  nlwp, // stat,status number of threads, or 0 if no clue
116  tgid, // (special) task group ID, the POSIX PID (see also: tid)
117  tty, // stat full device number of controlling terminal
118  euid, egid, // stat(),status effective
119  ruid, rgid, // status real
120  suid, sgid, // status saved
121  fuid, fgid, // status fs (used for file access only)
122  tpgid, // stat terminal process group id
123  exit_signal,// stat might not be SIGCHLD
124  processor; // stat current (or most recent?) CPU
125 } proc_t;
126 
127 // PROCTAB: data structure holding the persistent information readproc needs
128 // from openproc(). The setup is intentionally similar to the dirent interface
129 // and other system table interfaces (utmp+wtmp come to mind).
130 
131 #include <sys/types.h>
132 #include <dirent.h>
133 #include <unistd.h>
134 
135 #define PROCPATHLEN 64
136 
137 typedef struct PROCTAB
138 {
139  DIR* procfs;
140  DIR* taskdir; // for threads
141  pid_t taskdir_user; // for threads
142  int did_fake; // used when taskdir is missing
143  int(*finder)(struct PROCTAB *restrict const, proc_t *restrict const);
144  proc_t*(*reader)(struct PROCTAB *restrict const, proc_t *restrict const);
145  int(*taskfinder)(struct PROCTAB *restrict const, const proc_t *restrict const, proc_t *restrict const, char *restrict const);
146  proc_t*(*taskreader)(struct PROCTAB *restrict const, const proc_t *restrict const, proc_t *restrict const, char *restrict const);
147  pid_t* pids; // pids of the procs
148  uid_t* uids; // uids of procs
149  int nuid; // cannot really sentinel-terminate unsigned short[]
150  int i; // generic
151  unsigned flags;
152  unsigned u; // generic
153  void * vp; // generic
154  char path[PROCPATHLEN]; // must hold /proc/2000222000/task/2000222000/cmdline
155  unsigned pathlen; // length of string in the above (w/o '\0')
156 } PROCTAB;
157 
158 // initialize a PROCTAB structure holding needed call-to-call persistent data
159 //PROCTAB* openproc(int flags, ... /* pid_t*|uid_t*|dev_t*|char* [, int n] */ );
160 
161 // clean-up open files, etc from the openproc()
162 //void closeproc(PROCTAB* PT);
163 
164 // retrieve the next process matching the criteria set by the openproc()
165 //proc_t* readproc(PROCTAB *restrict const PT, proc_t *restrict p);
166 
167 // deallocate space allocated by readproc
168 //void freeproc(proc_t* p);
169 
170 //fill out a proc_t for a single task
171 //proc_t * getProcStats(pid_t pid, proc_t *p);
172 
173 #define PROC_FILLMEM 0x0001 // read statm
174 #define PROC_FILLSTATUS 0x0020 // read status -- currently unconditional
175 #define PROC_FILLSTAT 0x0040 // read stat -- currently unconditional
176 #define PROC_FILLARG 0x0100 // alloc and fill in `cmdline'
177 // Obsolete, consider only processes with one of the passed:
178 #define PROC_PID 0x1000 // process id numbers ( 0 terminated)
179 #define PROC_UID 0x4000 // user id numbers ( length needed )
180 
182 //-----------------------------------------------------------------------------
183 #include <Poco/ThreadPool.h>
184 #include <Poco/Task.h>
185 #include <Poco/TaskManager.h>
186 #include <Poco/Mutex.h>
187 #include <Poco/ScopedLock.h>
188 #include <Poco/Logger.h>
189 #include <vector>
190 
191 #include "DRCEProcessData.hpp"
192 #include "DRCEMessageConst.hpp"
193 
194 namespace HCE
195 {
196 namespace drce
197 {
198 //-----------------------------------------------------------------------------
200 {
201 public:
202  static DRCEReadProcessData& getInstance(void);
203  static void shutdown(void);
204 
205  static std::vector<ProcessData> getProcessDataList(void);
206  static ProcessData getProcessData(pid_t pid);
207  static std::vector<pid_t> getAllProcessIds(pid_t pid);
208 
209  static void getProcessAndThreads(unsigned int& processCount, unsigned int& threadsCount);
210 protected:
212  static Poco::Mutex mutex;
213 private:
214  DRCEReadProcessData(void) { Poco::Logger::root().log(Poco::Message(drce_const::moduleName, "DRCEReadProcessData was created", Poco::Message::PRIO_INFORMATION)); };
215  ~DRCEReadProcessData(void) { Poco::Logger::root().log(Poco::Message(drce_const::moduleName, "DRCEReadProcessData was deleted", Poco::Message::PRIO_INFORMATION)); }
216  DRCEReadProcessData(const DRCEReadProcessData&);
217  DRCEReadProcessData& operator=(const DRCEReadProcessData&);
218  static void getChildrenList(pid_t pid, std::vector<pid_t>& children);
219 };
220 //-----------------------------------------------------------------------------
221 //-----------------------------------------------------------------------------
222 } // end namespace drce
223 } // end namespace HCE
224 
225 #endif // DRCE_READ_PROCESS_DATA