4 This piece of code is heavily based upon some code in timecop's <timecop@japan.co.jp>
5 "BubbleMon dockapp 1.2" The FreeBSD code there is orignally by oleg dashevskii
6 <od@iclub.nsu.ru> and changed by Jonas Aaberg <cja@gmx.net to suit Sherman's aquarium.
8 Thanks goes out to bodnar istvan <bistvan@sliced.hu> for doing the FreeBSD testing for me!
13 #include <sys/dkstat.h>
14 #include <sys/vmmeter.h>
15 #include <sys/resource.h>
16 #include <sys/sysctl.h>
17 #include <vm/vm_param.h>
24 #define pagetob(size) ((size) << pageshift)
26 static kvm_t *kd = NULL;
27 static struct nlist nlst[] = {
34 static int previous_total=0, previous_load=0;
36 static u_int64_t oload = 0, ototal = 0;
37 static int firsttimes = 0, current = 0;
38 static int cpu_average_list[CPUSMOOTHNESS];
41 static int status_init_cpu_load_freebsd(void)
43 /* calculate page shift to convert pages into kilobytes */
44 int pagesize = getpagesize();
47 while (pagesize > 1) {
52 /* open kernel memory */
53 kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
56 puts("Could not open kernel virtual memory");
62 if (nlst[0].n_type == 0 || nlst[1].n_type == 0 || nlst[2].n_type == 0) {
63 puts("Error extracting symbols");
67 /* drop setgid & setuid (the latter should not be there really) */
71 if (geteuid() != getuid() || getegid() != getgid()) {
72 puts("Unable to drop privileges");
79 void status_init(void)
81 status_init_cpu_load_freebsd();
84 void status_exit(void)
86 /* I guess it should actually CPU be closed, but I don't know how.*/
92 /* Returns the current CPU load in percent */
96 unsigned long int cpu_time[CPUSTATES];
100 if (firsttimes == 0) {
101 for (i = 0; i < CPUSMOOTHNESS; i++)
102 cpu_average_list[i] = 0;
104 /* Wait until we have CPUSMOOTHNESS messures */
105 if (firsttimes != CPUSMOOTHNESS)
109 if (kvm_read(kd, nlst[0].n_value, &cpu_time, sizeof(cpu_time))
113 load = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_NICE];
114 total = load + cpu_time[CP_IDLE];
117 if(total!=previous_total)
118 cpu_average_list[current] = (100 * (load - previous_load)) / (total - previous_total);
120 cpu_average_list[current] = (load - previous_load);
123 if (current == CPUSMOOTHNESS)
126 previous_load = load;
127 previous_total = total;
129 if (firsttimes != CPUSMOOTHNESS)
134 for (i = 0; i < CPUSMOOTHNESS; i++)
135 cpuload += cpu_average_list[i];
136 return (cpuload / CPUSMOOTHNESS);
142 int status_sensors(int, int)
146 int status_swap(void)
150 int status_net(int type, int direction)
158 int status_disc(char *drive)