code cleanup
Costa Tsaousis (ktsaou) committed
Mar 30, 2015 at 01:30 UTC
f450ffa7187ae2f86a1c458a965f9eb9dfb0b475
1 file changed
+189
-185
src/plugins.d/apps_plugin.c
+189
-185
@@ -460,13 +460,13 @@ int read_process_groups(const char *name)
460
struct pid_stat {
461
int32_t pid;
462
char comm[MAX_COMPARE_NAME + 1];
463
- char state;
463
+ // char state;
464
int32_t ppid;
465
- int32_t pgrp;
466
- int32_t session;
467
- int32_t tty_nr;
468
- int32_t tpgid;
469
- uint64_t flags;
465
+ // int32_t pgrp;
466
+ // int32_t session;
467
+ // int32_t tty_nr;
468
+ // int32_t tpgid;
469
+ // uint64_t flags;
470
unsigned long long minflt;
471
unsigned long long cminflt;
472
unsigned long long majflt;
@@ -475,33 +475,33 @@ struct pid_stat {
475
unsigned long long stime;
476
unsigned long long cutime;
477
unsigned long long cstime;
478
- int64_t priority;
479
- int64_t nice;
478
+ // int64_t priority;
479
+ // int64_t nice;
480
int32_t num_threads;
481
- int64_t itrealvalue;
482
- unsigned long long starttime;
483
- unsigned long long vsize;
481
+ // int64_t itrealvalue;
482
+ // unsigned long long starttime;
483
+ // unsigned long long vsize;
484
unsigned long long rss;
485
- unsigned long long rsslim;
486
- unsigned long long starcode;
487
- unsigned long long endcode;
488
- unsigned long long startstack;
489
- unsigned long long kstkesp;
490
- unsigned long long kstkeip;
491
- uint64_t signal;
492
- uint64_t blocked;
493
- uint64_t sigignore;
494
- uint64_t sigcatch;
495
- uint64_t wchan;
496
- uint64_t nswap;
497
- uint64_t cnswap;
498
- int32_t exit_signal;
499
- int32_t processor;
500
- uint32_t rt_priority;
501
- uint32_t policy;
502
- unsigned long long delayacct_blkio_ticks;
503
- uint64_t guest_time;
504
- int64_t cguest_time;
485
+ // unsigned long long rsslim;
486
+ // unsigned long long starcode;
487
+ // unsigned long long endcode;
488
+ // unsigned long long startstack;
489
+ // unsigned long long kstkesp;
490
+ // unsigned long long kstkeip;
491
+ // uint64_t signal;
492
+ // uint64_t blocked;
493
+ // uint64_t sigignore;
494
+ // uint64_t sigcatch;
495
+ // uint64_t wchan;
496
+ // uint64_t nswap;
497
+ // uint64_t cnswap;
498
+ // int32_t exit_signal;
499
+ // int32_t processor;
500
+ // uint32_t rt_priority;
501
+ // uint32_t policy;
502
+ // unsigned long long delayacct_blkio_ticks;
503
+ // uint64_t guest_time;
504
+ // int64_t cguest_time;
505
506
unsigned long long statm_size;
507
unsigned long long statm_resident;
@@ -578,6 +578,7 @@ struct pid_stat *get_pid_entry(pid_t pid)
578
all_pids[pid]->next = root_of_pids;
579
root_of_pids = all_pids[pid];
580
581
+ all_pids[pid]->pid = pid;
582
all_pids[pid]->new_entry = 1;
583
584
return all_pids[pid];
@@ -598,6 +599,145 @@ void del_pid_entry(pid_t pid)
599
all_pids[pid] = NULL;
600
}
601
602
+
603
+// ----------------------------------------------------------------------------
604
+// update pids from proc
605
+
606
+int read_proc_pid_stat(struct pid_stat *p) {
607
+ char filename[FILENAME_MAX + 1];
608
+
609
+ snprintf(filename, FILENAME_MAX, "/proc/%d/stat", p->pid);
610
+
611
+ procfile *ff = procfile_open(filename, "");
612
+ if(!ff) return 1;
613
+
614
+ ff = procfile_readall(ff);
615
+ if(!ff) {
616
+ procfile_close(ff);
617
+ return 1;
618
+ }
619
+
620
+ file_counter++;
621
+
622
+ int len = strlen(procfile_lineword(ff, 0, 1));
623
+ if(len < 2) {
624
+ error("apps.plugin: File %s states too small command name '%s'. Ignoring file.", filename, procfile_lineword(ff, 0, 1));
625
+ procfile_close(ff);
626
+ return 1;
627
+ }
628
+
629
+ len -= 2; // remove ()
630
+ if(len > MAX_COMPARE_NAME) len = MAX_COMPARE_NAME;
631
+ strncpy(p->comm, &(procfile_lineword(ff, 0, 1))[1], len);
632
+
633
+ // p->pid = atol(procfile_lineword(ff, 0, 0));
634
+ // comm is at 1
635
+ // p->state = *(procfile_lineword(ff, 0, 2));
636
+ p->ppid = atol(procfile_lineword(ff, 0, 3));
637
+ // p->pgrp = atol(procfile_lineword(ff, 0, 4));
638
+ // p->session = atol(procfile_lineword(ff, 0, 5));
639
+ // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
640
+ // p->tpgid = atol(procfile_lineword(ff, 0, 7));
641
+ // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
642
+ p->minflt = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
643
+ p->cminflt = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
644
+ p->majflt = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
645
+ p->cmajflt = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
646
+ p->utime = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
647
+ p->stime = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
648
+ p->cutime = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
649
+ p->cstime = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
650
+ // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
651
+ // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
652
+ p->num_threads = atol(procfile_lineword(ff, 0, 19));
653
+ // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
654
+ // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
655
+ // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
656
+ p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
657
+ // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
658
+ // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
659
+ // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
660
+ // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
661
+ // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
662
+ // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
663
+ // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
664
+ // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
665
+ // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
666
+ // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
667
+ // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
668
+ // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
669
+ // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
670
+ // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
671
+ // p->processor = atol(procfile_lineword(ff, 0, 38));
672
+ // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
673
+ // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
674
+ // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
675
+ // p->guest_time = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
676
+ // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
677
+
678
+ if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: VALUES: %s utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->comm, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
679
+
680
+ procfile_close(ff);
681
+ return 0;
682
+}
683
+
684
+int read_proc_pid_statm(struct pid_stat *p) {
685
+ char filename[FILENAME_MAX + 1];
686
+
687
+ snprintf(filename, FILENAME_MAX, "/proc/%d/statm", p->pid);
688
+
689
+ procfile *ff = procfile_open(filename, "");
690
+ if(!ff) return 1;
691
+
692
+ ff = procfile_readall(ff);
693
+ if(!ff) {
694
+ procfile_close(ff);
695
+ return 1;
696
+ }
697
+
698
+ file_counter++;
699
+
700
+ p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
701
+ p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
702
+ p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
703
+ p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
704
+ p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
705
+ p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
706
+ p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
707
+
708
+ procfile_close(ff);
709
+ return 0;
710
+}
711
+
712
+int read_proc_pid_io(struct pid_stat *p) {
713
+ char filename[FILENAME_MAX + 1];
714
+
715
+ snprintf(filename, FILENAME_MAX, "/proc/%d/io", p->pid);
716
+
717
+ procfile *ff = procfile_open(filename, ":");
718
+ if(!ff) return 1;
719
+
720
+ ff = procfile_readall(ff);
721
+ if(!ff) {
722
+ procfile_close(ff);
723
+ return 1;
724
+ }
725
+
726
+ file_counter++;
727
+
728
+ p->io_logical_bytes_read = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
729
+ p->io_logical_bytes_written = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
730
+ p->io_read_calls = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
731
+ p->io_write_calls = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
732
+ p->io_storage_bytes_read = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
733
+ p->io_storage_bytes_written = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
734
+ p->io_cancelled_write_bytes = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
735
+
736
+ procfile_close(ff);
737
+ return 0;
738
+}
739
+
740
+
741
// ----------------------------------------------------------------------------
742
743
#ifdef INCLUDE_CHILDS
@@ -839,9 +979,6 @@ unsigned long file_descriptor_find_or_add(const char *name)
979
}
980
981
842
-// ----------------------------------------------------------------------------
843
-// update pids from proc
844
-
982
// 1. read all files in /proc
983
// 2. for each numeric directory:
984
// i. read /proc/pid/stat
@@ -862,8 +999,6 @@ int update_from_proc(void)
999
{
1000
static long count_errors = 0;
1001
865
- char buffer[PROC_BUFFER + 1];
866
- char name[PROC_BUFFER + 1];
1002
char filename[FILENAME_MAX+1];
1003
DIR *dir = opendir("/proc");
1004
if(!dir) return 0;
@@ -887,81 +1022,39 @@ int update_from_proc(void)
1022
pid_t pid = strtoul(file->d_name, &endptr, 10);
1023
if(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0') continue;
1024
1025
+ p = get_pid_entry(pid);
1026
+ if(!p) continue;
1027
1028
// --------------------------------------------------------------------
1029
// /proc/<pid>/stat
1030
894
- snprintf(filename, FILENAME_MAX, "/proc/%s/stat", file->d_name);
895
- int fd = open(filename, O_RDONLY);
896
- if(fd == -1) {
897
- if(errno != ENOENT && errno != ESRCH) {
898
- if(!count_errors++ || debug)
899
- error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
900
- }
1031
+ if(read_proc_pid_stat(p)) {
1032
+ error("Cannot process /proc/%d/stat", pid);
1033
continue;
1034
}
903
- file_counter++;
1035
+ if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
1036
905
- int bytes = read(fd, buffer, PROC_BUFFER);
906
- close(fd);
1037
908
- if(bytes == -1) {
909
- if(!count_errors++ || debug)
910
- error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
1038
+ // --------------------------------------------------------------------
1039
+ // /proc/<pid>/statm
1040
+
1041
+ if(read_proc_pid_statm(p)) {
1042
+ error("Cannot process /proc/%d/statm", pid);
1043
continue;
1044
}
1045
914
- if(bytes < 10) continue;
915
- buffer[bytes] = '\0';
916
- if(debug) fprintf(stderr, "apps.plugin: READ stat: %s", buffer);
917
-
918
- p = get_pid_entry(pid);
919
- if(!p) continue;
920
-
921
- int parsed = sscanf(buffer,
922
- "%d (%[^)]) %c" // pid, comm, state
923
- " %d %d %d %d %d" // ppid, pgrp, session, tty_nr, tpgid
924
- " %" PRIu64 " %llu %llu %llu %llu" // flags, minflt, cminflt, majflt, cmajflt
925
- " %llu %llu %llu %llu" // utime, stime, cutime, cstime
926
- " %" PRId64 " %" PRId64 // priority, nice
927
- " %d" // num_threads
928
- " %" PRId64 // itrealvalue
929
- " %llu" // starttime
930
- " %llu" // vsize
931
- " %llu" // rss
932
- " %llu %llu %llu %llu %llu %llu" // rsslim, starcode, endcode, startstack, kstkesp, kstkeip
933
- " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 // signal, blocked, sigignore, sigcatch
934
- " %" PRIu64 " %" PRIu64 " %" PRIu64 // wchan, nswap, cnswap
935
- " %d %d" // exit_signal, processor
936
- " %u %u" // rt_priority, policy
937
- " %llu %" PRIu64 " %" PRId64 // delayacct_blkio_ticks, guest_time, cguest_time
938
- , &p->pid, name, &p->state
939
- , &p->ppid, &p->pgrp, &p->session, &p->tty_nr, &p->tpgid
940
- , &p->flags, &p->minflt, &p->cminflt, &p->majflt, &p->cmajflt
941
- , &p->utime, &p->stime, &p->cutime, &p->cstime
942
- , &p->priority, &p->nice
943
- , &p->num_threads
944
- , &p->itrealvalue
945
- , &p->starttime
946
- , &p->vsize
947
- , &p->rss
948
- , &p->rsslim, &p->starcode, &p->endcode, &p->startstack, &p->kstkesp, &p->kstkeip
949
- , &p->signal, &p->blocked, &p->sigignore, &p->sigcatch
950
- , &p->wchan, &p->nswap, &p->cnswap
951
- , &p->exit_signal, &p->processor
952
- , &p->rt_priority, &p->policy
953
- , &p->delayacct_blkio_ticks, &p->guest_time, &p->cguest_time
954
- );
955
- strncpy(p->comm, name, MAX_COMPARE_NAME);
956
- p->comm[MAX_COMPARE_NAME] = '\0';
1046
958
- if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: VALUES: %s utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->comm, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1047
+ // --------------------------------------------------------------------
1048
+ // /proc/<pid>/io
1049
960
- if(parsed < 39) {
961
- if(!count_errors++ || debug || (p->target && p->target->debug))
962
- error("apps.plugin: ERROR: file %s gave %d results (expected 44)\n", filename, parsed);
1050
+ if(read_proc_pid_io(p)) {
1051
+ error("Cannot process /proc/%d/io", pid);
1052
+ continue;
1053
}
1054
1055
+ // --------------------------------------------------------------------
1056
+ // link it
1057
+
1058
// check if it is target
1059
// we do this only once, the first time this pid is loaded
1060
if(p->new_entry) {
@@ -980,95 +1073,6 @@ int update_from_proc(void)
1073
}
1074
}
1075
983
- // just a few checks
984
- if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
985
-
986
-
987
- // --------------------------------------------------------------------
988
- // /proc/<pid>/statm
989
-
990
- snprintf(filename, FILENAME_MAX, "/proc/%s/statm", file->d_name);
991
- fd = open(filename, O_RDONLY);
992
- if(fd == -1) {
993
- if(errno != ENOENT && errno != ESRCH) {
994
- if(!count_errors++ || debug || (p->target && p->target->debug))
995
- error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
996
- }
997
- }
998
- else {
999
- file_counter++;
1000
- bytes = read(fd, buffer, PROC_BUFFER);
1001
- close(fd);
1002
-
1003
- if(bytes == -1) {
1004
- if(!count_errors++ || debug || (p->target && p->target->debug))
1005
- error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
1006
- }
1007
- else if(bytes > 10) {
1008
- buffer[bytes] = '\0';
1009
- if(debug || (p->target && p->target->debug))
1010
- fprintf(stderr, "apps.plugin: READ statm: %s", buffer);
1011
-
1012
- parsed = sscanf(buffer,
1013
- "%llu %llu %llu %llu %llu %llu %llu"
1014
- , &p->statm_size
1015
- , &p->statm_resident
1016
- , &p->statm_share
1017
- , &p->statm_text
1018
- , &p->statm_lib
1019
- , &p->statm_data
1020
- , &p->statm_dirty
1021
- );
1022
-
1023
- if(parsed < 7) {
1024
- if(!count_errors++ || debug || (p->target && p->target->debug))
1025
- error("apps.plugin: ERROR: file %s gave %d results (expected 7)", filename, parsed);
1026
- }
1027
- }
1028
- }
1029
-
1030
- // --------------------------------------------------------------------
1031
- // /proc/<pid>/io
1032
-
1033
- snprintf(filename, FILENAME_MAX, "/proc/%s/io", file->d_name);
1034
- fd = open(filename, O_RDONLY);
1035
- if(fd == -1) {
1036
- if(errno != ENOENT && errno != ESRCH) {
1037
- if(!count_errors++ || debug || (p->target && p->target->debug))
1038
- error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
1039
- }
1040
- }
1041
- else {
1042
- file_counter++;
1043
- bytes = read(fd, buffer, PROC_BUFFER);
1044
- close(fd);
1045
-
1046
- if(bytes == -1) {
1047
- if(!count_errors++ || debug || (p->target && p->target->debug))
1048
- error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
1049
- }
1050
- else if(bytes > 10) {
1051
- buffer[bytes] = '\0';
1052
- if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: READ io: %s", buffer);
1053
-
1054
- parsed = sscanf(buffer,
1055
- "rchar: %llu\nwchar: %llu\nsyscr: %llu\nsyscw: %llu\nread_bytes: %llu\nwrite_bytes: %llu\ncancelled_write_bytes: %llu"
1056
- , &p->io_logical_bytes_read
1057
- , &p->io_logical_bytes_written
1058
- , &p->io_read_calls
1059
- , &p->io_write_calls
1060
- , &p->io_storage_bytes_read
1061
- , &p->io_storage_bytes_written
1062
- , &p->io_cancelled_write_bytes
1063
- );
1064
-
1065
- if(parsed < 7) {
1066
- if(!count_errors++ || debug || (p->target && p->target->debug))
1067
- error("apps.plugin: ERROR: file %s gave %d results (expected 7)", filename, parsed);
1068
- }
1069
- }
1070
- }
1071
-
1076
// --------------------------------------------------------------------
1077
// /proc/<pid>/fd
1078