summaryrefslogtreecommitdiff
path: root/sysutils/conky/patches/patch-src_netbsd.c
blob: dad28707155c700ebb85ac1e609523c9752090fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
$NetBSD: patch-src_netbsd.c,v 1.4 2020/08/27 11:25:29 nia Exp $

Many fixes and addons for conky to work on NetBSD.

--- src/netbsd.c.orig	2012-05-03 21:08:27.000000000 +0000
+++ src/netbsd.c
@@ -30,337 +30,803 @@
 
 #include "netbsd.h"
 #include "net_stat.h"
+#include "top.h"
+#include <sys/types.h>
+#include <sys/statvfs.h>
+#include <ifaddrs.h>
 
-static kvm_t *kd = NULL;
-int kd_init = 0, nkd_init = 0;
-u_int32_t sensvalue;
-char errbuf[_POSIX2_LINE_MAX];
+#define P_BOOL		0
+#define P_UINT8		1
+#define P_INT64		2
+#define P_STRING	3
 
-static int init_kvm(void)
+typedef struct Devquery {
+	int			type;
+	char		*dev;
+	char		*key;
+	char		*row;
+} Devquery;
+
+u_int32_t		sensvalue;
+char			errbuf[_POSIX2_LINE_MAX];
+static short	cpu_setup = 0;
+
+int				sysmon_fd;
+
+static inline void proc_find_top(struct process **cpu, struct process **mem);
+
+int8_t envsys_get_val(Devquery, void *);
+
+void
+prepare_update(void)
 {
-	if (kd_init) {
-		return 0;
-	}
+}
 
-	kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
-	if (kd == NULL) {
-		warnx("cannot kvm_openfiles: %s", errbuf);
-		return -1;
+int
+update_uptime(void)
+{
+	int 			mib[2] = { CTL_KERN, KERN_BOOTTIME };
+	struct timeval	boottime;
+	time_t			now;
+	size_t			size;
+
+	size = sizeof(boottime);
+
+	if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
+		warn("sysctl kern.boottime failed");
+		info.uptime = 0;
+	} else {
+		time(&now);
+		info.uptime = now - boottime.tv_sec;
 	}
-	kd_init = 1;
+
 	return 0;
 }
 
