${top} fixes for FreeBSD
[monky] / src / freebsd.c
index 20c4e27..a8cd8b1 100644 (file)
@@ -1,4 +1,7 @@
-/* Conky, a system monitor, based on torsmo
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * Conky, a system monitor, based on torsmo
  *
  * Any original torsmo code is licensed under the BSD license
  *
@@ -6,7 +9,7 @@
  *
  * Please see COPYING for details
  *
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
@@ -51,6 +54,7 @@
 #include "conky.h"
 #include "freebsd.h"
 #include "logging.h"
+#include "net_stat.h"
 #include "top.h"
 #include "diskio.h"
 
 #define        FREEBSD_DEBUG
 #endif
 
-inline void proc_find_top(struct process **cpu, struct process **mem);
+__attribute__((gnu_inline)) inline void
+proc_find_top(struct process **cpu, struct process **mem, struct process **time);
 
 static short cpu_setup = 0;
 
-static int getsysctl(char *name, void *ptr, size_t len)
+static int getsysctl(const char *name, void *ptr, size_t len)
 {
        size_t nlen = len;
 
@@ -109,11 +114,11 @@ static int swapmode(unsigned long *retavail, unsigned long *retfree)
        return n;
 }
 
-void prepare_update()
+void prepare_update(void)
 {
 }
 
-void update_uptime()
+int update_uptime(void)
 {
        int mib[2] = { CTL_KERN, KERN_BOOTTIME };
        struct timeval boottime;
@@ -128,6 +133,8 @@ void update_uptime()
                fprintf(stderr, "Could not get uptime\n");
                info.uptime = 0;
        }
+
+       return 0;
 }
 
 int check_mount(char *s)
@@ -145,7 +152,7 @@ int check_mount(char *s)
        return 0;
 }
 
-void update_meminfo()
+int update_meminfo(void)
 {
        u_int total_pages, inactive_pages, free_pages;
        unsigned long swap_avail, swap_free;
@@ -171,13 +178,17 @@ void update_meminfo()
        if ((swapmode(&swap_avail, &swap_free)) >= 0) {
                info.swapmax = swap_avail;
                info.swap = (swap_avail - swap_free);
+               info.swapfree = swap_free;
        } else {
                info.swapmax = 0;
                info.swap = 0;
+               info.swapfree = 0;
        }
+
+       return 0;
 }
 
-void update_net_stats()
+int update_net_stats(void)
 {
        struct net_stat *ns;
        double delta;
@@ -188,15 +199,15 @@ void update_net_stats()
        /* get delta */
        delta = current_update_time - last_update_time;
        if (delta <= 0.0001) {
-               return;
+               return 0;
        }
 
        if (getifaddrs(&ifap) < 0) {
-               return;
+               return 0;
        }
 
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-               ns = get_net_stat((const char *) ifa->ifa_name);
+               ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
 
                if (ifa->ifa_flags & IFF_UP) {
                        struct ifaddrs *iftmp;
@@ -248,18 +259,20 @@ void update_net_stats()
        }
 
        freeifaddrs(ifap);
+       return 0;
 }
 
-void update_total_processes()
+int update_total_processes(void)
 {
        int n_processes;
 
        kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
 
        info.procs = n_processes;
+       return 0;
 }
 
-void update_running_processes()
+int update_running_processes(void)
 {
        struct kinfo_proc *p;
        int n_processes;
@@ -277,42 +290,41 @@ void update_running_processes()
        }
 
        info.run_procs = cnt;
+       return 0;
 }
 
