summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAl 'netstar' Poole <netstar@gmail.com>2017-02-15 15:48:09 -0600
committerStephen 'Okra' Houston <smhouston88@gmail.com>2017-02-15 15:53:29 -0600
commit784ebcb18710c147004f749033fec579bc2b80d4 (patch)
treeecf490d5854484d559c0ef0ff74345bd4a824f16
parent6becc2b1796bad6f09264821259dcb8402a03b85 (diff)
Sysinfo Gadgets: Add sysctl/bsd support to the remaining sysinfo gadgets.
These gadgets include cpumonitor, memusage, and netstatus
-rw-r--r--src/modules/Makefile_sysinfo.mk20
-rw-r--r--src/modules/sysinfo/cpumonitor/cpumonitor.c8
-rw-r--r--src/modules/sysinfo/cpumonitor/cpumonitor.h2
-rw-r--r--src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c140
-rw-r--r--src/modules/sysinfo/memusage/memusage.c6
-rw-r--r--src/modules/sysinfo/memusage/memusage.h8
-rw-r--r--src/modules/sysinfo/memusage/memusage_sysctl.c147
-rw-r--r--src/modules/sysinfo/netstatus/netstatus.c5
-rw-r--r--src/modules/sysinfo/netstatus/netstatus.h2
-rw-r--r--src/modules/sysinfo/netstatus/netstatus_sysctl.c203
10 files changed, 537 insertions, 4 deletions
diff --git a/src/modules/Makefile_sysinfo.mk b/src/modules/Makefile_sysinfo.mk
index ae2a95098..e4af9a10f 100644
--- a/src/modules/Makefile_sysinfo.mk
+++ b/src/modules/Makefile_sysinfo.mk
@@ -44,22 +44,34 @@ else
44if HAVE_OPENBSD 44if HAVE_OPENBSD
45src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \ 45src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \
46 src/modules/sysinfo/thermal/thermal_sysctl.c \ 46 src/modules/sysinfo/thermal/thermal_sysctl.c \
47 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c 47 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c \
48 src/modules/sysinfo/netstatus/netstatus_sysctl.c \
49 src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c \
50 src/modules/sysinfo/memusage/memusage_sysctl.c
48else 51else
49if HAVE_NETBSD 52if HAVE_NETBSD
50src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \ 53src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \
51 src/modules/sysinfo/thermal/thermal_sysctl.c \ 54 src/modules/sysinfo/thermal/thermal_sysctl.c \
52 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c 55 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c \
56 src/modules/sysinfo/netstatus/netstatus_sysctl.c \
57 src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c \
58 src/modules/sysinfo/memusage/memusage_sysctl.c
53else 59else
54if HAVE_DRAGONFLY 60if HAVE_DRAGONFLY
55src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \ 61src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \
56 src/modules/sysinfo/thermal/thermal_sysctl.c \ 62 src/modules/sysinfo/thermal/thermal_sysctl.c \
57 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c 63 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c \
64 src/modules/sysinfo/netstatus/netstatus_sysctl.c \
65 src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c \
66 src/modules/sysinfo/memusage/memusage_sysctl.c
58else 67else
59if HAVE_FREEBSD 68if HAVE_FREEBSD
60src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \ 69src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_sysctl.c \
61 src/modules/sysinfo/thermal/thermal_sysctl.c \ 70 src/modules/sysinfo/thermal/thermal_sysctl.c \
62 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c 71 src/modules/sysinfo/cpuclock/cpuclock_sysctl.c \
72 src/modules/sysinfo/netstatus/netstatus_sysctl.c \
73 src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c \
74 src/modules/sysinfo/memusage/memusage_sysctl.c
63else 75else
64src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_upower.c \ 76src_modules_sysinfo_module_la_SOURCES += src/modules/sysinfo/batman/batman_upower.c \
65 src/modules/sysinfo/thermal/thermal_sysctl.c \ 77 src/modules/sysinfo/thermal/thermal_sysctl.c \
diff --git a/src/modules/sysinfo/cpumonitor/cpumonitor.c b/src/modules/sysinfo/cpumonitor/cpumonitor.c
index beb7bc3f3..f0ea58673 100644
--- a/src/modules/sysinfo/cpumonitor/cpumonitor.c
+++ b/src/modules/sysinfo/cpumonitor/cpumonitor.c
@@ -160,7 +160,11 @@ _cpumonitor_cb_usage_check_main(void *data, Ecore_Thread *th)
160 for (;;) 160 for (;;)
161 { 161 {
162 if (ecore_thread_check(th)) break; 162 if (ecore_thread_check(th)) break;
163#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
164 _cpumonitor_sysctl_getusage(thc->inst);
165#else
163 _cpumonitor_proc_getusage(thc->inst); 166 _cpumonitor_proc_getusage(thc->inst);
167#endif
164 ecore_thread_feedback(th, NULL); 168 ecore_thread_feedback(th, NULL);
165 if (ecore_thread_check(th)) break; 169 if (ecore_thread_check(th)) break;
166 usleep((1000000.0 / 8.0) * (double)thc->interval); 170 usleep((1000000.0 / 8.0) * (double)thc->interval);
@@ -226,7 +230,11 @@ _cpumonitor_config_updated(Instance *inst)
226 { 230 {
227 thc->inst = inst; 231 thc->inst = inst;
228 thc->interval = inst->cfg->cpumonitor.poll_interval; 232 thc->interval = inst->cfg->cpumonitor.poll_interval;
233#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
234 thc->cores = _cpumonitor_sysctl_getcores();
235#else
229 thc->cores = _cpumonitor_proc_getcores(); 236 thc->cores = _cpumonitor_proc_getcores();
237#endif
230 for (i = 0; i < thc->cores; i++) 238 for (i = 0; i < thc->cores; i++)
231 { 239 {
232 core = E_NEW(CPU_Core, 1); 240 core = E_NEW(CPU_Core, 1);
diff --git a/src/modules/sysinfo/cpumonitor/cpumonitor.h b/src/modules/sysinfo/cpumonitor/cpumonitor.h
index ebbc00407..382114066 100644
--- a/src/modules/sysinfo/cpumonitor/cpumonitor.h
+++ b/src/modules/sysinfo/cpumonitor/cpumonitor.h
@@ -6,5 +6,7 @@
6void _cpumonitor_config_updated(Instance *inst); 6void _cpumonitor_config_updated(Instance *inst);
7int _cpumonitor_proc_getcores(void); 7int _cpumonitor_proc_getcores(void);
8void _cpumonitor_proc_getusage(Instance *inst); 8void _cpumonitor_proc_getusage(Instance *inst);
9int _cpumonitor_sysctl_getcores(void);
10void _cpumonitor_sysctl_getusage(Instance *inst);
9Evas_Object *cpumonitor_configure(Instance *inst); 11Evas_Object *cpumonitor_configure(Instance *inst);
10#endif 12#endif
diff --git a/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c b/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c
new file mode 100644
index 000000000..8c3a53c74
--- /dev/null
+++ b/src/modules/sysinfo/cpumonitor/cpumonitor_sysctl.c
@@ -0,0 +1,140 @@
1#include "cpumonitor.h"
2
3#if defined(__FreeBSD__) || defined (__DragonFly__)
4# include <sys/types.h>
5# include <sys/sysctl.h>
6# define CPU_STATES 5
7#endif
8
9#if defined(__OpenBSD__)
10# include <sys/sysctl.h>
11# define CPU_STATES 6
12#endif
13
14 int
15_cpumonitor_sysctl_getcores(void)
16{
17 int cores = 0;
18#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
19 size_t len;
20 int mib[2] = { CTL_HW, HW_NCPU };
21
22 len = sizeof(cores);
23 if (sysctl(mib, 2, &cores, &len, NULL, 0) < 0) return 0;
24#endif
25 return cores;
26}
27
28
29void
30_cpumonitor_sysctl_getusage(Instance *inst)
31{
32 CPU_Core *core;
33 size_t size;
34 unsigned long percent_all = 0, total_all = 0, idle_all = 0;
35 int i, j;
36#if defined(__FreeBSD__) || defined(__DragonFly__)
37 int ncpu = _cpumonitor_sysctl_getcores();
38 if (!ncpu) return;
39 size = sizeof(long) * (CPU_STATES * ncpu);
40 long cpu_times[ncpu][CPU_STATES];
41
42 if (sysctlbyname("kern.cp_times", cpu_times, &size, NULL, 0) < 0)
43 return;
44
45 for (i = 0; i < ncpu; i++)
46 {
47 long *cpu = cpu_times[i];
48 double total = 0;
49
50 for (j = 0; j < CPU_STATES; j++)
51 total += cpu[j];
52
53 double used = total - (cpu[4]); // + cpu[2] + cpu[1] + cpu[0]);
54 double ratio = (float) total / 100.0;
55
56 double percent = ((double)used / (double)ratio);
57 if (percent > 100)
58 percent = 100;
59 else if (percent < 0)
60 percent = 0;
61
62 core = eina_list_nth(inst->cfg->cpumonitor.cores, i);
63 core->percent = (int) percent;
64 core->total = total;
65 core->idle = cpu[4];
66
67 percent_all += (int) percent;
68 total_all += total;
69 idle_all += core->idle;
70 }
71 inst->cfg->cpumonitor.total = total_all / ncpu;
72 inst->cfg->cpumonitor.idle = idle_all / ncpu;
73 inst->cfg->cpumonitor.percent = (int) (percent_all / ncpu);
74#elif defined(__OpenBSD__)
75 int ncpu = _cpumonitor_sysctl_getcores();
76 if (!ncpu) return;
77 if (ncpu == 1)
78 {
79 long cpu_times[CPU_STATES];
80 int cpu_time_mib[] = { CTL_KERN, KERN_CPTIME };
81 size = CPU_STATES * sizeof(long);
82 if (sysctl(cpu_time_mib, 2, &cpu_times, &size, NULL, 0) < 0)
83 return;
84
85 long total = 0;
86 for (j = 0; j < CPU_STATES; j++)
87 total += cpu_times[j];
88
89 //user", "nice", "system", "interrupt", "idle", NULL
90 long used = total - cpu_times[0] + cpu_times[1] + cpu_times[2] + cpu_times[3];
91 double ratio = (double) total / 100.0;
92 double percent = 100.0 - ((double) used / (double) ratio);
93 if (percent > 100)
94 percent = 100;
95 else if (percent < 0)
96 percent = 0;
97
98 inst->cfg->cpumonitor.total = total;
99 inst->cfg->cpumonitor.idle = cpu_times[3];
100 inst->cfg->cpumonitor.percent = (int) percent;
101 }
102 else if (ncpu > 1)
103 {
104 for (i = 0; i < ncpu; i++)
105 {
106 long cpu_times[CPU_STATES];
107 size = CPU_STATES * sizeof(long);
108 int cpu_time_mib[] = { CTL_KERN, KERN_CPTIME2, 0 };
109 cpu_time_mib[2] = i;
110 if (sysctl(cpu_time_mib, 3, &cpu_times, &size, NULL, 0) < 0)
111 return;
112
113 long total = 0;
114 for (j = 0; j < CPU_STATES - 1; j++)
115 total += cpu_times[j];
116
117 long used = total - (cpu_times[4]);
118 double ratio = (float) total / 100.0;
119 int percent = used / ratio;
120 if (percent > 100)
121 percent = 100;
122 else if (percent < 0)
123 percent = 0;
124 core = eina_list_nth(inst->cfg->cpumonitor.cores, i);
125 core->percent = (int) percent;
126 core->total = total;
127 core->idle = cpu_times[4];
128
129 percent_all += (int) percent;
130 total_all += total;
131 idle_all += core->idle;
132 }
133
134 inst->cfg->cpumonitor.total = total_all / ncpu;
135 inst->cfg->cpumonitor.idle = idle_all / ncpu;
136 inst->cfg->cpumonitor.percent = (percent_all / ncpu) + (percent_all % ncpu);
137 }
138#endif
139}
140
diff --git a/src/modules/sysinfo/memusage/memusage.c b/src/modules/sysinfo/memusage/memusage.c
index 0260d4d7f..ef44cffa6 100644
--- a/src/modules/sysinfo/memusage/memusage.c
+++ b/src/modules/sysinfo/memusage/memusage.c
@@ -275,9 +275,15 @@ _memusage_cb_usage_check_main(void *data, Ecore_Thread *th)
275 for (;;) 275 for (;;)
276 { 276 {
277 if (ecore_thread_check(th)) break; 277 if (ecore_thread_check(th)) break;
278#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
279 _memusage_sysctl_getusage(&thc->mem_total, &thc->mem_used,
280 &thc->mem_cached, &thc->mem_buffers, &thc->mem_shared,
281 &thc->swp_total, &thc->swp_used);
282#else
278 _memusage_proc_getusage(&thc->mem_total, &thc->mem_used, 283 _memusage_proc_getusage(&thc->mem_total, &thc->mem_used,
279 &thc->mem_cached, &thc->mem_buffers, &thc->mem_shared, 284 &thc->mem_cached, &thc->mem_buffers, &thc->mem_shared,
280 &thc->swp_total, &thc->swp_used); 285 &thc->swp_total, &thc->swp_used);
286#endif
281 if (thc->mem_total > 0) 287 if (thc->mem_total > 0)
282 thc->mem_percent = 100 * ((float)thc->mem_used / (float)thc->mem_total); 288 thc->mem_percent = 100 * ((float)thc->mem_used / (float)thc->mem_total);
283 if (thc->swp_total > 0) 289 if (thc->swp_total > 0)
diff --git a/src/modules/sysinfo/memusage/memusage.h b/src/modules/sysinfo/memusage/memusage.h
index 7788cf229..4088daa21 100644
--- a/src/modules/sysinfo/memusage/memusage.h
+++ b/src/modules/sysinfo/memusage/memusage.h
@@ -14,4 +14,12 @@ void _memusage_proc_getusage(unsigned long *mem_total,
14 unsigned long *swp_total, 14 unsigned long *swp_total,
15 unsigned long *swp_used); 15 unsigned long *swp_used);
16 16
17void _memusage_sysctl_getusage(unsigned long *mem_total,
18 unsigned long *mem_used,
19 unsigned long *mem_cached,
20 unsigned long *mem_buffers,
21 unsigned long *mem_shared,
22 unsigned long *swp_total,
23 unsigned long *swp_used);
24
17#endif 25#endif
diff --git a/src/modules/sysinfo/memusage/memusage_sysctl.c b/src/modules/sysinfo/memusage/memusage_sysctl.c
new file mode 100644
index 000000000..1694861d5
--- /dev/null
+++ b/src/modules/sysinfo/memusage/memusage_sysctl.c
@@ -0,0 +1,147 @@
1#include "memusage.h"
2
3#if defined(__FreeBSD__) || defined(__DragonFly__)
4# include <sys/types.h>
5# include <sys/sysctl.h>
6# include <vm/vm_param.h>
7#endif
8
9#if defined(__OpenBSD__)
10# include <sys/param.h>
11# include <sys/sysctl.h>
12# include <sys/swap.h>
13# include <sys/mount.h>
14#endif
15
16#if defined(__FreeBSD__) || defined(__DragonFly__)
17static long int
18_sysctlfromname(const char *name, void *mib, int depth, size_t *len)
19{
20 long int result;
21
22 if (sysctlnametomib(name, mib, len) < 0) return -1;
23
24 *len = sizeof(result);
25 if (sysctl(mib, depth, &result, len, NULL, 0) < 0) return -1;
26
27 return result;
28}
29#endif
30
31void _memusage_sysctl_getusage(unsigned long *mem_total,
32 unsigned long *mem_used,
33 unsigned long *mem_cached,
34 unsigned long *mem_buffers,
35 unsigned long *mem_shared,
36 unsigned long *swp_total,
37 unsigned long *swp_used)
38{
39 size_t len;
40 int i = 0;
41 *mem_total = *mem_used = *mem_cached = *mem_buffers = *mem_shared = 0;
42 *swp_total = *swp_used = 0;
43#if defined(__FreeBSD__) || defined(__DragonFly__)
44 int total_pages = 0, free_pages = 0, inactive_pages = 0;
45 long int result = 0;
46 int page_size = getpagesize();
47
48 int *mib = malloc(sizeof(int) * 4);
49 if (mib == NULL) return;
50
51 mib[0] = CTL_HW; mib[1] = HW_PHYSMEM;
52 len = sizeof(*mem_total);
53 if (sysctl(mib, 2, mem_total, &len, NULL, 0) == -1)
54 return;
55 *mem_total /= 1024;
56
57 total_pages = _sysctlfromname("vm.stats.vm.v_page_count", mib, 4, &len);
58 if (total_pages < 0) return;
59
60 free_pages = _sysctlfromname("vm.stats.vm.v_free_count", mib, 4, &len);
61 if (free_pages < 0) return;
62
63 inactive_pages = _sysctlfromname("vm.stats.vm.v_inactive_count", mib, 4, &len);
64 if (inactive_pages < 0) return;
65
66 *mem_used = (total_pages - free_pages - inactive_pages) * page_size >> 10;
67
68 result = _sysctlfromname("vfs.bufspace", mib, 2, &len);
69 if (result < 0) return;
70 *mem_buffers = (result / 1024);
71
72 result = _sysctlfromname("vm.stats.vm.v_active_count", mib, 4, &len);
73 if (result < 0) return;
74 *mem_cached = ((result * page_size) / 1024);
75
76 result = _sysctlfromname("vm.stats.vm.v_cache_count", mib, 4, &len);
77 if (result < 0) return;
78 *mem_shared = (result * page_size) / 1024;
79
80 result = _sysctlfromname("vm.swap_total", mib, 2, &len);
81 if (result < 0) return;
82 *swp_total = (result / 1024);
83
84 struct xswdev xsw;
85 // previous mib is important for this one...
86
87 while (i++)
88 {
89 mib[2] = i;
90 len = sizeof(xsw);
91 if (sysctl(mib, 3, &xsw, &len, NULL, 0) == -1) break;
92
93 *swp_used += xsw.xsw_used * page_size;
94 }
95
96 *swp_used >>= 10;
97
98 free(mib);
99#elif defined(__OpenBSD__)
100 static int mib[] = { CTL_HW, HW_PHYSMEM64 };
101 static int bcstats_mib[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT };
102 struct bcachestats bcstats;
103 static int uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
104 struct uvmexp uvmexp;
105 int nswap, rnswap;
106 struct swapent *swdev = NULL;
107
108 len = sizeof(*mem_total);
109 if (sysctl(mib, 2, mem_total, &len, NULL, 0) == -1)
110 return;
111
112 len = sizeof(uvmexp);
113 if (sysctl(uvmexp_mib, 2, &uvmexp, &len, NULL, 0) == -1)
114 return;
115
116 len = sizeof(bcstats);
117 if (sysctl(bcstats_mib, 3, &bcstats, &len, NULL, 0) == -1)
118 return;
119
120 // Don't fail if there's not swap!
121 nswap = swapctl(SWAP_NSWAP, 0, 0);
122 if (nswap == 0) goto swap_out;
123
124 swdev = calloc(nswap, sizeof(*swdev));
125 if (swdev == NULL) goto swap_out;
126
127 rnswap = swapctl(SWAP_STATS, swdev, nswap);
128 if (rnswap == -1) goto swap_out;
129
130 for (i = 0; i < nswap; i++)// nswap; i++)
131 {
132 if (swdev[i].se_flags & SWF_ENABLE)
133 {
134 *swp_used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
135 *swp_total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
136 }
137 }
138swap_out:
139 if (swdev) free(swdev);
140
141 *mem_total /= 1024;
142 *mem_cached = (uvmexp.pagesize * bcstats.numbufpages) / 1024;
143 *mem_used = (uvmexp.active * uvmexp.pagesize) / 1024 ;
144 *mem_buffers = (uvmexp.pagesize * (uvmexp.npages - uvmexp.free)) / 1024;
145 *mem_shared = (uvmexp.pagesize * uvmexp.wired) / 1024;
146#endif
147}
diff --git a/src/modules/sysinfo/netstatus/netstatus.c b/src/modules/sysinfo/netstatus/netstatus.c
index b5ebf836d..ce1494bb1 100644
--- a/src/modules/sysinfo/netstatus/netstatus.c
+++ b/src/modules/sysinfo/netstatus/netstatus.c
@@ -128,8 +128,13 @@ _netstatus_cb_usage_check_main(void *data, Ecore_Thread *th)
128 for (;;) 128 for (;;)
129 { 129 {
130 if (ecore_thread_check(th)) break; 130 if (ecore_thread_check(th)) break;
131#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
132 _netstatus_sysctl_getrstatus(thc->inst);
133 _netstatus_sysctl_gettstatus(thc->inst);
134#else
131 _netstatus_proc_getrstatus(thc->inst); 135 _netstatus_proc_getrstatus(thc->inst);
132 _netstatus_proc_gettstatus(thc->inst); 136 _netstatus_proc_gettstatus(thc->inst);
137#endif
133 ecore_thread_feedback(th, NULL); 138 ecore_thread_feedback(th, NULL);
134 if (ecore_thread_check(th)) break; 139 if (ecore_thread_check(th)) break;
135 usleep((1000000.0 / 8.0) * (double)thc->interval); 140 usleep((1000000.0 / 8.0) * (double)thc->interval);
diff --git a/src/modules/sysinfo/netstatus/netstatus.h b/src/modules/sysinfo/netstatus/netstatus.h
index 5f1772ed8..3b21ec4f3 100644
--- a/src/modules/sysinfo/netstatus/netstatus.h
+++ b/src/modules/sysinfo/netstatus/netstatus.h
@@ -19,5 +19,7 @@ struct _Netstatus_Config
19void _netstatus_config_updated(Instance *inst); 19void _netstatus_config_updated(Instance *inst);
20void _netstatus_proc_getrstatus(Instance *inst); 20void _netstatus_proc_getrstatus(Instance *inst);
21void _netstatus_proc_gettstatus(Instance *inst); 21void _netstatus_proc_gettstatus(Instance *inst);
22void _netstatus_sysctl_getrstatus(Instance *inst);
23void _netstatus_sysctl_gettstatus(Instance *inst);
22Evas_Object *netstatus_configure(Instance *inst); 24Evas_Object *netstatus_configure(Instance *inst);
23#endif 25#endif
diff --git a/src/modules/sysinfo/netstatus/netstatus_sysctl.c b/src/modules/sysinfo/netstatus/netstatus_sysctl.c
new file mode 100644
index 000000000..e37b8af95
--- /dev/null
+++ b/src/modules/sysinfo/netstatus/netstatus_sysctl.c
@@ -0,0 +1,203 @@
1#include "netstatus.h"
2#if defined(__FreeBSD__) || defined(__DragonFly__)
3# include <sys/socket.h>
4# include <sys/sysctl.h>
5# include <net/if.h>
6# include <net/if_mib.h>
7#endif
8
9#if defined(__OpenBSD__)
10# include <sys/types.h>
11# include <sys/socket.h>
12# include <sys/sysctl.h>
13# include <sys/sockio.h>
14# include <sys/ioctl.h>
15# include <net/if.h>
16# include <net/if_types.h>
17# include <ifaddrs.h>
18#endif
19
20#if defined(__FreeBSD__) || defined(__DragonFly__)
21
22static int
23get_ifmib_general(int row, struct ifmibdata *data)
24{
25 int mib[6];
26 size_t len;
27
28 mib[0] = CTL_NET;
29 mib[1] = PF_LINK;
30 mib[2] = NETLINK_GENERIC;
31 mib[3] = IFMIB_IFDATA;
32 mib[4] = row;
33 mib[5] = IFDATA_GENERAL;
34
35 len = sizeof(*data);
36
37 return sysctl(mib, 6, data, &len, NULL, 0);
38}
39
40static void
41_freebsd_generic_network_status(unsigned long int *in, unsigned long int *out)
42{
43 struct ifmibdata *ifmd;
44 size_t len;
45 int i, count;
46
47 len = sizeof(count);
48 if (sysctlbyname("net.link.generic.system.ifcount", &count, &len, NULL, 0) < 0)
49 return;
50
51 ifmd = malloc(sizeof(struct ifmibdata));
52 if (!ifmd)
53 return;
54
55 for (i = 1; i <= count; i++)
56 {
57 get_ifmib_general(i, ifmd);
58 *in += ifmd->ifmd_data.ifi_ibytes;
59 *out += ifmd->ifmd_data.ifi_obytes;
60 }
61
62 free(ifmd);
63}
64
65#endif
66
67#if defined(__OpenBSD__)
68static void
69_openbsd_generic_network_status(unsigned long int *in, unsigned long int *out)
70{
71 struct ifaddrs *interfaces, *ifa;
72
73 if (getifaddrs(&interfaces) < 0)
74 return;
75
76 int sock = socket(AF_INET, SOCK_STREAM, 0);
77 if (sock < 0)
78 return;
79
80 for (ifa = interfaces; ifa; ifa = ifa->ifa_next)
81 {
82 struct ifreq ifreq;
83 struct if_data if_data;
84
85 ifreq.ifr_data = (void *)&if_data;
86 strncpy(ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ-1);
87 if (ioctl(sock, SIOCGIFDATA, &ifreq) < 0)
88 return;
89
90 struct if_data * const ifi = &if_data;
91 if (ifi->ifi_type == IFT_ETHER || ifi->ifi_type == IFT_IEEE80211)
92 {
93 if (ifi->ifi_ibytes)
94 *in += ifi->ifi_ibytes;
95 else
96 *in += 0;
97
98 if (ifi->ifi_obytes)
99 *out += ifi->ifi_obytes;
100 else
101 *out += 0;
102 }
103 }
104 close(sock);
105}
106#endif
107
108void
109_netstatus_sysctl_getrstatus(Instance *inst)
110{
111 char rin[256];
112 long tot_in = 0, diffin;
113 int percent = 0;
114 unsigned long int incoming = 0, outgoing = 0;
115#if defined(__OpenBSD__)
116 _openbsd_generic_network_status(&incoming, &outgoing);
117#elif defined(__FreeBSD__) || defined(__DragonFly__)
118 _freebsd_generic_network_status(&incoming, &outgoing);
119#endif
120
121 tot_in = incoming;
122 diffin = tot_in - inst->cfg->netstatus.in;
123 if (!inst->cfg->netstatus.in)
124 inst->cfg->netstatus.in = tot_in;
125 else
126 {
127 inst->cfg->netstatus.in = tot_in;
128 if (inst->cfg->netstatus.automax)
129 {
130 if (diffin > inst->cfg->netstatus.inmax)
131 inst->cfg->netstatus.inmax = diffin;
132 }
133 inst->cfg->netstatus.incurrent = diffin;
134 if (inst->cfg->netstatus.inmax > 0)
135 percent = 100 * ((float)inst->cfg->netstatus.incurrent / (float)inst->cfg->netstatus.inmax);
136 if (percent > 100) percent = 100;
137 else if (percent < 0) percent = 0;
138 inst->cfg->netstatus.inpercent = percent;
139 }
140 if (!diffin)
141 {
142 snprintf(rin, sizeof(rin), "%s: 0 B/s", _("Receiving"));
143 }
144 else
145 {
146 if (diffin > 1048576)
147 snprintf(rin, sizeof(rin), "%s: %.2f MB/s", _("Receiving"), ((float)diffin / 1048576));
148 else if ((diffin > 1024) && (diffin < 1048576))
149 snprintf(rin, sizeof(rin), "%s: %lu KB/s", _("Receiving"), (diffin / 1024));
150 else
151 snprintf(rin, sizeof(rin), "%s: %lu B/s", _("Receiving"), diffin);
152 }
153 eina_stringshare_replace(&inst->cfg->netstatus.instring, rin);
154}
155
156void
157_netstatus_sysctl_gettstatus(Instance *inst)
158{
159 char rout[256];
160 long tot_out = 0, diffout;
161 int percent = 0;
162 unsigned long int incoming = 0, outgoing = 0;
163#if defined(__OpenBSD__)
164 _openbsd_generic_network_status(&incoming, &outgoing);
165#elif defined(__FreeBSD__) || defined(__DragonFly__)
166 _freebsd_generic_network_status(&incoming, &outgoing);
167#endif
168 tot_out = outgoing;
169
170 diffout = tot_out - inst->cfg->netstatus.out;
171 if (!inst->cfg->netstatus.out)
172 inst->cfg->netstatus.out = tot_out;
173 else
174 {
175 inst->cfg->netstatus.out = tot_out;
176 if (inst->cfg->netstatus.automax)
177 {
178 if (diffout > inst->cfg->netstatus.outmax)
179 inst->cfg->netstatus.outmax = diffout;
180 }
181 inst->cfg->netstatus.outcurrent = diffout;
182 if (inst->cfg->netstatus.outcurrent > 0)
183 percent = 100 * ((float)inst->cfg->netstatus.outcurrent / (float)inst->cfg->netstatus.outmax);
184 if (percent > 100) percent = 100;
185 else if (percent < 0) percent = 0;
186 inst->cfg->netstatus.outpercent = percent;
187 }
188 if (!diffout)
189 {
190 snprintf(rout, sizeof(rout), "%s: 0 B/s", _("Sending"));
191 }
192 else
193 {
194 if (diffout > 1048576)
195 snprintf(rout, sizeof(rout), "%s: %.2f MB/s", _("Sending"), ((float)diffout / 1048576));
196 else if ((diffout > 1024) && (diffout < 1048576))
197 snprintf(rout, sizeof(rout), "%s: %lu KB/s", _("Sending"), (diffout / 1024));
198 else
199 snprintf(rout, sizeof(rout), "%s: %lu B/s", _("Sending"), diffout);
200 }
201 eina_stringshare_replace(&inst->cfg->netstatus.outstring, rout);
202}
203