-static int swapmode(int *retavail, int *retfree)
-{
-	int n;
-	struct swapent *sep;
+/* checks is mp is a mounted mountpoint */
+int
+check_mount(char *mp)
+{
+	int				nbmount, i;
+	struct statvfs	*mntbuf;
+
+	nbmount = getmntinfo(&mntbuf, MNT_NOWAIT);
+
+	for (i = 0; i < nbmount; i++) {
+		if (strcmp(mntbuf[i].f_mntonname, mp) == 0) {
+			return 1;
+		}
+	}
 
-	*retavail = 0;
-	*retfree = 0;
+	return 0;
+}
 
-	n = swapctl(SWAP_NSWAP, 0, 0);
+/* mostly from vmstat.c */
+int
+update_meminfo(void)
+{
+	int						mib[] = { CTL_VM, VM_UVMEXP2 };
+	struct uvmexp_sysctl	uvmexp;
+	size_t					ssize;
+
+	ssize = sizeof(uvmexp);
+	memset(&uvmexp, 0, ssize);
+
+	info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = 0;
+	info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
+	info.bufmem = 0;
 
-	if (n < 1) {
-		warn("could not get swap information");
+	if (sysctl(mib, 2, &uvmexp, &ssize, NULL, 0) < 0) {
+		warn("sysctl vm.uvmexp2 failed");
 		return 0;
 	}
 
-	sep = (struct swapent *) malloc(n * (sizeof(*sep)));
+	info.memmax = uvmexp.npages * uvmexp.pagesize / 1024;
+	info.memfree = uvmexp.free * uvmexp.pagesize / 1024;
+	info.memeasyfree = uvmexp.inactive * uvmexp.pagesize / 1024;
+
+	info.swapmax = uvmexp.swpages * uvmexp.pagesize / 1024;
+	info.swapfree = (uvmexp.swpages - uvmexp.swpginuse) * \
+		uvmexp.pagesize / 1024;
+
+	info.buffers = uvmexp.filepages * uvmexp.pagesize / 1024;
+	info.cached = uvmexp.execpages * uvmexp.pagesize / 1024;
+
+	info.mem = info.memmax - info.memfree;
+	info.bufmem = info.cached + info.buffers;
+	info.swap = info.swapmax - info.swapfree;
+
+	/*
+	 * Counter-adjust for the adjustment in update_stuff in common.c so
+	 * that memeasyfree is the inactive memory.  Since inactive memory
+	 * partially overlaps with buffer memory, but the size of the
+	 * overlap is not measured, attempting to split it into non-bufmem
+	 * and bufmem parts, as common.c does, can't work.  So instead we
+	 * report inactive memory as memeasyfree.
+	 */
+	info.memeasyfree -= info.bufmem;
+
+	return 0;
+}
+
+int
+update_net_stats(void)
+{
+	struct net_stat *ns;
+	double delta;
+	long long r, t, last_recv, last_trans;
+	struct ifaddrs *ifap, *ifa;
+	struct if_data *ifd;
 
-	if (sep == NULL) {
-		warn("memory allocation failed");
+	/* get delta */
+	delta = current_update_time - last_update_time;
+	if (delta <= 0.0001) {
 		return 0;
 	}
 
-	if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
-		warn("could not get swap stats");
+	if (getifaddrs(&ifap) < 0) {
 		return 0;
 	}
-	for (; n > 0; n--) {
-		*retavail += (int) dbtob(sep[n - 1].se_nblks);
-		*retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
+
+	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+		ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
+
+		if (ifa->ifa_flags & IFF_UP) {
+			struct ifaddrs *iftmp;
+
+			ns->up = 1;
+			last_recv = ns->recv;
+			last_trans = ns->trans;
+
+			if (ifa->ifa_addr->sa_family != AF_LINK) {
+				continue;
+			}
+
+			for (iftmp = ifa->ifa_next;
+				 iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
+				 iftmp = iftmp->ifa_next) {
+				if (iftmp->ifa_addr->sa_family == AF_INET) {
+					memcpy(&(ns->addr), iftmp->ifa_addr,
+						iftmp->ifa_addr->sa_len);
+				}
+			}
+
+			ifd = (struct if_data *) ifa->ifa_data;
+			r = ifd->ifi_ibytes;
+			t = ifd->ifi_obytes;
+
+			if (r < ns->last_read_recv) {
+				ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
+			} else {
+				ns->recv += (r - ns->last_read_recv);
+			}
+
+			ns->last_read_recv = r;
+
+			if (t < ns->last_read_trans) {
+				ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
+			} else {
+				ns->trans += (t - ns->last_read_trans);
+			}
+
+			ns->last_read_trans = t;
+
+			/* calculate speeds */
+			ns->recv_speed = (ns->recv - last_recv) / delta;
+			ns->trans_speed = (ns->trans - last_trans) / delta;
+		} else {
+			ns->up = 0;
+		}
 	}
-	*retavail = (int) (*retavail / 1024);
-	*retfree = (int) (*retfree / 1024);
 
-	return 1;
-}
+	freeifaddrs(ifap);
 
-void prepare_update()
-{
+	return 0;
 }
 
-void update_uptime()
+int
+update_total_processes(void)
 {
-	int mib[2] = { CTL_KERN, KERN_BOOTTIME };
-	struct timeval boottime;
-	time_t now;
-	int size = sizeof(boottime);
+	int		mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
+	size_t	size;
 
-	if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
-			&& (boottime.tv_sec != 0)) {
-		time(&now);
-		info.uptime = now - boottime.tv_sec;
-	} else {
-		warn("could not get uptime");
-		info.uptime = 0;
+	if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
+		warn("sysctl KERN_PROC_ALL failed");
+		return 0;
 	}
+
+	info.procs = (size / sizeof (struct kinfo_proc));
+
+	return 0;
 }
 
-int check_mount(char *s)
+int
+update_running_processes()
 {
-	/* stub */
+	int					n_processes, i, cnt = 0;
+	struct kinfo_proc2	*p;
+
+	info.run_procs = 0;
+
+	p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
+		&n_processes);
+
+	for (i = 0; i < n_processes; i++)
+		if (p[i].p_stat == LSRUN ||
+			p[i].p_stat == LSIDL || 
+			p[i].p_stat == LSONPROC)
+			cnt++;
+
+	info.run_procs = cnt;
+
 	return 0;
 }
 
-void update_meminfo()
+struct cpu_load_struct {
+	unsigned long load[5];
+};
+
+struct cpu_load_struct fresh = {
+	{0, 0, 0, 0, 0}
+};
+
+long *oldtotal = NULL, *oldused = NULL;
+
+void
+get_cpu_count()
 {
-	int mib[] = { CTL_VM, VM_UVMEXP2 };
-	int total_pages, inactive_pages, free_pages;
-	int swap_avail, swap_free;
-	const int pagesize = getpagesize();
-	struct uvmexp_sysctl uvmexp;
-	size_t size = sizeof(uvmexp);
+	static int mib[] = { CTL_HW, HW_NCPU };
+	size_t len = sizeof(int);
+	int cpu_count;
 
-	if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
-		warn("could not get memory info");
-		return;
-	}
+	if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) < 0)
+		cpu_count = 1;
 
