sm_cpu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* $Id: sm_cpu.c,v 1.26 2007/07/09 12:54:18 dijkstra Exp $ */
  2. /* The author of this code is Willem Dijkstra (wpd@xs4all.nl).
  3. *
  4. * The percentages function was written by William LeFebvre and is part
  5. * of the 'top' utility. His copyright statement is below.
  6. *
  7. * Copyright (c) 2001-2007 Willem Dijkstra
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer in the documentation and/or other materials provided
  19. * with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. /*
  36. * Top users/processes display for Unix
  37. * Version 3
  38. *
  39. * This program may be freely redistributed,
  40. * but this entire comment MUST remain intact.
  41. *
  42. * Copyright (c) 1984, 1989, William LeFebvre, Rice University
  43. * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
  44. */
  45. /*
  46. * Get current cpu statistics in percentages (total of all counts = 100.0)
  47. * and returns them in symon_buf as
  48. *
  49. * user : nice : system : interrupt : idle
  50. *
  51. * This module uses the sysctl interface and can run as any user.
  52. */
  53. #include "conf.h"
  54. #include <sys/dkstat.h>
  55. #include <sys/param.h>
  56. #include <sys/sysctl.h>
  57. #include <errno.h>
  58. #include <limits.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "error.h"
  62. #include "symon.h"
  63. /*
  64. * percentages(cnt, out, new, old, diffs) - calculate percentage change
  65. * between array "old" and "new", putting the percentages i "out".
  66. * "cnt" is size of each array and "diffs" is used for scratch space.
  67. * The array "old" is updated on each call.
  68. * The routine assumes modulo arithmetic. This function is especially
  69. * useful on BSD mchines for calculating cpu state percentages.
  70. */
  71. int
  72. percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
  73. {
  74. int64_t change, total_change, *dp, half_total;
  75. int i;
  76. /* initialization */
  77. total_change = 0;
  78. dp = diffs;
  79. /* calculate changes for each state and the overall change */
  80. for (i = 0; i < cnt; i++) {
  81. if ((change = *new - *old) < 0) {
  82. /* this only happens when the counter wraps */
  83. change = (QUAD_MAX - *old) + *new;
  84. }
  85. total_change += (*dp++ = change);
  86. *old++ = *new++;
  87. }
  88. /* avoid divide by zero potential */
  89. if (total_change == 0)
  90. total_change = 1;
  91. /* calculate percentages based on overall change, rounding up */
  92. half_total = total_change / 2l;
  93. for (i = 0; i < cnt; i++)
  94. *out++ = ((*diffs++ * 1000 + half_total) / total_change);
  95. /* return the total in case the caller wants to use it */
  96. return (total_change);
  97. }
  98. void
  99. init_cpu(struct stream *st)
  100. {
  101. char buf[SYMON_MAX_OBJSIZE];
  102. #ifdef HAS_KERN_CPTIME2
  103. const char *errstr;
  104. int mib[2] = {CTL_HW, HW_NCPU};
  105. int ncpu;
  106. long num;
  107. size_t size = sizeof(ncpu);
  108. #endif
  109. st->parg.cp.mib[0] = CTL_KERN;
  110. st->parg.cp.mib[1] = KERN_CPTIME;
  111. st->parg.cp.miblen = 2;
  112. #ifdef HAS_KERN_CPTIME2
  113. if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
  114. warning("could not determine number of cpus: %.200s", strerror(errno));
  115. ncpu = 1;
  116. }
  117. num = strtonum(st->arg, 0, SYMON_MAXCPUID - 1, &errstr);
  118. if (errstr != NULL) {
  119. fatal("cpu(%.200s) is invalid: %.200s", st->arg, errstr);
  120. }
  121. if (ncpu > 1) {
  122. st->parg.cp.mib[0] = CTL_KERN;
  123. st->parg.cp.mib[1] = KERN_CPTIME2;
  124. st->parg.cp.mib[2] = num;
  125. st->parg.cp.miblen = 3;
  126. if (st->parg.cp.mib[2] >= ncpu) {
  127. fatal("cpu(%d) is not present", st->parg.cp.mib[2]);
  128. }
  129. }
  130. #endif
  131. /* Call get_cpu once to fill the cp_old structure */
  132. get_cpu(buf, sizeof(buf), st);
  133. info("started module cpu(%.200s)", st->arg);
  134. }
  135. void
  136. gets_cpu()
  137. {
  138. /* EMPTY */
  139. }
  140. int
  141. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  142. {
  143. int i, total;
  144. size_t len;
  145. len = sizeof(st->parg.cp.time2);
  146. if (sysctl(st->parg.cp.mib, st->parg.cp.miblen, &st->parg.cp.time2, &len, NULL, 0) < 0) {
  147. warning("%s:%d: sysctl kern.cp_time* for cpu%d failed", __FILE__, __LINE__,
  148. st->parg.cp.mib[2]);
  149. return 0;
  150. }
  151. /* sysctl will return 32 bit longs for CPTIME and 64 bit longs for CPTIME2 */
  152. if (st->parg.cp.mib[1] == KERN_CPTIME) {
  153. bcopy(st->parg.cp.time2, st->parg.cp.time1, sizeof(st->parg.cp.time1));
  154. for (i = 0; i < CPUSTATES; i++) {
  155. st->parg.cp.time2[i] = (int64_t) st->parg.cp.time1[i];
  156. }
  157. }
  158. /* convert cp_time counts to percentages */
  159. total = percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time2, st->parg.cp.old, st->parg.cp.diff);
  160. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  161. (double) (st->parg.cp.states[CP_USER] / 10.0),
  162. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  163. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  164. (double) (st->parg.cp.states[CP_INTR] / 10.0),
  165. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  166. }