-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmntr.c
1225 lines (1172 loc) · 31.8 KB
/
mntr.c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#include <proc/readproc.h>
#endif
#include <cairo/cairo.h>
#include <cairo/cairo-svg.h>
#include "thpool.h"
#include "kvec.h"
#define ARR "\e[2m\xE2\x97\x82\e[0m"
#define INF "\e[1;34m\xE2\x84\xb9\e[0;0m"
#define VERSION "0.2.6"
extern char *__progname;
typedef kvec_t(pid_t) kv_t;
#define GB(x) ((size_t) (x) << 30)
#define basename(str) (strrchr(str, '/') ? strrchr(str, '/') + 1 : str)
#define PP fprintf(stderr, "%s\t%d\t<%s>\n", __FILE__, __LINE__, __func__);
#define PPT do \
{ \
char buf[80]; \
time_t now = time(0); \
strftime(buf, sizeof(buf), "\e[34m%D %X\e[0m", localtime(&now)); \
fprintf(stderr, "%s %s %s\t%d\t<%s>\n", INF, buf, __FILE__, __LINE__, __func__); \
} while (0);
#define CHUNK 0xFFFF
// plot dimensions
#define MARGIN 55
#define DIM_X 605
#define DIM_Y 165
#define WIDTH (DIM_X + MARGIN)
#define HEIGHT (DIM_Y + MARGIN)
typedef struct
{
int utime_ticks;
int cutime_ticks;
int stime_ticks;
int cstime_ticks;
int vsize; // virtual memory size in bytes
} pstat_t;
typedef struct
{
pid_t pid;
bool use_shm;
long shm;
double rss, shr, cpu;
} usg_t;
typedef struct
{
long unsigned rss, shr;
} mem_t;
typedef struct
{
unsigned long ts;
double rss, shr, cpu;
char cmd[PATH_MAX];
} mn_t;
void ttoa(time_t t);
time_t atot(const char *a);
void stoa(const int sec, char **a);
bool use_shm(const pid_t pid);
bool ends_with(const char *str, const char *sfx);
int pgrep(const char *proc);
int chk_pid(pid_t pid);
int ncpid(const pid_t ppid);
void get_cpid(const pid_t ppid, pid_t *pid);
void pids_of_ppid(const pid_t ppid, kv_t *kv);
void pid_to_name(const pid_t pid, char cmd[PATH_MAX]);
unsigned nprocs();
void calc_usg(void *_usg);
void calc_usgd(const pid_t ppid, mn_t **mns, int *m, int *n, const double shm, FILE *fp);
long size_of(const char *fn);
char *get_now(void);
void get_mem(const pid_t pid, mem_t *mem);
void get_cpu(const pstat_t *cur_usage, const pstat_t *last_usage, int *ucpu_usage, int *scpu_usage);
int get_usg(const pid_t pid, pstat_t* result);
int ndigit(const char *str);
void ldlg(const char *fn, mn_t **mns, int *m, int *n);
void draw_rrect(cairo_t *cr);
void draw_box(cairo_t *cr, double x, double y, double width, double height);
void draw_arrow(cairo_t *cr, double start_x, double start_y, double end_x, double end_y);
void draw_xlab(cairo_t *cr, const char *xlab);
void draw_ylab(cairo_t *cr, const char *ylab);
void draw_y2lab(cairo_t *cr, const char *ylab);
void draw_yticks(cairo_t *cr, const double ymax);
void draw_y2ticks(cairo_t *cr, const double ymax);
void draw_cpu(cairo_t *cr, mn_t **mns, const int n);
void draw_rss(cairo_t *cr, mn_t **mns, const int n);
void draw_shr(cairo_t *cr, mn_t **mns, const int n);
void do_drawing(cairo_t *cr, mn_t **mns, const int n, const char *st);
void usage();
int main(int argc, char *argv[])
{
char *st = get_now();
int i, m = CHUNK, n = 0;
mn_t **mns = calloc(m, sizeof(mn_t *));
pid_t pid = 0, self = getpid();
char *pidir = 0, str[PATH_MAX];
setenv("FONTCONFIG_PATH", "/etc/fonts", 1);
if (argc == 1)
{
usage();
free(mns);
exit(1);
}
else if (argc == 2)
{
if (!strcmp(basename(argv[0]), basename(argv[1])))
return 0;
if (!strncmp(argv[1], "-h", 2) || !strncmp(argv[1], "--h", 3))
{
usage();
free(mns);
return 0;
}
if (!strncmp(argv[1], "-v", 2) || !strncmp(argv[1], "--v", 3))
{
puts(VERSION);
free(mns);
return 0;
}
if (strlen(argv[1]) == ndigit(argv[1])) // process ID
pid = atoi(argv[1]);
else
{
if (access(argv[1], X_OK))
{
fprintf(stderr, "Permission denied for [%s]\n", argv[1]);
exit(1);
}
else
pid = pgrep(argv[1]);
}
if (pid)
{
char *log;
asprintf(&log, "%d.log", pid);
FILE *fp = fopen(log, "w");
if (!fp)
{
perror("Error opening log file");
exit(1);
}
long shm = size_of("/dev/shm");
fputs("#TIMESTAMP\tRSS\tSHR\tCPU\tCOMMAND\n", fp);
while (1)
{
if ((kill(pid, 0) == -1 && errno == ESRCH))
break;
calc_usgd(pid, mns, &m, &n, shm, fp);
sleep(2);
}
fclose(fp);
free(log);
}
}
else
{
char **args = malloc(sizeof(char *) * argc);
for (i = 0; i < argc - 1; ++i)
args[i] = argv[i + 1];
args[argc - 1] = NULL;
pid_t fpid = fork();
if (fpid == 0)
{
if (execvp(argv[1], args) < 0)
{
perror("Error running command");
exit(-1);
}
free(args);
}
else if (fpid > 1)
{
int status;
pid = getpid();
get_cpid(pid, &pid);
if (pid)
{
char *log;
asprintf(&log, "%d.log", pid);
FILE *fp = fopen(log, "w");
if (!fp)
{
perror("Error opening log file");
exit(1);
}
long shm = size_of("/dev/shm");
fputs("#TIMESTAMP\tRSS\tSHR\tCPU\tCOMMAND\n", fp);
while (1)
{
if ((kill(pid, 0) == -1 && errno == ESRCH))
break;
waitpid(pid, &status, WNOHANG|WUNTRACED);
if (WIFEXITED(status) == 0 && WEXITSTATUS(status) == 0)
break;
else
{
calc_usgd(pid, mns, &m, &n, shm, fp);
sleep(2);
}
}
fclose(fp);
free(log);
}
}
else
{
printf("Error: Fork process failed");
exit(-1);
}
}
if (n)
{
if (n < m)
mns = realloc(mns, n * sizeof(mn_t *));
char *svg;
asprintf(&svg, "%d.svg", pid);
cairo_surface_t *sf = cairo_svg_surface_create(svg, WIDTH, HEIGHT);
cairo_t *cr = cairo_create(sf);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST);
// draw lines
do_drawing(cr, mns, n, st);
// clean canvas
cairo_surface_destroy(sf);
cairo_destroy(cr);
for (i = 0; i < n; ++i)
free(mns[i]);
free(svg);
}
free(mns);
free(st);
return 0;
}
char *get_now(void)
{
char buf[80];
time_t now = time(0);
strftime(buf,sizeof(buf),"%D %X" ,localtime(&now));
return strdup(buf);
}
/*
* read /proc data into the passed variables
* returns 0 on success, -1 on error
*/
void get_mem(const pid_t pid, mem_t *mem)
{
char *statm;
asprintf(&statm, "/proc/%d/statm", pid);
FILE *fpstat = fopen(statm, "r");
free(statm);
if (!fpstat) return;
if (fscanf(fpstat, "%*d %ld %ld %*[^\1]", &mem->rss, &mem->shr) == EOF)
{
fclose(fpstat);
return;
}
mem->rss *= getpagesize();
mem->shr *= getpagesize();
mem->rss -= mem->shr;
fclose(fpstat);
}
/*
* read /proc data into the passed struct pstat
* returns 0 on success, -1 on error
*/
int get_usg(const pid_t pid, pstat_t* result)
{
//convert pid to string
char pid_s[20];
snprintf(pid_s, sizeof(pid_s), "%d", pid);
char stat_filepath[30] = "/proc/";
strncat(stat_filepath, pid_s,
sizeof(stat_filepath) - strlen(stat_filepath) -1);
strncat(stat_filepath, "/stat",
sizeof(stat_filepath) - strlen(stat_filepath) -1);
FILE *fpstat = fopen(stat_filepath, "r");
if (!fpstat)
return -1;
//read values from /proc/pid/stat
bzero(result, sizeof(pstat_t));
if (fscanf(fpstat, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
"%lu %ld %ld %*d %*d %*d %*d %*u %lu %*d",
&result->utime_ticks, &result->stime_ticks,
&result->cutime_ticks, &result->cstime_ticks,
&result->vsize) == EOF)
return -1;
fclose(fpstat);
return 0;
}
/*
* calculates the elapsed CPU usage between 2 measuring points in ticks
*/
void get_cpu(const pstat_t *cur_usage, const pstat_t *last_usage,
int *ucpu_usage, int *scpu_usage)
{
*ucpu_usage = (cur_usage->utime_ticks + cur_usage->cutime_ticks) -
(last_usage->utime_ticks + last_usage->cutime_ticks);
*scpu_usage = (cur_usage->stime_ticks + cur_usage->cstime_ticks) -
(last_usage->stime_ticks + last_usage->cstime_ticks);
}
int chk_pid(pid_t pid)
{
int ok = 1;
struct stat st;
char a[NAME_MAX];
snprintf(a, sizeof(a), "/proc/%d", pid);
if (stat(a, &st) == -1 && errno == ENOENT)
ok = 0;
return ok;
}
int ncpid(const pid_t ppid)
{
int n = 0;
static proc_t pinfo;
memset(&pinfo, 0, sizeof(pinfo));
PROCTAB *proc = openproc(PROC_FILLSTAT);
if (!proc) return n;
while (readproc(proc, &pinfo))
n += pinfo.ppid == ppid;
closeproc(proc);
return n;
}
void get_cpid(const pid_t ppid, pid_t *pid)
{
static proc_t pinfo;
memset(&pinfo, 0, sizeof(pinfo));
PROCTAB *proc = openproc(PROC_FILLSTAT);
if (!proc) return;
while (readproc(proc, &pinfo))
if (pinfo.ppid == ppid)
*pid = pinfo.tgid;
closeproc(proc);
}
void pids_of_ppid(const pid_t ppid, kv_t *kv)
{
static proc_t pinfo;
int i, j = kv_size(*kv), k;
memset(&pinfo, 0, sizeof(pinfo));
PROCTAB *proc = openproc(PROC_FILLSTAT);
if (!proc) return;
while (readproc(proc, &pinfo))
if (pinfo.ppid == ppid)
kv_push(pid_t, *kv, pinfo.tgid);
closeproc(proc);
k = kv_size(*kv);
for (i = j; i < k; ++i)
pids_of_ppid(kv_A(*kv, i), kv);
}
int pgrep(const char *proc)
{
int _pid = 0;
const char* directory = "/proc";
char task_name[PATH_MAX];
DIR *dir = opendir(directory);
if (dir)
{
struct dirent *de = 0;
while ((de = readdir(dir)) != 0)
{
if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
continue;
int pid = -1;
int res = sscanf(de->d_name, "%d", &pid);
if (res == 1)
{
// we have a valid pid
// open the cmdline file to determine what's the name of the process running
char cmdline_file[PATH_MAX] = {0};
sprintf(cmdline_file, "%s/%d/cmdline", directory, pid);
FILE *fp = fopen(cmdline_file, "r");
if (!fp)
return _pid;
if (fgets(task_name, PATH_MAX - 1, fp))
{
if (!strcmp(basename(task_name), basename(proc)))
{
_pid = pid;
break;
}
}
fclose(fp);
}
}
closedir(dir);
}
return _pid;
}
bool use_shm(const pid_t pid)
{
bool shm = false;
char cmdline[PATH_MAX] = {0};
sprintf(cmdline, "/proc/%d/cmdline", pid);
if (!access(cmdline, R_OK))
{
FILE *fp = fopen(cmdline, "r");
int i;
size_t sz = 0;
char *line = NULL;
if (fp && (i = getline(&line, &sz, fp)) >= 1)
{
line[--i] = '\0';
for (--i; i >= 0; --i)
if (line[i] == '\0')
line[i] = ' ';
shm = (bool)strstr(line, "/dev/shm");
free(line);
fclose(fp);
}
}
return shm;
}
long size_of(const char *dirname)
{
struct stat st;
DIR *dir = opendir(dirname);
if (dir == 0)
return 0;
struct dirent *dit;
long size = 0;
long total_size = 0;
char path[PATH_MAX];
while ((dit = readdir(dir)) != NULL)
{
if (!strcmp(dit->d_name, ".") || !strcmp(dit->d_name, ".."))
continue;
sprintf(path, "%s/%s", dirname, dit->d_name);
if (lstat(path, &st) != 0)
continue;
size = st.st_size;
if (S_ISDIR(st.st_mode))
{
long dir_size = size_of(path) + size;
total_size += dir_size;
}
else
total_size += size;
}
return total_size;
}
bool ends_with(const char *str, const char *sfx)
{
bool ret = false;
int str_len = strlen(str);
int sfx_len = strlen(sfx);
if ((str_len >= sfx_len) && (0 == strcasecmp(str + (str_len-sfx_len), sfx)))
ret = true;
return ret;
}
void pid_to_name(const pid_t pid, char cmd[PATH_MAX])
{
char cmdline[PATH_MAX] = {0};
sprintf(cmdline, "/proc/%d/cmdline", pid);
if (!access(cmdline, R_OK))
{
FILE *fp = fopen(cmdline, "r");
int i, j;
size_t sz = 0;
char *line = NULL;
if (fp && (j = getline(&line, &sz, fp)) >= 1)
{
char *p = line;
while (ends_with(p, "python") ||
ends_with(p, "python2") ||
ends_with(p, "python3") ||
ends_with(p, "perl") ||
ends_with(p, "java") ||
ends_with(p, "Rscript") ||
*p == '-')
p += strlen(p) + 1;
strcpy(cmd, basename(p));
free(line);
fclose(fp);
}
else
cmd[0] = '\0';
}
}
unsigned nprocs()
{
unsigned np = 1;
#ifdef __linux__
np = get_nprocs();
#else
np = sysconf(_SC_NPROCESSORS_ONLN);
#endif
return np;
}
void calc_usg(void *_usg)
{
usg_t *usg = (usg_t *)_usg;
pid_t pid = usg->pid;
if (!chk_pid(pid))
return;
int up = 0, idle = 0;
pstat_t last, current;
mem_t mem = {0, 0};
get_mem(pid, &mem);
int rl = get_usg(pid, &last);
sleep(1);
int rc = get_usg(pid, ¤t);
get_cpu(¤t, &last, &up, &idle);
usg->rss = mem.rss / pow(1024.0, 3);
usg->shr = mem.shr / pow(1024.0, 3) + (usg->use_shm ? fmax(0, (size_of("/dev/shm") - usg->shm) / pow(1024.0, 3)) : 0);
usg->cpu = !(rl + rc) ? up + idle : 0;
}
void calc_usgd(const pid_t ppid, mn_t **mns, int *m, int *n, const double shm, FILE *fp)
{
int i, j;
kv_t kv;
kv_init(kv);
char cmd[PATH_MAX], *cmds = NULL, *cmds_ascii = NULL;
pid_to_name(ppid, cmd);
asprintf(&cmds, "%s", cmd);
asprintf(&cmds_ascii, "%s", cmd);
pids_of_ppid(ppid, &kv);
int kn = kv_size(kv);
usg_t *usg = calloc(kn + 1, sizeof(usg_t));
usg[0].pid = ppid;
usg[0].shm = shm;
usg[0].use_shm = use_shm(ppid);
for (i = 0; i < kn; ++i)
{
pid_t pid = kv_A(kv, i);
usg[i + 1].pid = pid;
usg[i + 1].shm = shm;
usg[i + 1].use_shm = use_shm(pid);
}
i = j = 0;
threadpool thpool = thpool_init(kn + 1);
thpool_add_work(thpool, calc_usg, (void *)(uintptr_t)(usg));
for (i = 0; i < kn; ++i)
{
pid_t pid = kv_A(kv, i);
if (!chk_pid(pid))
continue;
pid_to_name(pid, cmd);
if (strcmp("sh", cmd) && strcmp("bash", cmd) && strcmp("xargs", cmd) &&
!strstr(cmd, "systemd") && !strstr(cmds, cmd))
{
asprintf(&cmds, "%s%s%s", cmds, ARR, cmd);
asprintf(&cmds_ascii, "%s;%s", cmds_ascii, cmd);
}
thpool_add_work(thpool, calc_usg, (void *)(uintptr_t)(usg + i + 1));
}
thpool_wait(thpool);
thpool_destroy(thpool);
double rss = usg[0].rss, shr = usg[0].shr, cpu = usg[0].cpu;
for (i = 0; i < kn; ++i)
{
rss += usg[i + 1].rss;
shr = fmax(shr, usg[i + 1].shr);
cpu += usg[i + 1].cpu;
}
free(usg);
kv_destroy(kv);
char buf[32];
time_t _now = time(0);
strftime(buf, sizeof(buf), "%x %X", localtime(&_now));
if (strlen(cmds))
{
fprintf(fp, "%s\t%f\t%f\t%.3f\t%s\n", buf, fmax(0, rss), shr, cpu > nprocs() * 100 ? 100 : cpu, cmds);
mn_t *mn = calloc(1, sizeof(mn_t));
mn->ts = atot(buf);
mn->rss = fmax(0, rss);
mn->shr = shr;
mn->cpu = cpu > nprocs() * 100 ? 100 : cpu;
char *p = NULL, *q = strdup(cmds_ascii);
if ((p = strchr(q, ';')))
{
if ((p = strchr(p + 1, ';')))
*p = '\0';
strcpy(mn->cmd, strchr(q, ';') + 1);
}
else
strcpy(mn->cmd, q);
free(q);
mns[*n] = mn;
if (*n + 1 == *m)
{
*m <<= 1;
mns = realloc(mns, *m * sizeof(mn_t *));
}
(*n)++;
}
free(cmds);
free(cmds_ascii);
}
int ndigit(const char *str)
{
int i = 0, n = 0;
while (str[i])
n += (bool)isdigit(str[i++]);
return n;
}
void usage()
{
puts("\e[4mMonitor MEM & CPU of process by name, pid or cmds\e[0m");
puts("Examples:");
printf(" \e[1;31m%s\e[0;0m \e[35m<cmd>\e[0m\n", __progname);
printf(" \e[1;31m%s\e[0;0m \e[35m<pid>\e[0m\n", __progname);
printf(" \e[1;31m%s\e[0;0m \e[35m<cmd> <args> ...\e[0m\n", __progname);
}
// convert timestamp string to hms
void ttoa(time_t t)
{
char a[9];
struct tm *s = localtime(&t);
strftime(a, sizeof(a), "%H:%M:%S", s);
puts(a);
}
// convert timestamp string to time in seconds since 1900
time_t atot(const char *a)
{
struct tm tm;
strptime(a, "%m/%d/%y %H:%M:%S %Y", &tm);
tm.tm_isdst = -1;
time_t t = mktime(&tm);
return t;
}
// convert seconds to time hms string
void stoa(const int sec, char **a)
{
int h, m, s;
h = (sec/3600);
m = (sec -(3600*h))/60;
s = (sec -(3600*h)-(m*60));
asprintf(a, "%02d:%02d:%02d", h, m, s);
}
// load mntr log
/*
#TIMESTAMP RSS SHR CPU COMMAND
03/25/22 22:05:32 0.000050 0.000000 0.000 mntr
03/25/22 22:05:35 0.607754 0.001118 3141.000 gptk[2m◂[0mtrimadap
...
03/25/22 22:25:36 0.117447 0.001102 775.000 gptk[2m◂[0mcross[2m◂[0mminimap2[2m◂[0msamtools
*/
void ldlg(const char *fn, mn_t **mns, int *m, int *n)
{
size_t sz = 0;
int i = 0;
char *line = NULL, *data = NULL, timestamp[18];
FILE *fp = fopen(fn, "r");
if (!fp)
{
perror("Error reading input!\n");
exit(EXIT_FAILURE);
}
if (getline(&line, &sz, fp) < 0) // skip header
{
perror("Error reading input!\n");
exit(EXIT_FAILURE);
}
while (getline(&line, &sz, fp) >= 0)
{
mn_t *mn = calloc(1, sizeof(mn_t));
*(line + strlen(line) - 1) = '\0';
strncpy(timestamp, line, 17);
timestamp[17] = '\0';
sscanf(line + 18, "%lf %lf %lf %s", &mn->rss, &mn->shr, &mn->cpu, mn->cmd);
mn->ts = atot(timestamp);
mns[*n] = mn;
if (*n + 1 == *m)
{
*m <<= 1;
mns = realloc(mns, *m * sizeof(mn_t *));
}
(*n)++;
}
free(line);
fclose(fp);
if (*n && *n < *m)
mns = realloc(mns, *n * sizeof(mn_t *));
}
void draw_box(cairo_t *cr, double x, double y, double width, double height)
{
cairo_save(cr);
double w1 = 1.0, w2 = 1.0;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2) / 2.0);
cairo_set_source_rgb(cr, 0.6, 0.6, 0.6);
cairo_rectangle(cr, x, y, width, height);
cairo_stroke(cr);
cairo_restore(cr);
}
void draw_rect(cairo_t *cr, double x, double y, double width, double height)
{
cairo_save(cr);
cairo_new_sub_path (cr);
cairo_rectangle(cr, x, y, width, height);
cairo_set_source_rgba (cr, .96, .96, .96, 0.86);
cairo_fill(cr);
cairo_restore(cr);
}
void draw_rrect(cairo_t *cr)
{
// a custom shape that could be wrapped in a function
double x = 0, // parameters like cairo_rectangle
y = 0,
width = WIDTH,
height = HEIGHT,
aspect = 1.0, // aspect ratio
corner_radius = height / 60.0; // and corner curvature radius
double radius = corner_radius / aspect;
double degrees = M_PI / 180.0;
cairo_new_sub_path (cr);
cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path (cr);
cairo_set_source_rgba (cr, .96, .96, .96, .5);
cairo_fill(cr);
}
void draw_arrow(
cairo_t *cr,
double start_x,
double start_y,
double end_x,
double end_y)
{
double angle = atan2(end_y - start_y, end_x - start_x) + M_PI;
double arrow_degrees_ = M_PI / 15;
double arrow_length_ = 10;
double x1 = end_x + arrow_length_ * cos(angle - arrow_degrees_);
double y1 = end_y + arrow_length_ * sin(angle - arrow_degrees_);
double x2 = end_x + arrow_length_ * cos(angle + arrow_degrees_);
double y2 = end_y + arrow_length_ * sin(angle + arrow_degrees_);
double w1 = 1.0, w2 = 1.0;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2) / 2.0);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, start_x, start_y);
cairo_line_to(cr, (x1 + x2) / 2, (y1 + y2) / 2);
cairo_stroke(cr);
cairo_set_line_width(cr, fmin(w1, w2) / 2);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
cairo_move_to(cr, x1, y1);
cairo_line_to(cr, x2, y2);
cairo_line_to(cr, end_x, end_y);
cairo_line_to(cr, x1, y1);
cairo_close_path(cr);
cairo_fill(cr);
}
void draw_xlab(cairo_t *cr, const char *xlab)
{
double x, y;
cairo_text_extents_t ext;
cairo_set_font_size(cr, 10.0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_text_extents(cr, xlab, &ext);
x = DIM_X / 2.0 - (ext.width / 2.0 + ext.x_bearing);
y = DIM_Y + MARGIN / 4.0 - (ext.height / 2 + ext.y_bearing);
cairo_move_to(cr, x, y);
cairo_show_text(cr, xlab);
}
void draw_ylab(cairo_t *cr, const char *ylab)
{
cairo_save(cr);
cairo_text_extents_t ext;
cairo_set_source_rgb(cr, 87 / 255.0, 122 / 255.0, 166 / 255.0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
//cairo_translate(cr, MARGIN / 1.25, HEIGHT / 2.0); // translate origin to the center
cairo_translate(cr, MARGIN / 2.0, HEIGHT / 2.0); // translate origin to the center
cairo_rotate(cr, 3 * M_PI / 2.0);
cairo_text_extents(cr, ylab, &ext);
//cairo_move_to(cr, MARGIN / 5, -MARGIN);
cairo_move_to(cr, MARGIN / 5.0, -MARGIN / 1.5);
cairo_show_text(cr, ylab);
cairo_restore(cr);
}
void draw_y2lab(cairo_t *cr, const char *ylab)
{
cairo_save(cr);
cairo_text_extents_t ext;
cairo_set_source_rgb(cr, 166 / 255.0, 122 / 255.0, 87 / 255.0);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_translate(cr, MARGIN / 2, HEIGHT / 2.0); // translate origin to the center
cairo_rotate(cr, M_PI / 2.0); // was 270
cairo_text_extents(cr, ylab, &ext);
//cairo_move_to(cr, MARGIN / 2.5 + WIDTH, -MARGIN * 1.25);
cairo_move_to(cr, -MARGIN, MARGIN * 1.25 - WIDTH);
cairo_show_text(cr, ylab);
cairo_restore(cr);
}
void draw_cpu(cairo_t *cr, mn_t **mns, const int n)
{
int i;
double w1 = 1.0, w2 = 1.0, x = 0, y = 0, x1, y1;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2));
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_source_rgb(cr, 87 / 255.0, 122 / 255.0, 166 / 255.0);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
// get max cpu usage
double cpu_max = 100;
for (i = 0; i < n; ++i)
cpu_max = fmax(cpu_max, mns[i]->cpu);
if (cpu_max)
{
// draw cpu history
unsigned long offset = mns[0]->ts;
for (i = 0; i < n; ++i)
{
x1 = (double)(mns[i]->ts - offset) / (mns[n - 1]->ts - offset);
y1 = (double)mns[i]->cpu / cpu_max;
cairo_move_to(cr, x, 1 - y);
cairo_line_to(cr, x1, 1 - y1);
x = x1;
y = y1;
}
cairo_save(cr);
cairo_scale(cr, 1.0, (double)DIM_X / DIM_Y);
cairo_stroke(cr);
cairo_restore(cr);
}
}
void draw_rss(cairo_t *cr, mn_t **mns, const int n)
{
int i;
double w1 = 1.0, w2 = 1.0, x = 0, y = 0, x1, y1;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2));
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_source_rgb(cr, 166 / 255.0, 122 / 255.0, 87 / 255.0);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
// get max cpu usage
double mem_max = 0;
for (i = 0; i < n; ++i)
mem_max = fmax(mem_max, fmax(mns[i]->rss, mns[i]->shr));
// draw cpu history
unsigned long offset = mns[0]->ts;
for (i = 0; i < n; ++i)
{
x1 = (double)(mns[i]->ts - offset) / (mns[n - 1]->ts - offset);
y1 = (double)mns[i]->rss / mem_max;
cairo_move_to(cr, x, 1 - y);
cairo_line_to(cr, x1, 1 - y1);
x = x1;
y = y1;
}
cairo_save(cr);
cairo_scale(cr, 1.0, (double)DIM_X / DIM_Y);
cairo_stroke(cr);
cairo_restore(cr);
}
void draw_shr(cairo_t *cr, mn_t **mns, const int n)
{
int i;
double w1 = 1.0, w2 = 1.0, x = 0, y = 0, x1, y1;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2));
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_source_rgb(cr, 218 / 255.0, 165 / 255.0, 32 / 255.0);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
// get max cpu usage
double mem_max = 0;
for (i = 0; i < n; ++i)
mem_max = fmax(mem_max, fmax(mns[i]->rss, mns[i]->shr));
// draw cpu history
unsigned long offset = mns[0]->ts;
for (i = 0; i < n; ++i)
{
x1 = (double)(mns[i]->ts - offset) / (mns[n - 1]->ts - offset);
y1 = (double)mns[i]->shr / mem_max;
cairo_move_to(cr, x, 1 - y);
cairo_line_to(cr, x1, 1 - y1);
x = x1;
y = y1;
}
cairo_save(cr);
cairo_scale(cr, 1.0, (double)DIM_X / DIM_Y);
cairo_stroke(cr);
cairo_restore(cr);
}
void draw_steps(cairo_t *cr, mn_t **mns, const int n)
{
int i;
double w1 = 1.0, w2 = 1.0, x0 = 0, x, y;
cairo_device_to_user_distance(cr, &w1, &w2);
cairo_set_line_width(cr, fmin(w1, w2) / 1.25);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_source_rgb(cr, 87 / 255.0, 122 / 255.0, 166 / 255.0);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
// draw 1st mark mntr ignored
char *cmd1 = mns[0]->cmd;
unsigned long offset = mns[0]->ts;
cairo_text_extents_t ext;
int block = 1;
cairo_set_source_rgb(cr, 166 / 255.0, 122 / 255.0, 87 / 255.0);
for (i = 1; i < n; ++i)
{
if (strcmp(mns[i]->cmd, mns[i - 1]->cmd)) // new command encountered
{
// process the previous command
cairo_text_extents(cr, mns[i - 1]->cmd, &ext);
x = DIM_X * (double)(mns[i - 1]->ts - offset) / (mns[n - 1]->ts - offset);
//y = - (ext.height / 2 + ext.y_bearing); // topright
y = - ext.height - ext.y_bearing; // topright
block += (x - x0 > 0.1);
if (block && block % 2)
draw_rect(cr, x0, 0, x - x0, DIM_Y);
if (x - x0 > 0.05)
{
cairo_move_to(cr, (x + x0) / 2.0, y);
cairo_save(cr);
cairo_translate(cr, MARGIN / 2.0, HEIGHT / 2.0); // translate origin to the center
cairo_rotate(cr, -M_PI / 6.0);
cairo_set_font_size(cr, 8.0);
cairo_select_font_face(cr, "Mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_show_text(cr, mns[i - 1]->cmd);
cairo_restore(cr);
}
x0 = DIM_X * (double)(mns[i]->ts - offset) / (mns[n - 1]->ts - offset);
}
}
// the last one
block += (x - x0 > 0.1);
if (block && block % 2)
draw_rect(cr, x0, 0, x - x0, DIM_Y);
cairo_text_extents(cr, mns[n - 1]->cmd, &ext);
x = DIM_X * (double)(mns[n - 1]->ts - offset) / (mns[n - 1]->ts - offset);
y = - ext.height - ext.y_bearing; // topright
if (x - x0 > 0.01)
{
cairo_move_to(cr, (x + x0) / 2.0, y);
cairo_save(cr);
cairo_translate(cr, MARGIN / 2.0, HEIGHT / 2.0); // translate origin to the center
cairo_rotate(cr, -M_PI / 6.0);
cairo_set_font_size(cr, 8.0);
cairo_select_font_face(cr, "Mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_show_text(cr, mns[n - 1]->cmd);
cairo_restore(cr);