-	total_pages = uvmexp.npages;
-	free_pages = uvmexp.free;
-	inactive_pages = uvmexp.inactive;
+	info.cpu_count = cpu_count;
 
-	info.memmax = (total_pages * pagesize) >> 10;
-	info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
-	info.memeasyfree = info.memfree = info.memmax - info.mem;
+	info.cpu_usage = malloc(info.cpu_count * sizeof(float));
 
-	if (swapmode(&swap_avail, &swap_free) >= 0) {
-		info.swapmax = swap_avail;
-		info.swap = (swap_avail - swap_free);
-		info.swapfree = swap_free;
-	}
+	if (info.cpu_usage == NULL)
+		warn("malloc");
 }
 
-void update_net_stats()
+struct cpu_info {
+	long oldtotal;
+	long oldused;
+};
+
+int
+update_cpu_usage(void)
 {
-	int i;
-	double delta;
-	struct ifnet ifnet;
-	struct ifnet_head ifhead;	/* interfaces are in a tail queue */
-	u_long ifnetaddr;
-	static struct nlist namelist[] = {
-		{ "_ifnet" },
-		{ NULL }
-	};
-	static kvm_t *nkd;
-
-	if (!nkd_init) {
-		nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
-		if (nkd == NULL) {
-			warnx("cannot kvm_openfiles: %s", errbuf);
-			warnx("maybe you need to setgid kmem this program?");
-			return;
-		} else if (kvm_nlist(nkd, namelist) != 0) {
-			warn("cannot kvm_nlist");
-			return;
-		} else {
-			nkd_init = 1;
-		}
+	/* mostly taken from freebsd.c */
+	int i, j = 0;
+	uint64_t used, total;
+	uint64_t *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)) {
+		get_cpu_count();
+		cpu_setup = 1;
 	}
 
-	if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
-			sizeof(ifhead)) < 0) {
-		warn("cannot kvm_read");
-		return;
+	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;
 	}
 
-	/* get delta */
-	delta = current_update_time - last_update_time;
-	if (delta <= 0.0001) {
-		return;
+	/* cpu[0] is overall stats, get it from separate sysctl */
+	cp_len = CPUSTATES * sizeof(uint64_t);
+	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");
 	}
 
