patch-src_openbsd_c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. $OpenBSD: patch-src_openbsd_c,v 1.18 2017/04/19 20:37:42 sthen Exp $
  2. declarations in common.h got changed, but upstream forgot to do the
  3. corresponding changes into openbsd.c
  4. Adapt to new OpenBSD kinfo_proc API.
  5. Protect kvm_getprocs and global vars with mutexes.
  6. dkstat.h is going away on OpenBSD, so use sys/sched.h instead for CP_*
  7. Make "inline void proc_find_top" static to fix building with clang.
  8. --- src/openbsd.c.orig Thu May 3 23:08:27 2012
  9. +++ src/openbsd.c Wed Apr 19 22:14:41 2017
  10. @@ -28,10 +28,11 @@
  11. *
  12. */
  13. -#include <sys/dkstat.h>
  14. #include <sys/param.h>
  15. +#include <sys/sched.h>
  16. #include <sys/resource.h>
  17. #include <sys/socket.h>
  18. +#include <sys/proc.h>
  19. #include <sys/sysctl.h>
  20. #include <sys/time.h>
  21. #include <sys/types.h>
  22. @@ -53,6 +54,7 @@
  23. #include <ifaddrs.h>
  24. #include <limits.h>
  25. #include <unistd.h>
  26. +#include <pthread.h>
  27. #include <machine/apmvar.h>
  28. #include <net80211/ieee80211.h>
  29. @@ -70,7 +72,7 @@
  30. #define LOG1024 10
  31. #define pagetok(size) ((size) << pageshift)
  32. -inline void proc_find_top(struct process **cpu, struct process **mem);
  33. +static inline void proc_find_top(struct process **cpu, struct process **mem);
  34. static short cpu_setup = 0;
  35. static kvm_t *kd = 0;
  36. @@ -81,6 +83,8 @@ size_t len = 0;
  37. int init_kvm = 0;
  38. int init_sensors = 0;
  39. +pthread_mutex_t kvm_mutex = PTHREAD_MUTEX_INITIALIZER;
  40. +
  41. static int kvm_init()
  42. {
  43. if (init_kvm) {
  44. @@ -140,7 +144,7 @@ int check_mount(char *s)
  45. return 0;
  46. }
  47. -void update_uptime()
  48. +int update_uptime()
  49. {
  50. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  51. struct timeval boottime;
  52. @@ -155,9 +159,10 @@ void update_uptime()
  53. NORM_ERR("Could not get uptime");
  54. info.uptime = 0;
  55. }
  56. + return 0;
  57. }
  58. -void update_meminfo()
  59. +int update_meminfo()
  60. {
  61. static int mib[2] = { CTL_VM, VM_METER };
  62. struct vmtotal vmtotal;
  63. @@ -194,9 +199,10 @@ void update_meminfo()
  64. info.swap = 0;
  65. info.swapfree = 0;
  66. }
  67. + return 0;
  68. }
  69. -void update_net_stats()
  70. +int update_net_stats()
  71. {
  72. struct net_stat *ns;
  73. double delta;
  74. @@ -207,11 +213,11 @@ void update_net_stats()
  75. /* get delta */
  76. delta = current_update_time - last_update_time;
  77. if (delta <= 0.0001) {
  78. - return;
  79. + return 0;
  80. }
  81. if (getifaddrs(&ifap) < 0) {
  82. - return;
  83. + return 0;
  84. }
  85. for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
  86. @@ -266,28 +272,36 @@ void update_net_stats()
  87. }
  88. freeifaddrs(ifap);
  89. + return 0;
  90. }
  91. -void update_total_processes()
  92. +int update_total_processes()
  93. {
  94. - int n_processes;
  95. + int n_processes = 0;
  96. + int max_size = sizeof(struct kinfo_proc);
  97. +
  98. kvm_init();
  99. - kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
  100. + pthread_mutex_lock(&kvm_mutex);
  101. + kvm_getprocs(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
  102. + pthread_mutex_unlock(&kvm_mutex);
  103. info.procs = n_processes;
  104. + return 0;
  105. }
  106. -void update_running_processes()
  107. +int update_running_processes()
  108. {
  109. - struct kinfo_proc2 *p;
  110. - int n_processes;
  111. + struct kinfo_proc *p;
  112. + int n_processes = 0;
  113. int i, cnt = 0;
  114. kvm_init();
  115. - int max_size = sizeof(struct kinfo_proc2);
  116. + int max_size = sizeof(struct kinfo_proc);
  117. - p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
  118. + pthread_mutex_lock(&kvm_mutex);
  119. + p = kvm_getprocs(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
  120. + pthread_mutex_unlock(&kvm_mutex);
  121. for (i = 0; i < n_processes; i++) {
  122. if (p[i].p_stat == SRUN) {
  123. cnt++;
  124. @@ -295,96 +309,64 @@ void update_running_processes()
  125. }
  126. info.run_procs = cnt;
  127. + return 0;
  128. }
  129. -/* new SMP code can be enabled by commenting the following line */
  130. -#define OLDCPU
  131. -
  132. -#ifdef OLDCPU
  133. -struct cpu_load_struct {
  134. - unsigned long load[5];
  135. -};
  136. -
  137. -struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
  138. -long cpu_used, oldtotal, oldused;
  139. -#else
  140. #include <assert.h>
  141. int64_t *fresh = NULL;
  142. /* XXX is 8 enough? - What's the constant for MAXCPU? */
  143. /* allocate this with malloc would be better */
  144. int64_t oldtotal[8], oldused[8];
  145. -#endif
  146. void get_cpu_count()
  147. {
  148. int cpu_count = 1; /* default to 1 cpu */
  149. -#ifndef OLDCPU
  150. + static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
  151. +
  152. int mib[2] = { CTL_HW, HW_NCPU };
  153. size_t len = sizeof(cpu_count);
  154. if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
  155. NORM_ERR("error getting cpu count, defaulting to 1");
  156. }
  157. -#endif
  158. +
  159. + pthread_mutex_lock(&count_mutex);
  160. +
  161. + if (info.cpu_count == cpu_count) {
  162. + pthread_mutex_unlock(&count_mutex);
  163. + return;
  164. + }
  165. +
  166. info.cpu_count = cpu_count;
  167. + free(info.cpu_usage);
  168. +
  169. info.cpu_usage = malloc(info.cpu_count * sizeof(float));
  170. if (info.cpu_usage == NULL) {
  171. CRIT_ERR(NULL, NULL, "malloc");
  172. }
  173. -#ifndef OLDCPU
  174. - assert(fresh == NULL); /* XXX Is this leaking memory? */
  175. + free(fresh);
  176. /* XXX Where shall I free this? */
  177. if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
  178. CRIT_ERR(NULL, NULL, "calloc");
  179. }
  180. -#endif
  181. +
  182. + pthread_mutex_unlock(&count_mutex);
  183. }
  184. -void update_cpu_usage()
  185. +int update_cpu_usage()
  186. {
  187. -#ifdef OLDCPU
  188. - int mib[2] = { CTL_KERN, KERN_CPTIME };
  189. - long used, total;
  190. - long cp_time[CPUSTATES];
  191. - size_t len = sizeof(cp_time);
  192. -#else
  193. size_t size;
  194. unsigned int i;
  195. -#endif
  196. /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
  197. if ((cpu_setup == 0) || (!info.cpu_usage)) {
  198. get_cpu_count();
  199. cpu_setup = 1;
  200. }
  201. -
  202. -#ifdef OLDCPU
  203. - if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
  204. - NORM_ERR("Cannot get kern.cp_time");
  205. - }
  206. -
  207. - fresh.load[0] = cp_time[CP_USER];
  208. - fresh.load[1] = cp_time[CP_NICE];
  209. - fresh.load[2] = cp_time[CP_SYS];
  210. - fresh.load[3] = cp_time[CP_IDLE];
  211. - fresh.load[4] = cp_time[CP_IDLE];
  212. -
  213. - used = fresh.load[0] + fresh.load[1] + fresh.load[2];
  214. - total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
  215. -
  216. - if ((total - oldtotal) != 0) {
  217. - info.cpu_usage[0] = ((double) (used - oldused)) /
  218. - (double) (total - oldtotal);
  219. - } else {
  220. - info.cpu_usage[0] = 0;
  221. - }
  222. -
  223. - oldused = used;
  224. - oldtotal = total;
  225. -#else
  226. +
  227. if (info.cpu_count > 1) {
  228. size = CPUSTATES * sizeof(int64_t);
  229. for (i = 0; i < info.cpu_count; i++) {
  230. @@ -426,10 +408,11 @@ void update_cpu_usage()
  231. oldused[i] = used;
  232. oldtotal[i] = total;
  233. }
  234. -#endif
  235. +
  236. + return 0;
  237. }
  238. -void update_load_average()
  239. +int update_load_average()
  240. {
  241. double v[3];
  242. @@ -438,6 +421,7 @@ void update_load_average()
  243. info.loadavg[0] = (float) v[0];
  244. info.loadavg[1] = (float) v[1];
  245. info.loadavg[2] = (float) v[2];
  246. + return 0;
  247. }
  248. #define OBSD_MAX_SENSORS 256
  249. @@ -606,10 +590,11 @@ char get_freq(char *p_client_buffer, size_t client_buf
  250. return 1;
  251. }
  252. -void update_top()
  253. +int update_top()
  254. {
  255. kvm_init();
  256. proc_find_top(info.cpu, info.memu);
  257. + return 0;
  258. }
  259. #if 0
  260. @@ -665,19 +650,11 @@ cleanup:
  261. }
  262. #endif
  263. -void clear_diskio_stats()
  264. +int update_diskio()
  265. {
  266. + return 0; /* XXX: implement? hifi: not sure how */
  267. }
  268. -struct diskio_stat *prepare_diskio_stat(const char *s)
  269. -{
  270. -}
  271. -
  272. -void update_diskio()
  273. -{
  274. - return; /* XXX: implement? hifi: not sure how */
  275. -}
  276. -
  277. /* While topless is obviously better, top is also not bad. */
  278. int comparecpu(const void *a, const void *b)
  279. @@ -706,10 +683,10 @@ int comparemem(const void *a, const void *b)
  280. return 0;
  281. }
  282. -inline void proc_find_top(struct process **cpu, struct process **mem)
  283. +static inline void proc_find_top(struct process **cpu, struct process **mem)
  284. {
  285. - struct kinfo_proc2 *p;
  286. - int n_processes;
  287. + struct kinfo_proc *p;
  288. + int n_processes = 0;
  289. int i, j = 0;
  290. struct process *processes;
  291. int mib[2];
  292. @@ -730,9 +707,11 @@ inline void proc_find_top(struct process **cpu, struct
  293. /* translate bytes into page count */
  294. total_pages = usermem / pagesize;
  295. - int max_size = sizeof(struct kinfo_proc2);
  296. + int max_size = sizeof(struct kinfo_proc);
  297. - p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
  298. +
  299. + pthread_mutex_lock(&kvm_mutex);
  300. + p = kvm_getprocs(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
  301. processes = malloc(n_processes * sizeof(struct process));
  302. for (i = 0; i < n_processes; i++) {
  303. @@ -740,9 +719,12 @@ inline void proc_find_top(struct process **cpu, struct
  304. processes[j].pid = p[i].p_pid;
  305. processes[j].name = strndup(p[i].p_comm, text_buffer_size);
  306. processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
  307. + processes[j].vsize = p[i].p_vm_map_size;
  308. + processes[j].rss = p[i].p_vm_rssize * PAGE_SIZE;
  309. j++;
  310. }
  311. }
  312. + pthread_mutex_unlock(&kvm_mutex);
  313. qsort(processes, j - 1, sizeof(struct process), comparemem);
  314. for (i = 0; i < 10; i++) {
  315. @@ -752,6 +734,8 @@ inline void proc_find_top(struct process **cpu, struct
  316. tmp->pid = processes[i].pid;
  317. tmp->amount = processes[i].amount;
  318. tmp->name = strndup(processes[i].name, text_buffer_size);
  319. + tmp->vsize = processes[i].vsize;
  320. + tmp->rss = processes[i].rss;
  321. ttmp = mem[i];
  322. mem[i] = tmp;
  323. @@ -769,6 +753,8 @@ inline void proc_find_top(struct process **cpu, struct
  324. tmp->pid = processes[i].pid;
  325. tmp->amount = processes[i].amount;
  326. tmp->name = strndup(processes[i].name, text_buffer_size);
  327. + tmp->vsize = processes[i].vsize;
  328. + tmp->rss = processes[i].rss;
  329. ttmp = cpu[i];
  330. cpu[i] = tmp;
  331. @@ -784,7 +770,6 @@ inline void proc_find_top(struct process **cpu, struct
  332. free(processes);
  333. }
  334. -#if defined(i386) || defined(__i386__)
  335. #define APMDEV "/dev/apm"
  336. #define APM_UNKNOWN 255
  337. @@ -908,7 +893,6 @@ char *get_apm_battery_time()
  338. return out;
  339. }
  340. -#endif
  341. /* empty stubs so conky links */
  342. void prepare_update()
  343. @@ -923,8 +907,4 @@ int get_entropy_avail(unsigned int *val)
  344. int get_entropy_poolsize(unsigned int *val)
  345. {
  346. return 1;
  347. -}
  348. -
  349. -void free_all_processes(void)
  350. -{
  351. }