This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocesstools.h
334 lines (238 loc) · 9.68 KB
/
processtools.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @file processtools.h
* @brief Command line interface
*
* Command line interface (CLI) definitions and function prototypes
*
* @bug No known bugs.
*
*/
/* =============================================================================================== */
/* =============================================================================================== */
/* DEFINES, MACROS */
/* =============================================================================================== */
/* =============================================================================================== */
#ifndef _PROCESSTOOLS_H
#define _PROCESSTOOLS_H
#include <semaphore.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#define PROCESSINFOLISTSIZE 10000
#define MAXNBSUBPROCESS 50
#define MAXNBCPU 100
// timing info for real-time loop processes
#define PROCESSINFO_NBtimer 100
#ifndef __STDC_LIB_EXT1__
typedef int errno_t;
#endif
// --------------------- MANAGING PROCESSES -------------------------------
#define STRINGMAXLEN_PROCESSINFO_NAME 80
#define STRINGMAXLEN_PROCESSINFO_SRCFUNC 200
#define STRINGMAXLEN_PROCESSINFO_SRCFILE 200
#define STRINGMAXLEN_PROCESSINFO_TMUXNAME 100
#define STRINGMAXLEN_PROCESSINFO_STATUSMSG 200
#define STRINGMAXLEN_PROCESSINFO_LOGFILENAME 250
#define STRINGMAXLEN_PROCESSINFO_DESCRIPTION 200
/**
*
* This structure hold process information and hooks required for basic
* monitoring and control Unlike the larger DATA structure above, it is meant to
* be stored in shared memory for fast access by other processes
*
*
* File name: /tmp/proc.PID.shm
*
*/
typedef struct {
char name[STRINGMAXLEN_PROCESSINFO_NAME]; /// process name (human-readable)
char source_FUNCTION[STRINGMAXLEN_PROCESSINFO_SRCFUNC]; /// source code function
char source_FILE[STRINGMAXLEN_PROCESSINFO_SRCFILE]; /// source code file
int source_LINE; /// source code line
pid_t PID; /// process ID; file name is /tmp/proc.PID.shm
struct timespec createtime; // time at which pinfo was created
long loopcnt; // counter, useful for loop processes to monitor activity
long loopcntMax; // exit loop if loopcnt = loopcntMax. Set to -1 for infinite loop
int CTRLval; // control value to be externally written.
// 0: run (default)
// 1: pause
// 2: increment single step (will go back to 1)
// 3: exit loop
char tmuxname[STRINGMAXLEN_PROCESSINFO_TMUXNAME]; // name of tmux session in which process is running, or
// "NULL"
int loopstat;
// 0: INIT Initialization before loop
// 1: ACTIVE in loop
// 2: PAUSED loop paused (do not iterate)
// 3: STOPPED terminated (clean exit following user request to stop process)
// 4: ERROR process could not run, typically used when loop can't start, e.g. missing input
// 5: SPINNING do not compute (loop iterates, but does not compute. output stream(s) will still be posted/incremented)
// 6: CRASHED pid has gone away without proper exit sequence. Will attempt to generate exit log file (using atexit) to identify crash location
char statusmsg[STRINGMAXLEN_PROCESSINFO_STATUSMSG]; // status message
int statuscode; // status code
FILE *logFile;
char logfilename[STRINGMAXLEN_PROCESSINFO_LOGFILENAME];
// OPTIONAL TIMING MEASUREMENT
// Used to measure how long loop process takes to complete task
// Provides means to stop/pause loop process if timing constraints exceeded
//
int MeasureTiming; // 1 if timing is measured, 0 otherwise
int RT_priority; // -1 if unused. 0-99 for higher priority
// the last PROCESSINFO_NBtimer times are stored in a circular buffer, from
// which timing stats are derived
int timerindex; // last written index in circular buffer
int timingbuffercnt; // increments every cycle of the circular buffer
struct timespec texecstart[PROCESSINFO_NBtimer]; // task starts
struct timespec texecend[PROCESSINFO_NBtimer]; // task ends
long dtmedian_iter_ns; // median time offset between iterations [nanosec]
long dtmedian_exec_ns; // median compute/busy time [nanosec]
// If enabled=1, pause process if dtiter larger than limit
int dtiter_limit_enable;
long dtiter_limit_value;
long dtiter_limit_cnt;
// If enabled=1, pause process if dtexec larger than limit
int dtexec_limit_enable;
long dtexec_limit_value;
long dtexec_limit_cnt;
char description[STRINGMAXLEN_PROCESSINFO_DESCRIPTION];
} PROCESSINFO;
//
// This structure maintains a list of active processes
// It is used to quickly build (without scanning directory) an array of
// PROCESSINFO
//
typedef struct {
pid_t PIDarray[PROCESSINFOLISTSIZE];
int active[PROCESSINFOLISTSIZE];
char pnamearray[PROCESSINFOLISTSIZE][STRINGMAXLEN_PROCESSINFO_NAME]; // short name
} PROCESSINFOLIST;
typedef struct
{
int active;
pid_t PID;
char name[40];
long updatecnt;
long loopcnt;
int loopstat;
int createtime_hr;
int createtime_min;
int createtime_sec;
long createtime_ns;
char cpuset[16]; /**< cpuset name */
char cpusallowed[20];
int cpuOKarray[MAXNBCPU];
int threads;
int rt_priority;
float memload;
char statusmsg[200];
char tmuxname[100];
int NBsubprocesses;
int subprocPIDarray[MAXNBSUBPROCESS];
double sampletimearray[MAXNBSUBPROCESS]; // time at which sampling was performed [sec]
double sampletimearray_prev[MAXNBSUBPROCESS];
long ctxtsw_voluntary[MAXNBSUBPROCESS];
long ctxtsw_nonvoluntary[MAXNBSUBPROCESS];
long ctxtsw_voluntary_prev[MAXNBSUBPROCESS];
long ctxtsw_nonvoluntary_prev[MAXNBSUBPROCESS];
long long cpuloadcntarray[MAXNBSUBPROCESS];
long long cpuloadcntarray_prev[MAXNBSUBPROCESS];
float subprocCPUloadarray[MAXNBSUBPROCESS];
float subprocCPUloadarray_timeaveraged[MAXNBSUBPROCESS];
long VmRSSarray[MAXNBSUBPROCESS];
int processorarray[MAXNBSUBPROCESS];
} PROCESSINFODISP;
typedef struct
{
int loop; // 1 : loop 0 : exit
long loopcnt;
int twaitus; // sleep time between scans
double dtscan; // measured time interval between scans [s]
pid_t scanPID;
int scandebugline; // for debugging
// ensure list of process and mmap operation blocks display
int SCANBLOCK_requested; // scan thread toggles to 1 to requests blocking
int SCANBLOCK_OK; // display thread toggles to 1 to let scan know it can proceed
PROCESSINFOLIST *pinfolist; // copy of pointer static PROCESSINFOLIST *pinfolist
long NBpinfodisp;
PROCESSINFODISP *pinfodisp;
int DisplayMode;
//
// these arrays are indexed together
// the index is different from the displayed order
// new process takes first available free index
//
PROCESSINFO *pinfoarray[PROCESSINFOLISTSIZE];
int pinfommapped[PROCESSINFOLISTSIZE]; // 1 if mmapped, 0 otherwise
pid_t PIDarray[PROCESSINFOLISTSIZE]; // used to track changes
int updatearray[PROCESSINFOLISTSIZE]; // 0: don't load, 1: (re)load
int fdarray[PROCESSINFOLISTSIZE]; // file descriptors
long loopcntarray[PROCESSINFOLISTSIZE];
long loopcntoffsetarray[PROCESSINFOLISTSIZE];
int selectedarray[PROCESSINFOLISTSIZE];
int sorted_pindex_time[PROCESSINFOLISTSIZE];
int NBcpus;
int NBcpusocket;
float CPUload[MAXNBCPU];
long long CPUcnt0[MAXNBCPU];
long long CPUcnt1[MAXNBCPU];
long long CPUcnt2[MAXNBCPU];
long long CPUcnt3[MAXNBCPU];
long long CPUcnt4[MAXNBCPU];
long long CPUcnt5[MAXNBCPU];
long long CPUcnt6[MAXNBCPU];
long long CPUcnt7[MAXNBCPU];
long long CPUcnt8[MAXNBCPU];
int CPUids[MAXNBCPU]; // individual cpus (same cores)
int CPUphys[MAXNBCPU]; // Physical CPU socket
int CPUpcnt[MAXNBCPU];
int NBpindexActive;
int pindexActive[PROCESSINFOLISTSIZE];
int psysinfostatus[PROCESSINFOLISTSIZE];
} PROCINFOPROC;
// --------------------- -------------------------------
typedef struct {
char name[200];
char description[200];
} STRINGLISTENTRY;
#ifdef __cplusplus
extern "C" {
#endif
PROCESSINFO * processinfo_setup(
char *pinfoname,
char descriptionstring[200],
char msgstring[200],
const char *functionname,
const char *filename,
int linenumber
);
errno_t processinfo_error(
PROCESSINFO *processinfo,
char *errmsgstring
);
errno_t processinfo_loopstart(
PROCESSINFO *processinfo
);
int processinfo_loopstep(
PROCESSINFO *processinfo
);
int processinfo_compute_status(
PROCESSINFO *processinfo
);
PROCESSINFO *processinfo_shm_create(const char *pname, int CTRLval);
PROCESSINFO *processinfo_shm_link(const char *pname, int *fd);
int processinfo_shm_close(PROCESSINFO *pinfo, int fd);
int processinfo_cleanExit(PROCESSINFO *processinfo);
int processinfo_SIGexit(PROCESSINFO *processinfo, int SignalNumber);
int processinfo_WriteMessage(PROCESSINFO *processinfo, const char *msgstring);
int processinfo_exec_start(PROCESSINFO *processinfo);
int processinfo_exec_end(PROCESSINFO *processinfo);
int processinfo_CatchSignals();
int processinfo_ProcessSignals(PROCESSINFO *processinfo);
errno_t processinfo_CTRLscreen();
#ifdef __cplusplus
}
#endif
#endif // _PROCESSTOOLS_H