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