sm_proc.c 7.5 KB

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