]> arthur.barton.de Git - netdata.git/blob - src/plugin_freebsd.c
Separate vm.stats.sys.v_forks module
[netdata.git] / src / plugin_freebsd.c
1 #include "common.h"
2
3 static struct freebsd_module {
4     const char *name;
5     const char *dim;
6
7     int enabled;
8
9     int (*func)(int update_every, usec_t dt);
10     usec_t duration;
11
12     RRDDIM *rd;
13
14 } freebsd_modules[] = {
15
16         { .name = "freebsd_old", .dim = "freebsd_old", .enabled = 1, .func = do_freebsd_sysctl_old },
17
18         // system metrics
19         { .name = "vm.loadavg", .dim = "loadavg", .enabled = 1, .func = do_vm_loadavg },
20         { .name = "vm.vmtotal", .dim = "vmtotal", .enabled = 1, .func = do_vm_vmtotal },
21         { .name = "kern.cp_time", .dim = "cp_time", .enabled = 1, .func = do_kern_cp_time },
22         { .name = "kern.cp_times", .dim = "cp_times", .enabled = 1, .func = do_kern_cp_times },
23         { .name = "hw.intrcnt", .dim = "hw_intr", .enabled = 1, .func = do_hw_intcnt },
24         { .name = "vm.stats.sys.v_intr", .dim = "dev_intr", .enabled = 1, .func = do_vm_stats_sys_v_intr },
25         { .name = "vm.stats.sys.v_soft", .dim = "soft_intr", .enabled = 1, .func = do_vm_stats_sys_v_soft },
26         { .name = "vm.stats.sys.v_swtch", .dim = "context_swtch", .enabled = 1, .func = do_vm_stats_sys_v_swtch },
27         { .name = "vm.stats.vm.v_forks", .dim = "forks", .enabled = 1, .func = do_vm_stats_sys_v_forks },
28
29         // the terminator of this array
30         { .name = NULL, .dim = NULL, .enabled = 0, .func = NULL }
31 };
32
33 void *freebsd_main(void *ptr) {
34     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
35
36     info("FREEBSD Plugin thread created with task id %d", gettid());
37
38     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
39         error("Cannot set pthread cancel type to DEFERRED.");
40
41     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
42         error("Cannot set pthread cancel state to ENABLE.");
43
44     int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
45
46     // initialize FreeBSD plugin
47     if (freebsd_plugin_init())
48         netdata_exit = 1;
49
50     // check the enabled status for each module
51     int i;
52     for(i = 0 ; freebsd_modules[i].name ;i++) {
53         struct freebsd_module *pm = &freebsd_modules[i];
54
55         pm->enabled = config_get_boolean("plugin:freebsd", pm->name, pm->enabled);
56         pm->duration = 0ULL;
57         pm->rd = NULL;
58     }
59
60     usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
61     heartbeat_t hb;
62     heartbeat_init(&hb);
63
64     for(;;) {
65         usec_t hb_dt = heartbeat_next(&hb, step);
66         usec_t duration = 0ULL;
67
68         if(unlikely(netdata_exit)) break;
69
70         // BEGIN -- the job to be done
71
72         for(i = 0 ; freebsd_modules[i].name ;i++) {
73             struct freebsd_module *pm = &freebsd_modules[i];
74             if(unlikely(!pm->enabled)) continue;
75
76             debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
77
78             pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
79             pm->duration = heartbeat_dt_usec(&hb) - duration;
80             duration += pm->duration;
81
82             if(unlikely(netdata_exit)) break;
83         }
84
85         // END -- the job is done
86
87         // --------------------------------------------------------------------
88
89         if(vdo_cpu_netdata) {
90             static RRDSET *st = NULL;
91
92             if(unlikely(!st)) {
93                 st = rrdset_find_bytype_localhost("netdata", "plugin_freebsd_modules");
94
95                 if(!st) {
96                     st = rrdset_create_localhost("netdata", "plugin_freebsd_modules", NULL, "freebsd", NULL
97                     , "NetData FreeBSD Plugin Modules Durations", "milliseconds/run", 132001
98                     , localhost->rrd_update_every, RRDSET_TYPE_STACKED);
99
100                     for(i = 0 ; freebsd_modules[i].name ;i++) {
101                         struct freebsd_module *pm = &freebsd_modules[i];
102                         if(unlikely(!pm->enabled)) continue;
103
104                         pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
105                     }
106                 }
107             }
108             else rrdset_next(st);
109
110             for(i = 0 ; freebsd_modules[i].name ;i++) {
111                 struct freebsd_module *pm = &freebsd_modules[i];
112                 if(unlikely(!pm->enabled)) continue;
113
114                 rrddim_set_by_pointer(st, pm->rd, pm->duration);
115             }
116             rrdset_done(st);
117
118             global_statistics_charts();
119             registry_statistics();
120         }
121     }
122
123     info("FREEBSD thread exiting");
124
125     static_thread->enabled = 0;
126     pthread_exit(NULL);
127     return NULL;
128 }