milk  1.01
Modular Image processing Library toolKit
processtools.h
Go to the documentation of this file.
1 
11 /* =============================================================================================== */
12 /* =============================================================================================== */
13 /* DEFINES, MACROS */
14 /* =============================================================================================== */
15 /* =============================================================================================== */
16 
17 #ifndef _PROCESSTOOLS_H
18 #define _PROCESSTOOLS_H
19 
20 #include <semaphore.h>
21 #include <signal.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <sched.h>
28 
29 #define PROCESSINFOLISTSIZE 10000
30 
31 
32 #define MAXNBSUBPROCESS 50
33 #define MAXNBCPU 100
34 // timing info for real-time loop processes
35 #define PROCESSINFO_NBtimer 100
36 
37 
38 
39 #ifndef __STDC_LIB_EXT1__
40 typedef int errno_t;
41 #endif
42 
43 
44 
45 
46 // --------------------- MANAGING PROCESSES -------------------------------
47 
48 #define STRINGMAXLEN_PROCESSINFO_NAME 80
49 #define STRINGMAXLEN_PROCESSINFO_SRCFUNC 200
50 #define STRINGMAXLEN_PROCESSINFO_SRCFILE 200
51 #define STRINGMAXLEN_PROCESSINFO_TMUXNAME 100
52 #define STRINGMAXLEN_PROCESSINFO_STATUSMSG 200
53 #define STRINGMAXLEN_PROCESSINFO_LOGFILENAME 250
54 #define STRINGMAXLEN_PROCESSINFO_DESCRIPTION 200
55 
56 
57 // input stream triggering mode
58 
59 // trigger immediately
60 #define PROCESSINFO_TRIGGERMODE_IMMEDIATE 0
61 
62 // trigger when cnt0 increments
63 #define PROCESSINFO_TRIGGERMODE_CNT0 1
64 
65 // trigger when cnt1 increments
66 #define PROCESSINFO_TRIGGERMODE_CNT1 2
67 
68 // trigger when semaphore is posted
69 #define PROCESSINFO_TRIGGERMODE_SEMAPHORE 3
70 
71 // trigger after a time delay
72 #define PROCESSINFO_TRIGGERMODE_DELAY 4
73 
74 
75 // trigger is currently waiting for input
76 #define PROCESSINFO_TRIGGERSTATUS_WAITING 1
77 
78 #define PROCESSINFO_TRIGGERSTATUS_RECEIVED 2
79 #define PROCESSINFO_TRIGGERSTATUS_TIMEDOUT 3
80 
81 
92 typedef struct {
93  char name[STRINGMAXLEN_PROCESSINFO_NAME];
94 
95  char source_FUNCTION[STRINGMAXLEN_PROCESSINFO_SRCFUNC];
96  char source_FILE[STRINGMAXLEN_PROCESSINFO_SRCFILE];
98 
99  pid_t PID;
100 
101  struct timespec createtime; // time at which pinfo was created
102 
103  long loopcnt; // counter, useful for loop processes to monitor activity
104  long loopcntMax; // exit loop if loopcnt = loopcntMax. Set to -1 for infinite loop
105  int CTRLval; // control value to be externally written.
106  // 0: run (default)
107  // 1: pause
108  // 2: increment single step (will go back to 1)
109  // 3: exit loop
110 
111  char tmuxname[STRINGMAXLEN_PROCESSINFO_TMUXNAME]; // name of tmux session in which process is running, or
112  // "NULL"
113  int loopstat;
114  // 0: INIT Initialization before loop
115  // 1: ACTIVE in loop
116  // 2: PAUSED loop paused (do not iterate)
117  // 3: STOPPED terminated (clean exit following user request to stop process)
118  // 4: ERROR process could not run, typically used when loop can't start, e.g. missing input
119  // 5: SPINNING do not compute (loop iterates, but does not compute. output stream(s) will still be posted/incremented)
120  // 6: CRASHED pid has gone away without proper exit sequence. Will attempt to generate exit log file (using atexit) to identify crash location
121 
122  char statusmsg[STRINGMAXLEN_PROCESSINFO_STATUSMSG]; // status message
123  int statuscode; // status code
124 
125  FILE *logFile;
126  char logfilename[STRINGMAXLEN_PROCESSINFO_LOGFILENAME];
127 
128 
129 
130  // OPTIONAL INPUT STREAM SETUP
131  // Used to specify which stream will trigger the computation and track trigger state
132  // Enables use of function processinfo_waitoninputstream()
133  // Enables streamproctrace entry
134  // Must be inialized by processinfo_waitoninputstream_init()
135  int triggermode; // see TRIGGERMODE codes
136  imageID triggerstreamID; // -1 if not initialized
137  ino_t triggerstreaminode;
138  char triggerstreamname[STRINGMAXLEN_IMAGE_NAME];
139  int triggersem; // semaphore index
140  uint64_t triggerstreamcnt; // previous value of trigger counter, updates on trigger
141  struct timespec triggerdelay; // for PROCESSINFO_TRIGGERMODE_DELAY
142  struct timespec triggertimeout; // how long to wait until trigger ?
143  uint64_t trigggertimeoutcnt;
144  int triggermissedframe; // have we missed any frame, if yes how many ?
145  // 0 : no missed frame, loop has been waiting for semaphore to be posted
146  // 1 : no missed frame, but semaphore was already posted and at 1 when triggering
147  // 2+ : frame(s) missed
148  uint64_t triggermissedframe_cumul; // cumulative missed frames
149  int triggerstatus; // see TRIGGERSTATUS codes
150 
151 
152 
153  int RT_priority; // -1 if unused. 0-99 for higher priority
154  cpu_set_t CPUmask;
155 
156 
157  // OPTIONAL TIMING MEASUREMENT
158  // Used to measure how long loop process takes to complete task
159  // Provides means to stop/pause loop process if timing constraints exceeded
160  //
161  int MeasureTiming; // 1 if timing is measured, 0 otherwise
162 
163  // the last PROCESSINFO_NBtimer times are stored in a circular buffer, from
164  // which timing stats are derived
165  int timerindex; // last written index in circular buffer
166  int timingbuffercnt; // increments every cycle of the circular buffer
167  struct timespec texecstart[PROCESSINFO_NBtimer]; // task starts
168  struct timespec texecend[PROCESSINFO_NBtimer]; // task ends
169 
170  long dtmedian_iter_ns; // median time offset between iterations [nanosec]
171  long dtmedian_exec_ns; // median compute/busy time [nanosec]
172 
173  // If enabled=1, pause process if dtiter larger than limit
174  int dtiter_limit_enable;
175  long dtiter_limit_value;
176  long dtiter_limit_cnt;
177 
178  // If enabled=1, pause process if dtexec larger than limit
179  int dtexec_limit_enable;
180  long dtexec_limit_value;
181  long dtexec_limit_cnt;
182 
183 
184  char description[STRINGMAXLEN_PROCESSINFO_DESCRIPTION];
185 
186 } PROCESSINFO;
187 
188 
189 
190 
191 //
192 // This structure maintains a list of active processes
193 // It is used to quickly build (without scanning directory) an array of
194 // PROCESSINFO
195 //
196 typedef struct {
197  pid_t PIDarray[PROCESSINFOLISTSIZE];
198  int active[PROCESSINFOLISTSIZE];
199  char pnamearray[PROCESSINFOLISTSIZE][STRINGMAXLEN_PROCESSINFO_NAME]; // short name
200 
202 
203 
204 
205 typedef struct
206 {
207  int active;
208  pid_t PID;
209  char name[40];
210  long updatecnt;
211 
212  long loopcnt;
213  int loopstat;
214 
215  int createtime_hr;
216  int createtime_min;
217  int createtime_sec;
218  long createtime_ns;
219 
220  char cpuset[16];
221  char cpusallowed[20];
222  int cpuOKarray[MAXNBCPU];
223  int threads;
224 
225  int rt_priority;
226  float memload;
227 
228  char statusmsg[200];
229  char tmuxname[100];
230 
231  int NBsubprocesses;
232  int subprocPIDarray[MAXNBSUBPROCESS];
233 
234  double sampletimearray[MAXNBSUBPROCESS]; // time at which sampling was performed [sec]
235  double sampletimearray_prev[MAXNBSUBPROCESS];
236 
237  long ctxtsw_voluntary[MAXNBSUBPROCESS];
238  long ctxtsw_nonvoluntary[MAXNBSUBPROCESS];
239  long ctxtsw_voluntary_prev[MAXNBSUBPROCESS];
240  long ctxtsw_nonvoluntary_prev[MAXNBSUBPROCESS];
241 
242  long long cpuloadcntarray[MAXNBSUBPROCESS];
243  long long cpuloadcntarray_prev[MAXNBSUBPROCESS];
244  float subprocCPUloadarray[MAXNBSUBPROCESS];
245  float subprocCPUloadarray_timeaveraged[MAXNBSUBPROCESS];
246 
247  long VmRSSarray[MAXNBSUBPROCESS];
248 
249  int processorarray[MAXNBSUBPROCESS];
250 
251 
253 
254 
255 
256 
257 
258 typedef struct
259 {
260  int loop; // 1 : loop 0 : exit
261  long loopcnt;
262 
263  int twaitus; // sleep time between scans
264  double dtscan; // measured time interval between scans [s]
265  pid_t scanPID;
266  int scandebugline; // for debugging
267 
268 
269  // ensure list of process and mmap operation blocks display
270  int SCANBLOCK_requested; // scan thread toggles to 1 to requests blocking
271  int SCANBLOCK_OK; // display thread toggles to 1 to let scan know it can proceed
272 
273  PROCESSINFOLIST *pinfolist; // copy of pointer static PROCESSINFOLIST *pinfolist
274 
275  long NBpinfodisp;
276  PROCESSINFODISP *pinfodisp;
277 
278  int DisplayMode;
279  int DisplayDetailedMode;
280 
281  //
282  // these arrays are indexed together
283  // the index is different from the displayed order
284  // new process takes first available free index
285  //
286  PROCESSINFO *pinfoarray[PROCESSINFOLISTSIZE];
287  int pinfommapped[PROCESSINFOLISTSIZE]; // 1 if mmapped, 0 otherwise
288  pid_t PIDarray[PROCESSINFOLISTSIZE]; // used to track changes
289  int updatearray[PROCESSINFOLISTSIZE]; // 0: don't load, 1: (re)load
290  int fdarray[PROCESSINFOLISTSIZE]; // file descriptors
291  long loopcntarray[PROCESSINFOLISTSIZE];
292  long loopcntoffsetarray[PROCESSINFOLISTSIZE];
293  int selectedarray[PROCESSINFOLISTSIZE];
294 
295  int sorted_pindex_time[PROCESSINFOLISTSIZE];
296 
297 
298  int NBcpus;
299  int NBcpusocket;
300 
301  float CPUload[MAXNBCPU];
302  long long CPUcnt0[MAXNBCPU];
303  long long CPUcnt1[MAXNBCPU];
304  long long CPUcnt2[MAXNBCPU];
305  long long CPUcnt3[MAXNBCPU];
306  long long CPUcnt4[MAXNBCPU];
307  long long CPUcnt5[MAXNBCPU];
308  long long CPUcnt6[MAXNBCPU];
309  long long CPUcnt7[MAXNBCPU];
310  long long CPUcnt8[MAXNBCPU];
311 
312  int CPUids[MAXNBCPU]; // individual cpus (same cores)
313  int CPUphys[MAXNBCPU]; // Physical CPU socket
314 
315  int CPUpcnt[MAXNBCPU];
316 
317  int NBpindexActive;
318  int pindexActive[PROCESSINFOLISTSIZE];
319  int psysinfostatus[PROCESSINFOLISTSIZE];
320 
321 } PROCINFOPROC;
322 
323 
324 
325 
326 
327 // --------------------- -------------------------------
328 
329 typedef struct {
330  char name[200];
331  char description[200];
333 
334 #ifdef __cplusplus
335 extern "C" {
336 #endif
337 
338 
339 
340 PROCESSINFO * processinfo_setup(
341  char *pinfoname,
342  char descriptionstring[200],
343  char msgstring[200],
344  const char *functionname,
345  const char *filename,
346  int linenumber
347 );
348 
349 errno_t processinfo_error(
350  PROCESSINFO *processinfo,
351  char *errmsgstring
352 );
353 
354 errno_t processinfo_loopstart(
355  PROCESSINFO *processinfo
356 );
357 
358 int processinfo_loopstep(
359  PROCESSINFO *processinfo
360 );
361 
362 int processinfo_compute_status(
363  PROCESSINFO *processinfo
364 );
365 
366 
367 
368 
369 PROCESSINFO *processinfo_shm_create(const char *pname, int CTRLval);
370 PROCESSINFO *processinfo_shm_link(const char *pname, int *fd);
371 int processinfo_shm_close(PROCESSINFO *pinfo, int fd);
372 int processinfo_cleanExit(PROCESSINFO *processinfo);
373 int processinfo_SIGexit(PROCESSINFO *processinfo, int SignalNumber);
374 int processinfo_WriteMessage(PROCESSINFO *processinfo, const char *msgstring);
375 int processinfo_exec_start(PROCESSINFO *processinfo);
376 int processinfo_exec_end(PROCESSINFO *processinfo);
377 
378 
379 int processinfo_CatchSignals();
380 int processinfo_ProcessSignals(PROCESSINFO *processinfo);
381 
383  PROCESSINFO *processinfo,
384  imageID trigID,
385  int triggermode,
386  int semindexrequested
387 );
388 
390  PROCESSINFO *processinfo
391 );
392 
394  PROCESSINFO *processinfo,
395  imageID outstreamID
396 );
397 
398 
399 
400 errno_t processinfo_CTRLscreen();
401 
402 #ifdef __cplusplus
403 }
404 #endif
405 
406 #endif // _PROCESSTOOLS_H
PROCESSINFOLIST
Definition: processtools.h:196
PROCESSINFO
Definition: processtools.h:92
PROCESSINFODISP
Definition: processtools.h:205
STRINGLISTENTRY
Definition: processtools.h:329
processinfo_waitoninputstream
errno_t processinfo_waitoninputstream(PROCESSINFO *processinfo)
Wait on a stream.
Definition: processtools.c:1162
processinfo_CTRLscreen
errno_t processinfo_CTRLscreen()
Definition: processtools.c:2807
processinfo_waitoninputstream_init
errno_t processinfo_waitoninputstream_init(PROCESSINFO *processinfo, imageID trigID, int triggermode, int semindexrequested)
Set up input wait stream.
Definition: processtools.c:1086
PROCESSINFO::PID
pid_t PID
source code line
Definition: processtools.h:99
processinfo_shm_create
PROCESSINFO * processinfo_shm_create(const char *pname, int CTRLval)
Definition: processtools.c:546
PROCINFOPROC
Definition: processtools.h:258
PROCESSINFO::source_LINE
int source_LINE
source code file
Definition: processtools.h:97
processinfo_update_output_stream
errno_t processinfo_update_output_stream(PROCESSINFO *processinfo, imageID outstreamID)
Update ouput stream at completion of processinfo-enabled loop iteration.
Definition: processtools.c:1323