-	for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
-			ifnet.if_list.tqe_next && i < 16;
-			ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
-
-		struct net_stat *ns;
-		long long last_recv, last_trans;
-
-		kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
-		ns = get_net_stat(ifnet.if_xname, NULL, NULL);
-		ns->up = 1;
-		last_recv = ns->recv;
-		last_trans = ns->trans;
-
-		if (ifnet.if_ibytes < ns->last_read_recv) {
-			ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
-				ifnet.if_ibytes;
-		} else {
-			ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
-		}
+	total = 0;
+	for (j = 0; j < CPUSTATES; j++)
+		total += cp_time[j];
+
+	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;
+	}
+
+	cpu[0].oldused = used;
+	cpu[0].oldtotal = total;
+
+	free(cp_time);
+
+	/* per-core stats */
+	cp_len = CPUSTATES * sizeof(uint64_t) * 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_time", cp_time, &cp_len, NULL, 0) < 0 && errno != ENOMEM) {
+		fprintf(stderr, "Cannot get kern.cp_time SMP\n");
+	}
+
+	for (i = 0; i < info.cpu_count; i++)
+	{
+		total = 0;
+		for (j = 0; j < CPUSTATES; j++)
+			total += cp_time[i*CPUSTATES + j];
 
-		ns->last_read_recv = ifnet.if_ibytes;
+		used = total - cp_time[i*CPUSTATES + CP_IDLE];
 
-		if (ifnet.if_obytes < ns->last_read_trans) {
-			ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
-				ifnet.if_obytes;
+		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 {
-			ns->trans += (ifnet.if_obytes - ns->last_read_trans);
+			info.cpu_usage[i+1] = 0;
 		}
 
-		ns->last_read_trans = ifnet.if_obytes;
+		cpu[i+1].oldused = used;
+		cpu[i+1].oldtotal = total;
+	}
+
+	free(cp_time);
+	return 0;
+}
 
-		ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
-		ns->last_read_recv = ifnet.if_ibytes;
-		ns->trans += (ifnet.if_obytes - ns->last_read_trans);
-		ns->last_read_trans = ifnet.if_obytes;
+int update_load_average(void)
+{
+	double v[3];
+
+	getloadavg(v, 3);
 
-		ns->recv_speed = (ns->recv - last_recv) / delta;
-		ns->trans_speed = (ns->trans - last_trans) / delta;
+	info.loadavg[0] = (float) v[0];
+	info.loadavg[1] = (float) v[1];
+	info.loadavg[2] = (float) v[2];
+
+	return 0;
+}
+
+int open_acpi_temperature(const char *name)
+{
+	(void)name; /* useless on NetBSD */
+	return -1;
+}
+
+int get_entropy_avail(unsigned int *val)
+{
+	return 1;
+}
+
+int get_entropy_poolsize(unsigned int *val)
+{
+	return 1;
+}
+
+/* void */
+char
+get_freq(char *p_client_buffer, size_t client_buffer_size,
+	const char *p_format, int divisor, unsigned int cpu)
+{
+	int freq = cpu;
+
+	if (!p_client_buffer || client_buffer_size <= 0 || !p_format
+		|| divisor <= 0) {
+		return 0;
 	}
+
+	size_t size = sizeof(freq);
+
+	if (sysctlbyname("machdep.est.frequency.current",
+			NULL, &size, NULL, 0) == 0) {
+		sysctlbyname("machdep.est.frequency.current", &freq, &size, NULL, 0);
+		snprintf(p_client_buffer, client_buffer_size, p_format,
+			(float) freq / divisor);
+	} else if (sysctlbyname("machdep.tsc_freq", NULL, &size, NULL, 0) == 0) {
+		sysctlbyname("machdep.tsc_freq", &freq, &size, NULL, 0);
+		snprintf(p_client_buffer, client_buffer_size, p_format,
+			(float) freq / (1000000 * divisor));
+	} else
+		snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
+
+	return 1;
 }
 
-void update_total_processes()
+void update_diskio()
 {
-	/* It's easier to use kvm here than sysctl */
+	return; /* XXX: implement? hifi: not sure how */
+}
 
-	int n_processes;
+int update_top()
+{
+	proc_find_top(info.cpu, info.memu);
 
-	info.procs = 0;
+	return 0;
+}
 
-	if (init_kvm() < 0) {
-		return;
-	} else {
-		kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
-			&n_processes);
+int comparecpu(const void *a, const void *b)
+{
+	if (((struct process *) a)->amount > ((struct process *) b)->amount) {
+		return -1;
+	}
+	if (((struct process *) a)->amount < ((struct process *) b)->amount) {
+		return 1;
 	}
 
-	info.procs = n_processes;
+	return 0;
 }
 
