Fix removing the config and sending a SIGUSR1 results in segfault
[monky] / src / freebsd.c
index 01b40a1..a8a7d74 100644 (file)
@@ -1,4 +1,5 @@
 /* -*- 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
  *
@@ -8,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.
  *
@@ -24,8 +25,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * vim: ts=4 sw=4 noet ai cindent syntax=c
- *
  */
 
 #include <sys/ioctl.h>
@@ -55,6 +54,7 @@
 #include "conky.h"
 #include "freebsd.h"
 #include "logging.h"
+#include "net_stat.h"
 #include "top.h"
 #include "diskio.h"
 
@@ -286,40 +286,38 @@ void update_running_processes(void)
        info.run_procs = cnt;
 }
 
-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)
 {
-       /* 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(NULL, NULL, "malloc");
        }
 }
 
-/* XXX: SMP support */
+struct cpu_info {
+       long oldtotal;
+       long oldused;
+};
+
 void update_cpu_usage(void)
 {
+       int i, j = 0;
        long used, total;
-       long cp_time[CPUSTATES];
-       size_t cp_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)) {
@@ -327,28 +325,68 @@ void update_cpu_usage(void)
                cpu_setup = 1;
        }
 
-       if (sysctlbyname("kern.cp_time", &cp_time, &cp_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;
        }
 
-       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];
+       /* cpu[0] is overall stats, get it from separate sysctl */
+       cp_len = CPUSTATES * sizeof(long);
+       cp_time = malloc(cp_len);
 
-       used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-       total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
+       if (sysctlbyname("kern.cp_time", cp_time, &cp_len, NULL, 0) < 0) {
+               fprintf(stderr, "Cannot get kern.cp_time\n");
+       }
+
+       total = 0;
+       for (j = 0; j < CPUSTATES; j++)
+               total += cp_time[j];
 
-       if ((total - oldtotal) != 0) {
-               info.cpu_usage[0] = ((double) (used - oldused)) /
-                       (double) (total - oldtotal);
+       used = total - cp_time[CP_IDLE];
+
+       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);
 }
 
 void update_load_average(void)
@@ -480,10 +518,12 @@ int open_acpi_temperature(const char *name)
        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;
        }
@@ -508,65 +548,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)
-__attribute__((gnu_inline)) inline unsigned long long int rdtsc(void)
-{
-       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)
@@ -724,9 +705,9 @@ int comparecpu(const void *a, const void *b)
 
 int comparemem(const void *a, const void *b)
 {
-       if (((const struct process *)a)->totalmem > ((const struct process *)b)->totalmem) {
+       if (((const struct process *)a)->rss > ((const struct process *)b)->rss) {
                return -1;
-       } else if (((const struct process *)a)->totalmem < ((const struct process *)b)->totalmem) {
+       } else if (((const struct process *)a)->rss < ((const struct process *)b)->rss) {
                return 1;
        } else {
                return 0;
@@ -756,8 +737,6 @@ 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());
                        j++;
@@ -771,7 +750,6 @@ proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
                tmp->rss = processes[i].rss;
                tmp->vsize = processes[i].vsize;
@@ -791,7 +769,6 @@ proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
                tmp->rss = processes[i].rss;
                tmp->vsize = processes[i].vsize;
@@ -807,8 +784,8 @@ proc_find_top(struct process **cpu, struct process **mem)
 #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
 
@@ -964,9 +941,18 @@ 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;
+}
+
+int get_entropy_poolsize(unsigned int *val)
+{
+       /* Not applicable for FreeBSD as it uses the yarrow prng. */
+       (void)val;
+       return 1;
 }
 
 /* empty stub so conky links */