sm_proc.c 6.8 KB

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