-void update_running_processes()
+int comparemem(const void *a, const void *b)
 {
-	struct kinfo_proc2 *p;
-	int n_processes;
-	int i, cnt = 0;
+    if (((struct process *) a)->rss > ((struct process *) b)->rss) {
+		return -1;
+	}
 
-	info.run_procs = 0;
+	if (((struct process *) a)->rss < ((struct process *) b)->rss) {
+		return 1;
+	}
 
-	if (init_kvm() < 0) {
-		return;
-	} else {
-		p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
-			&n_processes);
-		for (i = 0; i < n_processes; i++) {
-			if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
-					|| p[i].p_stat == LSONPROC) {
-				cnt++;
+	return 0;
+}
+
+inline void proc_find_top(struct process **cpu, struct process **mem)
+{
+        struct kinfo_proc2 *p;
+        int n_processes;
+        int i, j = 0;
+        struct process *processes;
+        int mib[2];
+
+        u_int total_pages;
+        int64_t usermem;
+        int pagesize = getpagesize();
+
+        /* we get total pages count again to be sure it is up to date */
+        mib[0] = CTL_HW;
+        mib[1] = HW_USERMEM64;
+        size_t size = sizeof(usermem);
+
+        if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
+                err(EXIT_FAILURE, "error reading usermem");
+        }
+
+        /* translate bytes into page count */
+        total_pages = usermem / pagesize;
+
+        int max_size = sizeof(struct kinfo_proc2);
+
+        p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
+        processes = malloc(n_processes * sizeof(struct process));
+
+        for (i = 0; i < n_processes; i++) {
+			if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
+				processes[j].pid = p[i].p_pid;
+				processes[j].name = strndup(p[i].p_comm, text_buffer_size);
+				processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
+				processes[j].rss = p[i].p_vm_rssize * pagesize;
+				processes[j].vsize = p[i].p_vm_vsize;
+				j++;
 			}
-		}
-	}
+        }
 
-	info.run_procs = cnt;
+        qsort(processes, j - 1, sizeof(struct process), comparemem);
+        for (i = 0; i < 10; 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 = mem[i];
+			mem[i] = tmp;
+			if (ttmp != NULL) {
+				free(ttmp->name);
+				free(ttmp);
+			}
+        }
+
+        qsort(processes, j - 1, sizeof(struct process), comparecpu);
+        for (i = 0; i < 10; 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 = cpu[i];
+			cpu[i] = tmp;
+			if (ttmp != NULL) {
+				free(ttmp->name);
+				free(ttmp);
+			}
+        }
+
+        for (i = 0; i < j; i++) {
+			free(processes[i].name);
+        }
+        free(processes);
 }
 
-struct cpu_load_struct {
-	unsigned long load[5];
-};
+double
+get_acpi_temperature(int fd)
+{
+	Devquery dq_tz = { P_INT64, "acpitz0", "temperature", "cur-value" };
+	int64_t temp;
 
-struct cpu_load_struct fresh = {
-	{0, 0, 0, 0, 0}
-};
+	(void)fd;
+
+	if (envsys_get_val(dq_tz, (void *)&temp) < 0)
+		return 0.0;
 
-long cpu_used, oldtotal, oldused;
+	return (temp / 1000000.0) - 273.15;
+}
 
-void update_cpu_usage()
+void
+get_bat_life(char *bat, char *buf)
 {
-	long used, total;
-	static u_int64_t cp_time[CPUSTATES];
-	size_t len = sizeof(cp_time);
+	char row[32];
+	int64_t cur_charge, max_charge;
+	Devquery dq_charge = { P_INT64, bat, "charge", NULL};
 
-	info.cpu_usage = 0;
+	strcpy(row, "max-value");
+	dq_charge.row = &row[0];
 
-	if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
-		warn("cannot get kern.cp_time");
+	if (envsys_get_val(dq_charge, (void *)&max_charge) < 0) {
+		/* did not get any information from envsys */
+		strcpy(buf, "N/A");
+		return;
 	}
 
-	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];
+	strcpy(row, "cur-value");
+	dq_charge.row = &row[0];
 
