Sun Aug 30 11:04:48 2020 UTC ()
conky: Make getting CPU freq work on more NetBSD systems (notably mine)


(nia)
diff -r1.50 -r1.51 pkgsrc/sysutils/conky/Makefile
diff -r1.8 -r1.9 pkgsrc/sysutils/conky/distinfo
diff -r1.4 -r1.5 pkgsrc/sysutils/conky/patches/patch-src_netbsd.c

cvs diff -r1.50 -r1.51 pkgsrc/sysutils/conky/Makefile (expand / switch to unified diff)

--- pkgsrc/sysutils/conky/Makefile 2020/08/27 11:25:29 1.50
+++ pkgsrc/sysutils/conky/Makefile 2020/08/30 11:04:48 1.51
@@ -1,18 +1,18 @@ @@ -1,18 +1,18 @@
1# $NetBSD: Makefile,v 1.50 2020/08/27 11:25:29 nia Exp $ 1# $NetBSD: Makefile,v 1.51 2020/08/30 11:04:48 nia Exp $
2# 2#
3 3
4DISTNAME= conky-1.9.0 4DISTNAME= conky-1.9.0
5PKGREVISION= 43 5PKGREVISION= 44
6CATEGORIES= sysutils 6CATEGORIES= sysutils
7MASTER_SITES= ${MASTER_SITE_SOURCEFORGE:=conky/} 7MASTER_SITES= ${MASTER_SITE_SOURCEFORGE:=conky/}
8EXTRACT_SUFX= .tar.bz2 8EXTRACT_SUFX= .tar.bz2
9 9
10MAINTAINER= imil@NetBSD.org 10MAINTAINER= imil@NetBSD.org
11HOMEPAGE= http://conky.sourceforge.net/ 11HOMEPAGE= http://conky.sourceforge.net/
12COMMENT= Free, light-weight system monitor for X 12COMMENT= Free, light-weight system monitor for X
13LICENSE= gnu-gpl-v3 13LICENSE= gnu-gpl-v3
14 14
15GNU_CONFIGURE= yes 15GNU_CONFIGURE= yes
16USE_LIBTOOL= yes 16USE_LIBTOOL= yes
17USE_TOOLS+= pkg-config aclocal autoconf autoheader automake 17USE_TOOLS+= pkg-config aclocal autoconf autoheader automake
18 18

cvs diff -r1.8 -r1.9 pkgsrc/sysutils/conky/distinfo (expand / switch to unified diff)

--- pkgsrc/sysutils/conky/distinfo 2020/08/27 11:25:29 1.8
+++ pkgsrc/sysutils/conky/distinfo 2020/08/30 11:04:48 1.9
@@ -1,17 +1,17 @@ @@ -1,17 +1,17 @@
1$NetBSD: distinfo,v 1.8 2020/08/27 11:25:29 nia Exp $ 1$NetBSD: distinfo,v 1.9 2020/08/30 11:04:48 nia Exp $
2 2
3SHA1 (conky-1.9.0.tar.bz2) = 2069cbdb8e7224fcbf3c6b1ddebf64a51634b4cc 3SHA1 (conky-1.9.0.tar.bz2) = 2069cbdb8e7224fcbf3c6b1ddebf64a51634b4cc
4RMD160 (conky-1.9.0.tar.bz2) = 2df0e60d4236ed667bdae876e11ba28d05914e25 4RMD160 (conky-1.9.0.tar.bz2) = 2df0e60d4236ed667bdae876e11ba28d05914e25
5SHA512 (conky-1.9.0.tar.bz2) = 98c9f69fd8bee2062e3eddeb7a394cce932199ffd6c8664006d1222f0453569406613e63637848c77a455f7dae3f16be3079a2cab56ecafea51ec043de23c889 5SHA512 (conky-1.9.0.tar.bz2) = 98c9f69fd8bee2062e3eddeb7a394cce932199ffd6c8664006d1222f0453569406613e63637848c77a455f7dae3f16be3079a2cab56ecafea51ec043de23c889
6Size (conky-1.9.0.tar.bz2) = 626555 bytes 6Size (conky-1.9.0.tar.bz2) = 626555 bytes
7SHA1 (patch-configure.ac) = fab28e82ad01b2c346552af7537865e049098f2c 7SHA1 (patch-configure.ac) = fab28e82ad01b2c346552af7537865e049098f2c
8SHA1 (patch-data_Makefile.am) = af8df32076b8a389ee98e2550342dbb343f7bff3 8SHA1 (patch-data_Makefile.am) = af8df32076b8a389ee98e2550342dbb343f7bff3
9SHA1 (patch-src_Makefile.am) = f4e79c0f08a0886a061449b9a8a578173cda90db 9SHA1 (patch-src_Makefile.am) = f4e79c0f08a0886a061449b9a8a578173cda90db
10SHA1 (patch-src_config.h.in) = 31aa9cd4bd4ab775e6da633f86415ee8f5383538 10SHA1 (patch-src_config.h.in) = 31aa9cd4bd4ab775e6da633f86415ee8f5383538
11SHA1 (patch-src_conky.c) = e4c9173661cca437af0febbcae94eec72dc0ea4a 11SHA1 (patch-src_conky.c) = e4c9173661cca437af0febbcae94eec72dc0ea4a
12SHA1 (patch-src_core.c) = a64ff7cb05d7b6b62aea41e486c0a719e2c93faf 12SHA1 (patch-src_core.c) = a64ff7cb05d7b6b62aea41e486c0a719e2c93faf
13SHA1 (patch-src_fs.c) = 104fd3788f319689026702d6a149410184072e24 13SHA1 (patch-src_fs.c) = 104fd3788f319689026702d6a149410184072e24
14SHA1 (patch-src_netbsd.c) = 3cacc27e8ae6e20fe70385b764928767dab289d7 14SHA1 (patch-src_netbsd.c) = d9a23952c51eaa711b14c36f2b354972cd0b89bc
15SHA1 (patch-src_netbsd.h) = dee5c466ab5cc267f1da94848b0a8b598303dfe4 15SHA1 (patch-src_netbsd.h) = dee5c466ab5cc267f1da94848b0a8b598303dfe4
16SHA1 (patch-src_read__tcp.c) = 32fca1b7fdfdb2568b056705c7129807e281b0cc 16SHA1 (patch-src_read__tcp.c) = 32fca1b7fdfdb2568b056705c7129807e281b0cc
17SHA1 (patch-src_text__object.h) = 7f7e3684e21f9618a8ede37525f4a48f47a92050 17SHA1 (patch-src_text__object.h) = 7f7e3684e21f9618a8ede37525f4a48f47a92050

