forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps_plugin.c
More file actions
2890 lines (2346 loc) · 102 KB
/
apps_plugin.c
File metadata and controls
2890 lines (2346 loc) · 102 KB
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
#include "common.h"
#define MAX_COMPARE_NAME 100
#define MAX_NAME 100
#define MAX_CMDLINE 1024
// the rates we are going to send to netdata
// will have this detail
// a value of:
// 1 will send just integer parts to netdata
// 100 will send 2 decimal points
// 1000 will send 3 decimal points
// etc.
#define RATES_DETAIL 10000ULL
int debug = 0;
int update_every = 1;
unsigned long long global_iterations_counter = 1;
unsigned long long file_counter = 0;
int proc_pid_cmdline_is_needed = 0;
int include_exited_childs = 1;
char *config_dir = CONFIG_DIR;
pid_t *all_pids_sortlist = NULL;
// will be automatically set to 1, if guest values are collected
int show_guest_time = 0;
int show_guest_time_old = 0;
int enable_guest_charts = 0;
int enable_file_charts = 1;
// ----------------------------------------------------------------------------
void netdata_cleanup_and_exit(int ret) {
exit(ret);
}
// ----------------------------------------------------------------------------
// target
// target is the structure that process data are aggregated
struct target {
char compare[MAX_COMPARE_NAME + 1];
uint32_t comparehash;
size_t comparelen;
char id[MAX_NAME + 1];
uint32_t idhash;
char name[MAX_NAME + 1];
uid_t uid;
gid_t gid;
unsigned long long minflt;
unsigned long long cminflt;
unsigned long long majflt;
unsigned long long cmajflt;
unsigned long long utime;
unsigned long long stime;
unsigned long long gtime;
unsigned long long cutime;
unsigned long long cstime;
unsigned long long cgtime;
unsigned long long num_threads;
// unsigned long long rss;
unsigned long long statm_size;
unsigned long long statm_resident;
unsigned long long statm_share;
// unsigned long long statm_text;
// unsigned long long statm_lib;
// unsigned long long statm_data;
// unsigned long long statm_dirty;
unsigned long long io_logical_bytes_read;
unsigned long long io_logical_bytes_written;
// unsigned long long io_read_calls;
// unsigned long long io_write_calls;
unsigned long long io_storage_bytes_read;
unsigned long long io_storage_bytes_written;
// unsigned long long io_cancelled_write_bytes;
int *fds;
unsigned long long openfiles;
unsigned long long openpipes;
unsigned long long opensockets;
unsigned long long openinotifies;
unsigned long long openeventfds;
unsigned long long opentimerfds;
unsigned long long opensignalfds;
unsigned long long openeventpolls;
unsigned long long openother;
unsigned long processes; // how many processes have been merged to this
int exposed; // if set, we have sent this to netdata
int hidden; // if set, we set the hidden flag on the dimension
int debug;
int ends_with;
int starts_with; // if set, the compare string matches only the
// beginning of the command
struct target *target; // the one that will be reported to netdata
struct target *next;
};
// ----------------------------------------------------------------------------
// apps_groups.conf
// aggregate all processes in groups, to have a limited number of dimensions
struct target *apps_groups_root_target = NULL;
struct target *apps_groups_default_target = NULL;
long apps_groups_targets = 0;
struct target *users_root_target = NULL;
struct target *groups_root_target = NULL;
struct target *get_users_target(uid_t uid)
{
struct target *w;
for(w = users_root_target ; w ; w = w->next)
if(w->uid == uid) return w;
w = callocz(sizeof(struct target), 1);
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
snprintfz(w->id, MAX_NAME, "%u", uid);
w->idhash = simple_hash(w->id);
struct passwd *pw = getpwuid(uid);
if(!pw)
snprintfz(w->name, MAX_NAME, "%u", uid);
else
snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
netdata_fix_chart_name(w->name);
w->uid = uid;
w->next = users_root_target;
users_root_target = w;
if(unlikely(debug))
fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
return w;
}
struct target *get_groups_target(gid_t gid)
{
struct target *w;
for(w = groups_root_target ; w ; w = w->next)
if(w->gid == gid) return w;
w = callocz(sizeof(struct target), 1);
snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
snprintfz(w->id, MAX_NAME, "%u", gid);
w->idhash = simple_hash(w->id);
struct group *gr = getgrgid(gid);
if(!gr)
snprintfz(w->name, MAX_NAME, "%u", gid);
else
snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
netdata_fix_chart_name(w->name);
w->gid = gid;
w->next = groups_root_target;
groups_root_target = w;
if(unlikely(debug))
fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
return w;
}
// find or create a new target
// there are targets that are just aggregated to other target (the second argument)
struct target *get_apps_groups_target(const char *id, struct target *target) {
int tdebug = 0, thidden = 0, ends_with = 0;
const char *nid = id;
while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
if(nid[0] == '-') thidden = 1;
if(nid[0] == '+') tdebug = 1;
if(nid[0] == '*') ends_with = 1;
nid++;
}
uint32_t hash = simple_hash(id);
struct target *w, *last = apps_groups_root_target;
for(w = apps_groups_root_target ; w ; w = w->next) {
if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
return w;
last = w;
}
w = callocz(sizeof(struct target), 1);
strncpyz(w->id, nid, MAX_NAME);
w->idhash = simple_hash(w->id);
strncpyz(w->name, nid, MAX_NAME);
strncpyz(w->compare, nid, MAX_COMPARE_NAME);
size_t len = strlen(w->compare);
if(w->compare[len - 1] == '*') {
w->compare[len - 1] = '\0';
w->starts_with = 1;
}
w->ends_with = ends_with;
if(w->starts_with && w->ends_with)
proc_pid_cmdline_is_needed = 1;
w->comparehash = simple_hash(w->compare);
w->comparelen = strlen(w->compare);
w->hidden = thidden;
w->debug = tdebug;
w->target = target;
// append it, to maintain the order in apps_groups.conf
if(last) last->next = w;
else apps_groups_root_target = w;
if(unlikely(debug))
fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
, w->id
, w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
, w->target?w->target->id:w->id
, (w->hidden)?"hidden":"-"
, (w->debug)?"debug":"-"
);
return w;
}
// read the apps_groups.conf file
int read_apps_groups_conf(const char *name)
{
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
if(unlikely(debug))
fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
// ----------------------------------------
procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
if(!ff) return 1;
procfile_set_quotes(ff, "'\"");
ff = procfile_readall(ff);
if(!ff)
return 1;
unsigned long line, lines = procfile_lines(ff);
for(line = 0; line < lines ;line++) {
unsigned long word, words = procfile_linewords(ff, line);
struct target *w = NULL;
char *t = procfile_lineword(ff, line, 0);
if(!t || !*t) continue;
for(word = 0; word < words ;word++) {
char *s = procfile_lineword(ff, line, word);
if(!s || !*s) continue;
if(*s == '#') break;
if(t == s) continue;
struct target *n = get_apps_groups_target(s, w);
if(!n) {
error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
continue;
}
if(!w) w = n;
}
if(w) {
int tdebug = 0, thidden = 0;
while(t[0] == '-' || t[0] == '+') {
if(t[0] == '-') thidden = 1;
if(t[0] == '+') tdebug = 1;
t++;
}
strncpyz(w->name, t, MAX_NAME);
w->hidden = thidden;
w->debug = tdebug;
if(unlikely(debug))
fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
, w->name
, w->id
, w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
, w->target?w->target->id:w->id
, (w->hidden)?"hidden":"-"
, (w->debug)?"debug":"-"
);
}
}
procfile_close(ff);
apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
if(!apps_groups_default_target)
error("Cannot create default target");
else
strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
return 0;
}
// ----------------------------------------------------------------------------
// data to store for each pid
// see: man proc
#define PID_LOG_IO 0x00000001
#define PID_LOG_STATM 0x00000002
#define PID_LOG_CMDLINE 0x00000004
#define PID_LOG_FDS 0x00000008
#define PID_LOG_STAT 0x00000010
struct pid_stat {
int32_t pid;
char comm[MAX_COMPARE_NAME + 1];
char cmdline[MAX_CMDLINE + 1];
uint32_t log_thrown;
// char state;
int32_t ppid;
// int32_t pgrp;
// int32_t session;
// int32_t tty_nr;
// int32_t tpgid;
// uint64_t flags;
// these are raw values collected
unsigned long long minflt_raw;
unsigned long long cminflt_raw;
unsigned long long majflt_raw;
unsigned long long cmajflt_raw;
unsigned long long utime_raw;
unsigned long long stime_raw;
unsigned long long gtime_raw; // guest_time
unsigned long long cutime_raw;
unsigned long long cstime_raw;
unsigned long long cgtime_raw; // cguest_time
// these are rates
unsigned long long minflt;
unsigned long long cminflt;
unsigned long long majflt;
unsigned long long cmajflt;
unsigned long long utime;
unsigned long long stime;
unsigned long long gtime;
unsigned long long cutime;
unsigned long long cstime;
unsigned long long cgtime;
// int64_t priority;
// int64_t nice;
int32_t num_threads;
// int64_t itrealvalue;
// unsigned long long starttime;
// unsigned long long vsize;
// unsigned long long rss;
// unsigned long long rsslim;
// unsigned long long starcode;
// unsigned long long endcode;
// unsigned long long startstack;
// unsigned long long kstkesp;
// unsigned long long kstkeip;
// uint64_t signal;
// uint64_t blocked;
// uint64_t sigignore;
// uint64_t sigcatch;
// uint64_t wchan;
// uint64_t nswap;
// uint64_t cnswap;
// int32_t exit_signal;
// int32_t processor;
// uint32_t rt_priority;
// uint32_t policy;
// unsigned long long delayacct_blkio_ticks;
uid_t uid;
gid_t gid;
unsigned long long statm_size;
unsigned long long statm_resident;
unsigned long long statm_share;
// unsigned long long statm_text;
// unsigned long long statm_lib;
// unsigned long long statm_data;
// unsigned long long statm_dirty;
unsigned long long io_logical_bytes_read_raw;
unsigned long long io_logical_bytes_written_raw;
// unsigned long long io_read_calls_raw;
// unsigned long long io_write_calls_raw;
unsigned long long io_storage_bytes_read_raw;
unsigned long long io_storage_bytes_written_raw;
// unsigned long long io_cancelled_write_bytes_raw;
unsigned long long io_logical_bytes_read;
unsigned long long io_logical_bytes_written;
// unsigned long long io_read_calls;
// unsigned long long io_write_calls;
unsigned long long io_storage_bytes_read;
unsigned long long io_storage_bytes_written;
// unsigned long long io_cancelled_write_bytes;
int *fds; // array of fds it uses
int fds_size; // the size of the fds array
int children_count; // number of processes directly referencing this
int keep; // 1 when we need to keep this process in memory even after it exited
int keeploops; // increases by 1 every time keep is 1 and updated 0
int updated; // 1 when the process is currently running
int merged; // 1 when it has been merged to its parent
int new_entry; // 1 when this is a new process, just saw for the first time
int read; // 1 when we have already read this process for this iteration
int sortlist; // higher numbers = top on the process tree
// each process gets a unique number
struct target *target; // app_groups.conf targets
struct target *user_target; // uid based targets
struct target *group_target; // gid based targets
unsigned long long stat_collected_usec;
unsigned long long last_stat_collected_usec;
unsigned long long io_collected_usec;
unsigned long long last_io_collected_usec;
char *stat_filename;
char *statm_filename;
char *io_filename;
char *cmdline_filename;
struct pid_stat *parent;
struct pid_stat *prev;
struct pid_stat *next;
} *root_of_pids = NULL, **all_pids;
long all_pids_count = 0;
struct pid_stat *get_pid_entry(pid_t pid) {
if(all_pids[pid]) {
all_pids[pid]->new_entry = 0;
return all_pids[pid];
}
all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
all_pids[pid]->fds = callocz(sizeof(int), 100);
all_pids[pid]->fds_size = 100;
if(root_of_pids) root_of_pids->prev = all_pids[pid];
all_pids[pid]->next = root_of_pids;
root_of_pids = all_pids[pid];
all_pids[pid]->pid = pid;
all_pids[pid]->new_entry = 1;
all_pids_count++;
return all_pids[pid];
}
void del_pid_entry(pid_t pid) {
if(!all_pids[pid]) {
error("attempted to free pid %d that is not allocated.", pid);
return;
}
if(unlikely(debug))
fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
freez(all_pids[pid]);
all_pids[pid] = NULL;
all_pids_count--;
}
// ----------------------------------------------------------------------------
// update pids from proc
int read_proc_pid_cmdline(struct pid_stat *p) {
if(unlikely(!p->cmdline_filename)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
p->cmdline_filename = strdupz(filename);
}
int fd = open(p->cmdline_filename, O_RDONLY, 0666);
if(unlikely(fd == -1)) goto cleanup;
ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
close(fd);
if(unlikely(bytes < 0)) goto cleanup;
p->cmdline[bytes] = '\0';
for(i = 0; i < bytes ; i++)
if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
if(unlikely(debug))
fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
return 1;
cleanup:
// copy the command to the command line
strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
return 0;
}
int read_proc_pid_ownership(struct pid_stat *p) {
if(unlikely(!p->stat_filename)) {
error("pid %d does not have a stat_filename", p->pid);
return 0;
}
// ----------------------------------------
// read uid and gid
struct stat st;
if(stat(p->stat_filename, &st) != 0) {
error("Cannot stat file '%s'", p->stat_filename);
return 1;
}
p->uid = st.st_uid;
p->gid = st.st_gid;
return 1;
}
int read_proc_pid_stat(struct pid_stat *p) {
static procfile *ff = NULL;
if(unlikely(!p->stat_filename)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
p->stat_filename = strdupz(filename);
}
int set_quotes = (!ff)?1:0;
ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
if(unlikely(!ff)) goto cleanup;
// if(set_quotes) procfile_set_quotes(ff, "()");
if(set_quotes) procfile_set_open_close(ff, "(", ")");
ff = procfile_readall(ff);
if(unlikely(!ff)) goto cleanup;
p->last_stat_collected_usec = p->stat_collected_usec;
p->stat_collected_usec = time_usec();
file_counter++;
// p->pid = atol(procfile_lineword(ff, 0, 0+i));
strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
// p->state = *(procfile_lineword(ff, 0, 2));
p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3));
// p->pgrp = atol(procfile_lineword(ff, 0, 4));
// p->session = atol(procfile_lineword(ff, 0, 5));
// p->tty_nr = atol(procfile_lineword(ff, 0, 6));
// p->tpgid = atol(procfile_lineword(ff, 0, 7));
// p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
unsigned long long last;
last = p->minflt_raw;
p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
p->minflt = (p->minflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->cminflt_raw;
p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
p->cminflt = (p->cminflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->majflt_raw;
p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
p->majflt = (p->majflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->cmajflt_raw;
p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
p->cmajflt = (p->cmajflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->utime_raw;
p->utime_raw = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
p->utime = (p->utime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->stime_raw;
p->stime_raw = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
p->stime = (p->stime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->cutime_raw;
p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
p->cutime = (p->cutime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->cstime_raw;
p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
p->cstime = (p->cstime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
// p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
// p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19));
// p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
// p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
// p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
// p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
// p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
// p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
// p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
// p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
// p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
// p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
// p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
// p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
// p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
// p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
// p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
// p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
// p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
// p->exit_signal = atol(procfile_lineword(ff, 0, 37));
// p->processor = atol(procfile_lineword(ff, 0, 38));
// p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
// p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
// p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
if(enable_guest_charts) {
last = p->gtime_raw;
p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
p->gtime = (p->gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
last = p->cgtime_raw;
p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
p->cgtime = (p->cgtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
if (show_guest_time || p->gtime || p->cgtime) {
p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
show_guest_time = 1;
}
}
if(unlikely(debug || (p->target && p->target->debug)))
fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, threads=%d\n", global_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
if(unlikely(global_iterations_counter == 1)) {
p->minflt = 0;
p->cminflt = 0;
p->majflt = 0;
p->cmajflt = 0;
p->utime = 0;
p->stime = 0;
p->gtime = 0;
p->cutime = 0;
p->cstime = 0;
p->cgtime = 0;
}
return 1;
cleanup:
p->minflt = 0;
p->cminflt = 0;
p->majflt = 0;
p->cmajflt = 0;
p->utime = 0;
p->stime = 0;
p->gtime = 0;
p->cutime = 0;
p->cstime = 0;
p->cgtime = 0;
p->num_threads = 0;
// p->rss = 0;
return 0;
}
int read_proc_pid_statm(struct pid_stat *p) {
static procfile *ff = NULL;
if(unlikely(!p->statm_filename)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
p->statm_filename = strdupz(filename);
}
ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
if(unlikely(!ff)) goto cleanup;
ff = procfile_readall(ff);
if(unlikely(!ff)) goto cleanup;
file_counter++;
p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
// p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
// p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
// p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
// p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
return 1;
cleanup:
p->statm_size = 0;
p->statm_resident = 0;
p->statm_share = 0;
// p->statm_text = 0;
// p->statm_lib = 0;
// p->statm_data = 0;
// p->statm_dirty = 0;
return 0;
}
int read_proc_pid_io(struct pid_stat *p) {
static procfile *ff = NULL;
if(unlikely(!p->io_filename)) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
p->io_filename = strdupz(filename);
}
// open the file
ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
if(unlikely(!ff)) goto cleanup;
ff = procfile_readall(ff);
if(unlikely(!ff)) goto cleanup;
file_counter++;
p->last_io_collected_usec = p->io_collected_usec;
p->io_collected_usec = time_usec();
unsigned long long last;
last = p->io_logical_bytes_read_raw;
p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
last = p->io_logical_bytes_written_raw;
p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
// last = p->io_read_calls_raw;
// p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
// p->io_read_calls = (p->io_read_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
// last = p->io_write_calls_raw;
// p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
// p->io_write_calls = (p->io_write_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
last = p->io_storage_bytes_read_raw;
p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
last = p->io_storage_bytes_written_raw;
p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
// last = p->io_cancelled_write_bytes_raw;
// p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
// p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
if(unlikely(global_iterations_counter == 1)) {
p->io_logical_bytes_read = 0;
p->io_logical_bytes_written = 0;
// p->io_read_calls = 0;
// p->io_write_calls = 0;
p->io_storage_bytes_read = 0;
p->io_storage_bytes_written = 0;
// p->io_cancelled_write_bytes = 0;
}
return 1;
cleanup:
p->io_logical_bytes_read = 0;
p->io_logical_bytes_written = 0;
// p->io_read_calls = 0;
// p->io_write_calls = 0;
p->io_storage_bytes_read = 0;
p->io_storage_bytes_written = 0;
// p->io_cancelled_write_bytes = 0;
return 0;
}
unsigned long long global_utime = 0;
unsigned long long global_stime = 0;
unsigned long long global_gtime = 0;
int read_proc_stat() {
static char filename[FILENAME_MAX + 1] = "";
static procfile *ff = NULL;
static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0, collected_usec = 0, last_collected_usec = 0;
if(unlikely(!ff)) {
snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
if(unlikely(!ff)) goto cleanup;
}
ff = procfile_readall(ff);
if(unlikely(!ff)) goto cleanup;
last_collected_usec = collected_usec;
collected_usec = time_usec();
file_counter++;
unsigned long long last;
last = utime_raw;
utime_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
global_utime = (utime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
// nice time, on user time
last = ntime_raw;
ntime_raw = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
global_utime += (ntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
last = stime_raw;
stime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
global_stime = (stime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
last = gtime_raw;
gtime_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
global_gtime = (gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
if(enable_guest_charts) {
// guest nice time, on guest time
last = gntime_raw;
gntime_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
global_gtime += (gntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
// remove guest time from user time
global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
}
if(unlikely(global_iterations_counter == 1)) {
global_utime = 0;
global_stime = 0;
global_gtime = 0;
}
return 1;
cleanup:
global_utime = 0;
global_stime = 0;
global_gtime = 0;
return 0;
}
// ----------------------------------------------------------------------------
// file descriptor
// this is used to keep a global list of all open files of the system
// it is needed in order to calculate the unique files processes have open
#define FILE_DESCRIPTORS_INCREASE_STEP 100
struct file_descriptor {
avl avl;
#ifdef NETDATA_INTERNAL_CHECKS
uint32_t magic;
#endif /* NETDATA_INTERNAL_CHECKS */
uint32_t hash;
const char *name;
int type;
int count;
int pos;
} *all_files = NULL;
int all_files_len = 0;
int all_files_size = 0;
int file_descriptor_compare(void* a, void* b) {
#ifdef NETDATA_INTERNAL_CHECKS
if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
error("Corrupted index data detected. Please report this.");
#endif /* NETDATA_INTERNAL_CHECKS */
if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
return -1;
else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
return 1;
else
return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
}
int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
avl_tree all_files_index = {
NULL,
file_descriptor_compare
};
static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
struct file_descriptor tmp;
tmp.hash = (hash)?hash:simple_hash(name);
tmp.name = name;
tmp.count = 0;
tmp.pos = 0;
#ifdef NETDATA_INTERNAL_CHECKS
tmp.magic = 0x0BADCAFE;
#endif /* NETDATA_INTERNAL_CHECKS */
return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
}
#define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
#define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
#define FILETYPE_OTHER 0
#define FILETYPE_FILE 1
#define FILETYPE_PIPE 2
#define FILETYPE_SOCKET 3
#define FILETYPE_INOTIFY 4
#define FILETYPE_EVENTFD 5
#define FILETYPE_EVENTPOLL 6
#define FILETYPE_TIMERFD 7
#define FILETYPE_SIGNALFD 8
void file_descriptor_not_used(int id)
{
if(id > 0 && id < all_files_size) {
#ifdef NETDATA_INTERNAL_CHECKS
if(all_files[id].magic != 0x0BADCAFE) {
error("Ignoring request to remove empty file id %d.", id);
return;
}
#endif /* NETDATA_INTERNAL_CHECKS */
if(unlikely(debug))
fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
if(all_files[id].count > 0) {
all_files[id].count--;
if(!all_files[id].count) {
if(unlikely(debug))
fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
file_descriptor_remove(&all_files[id]);
#ifdef NETDATA_INTERNAL_CHECKS
all_files[id].magic = 0x00000000;
#endif /* NETDATA_INTERNAL_CHECKS */
all_files_len--;
}
}
else
error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
}
else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);