sm_proc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* $Id: sm_proc.c,v 1.4 2005/10/18 19:58:09 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2004 Matthew Gream
  4. * Copyright (c) 2001-2005 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 <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) ((size) << proc_pageshift)
  47. /* Globals for this module start with proc_ */
  48. static struct kinfo_proc *proc_ps = NULL;
  49. static int proc_max = 0;
  50. static int proc_cur = 0;
  51. static int proc_stathz = 0;
  52. static int proc_pageshift;
  53. static int proc_pagesize;
  54. /* get scale factor cpu percentage counter */
  55. #define FIXED_PCTCPU FSCALE
  56. typedef long pctcpu;
  57. #define pctdouble(p) ((double)(p) / FIXED_PCTCPU)
  58. void
  59. gets_proc()
  60. {
  61. int mib[3];
  62. int procs;
  63. size_t size;
  64. /* how much memory is needed */
  65. mib[0] = CTL_KERN;
  66. mib[1] = KERN_MAXPROC;
  67. size = sizeof(procs);
  68. if (sysctl(mib, 2, &procs, &size, NULL, 0) < 0) {
  69. fatal("%s:%d: sysctl failed: can't get kern.nproc",
  70. __FILE__, __LINE__);
  71. }
  72. /* increase buffers if necessary */
  73. if (procs > proc_max) {
  74. proc_max = (procs * 5) / 4;
  75. if (proc_max > SYMON_MAX_DOBJECTS) {
  76. fatal("%s:%d: dynamic object limit (%d) exceeded for kinfo_proc structures",
  77. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  78. }
  79. proc_ps = xrealloc(proc_ps, proc_max * sizeof(struct kinfo_proc));
  80. }
  81. /* read data in anger */
  82. mib[0] = CTL_KERN;
  83. mib[1] = KERN_PROC;
  84. mib[2] = KERN_PROC_ALL;
  85. size = proc_max * sizeof(struct kinfo_proc);
  86. if (sysctl(mib, 3, proc_ps, &size, NULL, 0) < 0) {
  87. warning("proc probe cannot get processes");
  88. proc_cur = 0;
  89. return;
  90. }
  91. if (size % sizeof(struct kinfo_proc) != 0) {
  92. warning("proc size mismatch: got %d bytes, not dividable by sizeof(kinfo_proc) %d",
  93. size, sizeof(struct kinfo_proc));
  94. proc_cur = 0;
  95. } else {
  96. proc_cur = size / sizeof(struct kinfo_proc);
  97. }
  98. }
  99. void
  100. privinit_proc()
  101. {
  102. /* EMPTY */
  103. }
  104. void
  105. init_proc(struct stream *st)
  106. {
  107. int mib[2] = {CTL_KERN, KERN_CLOCKRATE};
  108. struct clockinfo cinf;
  109. size_t size = sizeof(cinf);
  110. /* get clockrate */
  111. if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1) {
  112. fatal("%s:%d: could not get clockrate", __FILE__, __LINE__);
  113. }
  114. proc_stathz = cinf.stathz;
  115. /* get pagesize */
  116. proc_pagesize = sysconf(_SC_PAGESIZE);
  117. proc_pageshift = 0;
  118. while (proc_pagesize > 1) {
  119. proc_pageshift++;
  120. proc_pagesize >>= 1;
  121. }
  122. info("started module proc(%.200s)", st->arg);
  123. }
  124. int
  125. get_proc(char *symon_buf, int maxlen, struct stream *st)
  126. {
  127. int i;
  128. struct kinfo_proc *pp;
  129. u_quad_t cpu_ticks = 0;
  130. u_quad_t cpu_uticks = 0;
  131. u_quad_t cpu_iticks = 0;
  132. u_quad_t cpu_sticks =0;
  133. u_int32_t cpu_secs = 0;
  134. double cpu_pct = 0;
  135. double cpu_pcti = 0;
  136. u_int32_t mem_procsize = 0;
  137. u_int32_t mem_rss = 0;
  138. int n = 0;
  139. for (pp = proc_ps, i = 0; i < proc_cur; pp++, i++) {
  140. if (strncmp(st->arg, pp->kp_proc.p_comm, strlen(st->arg)) == 0) {
  141. /* cpu time - accumulated */
  142. cpu_uticks += pp->kp_proc.p_uticks; /* user */
  143. cpu_sticks += pp->kp_proc.p_sticks; /* sys */
  144. cpu_iticks += pp->kp_proc.p_iticks; /* int */
  145. /* cpu time - percentage since last measurement */
  146. cpu_pct = pctdouble(pp->kp_proc.p_pctcpu) * 100.0;
  147. cpu_pcti += cpu_pct;
  148. /* memory size - shared pages are counted multiple times */
  149. mem_procsize += pagetob(pp->kp_eproc.e_vm.vm_tsize + /* text pages */
  150. pp->kp_eproc.e_vm.vm_dsize + /* data */
  151. pp->kp_eproc.e_vm.vm_ssize); /* stack */
  152. mem_rss += pagetob(pp->kp_eproc.e_vm.vm_rssize); /* rss */
  153. n++;
  154. }
  155. }
  156. /* calc total cpu_secs spent */
  157. cpu_ticks = cpu_uticks + cpu_sticks + cpu_iticks;
  158. cpu_secs = cpu_ticks / proc_stathz;
  159. return snpack(symon_buf, maxlen, st->arg, MT_PROC,
  160. n,
  161. cpu_uticks, cpu_sticks, cpu_iticks, cpu_secs, cpu_pcti,
  162. mem_procsize, mem_rss );
  163. }