sm_proc.c 5.7 KB

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