cvs diff -r1.4 -r1.5 pkgsrc/sysutils/conky/patches/patch-src_netbsd.c (expand / switch to unified diff)

--- pkgsrc/sysutils/conky/patches/patch-src_netbsd.c 2020/08/27 11:25:29 1.4
+++ pkgsrc/sysutils/conky/patches/patch-src_netbsd.c 2020/08/30 11:04:48 1.5
@@ -1,53 +1,69 @@ @@ -1,53 +1,69 @@
1$NetBSD: patch-src_netbsd.c,v 1.4 2020/08/27 11:25:29 nia Exp $ 1$NetBSD: patch-src_netbsd.c,v 1.5 2020/08/30 11:04:48 nia Exp $
2 2
3Many fixes and addons for conky to work on NetBSD. 3Many fixes and addons for conky to work on NetBSD.
4 4
5--- src/netbsd.c.orig 2012-05-03 21:08:27.000000000 +0000 5--- src/netbsd.c.orig 2012-05-03 21:08:27.000000000 +0000
6+++ src/netbsd.c 6+++ src/netbsd.c
7@@ -30,337 +30,803 @@ 7@@ -30,337 +30,815 @@
8  8
9 #include "netbsd.h" 9 #include "netbsd.h"
10 #include "net_stat.h" 10 #include "net_stat.h"
11+#include "top.h" 11+#include "top.h"
12+#include <sys/types.h> 12+#include <sys/types.h>
13+#include <sys/statvfs.h> 13+#include <sys/statvfs.h>
14+#include <ifaddrs.h> 14+#include <ifaddrs.h>
15  15+
16-static kvm_t *kd = NULL; 
17-int kd_init = 0, nkd_init = 0; 
18-u_int32_t sensvalue; 
19-char errbuf[_POSIX2_LINE_MAX]; 
20+#define P_BOOL 0 16+#define P_BOOL 0
21+#define P_UINT8 1 17+#define P_UINT8 1
22+#define P_INT64 2 18+#define P_INT64 2
23+#define P_STRING 3 19+#define P_STRING 3
24  20+
25-static int init_kvm(void) 21+static char const *freq_sysctls[] = {
 22+#if defined(__powerpc__)
 23+ "machdep.intrepid.frequency.current",
 24+#endif
 25+#if defined(__mips__)
 26+ "machdep.loongson.frequency.current",
 27+#endif
 28+#if defined(__i386__) || defined(__x86_64__)
 29+ "machdep.est.frequency.current",
 30+ "machdep.powernow.frequency.current",
 31+#endif
 32+ "machdep.cpu.frequency.current",
 33+ "machdep.frequency.current",
 34+ NULL
 35+};
 36+