-	used = fresh.load[0] + fresh.load[1] + fresh.load[2];
-	total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
-
-	if ((total - oldtotal) != 0) {
-		info.cpu_usage = ((double) (used - oldused)) /
-			(double) (total - oldtotal);
-	} else {
-		info.cpu_usage = 0;
+	if (envsys_get_val(dq_charge, (void *)&cur_charge) < 0) {
+		/* did not get any information from envsys */
+		strcpy(buf, "N/A");
+		return;
 	}
 
-	oldused = used;
-	oldtotal = total;
+	snprintf(buf, 8, "%d%%", (int)(((float) cur_charge / max_charge) * 100));
 }
 
-void update_load_average()
+int
+get_bat_state(char *bat, char *buf)
 {
-	double v[3];
+	bool connected = false, charging = false;
+	char curcap[8];
+	Devquery dq_ac = { P_BOOL, "acpiacad0", "connected", "cur-value" };
+	Devquery dq_charging = { P_BOOL, bat, "charging", "cur-value" };
 
-	getloadavg(v, 3);
+	/* get AC state */
+	if (envsys_get_val(dq_ac, (void *)&connected) < 0) {
+		/* did not get any information from envsys */
+		strcpy(buf, "N/A");
+		return 0;
+	}
 
-	info.loadavg[0] = (float) v[0];
-	info.loadavg[1] = (float) v[1];
-	info.loadavg[2] = (float) v[2];
+	/* used by get_acpi_ac_adapter */
+	if (bat == NULL)
+		return connected;
+
+	/* is the battery charging ? */
+	(void)envsys_get_val(dq_charging, (void *)&charging);
+
+	/* get its current cap */
+	get_bat_life(bat, &curcap[0]);
+
+	if (connected)
+		if (charging)
+			snprintf(buf, 256, "charging (%s)", curcap);
+		else
+			strcpy(buf, "on-line");
+	else
+		snprintf(buf, 256, "off-line (%s)", curcap);
+
+	return 0;
 }
 
-double get_acpi_temperature(int fd)
+void
+get_bat_time(char *bat, char *buf, unsigned int n)
 {
-	return -1;
+	int64_t charge, discharge;
+	int	hours, minutes;
+	Devquery dq_discharge = { P_INT64, bat, "discharge rate",
+							  "cur-value"};
+	Devquery dq_charge = { P_INT64, bat, "charge", "cur-value"};
+
+	if ((envsys_get_val(dq_discharge, (void *)&discharge) < 0) || !discharge) {
+		strcpy(buf, "N/A");
+		return;
+	}
+	if (envsys_get_val(dq_charge, (void *)&charge) < 0) {
+		strcpy(buf, "N/A");
+		return;
+	}
+
+	hours = (int)((float) charge / discharge);
+	minutes = (int)((((float) charge / discharge) - hours) * 60);
+
+	snprintf(buf, n, "%d:%02d", hours, minutes);
 }
 
-void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
+void
+get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
 {
+	int bat_num;
+	char b_name[32];
+
+	sscanf(bat, "BAT%d", &bat_num);
+	sprintf(b_name, "acpibat%d", bat_num);
+
+	switch (item) {
+	case BATTERY_TIME:
+		get_bat_time(b_name, buf, n);
+		break;
+	case BATTERY_STATUS:
+		get_bat_state(b_name, buf);
+		break;
+	default:
+		fprintf(stderr, "Unknown requested battery stat %d\n", item);
+	}
 }
 
-int open_acpi_temperature(const char *name)
+void
+get_battery_short_status(char *buffer, unsigned int n, const char *bat)
 {
-	return -1;
+	get_battery_stuff(buffer, n, bat, BATTERY_STATUS);
+	if (0 == strncmp("charging", buffer, 8)) {
+		buffer[0] = 'C';
+		memmove(buffer + 1, buffer + 8, n - 8);
+	} else if (0 == strncmp("off-line", buffer, 11)) {
+		buffer[0] = 'D';
+		memmove(buffer + 1, buffer + 11, n - 11);
+	} else if (0 == strncmp("on-line", buffer, 12)) {
+		buffer[0] = 'A';
+		memmove(buffer + 1, buffer + 12, n - 12);
+	}
 }
 
