]> arthur.barton.de Git - netdata.git/blob - src/plugin_freebsd.c
4184039d518c6a03e241d1b7e9b717ce48d125c4
[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_intrcnt", .enabled = 1, .func = do_hw_intcnt },
24         { .name = "vm.stats.sys.v_intr", .dim = "dev_intrcnt", .enabled = 1, .func = do_vm_stats_sys_v_intr },
25
26         // the terminator of this array
27         { .name = NULL, .dim = NULL, .enabled = 0, .func = NULL }
28 };
29
30 void *freebsd_main(void *ptr) {
31     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
32
33     info("FREEBSD Plugin thread created with task id %d", gettid());
34
35     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
36         error("Cannot set pthread cancel type to DEFERRED.");
37
38     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
39         error("Cannot set pthread cancel state to ENABLE.");
40
41     int vdo_cpu_netdata = config_get_boolean("plugin:freebsd", "netdata server resources", 1);
42
43     // initialize FreeBSD plugin
44     if (freebsd_plugin_init())
45         netdata_exit = 1;
46
47     // check the enabled status for each module
48     int i;
49     for(i = 0 ; freebsd_modules[i].name ;i++) {
50         struct freebsd_module *pm = &freebsd_modules[i];
51
52         pm->enabled = config_get_boolean("plugin:freebsd", pm->name, pm->enabled);
53         pm->duration = 0ULL;
54         pm->rd = NULL;
55     }
56
57     usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
58     heartbeat_t hb;
59     heartbeat_init(&hb);
60
61     for(;;) {
62         usec_t hb_dt = heartbeat_next(&hb, step);
63         usec_t duration = 0ULL;
64
65         if(unlikely(netdata_exit)) break;
66
67         // BEGIN -- the job to be done
68
69         for(i = 0 ; freebsd_modules[i].name ;i++) {
70             struct freebsd_module *pm = &freebsd_modules[i];
71             if(unlikely(!pm->enabled)) continue;
72
73             debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
74
75             pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
76             pm->duration = heartbeat_dt_usec(&hb) - duration;
77             duration += pm->duration;
78
79             if(unlikely(netdata_exit)) break;
80         }
81
82         // END -- the job is done
83
84         // --------------------------------------------------------------------
85
86         if(vdo_cpu_netdata) {
87             static RRDSET *st = NULL;
88
89             if(unlikely(!st)) {
90                 st = rrdset_find_bytype_localhost("netdata", "plugin_freebsd_modules");
91
92                 if(!st) {
93                     st = rrdset_create_localhost("netdata", "plugin_freebsd_modules", NULL, "freebsd", NULL
94                     , "NetData FreeBSD Plugin Modules Durations", "milliseconds/run", 132001
95                     , localhost->rrd_update_every, RRDSET_TYPE_STACKED);
96
97                     for(i = 0 ; freebsd_modules[i].name ;i++) {
98                         struct freebsd_module *pm = &freebsd_modules[i];
99                         if(unlikely(!pm->enabled)) continue;
100
101                         pm->rd = rrddim_add(st, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
102                     }
103                 }
104             }
105             else rrdset_next(st);
106
107             for(i = 0 ; freebsd_modules[i].name ;i++) {
108                 struct freebsd_module *pm = &freebsd_modules[i];
109                 if(unlikely(!pm->enabled)) continue;
110
111                 rrddim_set_by_pointer(st, pm->rd, pm->duration);
112             }
113             rrdset_done(st);
114
115             global_statistics_charts();
116             registry_statistics();
117         }
118     }
119
120     info("FREEBSD thread exiting");
121
122     static_thread->enabled = 0;
123     pthread_exit(NULL);
124     return NULL;
125 }