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