sm_proc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2001-2010 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get process statistics from kernel and return them in symon_buf as
  32. *
  33. * number of processes : ticks_user : ticks_system : ticks_interrupt :
  34. * cpuseconds : procsizes : resident segment sizes
  35. *
  36. */
  37. #include "conf.h"
  38. #include <sys/param.h>
  39. #include <sys/sysctl.h>
  40. #include <limits.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include "error.h"
  44. #include "symon.h"
  45. #include "xmalloc.h"
  46. #define pagetob(size) (((u_int32_t)size) << proc_pageshift)
  47. /* Globals for this module start with proc_ */
  48. #ifdef HAS_KERN_PROC2
  49. static struct kinfo_proc2 *proc_ps = NULL;
  50. #else
  51. static struct kinfo_proc *proc_ps = NULL;
  52. #endif
  53. static int proc_max = 0;
  54. static int proc_cur = 0;
  55. static int proc_stathz = 0;
  56. static int proc_pageshift;
  57. static int proc_pagesize;
  58. /* get scale factor cpu percentage counter */
  59. #define FIXED_PCTCPU FSCALE
  60. typedef long pctcpu;
  61. #define pctdouble(p) ((double)(p) / FIXED_PCTCPU)
  62. void
  63. gets_proc()
  64. {
  65. int mib[6];
  66. int procs;
  67. size_t size;
  68. /* how much memory is needed */
  69. mib[0] = CTL_KERN;
  70. mib[1] = KERN_NPROCS;
  71. size = sizeof(procs);
  72. if (sysctl(mib, 2, &procs, &size, NULL, 0) < 0) {
  73. fatal("%s:%d: sysctl failed: can't get kern.nproc",
  74. __FILE__, __LINE__);
  75. }
  76. #ifdef HAS_KERN_PROC2
  77. /* increase buffers if necessary */
  78. if (procs > proc_max) {
  79. proc_max = (procs * 5) / 4;
  80. if (proc_max > SYMON_MAX_DOBJECTS) {
  81. fatal("%s:%d: dynamic object limit (%d) exceeded for kinfo_proc2 structures",
  82. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  83. }
  84. proc_ps = xrealloc(proc_ps, proc_max * sizeof(struct kinfo_proc2));
  85. }
  86. /* read data in anger */
  87. mib[0] = CTL_KERN;
  88. mib[1] = KERN_PROC2;
  89. mib[2] = KERN_PROC_KTHREAD;
  90. mib[3] = 0;
  91. mib[4] = sizeof(struct kinfo_proc2);
  92. mib[5] = proc_max;
  93. size = proc_max * sizeof(struct kinfo_proc2);
  94. if (sysctl(mib, 6, proc_ps, &size, NULL, 0) < 0) {
  95. warning("proc probe cannot get processes");
  96. proc_cur = 0;
  97. return;
  98. }
  99. if (size % sizeof(struct kinfo_proc2) != 0) {
  100. warning("proc size mismatch: got %d bytes, not dividable by sizeof(kinfo_proc2) %d",
  101. size, sizeof(struct kinfo_proc2));
  102. proc_cur = 0;
  103. } else {
  104. proc_cur = size / sizeof(struct kinfo_proc2);
  105. }
  106. #else
  107. /* increase buffers if necessary */
  108. if (procs > proc_max) {
  109. proc_max = (procs * 5) / 4;
  110. if (proc_max > SYMON_MAX_DOBJECTS) {
  111. fatal("%s:%d: dynamic object limit (%d) exceeded for kinfo_proc structures",
  112. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  113. }
  114. proc_ps = xrealloc(proc_ps, proc_max * sizeof(struct kinfo_proc));
  115. }
  116. /* read data in anger */
  117. mib[0] = CTL_KERN;
  118. mib[1] = KERN_PROC;
  119. mib[2] = KERN_PROC_KTHREAD;
  120. size = proc_max * sizeof(struct kinfo_proc);
  121. if (sysctl(mib, 3, proc_ps, &size, NULL, 0) < 0) {
  122. warning("proc probe cannot get processes");
  123. proc_cur = 0;
  124. return;
  125. }
  126. if (size % sizeof(struct kinfo_proc) != 0) {
  127. warning("proc size mismatch: got %d bytes, not dividable by sizeof(kinfo_proc) %d",
  128. size, sizeof(struct kinfo_proc));
  129. proc_cur = 0;
  130. } else {
  131. proc_cur = size / sizeof(struct kinfo_proc);
  132. }
  133. #endif
  134. }
  135. void
  136. privinit_proc()
  137. {
  138. /* EMPTY */
  139. }
  140. void
  141. init_proc(struct stream *st)
  142. {
  143. int mib[2] = {CTL_KERN, KERN_CLOCKRATE};
  144. struct clockinfo cinf;
  145. size_t size = sizeof(cinf);
  146. /* get clockrate */
  147. if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1) {
  148. fatal("%s:%d: could not get clockrate", __FILE__, __LINE__);
  149. }
  150. proc_stathz = cinf.stathz;
  151. /* get pagesize */
  152. proc_pagesize = sysconf(_SC_PAGESIZE);
  153. proc_pageshift = 0;
  154. while (proc_pagesize > 1) {
  155. proc_pageshift++;
  156. proc_pagesize >>= 1;
  157. }
  158. info("started module proc(%.200s)", st->arg);
  159. }
  160. int
  161. get_proc(char *symon_buf, int maxlen, struct stream *st)
  162. {
  163. int i;
  164. #ifdef HAS_KERN_PROC2
  165. struct kinfo_proc2 *pp;
  166. #else
  167. struct kinfo_proc *pp;
  168. #endif
  169. u_quad_t cpu_ticks = 0;
  170. u_quad_t cpu_uticks = 0;
  171. u_quad_t cpu_iticks = 0;
  172. u_quad_t cpu_sticks =0;
  173. u_int32_t cpu_secs = 0;
  174. double cpu_pct = 0;
  175. double cpu_pcti = 0;
  176. u_int32_t mem_procsize = 0;
  177. u_int32_t mem_rss = 0;
  178. int n = 0;
  179. for (pp = proc_ps, i = 0; i < proc_cur; pp++, i++) {
  180. #ifdef HAS_KERN_PROC2
  181. if (strncmp(st->arg, pp->p_comm, strlen(st->arg)) == 0) {
  182. /* cpu time - accumulated */
  183. cpu_uticks += pp->p_uticks; /* user */
  184. cpu_sticks += pp->p_sticks; /* sys */
  185. cpu_iticks += pp->p_iticks; /* int */
  186. /* cpu time - percentage since last measurement */
  187. cpu_pct = pctdouble(pp->p_pctcpu) * 100.0;
  188. cpu_pcti += cpu_pct;
  189. /* memory size - shared pages are counted multiple times */
  190. mem_procsize += pagetob(pp->p_vm_tsize + /* text pages */
  191. pp->p_vm_dsize + /* data */
  192. pp->p_vm_ssize); /* stack */
  193. mem_rss += pagetob(pp->p_vm_rssize); /* rss */
  194. #else
  195. if (strncmp(st->arg, pp->kp_proc.p_comm, strlen(st->arg)) == 0) {
  196. /* cpu time - accumulated */
  197. cpu_uticks += pp->kp_proc.p_uticks; /* user */
  198. cpu_sticks += pp->kp_proc.p_sticks; /* sys */
  199. cpu_iticks += pp->kp_proc.p_iticks; /* int */
  200. /* cpu time - percentage since last measurement */
  201. cpu_pct = pctdouble(pp->kp_proc.p_pctcpu) * 100.0;
  202. cpu_pcti += cpu_pct;
  203. /* memory size - shared pages are counted multiple times */
  204. mem_procsize += pagetob(pp->kp_eproc.e_vm.vm_tsize + /* text pages */
  205. pp->kp_eproc.e_vm.vm_dsize + /* data */
  206. pp->kp_eproc.e_vm.vm_ssize); /* stack */
  207. mem_rss += pagetob(pp->kp_eproc.e_vm.vm_rssize); /* rss */
  208. #endif
  209. n++;
  210. }
  211. }
  212. /* calc total cpu_secs spent */
  213. cpu_ticks = cpu_uticks + cpu_sticks + cpu_iticks;
  214. cpu_secs = cpu_ticks / proc_stathz;
  215. return snpack(symon_buf, maxlen, st->arg, MT_PROC,
  216. n,
  217. cpu_uticks, cpu_sticks, cpu_iticks, cpu_secs, cpu_pcti,
  218. mem_procsize, mem_rss );
  219. }