26+typedef struct Devquery { 37+typedef struct Devquery {
27+ int type; 38+ int type;
28+ char *dev; 39+ char *dev;
29+ char *key; 40+ char *key;
30+ char *row; 41+ char *row;
31+} Devquery; 42+} Devquery;
32+ 43+
33+u_int32_t sensvalue; 44+u_int32_t sensvalue;
34+char errbuf[_POSIX2_LINE_MAX]; 45+char errbuf[_POSIX2_LINE_MAX];
35+static short cpu_setup = 0; 46+static short cpu_setup = 0;
36+ 47+
37+int sysmon_fd; 48+int sysmon_fd;
38+ 49
 50-static kvm_t *kd = NULL;
 51-int kd_init = 0, nkd_init = 0;
 52-u_int32_t sensvalue;
 53-char errbuf[_POSIX2_LINE_MAX];
39+static inline void proc_find_top(struct process **cpu, struct process **mem); 54+static inline void proc_find_top(struct process **cpu, struct process **mem);
40+ 55
 56-static int init_kvm(void)
41+int8_t envsys_get_val(Devquery, void *); 57+int8_t envsys_get_val(Devquery, void *);
42+ 58+
43+void 59+void
44+prepare_update(void) 60+prepare_update(void)
45 { 61 {
46- if (kd_init) { 62- if (kd_init) {
47- return 0; 63- return 0;
48- } 64- }
49+} 65+}
50  66
51- kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); 67- kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
52- if (kd == NULL) { 68- if (kd == NULL) {
53- warnx("cannot kvm_openfiles: %s", errbuf); 69- warnx("cannot kvm_openfiles: %s", errbuf);
@@ -136,41 +152,41 @@ Many fixes and addons for conky to work  @@ -136,41 +152,41 @@ Many fixes and addons for conky to work
136+ info.mem = info.memmax - info.memfree; 152+ info.mem = info.memmax - info.memfree;
137+ info.bufmem = info.cached + info.buffers; 153+ info.bufmem = info.cached + info.buffers;
138+ info.swap = info.swapmax - info.swapfree; 154+ info.swap = info.swapmax - info.swapfree;
139+ 155+
140+ /* 156+ /*
141+ * Counter-adjust for the adjustment in update_stuff in common.c so 157+ * Counter-adjust for the adjustment in update_stuff in common.c so
142+ * that memeasyfree is the inactive memory. Since inactive memory 158+ * that memeasyfree is the inactive memory. Since inactive memory
143+ * partially overlaps with buffer memory, but the size of the 159+ * partially overlaps with buffer memory, but the size of the
144+ * overlap is not measured, attempting to split it into non-bufmem 160+ * overlap is not measured, attempting to split it into non-bufmem
145+ * and bufmem parts, as common.c does, can't work. So instead we 161+ * and bufmem parts, as common.c does, can't work. So instead we
146+ * report inactive memory as memeasyfree. 162+ * report inactive memory as memeasyfree.
147+ */ 163+ */
148+ info.memeasyfree -= info.bufmem; 164+ info.memeasyfree -= info.bufmem;
149+ 165
 166- if (sep == NULL) {
 167- warn("memory allocation failed");
150+ return 0; 168+ return 0;
151+} 169+}
152+ 170+
153+int 171+int
154+update_net_stats(void) 172+update_net_stats(void)
155+{ 173+{
156+ struct net_stat *ns; 174+ struct net_stat *ns;
157+ double delta; 175+ double delta;
158+ long long r, t, last_recv, last_trans; 176+ long long r, t, last_recv, last_trans;
159+ struct ifaddrs *ifap, *ifa; 177+ struct ifaddrs *ifap, *ifa;
160+ struct if_data *ifd; 178+ struct if_data *ifd;
161  179+
162- if (sep == NULL) { 
163- warn("memory allocation failed"); 
164+ /* get delta */ 180+ /* get delta */
165+ delta = current_update_time - last_update_time; 181+ delta = current_update_time - last_update_time;
166+ if (delta <= 0.0001) { 182+ if (delta <= 0.0001) {
167 return 0; 183 return 0;
168 } 184 }
169  185
170- if (swapctl(SWAP_STATS, (void *) sep, n) < n) { 186- if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
171- warn("could not get swap stats"); 187- warn("could not get swap stats");
172+ if (getifaddrs(&ifap) < 0) { 188+ if (getifaddrs(&ifap) < 0) {
173 return 0; 189 return 0;
174 } 190 }
175- for (; n > 0; n--) { 191- for (; n > 0; n--) {
176- *retavail += (int) dbtob(sep[n - 1].se_nblks); 192- *retavail += (int) dbtob(sep[n - 1].se_nblks);
@@ -424,27 +440,28 @@ Many fixes and addons for conky to work  @@ -424,27 +440,28 @@ Many fixes and addons for conky to work
424- last_trans = ns->trans; 440- last_trans = ns->trans;
425- 441-
426- if (ifnet.if_ibytes < ns->last_read_recv) { 442- if (ifnet.if_ibytes < ns->last_read_recv) {
427- ns->recv += ((long long) 4294967295U - ns->last_read_recv) + 443- ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
428- ifnet.if_ibytes; 444- ifnet.if_ibytes;
429- } else { 445- } else {
430- ns->recv += (ifnet.if_ibytes - ns->last_read_recv); 446- ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
431- } 447- }
432+ total = 0; 448+ total = 0;
433+ for (j = 0; j < CPUSTATES; j++) 449+ for (j = 0; j < CPUSTATES; j++)
434+ total += cp_time[j]; 450+ total += cp_time[j];
435+ 451+
436+ used = total - cp_time[CP_IDLE]; 452+ used = total - cp_time[CP_IDLE];
437+ 453
 454- ns->last_read_recv = ifnet.if_ibytes;
438+ if ((total - cpu[0].oldtotal) != 0) { 455+ if ((total - cpu[0].oldtotal) != 0) {
439+ info.cpu_usage[0] = ((double) (used - cpu[0].oldused)) / 456+ info.cpu_usage[0] = ((double) (used - cpu[0].oldused)) /
440+ (double) (total - cpu[0].oldtotal); 457+ (double) (total - cpu[0].oldtotal);
441+ } else { 458+ } else {
442+ info.cpu_usage[0] = 0; 459+ info.cpu_usage[0] = 0;
443+ } 460+ }
444+ 461+
445+ cpu[0].oldused = used; 462+ cpu[0].oldused = used;
446+ cpu[0].oldtotal = total; 463+ cpu[0].oldtotal = total;
447+ 464+
448+ free(cp_time); 465+ free(cp_time);
449+ 466+
450+ /* per-core stats */ 467+ /* per-core stats */
@@ -452,176 +469,185 @@ Many fixes and addons for conky to work  @@ -452,176 +469,185 @@ Many fixes and addons for conky to work
452+ cp_time = malloc(cp_len); 469+ cp_time = malloc(cp_len);
453+ 470+
454+ /* on e.g. i386 SMP we may have more values than actual cpus; this will just drop extra values */ 471+ /* on e.g. i386 SMP we may have more values than actual cpus; this will just drop extra values */
455+ if (sysctlbyname("kern.cp_time", cp_time, &cp_len, NULL, 0) < 0 && errno != ENOMEM) { 472+ if (sysctlbyname("kern.cp_time", cp_time, &cp_len, NULL, 0) < 0 && errno != ENOMEM) {
456+ fprintf(stderr, "Cannot get kern.cp_time SMP\n"); 473+ fprintf(stderr, "Cannot get kern.cp_time SMP\n");
457+ } 474+ }
458+ 475+
459+ for (i = 0; i < info.cpu_count; i++) 476+ for (i = 0; i < info.cpu_count; i++)
460+ { 477+ {
461+ total = 0; 478+ total = 0;
462+ for (j = 0; j < CPUSTATES; j++) 479+ for (j = 0; j < CPUSTATES; j++)
463+ total += cp_time[i*CPUSTATES + j]; 480+ total += cp_time[i*CPUSTATES + j];
464  481
465- ns->last_read_recv = ifnet.if_ibytes; 
466+ used = total - cp_time[i*CPUSTATES + CP_IDLE]; 
467  
468- if (ifnet.if_obytes < ns->last_read_trans) { 482- if (ifnet.if_obytes < ns->last_read_trans) {
469- ns->trans += ((long long) 4294967295U - ns->last_read_trans) + 483- ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
470- ifnet.if_obytes; 484- ifnet.if_obytes;
 485+ used = total - cp_time[i*CPUSTATES + CP_IDLE];
 486+
471+ if ((total - cpu[i+1].oldtotal) != 0) { 487+ if ((total - cpu[i+1].oldtotal) != 0) {
472+ info.cpu_usage[i+1] = ((double) (used - cpu[i+1].oldused)) / 488+ info.cpu_usage[i+1] = ((double) (used - cpu[i+1].oldused)) /
473+ (double) (total - cpu[i+1].oldtotal); 489+ (double) (total - cpu[i+1].oldtotal);
474 } else { 490 } else {
475- ns->trans += (ifnet.if_obytes - ns->last_read_trans); 491- ns->trans += (ifnet.if_obytes - ns->last_read_trans);
476+ info.cpu_usage[i+1] = 0; 492+ info.cpu_usage[i+1] = 0;
477 } 493 }
478  494
479- ns->last_read_trans = ifnet.if_obytes; 495- ns->last_read_trans = ifnet.if_obytes;
480+ cpu[i+1].oldused = used; 496+ cpu[i+1].oldused = used;
481+ cpu[i+1].oldtotal = total; 497+ cpu[i+1].oldtotal = total;
482+ } 498+ }
483+ 499+
484+ free(cp_time); 500+ free(cp_time);
485+ return 0; 501+ return 0;
486+} 502+}
487  503
488- ns->recv += (ifnet.if_ibytes - ns->last_read_recv); 504- ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
489- ns->last_read_recv = ifnet.if_ibytes; 505- ns->last_read_recv = ifnet.if_ibytes;
490- ns->trans += (ifnet.if_obytes - ns->last_read_trans); 506- ns->trans += (ifnet.if_obytes - ns->last_read_trans);
491- ns->last_read_trans = ifnet.if_obytes; 507- ns->last_read_trans = ifnet.if_obytes;
492+int update_load_average(void) 508+int update_load_average(void)
493+{ 509+{
494+ double v[3]; 510+ double v[3];
495+ 
496+ getloadavg(v, 3); 
497  511
498- ns->recv_speed = (ns->recv - last_recv) / delta; 512- ns->recv_speed = (ns->recv - last_recv) / delta;
499- ns->trans_speed = (ns->trans - last_trans) / delta; 513- ns->trans_speed = (ns->trans - last_trans) / delta;
 514- }
 515+ getloadavg(v, 3);
 516+
500+ info.loadavg[0] = (float) v[0]; 517+ info.loadavg[0] = (float) v[0];
501+ info.loadavg[1] = (float) v[1]; 518+ info.loadavg[1] = (float) v[1];
502+ info.loadavg[2] = (float) v[2]; 519+ info.loadavg[2] = (float) v[2];
503+ 520+
504+ return 0; 521+ return 0;
505+} 522 }
506+ 523
 524-void update_total_processes()
507+int open_acpi_temperature(const char *name) 525+int open_acpi_temperature(const char *name)
508+{ 526 {
 527- /* It's easier to use kvm here than sysctl */
509+ (void)name; /* useless on NetBSD */ 528+ (void)name; /* useless on NetBSD */
510+ return -1; 529+ return -1;
511+} 530+}
512+ 531
 532- int n_processes;
513+int get_entropy_avail(unsigned int *val) 533+int get_entropy_avail(unsigned int *val)
514+{ 534+{
515+ return 1; 535+ return 1;
516+} 536+}
517+ 537
 538- info.procs = 0;
518+int get_entropy_poolsize(unsigned int *val) 539+int get_entropy_poolsize(unsigned int *val)
519+{ 540+{
520+ return 1; 541+ return 1;
521+} 542+}
522+ 543
523+/* void */ 544- if (init_kvm() < 0) {
 545- return;
524+char 546+char
525+get_freq(char *p_client_buffer, size_t client_buffer_size, 547+get_freq(char *p_client_buffer, size_t client_buffer_size,
526+ const char *p_format, int divisor, unsigned int cpu) 548+ const char *p_format, int divisor, unsigned int cpu) {
527+{ 549+ const char **s;
528+ int freq = cpu; 550+ char name[64];
529+ 551+ int freq = 0;
530+ if (!p_client_buffer || client_buffer_size <= 0 || !p_format 552+ size_t freq_size = sizeof(freq);
531+ || divisor <= 0) { 553+
532+ return 0; 554+ snprintf(name, sizeof(name), "machdep.cpufreq.cpu%u.current", cpu - 1);
533 } 555+ if (sysctlbyname(name, &freq, &freq_size, NULL, 0) == -1) {
534+ 556+ for (s = freq_sysctls; *s != NULL; ++s) {
535+ size_t size = sizeof(freq); 557+ if (sysctlbyname(*s, &freq, &freq_size, NULL, 0) != -1)
536+ 558+ break;
537+ if (sysctlbyname("machdep.est.frequency.current", 559+ }
538+ NULL, &size, NULL, 0) == 0) { 560+ }
539+ sysctlbyname("machdep.est.frequency.current", &freq, &size, NULL, 0); 561+ if (freq > 0) {
540+ snprintf(p_client_buffer, client_buffer_size, p_format, 
541+ (float) freq / divisor); 
542+ } else if (sysctlbyname("machdep.tsc_freq", NULL, &size, NULL, 0) == 0) { 
543+ sysctlbyname("machdep.tsc_freq", &freq, &size, NULL, 0); 
544+ snprintf(p_client_buffer, client_buffer_size, p_format, 562+ snprintf(p_client_buffer, client_buffer_size, p_format,
545+ (float) freq / (1000000 * divisor)); 563+ (float)freq / divisor);
546+ } else 564 } else {
 565- kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
 566- &n_processes);
547+ snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f); 567+ snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
548+ 568 }
549+ return 1; 569+ return 1;
550 } 570+}
551  571
552-void update_total_processes() 572- info.procs = n_processes;
 573+
553+void update_diskio() 574+void update_diskio()
554 { 575+{
555- /* It's easier to use kvm here than sysctl */ 
556+ return; /* XXX: implement? hifi: not sure how */ 576+ return; /* XXX: implement? hifi: not sure how */
557+} 577 }
558  578
559- int n_processes; 579-void update_running_processes()
560+int update_top() 580+int update_top()
561+{ 581 {
 582- struct kinfo_proc2 *p;
 583- int n_processes;
 584- int i, cnt = 0;
562+ proc_find_top(info.cpu, info.memu); 585+ proc_find_top(info.cpu, info.memu);
563  586
564- info.procs = 0; 587- info.run_procs = 0;
565+ return 0; 588+ return 0;
566+} 589+}
567  590
568- if (init_kvm() < 0) { 591- if (init_kvm() < 0) {
569- return; 592- return;
570- } else { 593- } else {
571- kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 594- p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
572- &n_processes); 595- &n_processes);
 596- for (i = 0; i < n_processes; i++) {
 597- if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
 598- || p[i].p_stat == LSONPROC) {
 599- cnt++;
 600- }
 601- }
573+int comparecpu(const void *a, const void *b) 602+int comparecpu(const void *a, const void *b)
574+{ 603+{
575+ if (((struct process *) a)->amount > ((struct process *) b)->amount) { 604+ if (((struct process *) a)->amount > ((struct process *) b)->amount) {
576+ return -1; 605+ return -1;
577+ } 606+ }
578+ if (((struct process *) a)->amount < ((struct process *) b)->amount) { 607+ if (((struct process *) a)->amount < ((struct process *) b)->amount) {
579+ return 1; 608+ return 1;
580 } 609 }
581  610
582- info.procs = n_processes; 611- info.run_procs = cnt;
583+ return 0; 612+ return 0;
584 } 613 }
585  614
586-void update_running_processes() 615-struct cpu_load_struct {
 616- unsigned long load[5];
 617-};
587+int comparemem(const void *a, const void *b) 618+int comparemem(const void *a, const void *b)
588 { 619+{
589- struct kinfo_proc2 *p; 
590- int n_processes; 
591- int i, cnt = 0; 
592+ if (((struct process *) a)->rss > ((struct process *) b)->rss) { 620+ if (((struct process *) a)->rss > ((struct process *) b)->rss) {
593+ return -1; 621+ return -1;
594+ } 622+ }
595  623
596- info.run_procs = 0; 624-struct cpu_load_struct fresh = {
 625- {0, 0, 0, 0, 0}
 626-};
597+ if (((struct process *) a)->rss < ((struct process *) b)->rss) { 627+ if (((struct process *) a)->rss < ((struct process *) b)->rss) {
598+ return 1; 628+ return 1;
599+ } 629+ }
600  630
601- if (init_kvm() < 0) { 631-long cpu_used, oldtotal, oldused;
602- return; 
603- } else { 
604- p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 
605- &n_processes); 
606- for (i = 0; i < n_processes; i++) { 
607- if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL 
608- || p[i].p_stat == LSONPROC) { 
609- cnt++; 
610+ return 0; 632+ return 0;
611+} 633+}
612+ 634
 635-void update_cpu_usage()
613+inline void proc_find_top(struct process **cpu, struct process **mem) 636+inline void proc_find_top(struct process **cpu, struct process **mem)
614+{ 637 {
 638- long used, total;
 639- static u_int64_t cp_time[CPUSTATES];
 640- size_t len = sizeof(cp_time);
615+ struct kinfo_proc2 *p; 641+ struct kinfo_proc2 *p;
616+ int n_processes; 642+ int n_processes;
617+ int i, j = 0; 643+ int i, j = 0;
618+ struct process *processes; 644+ struct process *processes;
619+ int mib[2]; 645+ int mib[2];
620+ 646+
621+ u_int total_pages; 647+ u_int total_pages;
622+ int64_t usermem; 648+ int64_t usermem;
623+ int pagesize = getpagesize(); 649+ int pagesize = getpagesize();
624+ 650+
625+ /* we get total pages count again to be sure it is up to date */ 651+ /* we get total pages count again to be sure it is up to date */
626+ mib[0] = CTL_HW; 652+ mib[0] = CTL_HW;
627+ mib[1] = HW_USERMEM64; 653+ mib[1] = HW_USERMEM64;
@@ -637,130 +663,116 @@ Many fixes and addons for conky to work  @@ -637,130 +663,116 @@ Many fixes and addons for conky to work
637+ int max_size = sizeof(struct kinfo_proc2); 663+ int max_size = sizeof(struct kinfo_proc2);
638+ 664+
639+ p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); 665+ p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
640+ processes = malloc(n_processes * sizeof(struct process)); 666+ processes = malloc(n_processes * sizeof(struct process));
641+ 667+
642+ for (i = 0; i < n_processes; i++) { 668+ for (i = 0; i < n_processes; i++) {
643+ if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) { 669+ if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
644+ processes[j].pid = p[i].p_pid; 670+ processes[j].pid = p[i].p_pid;
645+ processes[j].name = strndup(p[i].p_comm, text_buffer_size); 671+ processes[j].name = strndup(p[i].p_comm, text_buffer_size);
646+ processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE; 672+ processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
647+ processes[j].rss = p[i].p_vm_rssize * pagesize; 673+ processes[j].rss = p[i].p_vm_rssize * pagesize;
648+ processes[j].vsize = p[i].p_vm_vsize; 674+ processes[j].vsize = p[i].p_vm_vsize;
649+ j++; 675+ j++;
650 } 676+ }
651- } 
652- } 
653+ } 677+ }
654  678
655- info.run_procs = cnt; 679- info.cpu_usage = 0;
656+ qsort(processes, j - 1, sizeof(struct process), comparemem); 680+ qsort(processes, j - 1, sizeof(struct process), comparemem);
657+ for (i = 0; i < 10; i++) { 681+ for (i = 0; i < 10; i++) {
658+ struct process *tmp, *ttmp; 682+ struct process *tmp, *ttmp;
659+  683+
660+ tmp = malloc(sizeof(struct process)); 684+ tmp = malloc(sizeof(struct process));
661+ memcpy(tmp, &processes[i], sizeof(struct process)); 685+ memcpy(tmp, &processes[i], sizeof(struct process));
662+ tmp->name = strndup(processes[i].name, text_buffer_size); 686+ tmp->name = strndup(processes[i].name, text_buffer_size);
663+ 687+
664+ ttmp = mem[i]; 688+ ttmp = mem[i];
665+ mem[i] = tmp; 689+ mem[i] = tmp;
666+ if (ttmp != NULL) { 690+ if (ttmp != NULL) {
667+ free(ttmp->name); 691+ free(ttmp->name);
668+ free(ttmp); 692+ free(ttmp);
669+ } 693+ }
670+ } 694+ }
671+ 695
 696- if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
 697- warn("cannot get kern.cp_time");
 698- }
672+ qsort(processes, j - 1, sizeof(struct process), comparecpu); 699+ qsort(processes, j - 1, sizeof(struct process), comparecpu);
673+ for (i = 0; i < 10; i++) { 700+ for (i = 0; i < 10; i++) {
674+ struct process *tmp, *ttmp; 701+ struct process *tmp, *ttmp;
675+ 702+
676+ tmp = malloc(sizeof(struct process)); 703+ tmp = malloc(sizeof(struct process));
677+ memcpy(tmp, &processes[i], sizeof(struct process)); 704+ memcpy(tmp, &processes[i], sizeof(struct process));
678+ tmp->name = strndup(processes[i].name, text_buffer_size); 705+ tmp->name = strndup(processes[i].name, text_buffer_size);
679+  706+
680+ ttmp = cpu[i]; 707+ ttmp = cpu[i];
681+ cpu[i] = tmp; 708+ cpu[i] = tmp;
682+ if (ttmp != NULL) { 709+ if (ttmp != NULL) {
683+ free(ttmp->name); 710+ free(ttmp->name);
684+ free(ttmp); 711+ free(ttmp);
685+ } 712+ }
686+ } 713+ }
687+ 714+
688+ for (i = 0; i < j; i++) { 715+ for (i = 0; i < j; i++) {
689+ free(processes[i].name); 716+ free(processes[i].name);
690+ } 717+ }
691+ free(processes); 718+ free(processes);
692 } 719+}
693  720+
694-struct cpu_load_struct { 
695- unsigned long load[5]; 
696-}; 
697+double 721+double
698+get_acpi_temperature(int fd) 722+get_acpi_temperature(int fd)
699+{ 723+{
700+ Devquery dq_tz = { P_INT64, "acpitz0", "temperature", "cur-value" }; 724+ Devquery dq_tz = { P_INT64, "acpitz0", "temperature", "cur-value" };
701+ int64_t temp; 725+ int64_t temp;
702  726
703-struct cpu_load_struct fresh = { 727- fresh.load[0] = cp_time[CP_USER];
704- {0, 0, 0, 0, 0} 728- fresh.load[1] = cp_time[CP_NICE];
705-}; 729- fresh.load[2] = cp_time[CP_SYS];
 730- fresh.load[3] = cp_time[CP_IDLE];
 731- fresh.load[4] = cp_time[CP_IDLE];
706+ (void)fd; 732+ (void)fd;
707+ 733
 734- used = fresh.load[0] + fresh.load[1] + fresh.load[2];
 735- total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
708+ if (envsys_get_val(dq_tz, (void *)&temp) < 0) 736+ if (envsys_get_val(dq_tz, (void *)&temp) < 0)
709+ return 0.0; 737+ return 0.0;
710  738
711-long cpu_used, oldtotal, oldused; 739- if ((total - oldtotal) != 0) {
 740- info.cpu_usage = ((double) (used - oldused)) /
 741- (double) (total - oldtotal);
 742- } else {
 743- info.cpu_usage = 0;
712+ return (temp / 1000000.0) - 273.15; 744+ return (temp / 1000000.0) - 273.15;
713+} 745+}
714  746+
715-void update_cpu_usage() 
716+void 747+void
717+get_bat_life(char *bat, char *buf) 748+get_bat_life(char *bat, char *buf)
718 { 749+{
719- long used, total; 
720- static u_int64_t cp_time[CPUSTATES]; 
721- size_t len = sizeof(cp_time); 
722+ char row[32]; 750+ char row[32];
723+ int64_t cur_charge, max_charge; 751+ int64_t cur_charge, max_charge;
724+ Devquery dq_charge = { P_INT64, bat, "charge", NULL}; 752+ Devquery dq_charge = { P_INT64, bat, "charge", NULL};
725  753+
726- info.cpu_usage = 0; 
727+ strcpy(row, "max-value"); 754+ strcpy(row, "max-value");
728+ dq_charge.row = &row[0]; 755+ dq_charge.row = &row[0];
729  756+
730- if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) { 
731- warn("cannot get kern.cp_time"); 
732+ if (envsys_get_val(dq_charge, (void *)&max_charge) < 0) { 757+ if (envsys_get_val(dq_charge, (void *)&max_charge) < 0) {
733+ /* did not get any information from envsys */ 758+ /* did not get any information from envsys */
734+ strcpy(buf, "N/A"); 759+ strcpy(buf, "N/A");
735+ return; 760+ return;
736 } 761+ }
737  762+
738- fresh.load[0] = cp_time[CP_USER]; 
739- fresh.load[1] = cp_time[CP_NICE]; 
740- fresh.load[2] = cp_time[CP_SYS]; 
741- fresh.load[3] = cp_time[CP_IDLE]; 
742- fresh.load[4] = cp_time[CP_IDLE]; 
743+ strcpy(row, "cur-value"); 763+ strcpy(row, "cur-value");
744+ dq_charge.row = &row[0]; 764+ dq_charge.row = &row[0];
745  765+
746- used = fresh.load[0] + fresh.load[1] + fresh.load[2]; 
747- total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; 
748- 
749- if ((total - oldtotal) != 0) { 
750- info.cpu_usage = ((double) (used - oldused)) / 
751- (double) (total - oldtotal); 
752- } else { 
753- info.cpu_usage = 0; 
754+ if (envsys_get_val(dq_charge, (void *)&cur_charge) < 0) { 766+ if (envsys_get_val(dq_charge, (void *)&cur_charge) < 0) {
755+ /* did not get any information from envsys */ 767+ /* did not get any information from envsys */
756+ strcpy(buf, "N/A"); 768+ strcpy(buf, "N/A");
757+ return; 769+ return;
758 } 770 }
759  771
760- oldused = used; 772- oldused = used;
761- oldtotal = total; 773- oldtotal = total;
762+ snprintf(buf, 8, "%d%%", (int)(((float) cur_charge / max_charge) * 100)); 774+ snprintf(buf, 8, "%d%%", (int)(((float) cur_charge / max_charge) * 100));
763 } 775 }
764  776
765-void update_load_average() 777-void update_load_average()
766+int 778+int