sm_proc.c 5.5 KB

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