-void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter)
+void
+get_acpi_ac_adapter(char *p_client_buffer,
+	size_t client_buffer_size, const char *adapter)
 {
-	(void) adapter; // only linux uses this
+	int connected;
+
+	(void)adapter; // only linux uses this
 
 	if (!p_client_buffer || client_buffer_size <= 0) {
 		return;
 	}
 
-	/* not implemented */
-	memset(p_client_buffer, 0, client_buffer_size);
+	connected = get_bat_state(NULL, NULL);
+
+	if (connected) {
+		strncpy(p_client_buffer, "Running on AC Power", client_buffer_size);
+	} else {
+		strncpy(p_client_buffer, "Running on battery", client_buffer_size);
+	}
 }
 
-/* char *get_acpi_fan() */
-void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
+int
+get_battery_perct(const char *bat)
 {
-	if (!p_client_buffer || client_buffer_size <= 0) {
-		return;
+	int bat_num, batperct;
+	char b_name[32];
+	int64_t cur_charge, max_charge;
+	Devquery dq_charge = { P_INT64, NULL, "charge", NULL};
+
+	sscanf(bat, "BAT%d", &bat_num);
+	snprintf(b_name, sizeof(b_name), "acpibat%d", bat_num);
+
+	dq_charge.dev = b_name;
+
+	dq_charge.row = "max-value";
+
+	if (envsys_get_val(dq_charge, (void *)&max_charge) < 0) {
+		return 0;
+	}
+
+	dq_charge.row = "cur-value";
+
+	if (envsys_get_val(dq_charge, (void *)&cur_charge) < 0) {
+		return 0;
 	}
 
+	return (int)(((float) cur_charge / max_charge) * 100);
+}
+
+int
+get_battery_perct_bar(const char *bat)
+{
+	int batperct = get_battery_perct(bat);
+	return (int)(batperct * 2.56 - 1);
+}
+
+void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
+{
 	/* not implemented */
-	memset(p_client_buffer, 0, client_buffer_size);
+	if (p_client_buffer && client_buffer_size > 0) {
+		memset(p_client_buffer, 0, client_buffer_size);
+	}
 }
 
-int get_entropy_avail(unsigned int *val)
+/*
+ * Here comes the mighty envsys backend
+ */
+void
+sysmon_open()
 {
-	return 1;
+    sysmon_fd = open(_DEV_SYSMON, O_RDONLY);
 }
 
-int get_entropy_poolsize(unsigned int *val)
+void
+sysmon_close()
 {
-	return 1;
+	if (sysmon_fd > -1)
+		close(sysmon_fd);
+}
+
+int8_t
+envsys_get_val(Devquery dq, void *val)
+{
+	char *descr;
+	const char *cval;
+	prop_dictionary_t dict;
+	prop_object_t device;
+	prop_object_iterator_t iter;
+	prop_object_t obj;
+	bool rc = false;
+
+	if (sysmon_fd < 0)
+		return -1;
+
+    if (prop_dictionary_recv_ioctl(sysmon_fd, ENVSYS_GETDICTIONARY, &dict)
+		!= 0)
+		return -1;
+
+	if ((device = prop_dictionary_get(dict, dq.dev)) == NULL)
+		return -1;
+      
+    iter = prop_array_iterator(device);
+
+    while((obj = prop_object_iterator_next(iter)))  {
+        descr = (char *)prop_string_cstring_nocopy(prop_dictionary_get(obj,
+				"description"));
+		if (descr != NULL && *descr) {
+			if(strcmp(descr, dq.key) == 0) {
+				switch(dq.type) {
+				case P_BOOL:
+					rc = prop_dictionary_get_bool(obj,
+						dq.row, (bool *)val);
+				case P_UINT8:
+					rc = prop_dictionary_get_uint8(obj,
+						dq.row, (uint8_t *)val);
+					break;
+				case P_INT64:
+					rc = prop_dictionary_get_int64(obj, dq.row,
+						(int64_t *)val);
+					break;
+				case P_STRING:
+					rc = prop_dictionary_get_cstring_nocopy(obj,
+						dq.row, &cval);
+					val = (void *)cval;
+					break;
+				}
+			}
+		}
+    }
+
+	prop_object_iterator_release(iter);
+	prop_object_release(dict);
+
+	if (rc == false) {
+		val = NULL;
+		return -1;
+	}
+
+	return 0;
 }