aboutsummaryrefslogtreecommitdiff
path: root/x11-wm/epplets/files/patch-epplets_E-MemWatch.c
blob: e8cb1adddae934aba6f4d0e8f393d00c600deda3 (plain) (blame)
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
--- epplets/E-MemWatch.c.orig	2012-09-30 11:38:35.000000000 +0200
+++ epplets/E-MemWatch.c	2013-05-15 09:51:28.000000000 +0200
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 1999-2000, Michael Jennings
+ *               2008, Pietro Cerutti <gahr@FreeBSD.org> (FreeBSD adaptation)
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -37,6 +38,13 @@
 #include "proc.h"
 #endif
 
+#ifdef __FreeBSD__
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <fcntl.h>
+#include <kvm.h>
+#endif
+
 #if 0
 #define D(x) do {printf("%10s | %7d:  [debug] ", __FILE__, __LINE__); printf x; fflush(stdout);} while (0)
 #else
@@ -74,7 +82,7 @@
    used = (unsigned long)mem.used;
    buffers = (unsigned long)mem.buffer;
    cached = (unsigned long)mem.cached;
-#else
+#elif defined(linux)
    FILE               *fp;
 
    if (!(fp = fopen("/proc/meminfo", "r")))
@@ -109,8 +117,20 @@
 	sscanf(buff, "%*s %lu %lu %*u %*u %lu %lu", &total, &used, &buffers,
 	       &cached);
      }
+#elif defined(__FreeBSD__)
+   int pagesize = getpagesize();
+   size_t len = sizeof(used);
+   sysctlbyname("hw.physmem", &total, &len, NULL, 0);
+   sysctlbyname("vm.stats.vm.v_free_count", &free, &len, NULL, 0);
+   sysctlbyname("vfs.bufspace", &buffers, &len, NULL, 0);
+   sysctlbyname("vm.stats.vm.v_cache_count", &cached, &len, NULL, 0);
+
+   cached *= pagesize;
+   free *= pagesize;
 
-#endif /* HAVE_LIBGTOP */
+   used = total - free;
+
+#endif 
 
    used -= (buffers + cached);
    mem_val = (int)((((float)used) / total) * 100.0);
@@ -123,15 +143,15 @@
      }
    else if (used < 1024 * 1024)
      {
-	Esnprintf(buff, sizeof(buff), "M: %luK", used / 1024);
+	Esnprintf(buff, sizeof(buff), "M: %dK", used / 1024);
      }
    else if (used < 1024 * 1024 * 1024)
      {
-	Esnprintf(buff, sizeof(buff), "M: %luM", used / (1024 * 1024));
+	Esnprintf(buff, sizeof(buff), "M: %dM", used / (1024 * 1024));
      }
    else
      {
-	Esnprintf(buff, sizeof(buff), "M: %luG", used / (1024 * 1024 * 1024));
+	Esnprintf(buff, sizeof(buff), "M: %.2lfG", used / (1024.0 * 1024.0 * 1024.0));
      }
    Epplet_change_label(mem_label, buff);
 
@@ -145,7 +165,7 @@
 	used = (unsigned long)swap.used;
      }
    while (swap.total == 0 && swap.used == 0 && check++ < 15);
-#else
+#elif defined(linux)
 
    if (kernel_2_6)
      {
@@ -174,8 +194,22 @@
      }
 
    fclose(fp);
-#endif /* HAVE_LIBGTOP */
-   swap_val = (int)((((float)used) / total) * 100.0);
+#elif defined(__FreeBSD__)
+   kvm_t *k;
+   struct kvm_swap swp;
+   
+   if(!(k = kvm_open(NULL, "/dev/null", NULL, O_RDONLY, "kvm_open"))) {
+      used = 0;
+      total = 1;
+   } else {
+      if(kvm_getswapinfo(k, &swp, 1, 0) != 1) {
+         used = (unsigned long)swp.ksw_used * pagesize;
+         total = (unsigned long)swp.ksw_total * pagesize;
+      }
+      kvm_close(k); 
+   }
+#endif 
+   swap_val = 100. * used / total;
 
    /*printf ("Swap: %lu %lu %d%%\n", total, used, swap_val); */
 
@@ -273,6 +307,7 @@
 {
 
    int                 prio;
+#ifdef linux
 
    /* check for Kernel 2.6 */
    FILE               *fp;
@@ -295,6 +330,7 @@
 
    fclose(fp);
    /* end check for Kernel 2.6 */
+#endif
 
    atexit(Epplet_cleanup);
    prio = getpriority(PRIO_PROCESS, getpid());