-struct cpu_load_struct {
-       unsigned long load[5];
-};
-
-struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
-long cpu_used, oldtotal, oldused;
-
-void get_cpu_count()
+void get_cpu_count(void)
 {
-       /* int cpu_count = 0; */
+       int cpu_count = 0;
+       size_t cpu_count_len = sizeof(cpu_count);
 
-       /* XXX: FreeBSD doesn't allow to get per CPU load stats on SMP machines.
-        * It's possible to get a CPU count, but as we fulfill only
-        * info.cpu_usage[0], it's better to report there's only one CPU.
-        * It should fix some bugs (e.g. cpugraph) */
-#if 0
        if (GETSYSCTL("hw.ncpu", cpu_count) == 0) {
                info.cpu_count = cpu_count;
+       } else {
+               fprintf(stderr, "Cannot get hw.ncpu\n");
+               info.cpu_count = 0;
        }
-#endif
-       info.cpu_count = 1;
 
-       info.cpu_usage = malloc(info.cpu_count * sizeof(float));
+       info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
        if (info.cpu_usage == NULL) {
-               CRIT_ERR("malloc");
+               CRIT_ERR(NULL, NULL, "malloc");
        }
 }
 
-/* XXX: SMP support */
-void update_cpu_usage()
+struct cpu_info {
+       long oldtotal;
+       long oldused;
+};
+
+int update_cpu_usage(void)
 {
+       int i, j = 0;
        long used, total;
-       long cp_time[CPUSTATES];
-       size_t len = sizeof(cp_time);
+       long *cp_time = NULL;
+       size_t cp_len;
+       static struct cpu_info *cpu = NULL;
+       unsigned int malloc_cpu_size = 0;
+       extern void* global_cpu;
 
        /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
        if ((cpu_setup == 0) || (!info.cpu_usage)) {
@@ -320,31 +332,72 @@ void update_cpu_usage()
                cpu_setup = 1;
        }
 
-       if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
-               fprintf(stderr, "Cannot get kern.cp_time");
+       if (!global_cpu) {
+               malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
+               cpu = malloc(malloc_cpu_size);
+               memset(cpu, 0, malloc_cpu_size);
+               global_cpu = cpu;
+       }
+
+       /* cpu[0] is overall stats, get it from separate sysctl */
+       cp_len = CPUSTATES * sizeof(long);
+       cp_time = malloc(cp_len);
+
+       if (sysctlbyname("kern.cp_time", cp_time, &cp_len, NULL, 0) < 0) {
+               fprintf(stderr, "Cannot get kern.cp_time\n");
        }
 
-       fresh.load[0] = cp_time[CP_USER];
-       fresh.load[1] = cp_time[CP_NICE];
-       fresh.load[2] = cp_time[CP_SYS];
-       fresh.load[3] = cp_time[CP_IDLE];
-       fresh.load[4] = cp_time[CP_IDLE];
+       total = 0;
+       for (j = 0; j < CPUSTATES; j++)
+               total += cp_time[j];
 
-       used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-       total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
+       used = total - cp_time[CP_IDLE];
 
-       if ((total - oldtotal) != 0) {
-               info.cpu_usage[0] = ((double) (used - oldused)) /
-                       (double) (total - oldtotal);
+       if ((total - cpu[0].oldtotal) != 0) {
+               info.cpu_usage[0] = ((double) (used - cpu[0].oldused)) /
+               (double) (total - cpu[0].oldtotal);
        } else {
                info.cpu_usage[0] = 0;
        }
 
-       oldused = used;
-       oldtotal = total;
+       cpu[0].oldused = used;
+       cpu[0].oldtotal = total;
+
+       free(cp_time);
+
+       /* per-core stats */
+       cp_len = CPUSTATES * sizeof(long) * info.cpu_count;
+       cp_time = malloc(cp_len);
+
+       /* on e.g. i386 SMP we may have more values than actual cpus; this will just drop extra values */
+       if (sysctlbyname("kern.cp_times", cp_time, &cp_len, NULL, 0) < 0 && errno != ENOMEM) {
+               fprintf(stderr, "Cannot get kern.cp_times\n");
+       }
+
+       for (i = 0; i < info.cpu_count; i++)
+       {
+               total = 0;
+               for (j = 0; j < CPUSTATES; j++)
+                       total += cp_time[i*CPUSTATES + j];
+
+               used = total - cp_time[i*CPUSTATES + CP_IDLE];
+
+               if ((total - cpu[i+1].oldtotal) != 0) {
+                       info.cpu_usage[i+1] = ((double) (used - cpu[i+1].oldused)) /
+                       (double) (total - cpu[i+1].oldtotal);
+               } else {
+                       info.cpu_usage[i+1] = 0;
+               }
+
+               cpu[i+1].oldused = used;
+               cpu[i+1].oldtotal = total;
+       }
+
+       free(cp_time);
+       return 0;
 }
 
-void update_load_average()
+int update_load_average(void)
 {
        double v[3];
 
@@ -353,11 +406,14 @@ void update_load_average()
        info.loadavg[0] = (double) v[0];
        info.loadavg[1] = (double) v[1];
        info.loadavg[2] = (double) v[2];
+
+       return 0;
 }
 
 double get_acpi_temperature(int fd)
 {
        int temp;
+       (void)fd;
 
        if (GETSYSCTL("hw.acpi.thermal.tz0.temperature", temp)) {
                fprintf(stderr,
@@ -386,6 +442,7 @@ static void get_battery_stats(int *battime, int *batcapacity, int *batstate, int
 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 {
        int battime, batcapacity, batstate, ac;
+       (void)bat;
 
        get_battery_stats(&battime, &batcapacity, &batstate, &ac);
 
@@ -466,14 +523,17 @@ int get_battery_perct_bar(const char *bar)
 
 int open_acpi_temperature(const char *name)
 {
+       (void)name;
        /* Not applicable for FreeBSD. */
        return 0;
 }
 
-void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
+void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
 {
        int state;
 
+       (void) adapter; // only linux uses this
+
        if (!p_client_buffer || client_buffer_size <= 0) {
                return;
        }
@@ -498,65 +558,6 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
        }
 }
 
-void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
-{
-       /* not implemented */
-       if (p_client_buffer && client_buffer_size > 0) {
-               memset(p_client_buffer, 0, client_buffer_size);
-       }
-}
-
-void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
-{
-       /* not implemented */
-       if (p_client_buffer && client_buffer_size > 0) {
-               memset(p_client_buffer, 0, client_buffer_size);
-       }
-}
-
-/* rdtsc() and get_freq_dynamic() copied from linux.c */
-
-#if  defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc()
-{
-       unsigned long long int x;
-
-       __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
-       return x;
-}
-#endif
-
-/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
-void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
-               const char *p_format, int divisor)
-{
-#if  defined(__i386) || defined(__x86_64)
-       struct timezone tz;
-       struct timeval tvstart, tvstop;
-       unsigned long long cycles[2];   /* gotta be 64 bit */
-       unsigned int microseconds;      /* total time taken */
-
-       memset(&tz, 0, sizeof(tz));
-
-       /* get this function in cached memory */
-       gettimeofday(&tvstart, &tz);
-       cycles[0] = rdtsc();
-       gettimeofday(&tvstart, &tz);
-
-       /* we don't trust that this is any specific length of time */
-       usleep(100);
-       cycles[1] = rdtsc();
-       gettimeofday(&tvstop, &tz);
-       microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
-               (tvstop.tv_usec - tvstart.tv_usec);
-
-       snprintf(p_client_buffer, client_buffer_size, p_format,
-               (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
-#else
-       get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
-#endif
-}
-
 /* void */
 char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format,
                int divisor, unsigned int cpu)
@@ -587,13 +588,14 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_fo
        return 1;
 }
 
-void update_top()
+int update_top(void)
 {
-       proc_find_top(info.cpu, info.memu);
+       proc_find_top(info.cpu, info.memu, info.time);
+       return 0;
 }
 
 #if 0
-void update_wifi_stats()
+void update_wifi_stats(void)
 {
        struct ifreq ifr;               /* interface stats */
        struct wi_req wireq;
@@ -608,7 +610,7 @@ void update_wifi_stats()
        }
 
        for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-               ns = get_net_stat((const char *) ifa->ifa_name);
+               ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
 
                s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
@@ -647,7 +649,7 @@ cleanup:
 }
 #endif
 
-void update_diskio()
+int update_diskio(void)
 {
        int devs_count, num_selected, num_selections, dn;
        struct device_selection *dev_select = NULL;
@@ -665,7 +667,7 @@ void update_diskio()
 
        if (devstat_getdevs(NULL, &statinfo_cur) < 0) {
                free(statinfo_cur.dinfo);
-               return;
+               return 0;
        }
 
        devs_count = statinfo_cur.dinfo->numdevs;
@@ -697,15 +699,16 @@ void update_diskio()
        }
 
        free(statinfo_cur.dinfo);
+       return 0;
 }
 
 /* While topless is obviously better, top is also not bad. */
 
 int comparecpu(const void *a, const void *b)
 {
-       if (((struct process *)a)->amount > ((struct process *)b)->amount) {
+       if (((const struct process *)a)->amount > ((const struct process *)b)->amount) {
                return -1;
-       } else if (((struct process *)a)->amount < ((struct process *)b)->amount) {
+       } else if (((const struct process *)a)->amount < ((const struct process *)b)->amount) {
                return 1;
        } else {
                return 0;
@@ -714,16 +717,24 @@ int comparecpu(const void *a, const void *b)
 
 int comparemem(const void *a, const void *b)
 {
-       if (((struct process *)a)->totalmem > ((struct process *)b)->totalmem) {
+       if (((const struct process *)a)->rss > ((const struct process *)b)->rss) {
                return -1;
-       } else if (((struct process *)a)->totalmem < ((struct process *)b)->totalmem) {
+       } else if (((const struct process *)a)->rss < ((const struct process *)b)->rss) {
                return 1;
        } else {
                return 0;
        }
 }
 
-inline void proc_find_top(struct process **cpu, struct process **mem)
+int comparetime(const void *va, const void *vb)
+{
+       struct process *a = (struct process *)va, *b = (struct process *)vb;
+
+       return b->total_cpu_time - a->total_cpu_time;
+}
+
+__attribute__((gnu_inline)) inline void
+proc_find_top(struct process **cpu, struct process **mem, struct process **time)
 {
        struct kinfo_proc *p;
        int n_processes;
@@ -734,7 +745,7 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
 
        /* we get total pages count again to be sure it is up to date */
        if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) != 0) {
-               CRIT_ERR("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
+               CRIT_ERR(NULL, NULL, "Cannot read sysctl \"vm.stats.vm.v_page_count\"");
        }
 
        p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
@@ -745,10 +756,11 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                        processes[j].pid = p[i].ki_pid;
                        processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
                        processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
-                       processes[j].totalmem = (float) (p[i].ki_rssize /
-                               (float) total_pages) * 100.0;
                        processes[j].vsize = p[i].ki_size;
                        processes[j].rss = (p[i].ki_rssize * getpagesize());
+                       /* ki_runtime is in microseconds, total_cpu_time in centiseconds.
+                        * Therefore we divide by 10000. */
+                       processes[j].total_cpu_time = p[i].ki_runtime / 10000;
                        j++;
                }
        }
@@ -758,12 +770,8 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                struct process *tmp, *ttmp;
 
                tmp = malloc(sizeof(struct process));
-               tmp->pid = processes[i].pid;
-               tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
+               memcpy(tmp, &processes[i], sizeof(struct process));
                tmp->name = strndup(processes[i].name, text_buffer_size);
-               tmp->rss = processes[i].rss;
-               tmp->vsize = processes[i].vsize;
 
                ttmp = mem[i];
                mem[i] = tmp;
@@ -778,12 +786,8 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                struct process *tmp, *ttmp;
 
                tmp = malloc(sizeof(struct process));
-               tmp->pid = processes[i].pid;
-               tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
+               memcpy(tmp, &processes[i], sizeof(struct process));
                tmp->name = strndup(processes[i].name, text_buffer_size);
-               tmp->rss = processes[i].rss;
-               tmp->vsize = processes[i].vsize;
 
                ttmp = cpu[i];
                cpu[i] = tmp;
@@ -793,11 +797,27 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                }
        }
 
+       qsort(processes, j - 1, sizeof(struct process), comparetime);
+       for (i = 0; i < 10 && i < n_processes; i++) {
+               struct process *tmp, *ttmp;
+
+               tmp = malloc(sizeof(struct process));
+               memcpy(tmp, &processes[i], sizeof(struct process));
+               tmp->name = strndup(processes[i].name, text_buffer_size);
+
+               ttmp = time[i];
+               time[i] = tmp;
+               if (ttmp != NULL) {
+                       free(ttmp->name);
+                       free(ttmp);
+               }
+       }
+
 #if defined(FREEBSD_DEBUG)
        printf("=====\nmem\n");
        for (i = 0; i < 10; i++) {
-               printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
-                               mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
+               printf("%d: %s(%d) %ld %ld\n", i, mem[i]->name,
+                               mem[i]->pid, mem[i]->vsize, mem[i]->rss);
        }
 #endif
 
@@ -820,10 +840,10 @@ int apm_getinfo(int fd, apm_info_t aip)
        return 0;
 }
 
-char *get_apm_adapter()
+char *get_apm_adapter(void)
 {
        int fd;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -834,20 +854,20 @@ char *get_apm_adapter()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       switch (info.ai_acline) {
+       switch (a_info.ai_acline) {
                case 0:
                        strncpy(out, "off-line", 16);
                        return out;
                        break;
                case 1:
-                       if (info.ai_batt_stat == 3) {
+                       if (a_info.ai_batt_stat == 3) {
                                strncpy(out, "charging", 16);
                                return out;
                        } else {
@@ -862,11 +882,11 @@ char *get_apm_adapter()
        }
 }
 
-char *get_apm_battery_life()
+char *get_apm_battery_life(void)
 {
        int fd;
        u_int batt_life;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -877,14 +897,14 @@ char *get_apm_battery_life()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       batt_life = info.ai_batt_life;
+       batt_life = a_info.ai_batt_life;
        if (batt_life == APM_UNKNOWN) {
                strncpy(out, "unknown", 16);
        } else if (batt_life <= 100) {
@@ -897,12 +917,12 @@ char *get_apm_battery_life()
        return out;
 }
 
-char *get_apm_battery_time()
+char *get_apm_battery_time(void)
 {
        int fd;
        int batt_time;
        int h, m, s;
-       struct apm_info info;
+       struct apm_info a_info;
        char *out;
 
        out = (char *) calloc(16, sizeof(char));
@@ -913,14 +933,14 @@ char *get_apm_battery_time()
                return out;
        }
 
-       if (apm_getinfo(fd, &info) != 0) {
+       if (apm_getinfo(fd, &a_info) != 0) {
                close(fd);
                strncpy(out, "ERR", 16);
                return out;
        }
        close(fd);
 
-       batt_time = info.ai_batt_time;
+       batt_time = a_info.ai_batt_time;
 
        if (batt_time == -1) {
                strncpy(out, "unknown", 16);
@@ -953,12 +973,16 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat)
        }
 }
 
-void update_entropy(void)
+int get_entropy_avail(unsigned int *val)
 {
        /* Not applicable for FreeBSD as it uses the yarrow prng. */
+       (void)val;
+       return 1;
 }
 
-/* empty stub so conky links */
-void free_all_processes(void)
+int get_entropy_poolsize(unsigned int *val)
 {
+       /* Not applicable for FreeBSD as it uses the yarrow prng. */
+       (void)val;
+       return 1;
 }