sm_cpu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* $Id: sm_cpu.c,v 1.4 2005/10/18 19:58:06 dijkstra Exp $ */
  2. /* The author of this code is Matthew Gream.
  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) 2004 Matthew Gream
  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 code is not re-entrant and UP only.
  52. *
  53. * This module uses the sysctl interface and can run as any user.
  54. */
  55. #include "conf.h"
  56. #include <sys/param.h>
  57. #include <sys/dkstat.h>
  58. #include <sys/sysctl.h>
  59. #include "error.h"
  60. #include "symon.h"
  61. __BEGIN_DECLS
  62. int percentages(int, int *, long *, long *, long *);
  63. __END_DECLS
  64. /* Globals for this module all start with cp_ */
  65. static char cp_time_mib_str[] = "kern.cp_time";
  66. static int cp_time_mib[CTL_MAXNAME];
  67. static size_t cp_time_len = 0;
  68. static size_t cp_size;
  69. /*
  70. * percentages(cnt, out, new, old, diffs) - calculate percentage change
  71. * between array "old" and "new", putting the percentages i "out".
  72. * "cnt" is size of each array and "diffs" is used for scratch space.
  73. * The array "old" is updated on each call.
  74. * The routine assumes modulo arithmetic. This function is especially
  75. * useful on BSD mchines for calculating cpu state percentages.
  76. */
  77. int
  78. percentages(int cnt, int *out, register long *new, register long *old, long *diffs)
  79. {
  80. register int i;
  81. register long change;
  82. register long total_change;
  83. register long *dp;
  84. long half_total;
  85. /* initialization */
  86. total_change = 0;
  87. dp = diffs;
  88. /* calculate changes for each state and the overall change */
  89. for (i = 0; i < cnt; i++) {
  90. if (*new < *old)
  91. change = (ULONG_MAX - *old) + *new;
  92. else
  93. change = *new - *old;
  94. total_change += (*dp++ = change);
  95. *old++ = *new++;
  96. }
  97. /* avoid divide by zero potential */
  98. if (total_change == 0)
  99. total_change = 1;
  100. /* calculate percentages based on overall change, rounding up */
  101. half_total = total_change / 2;
  102. for (i = 0; i < cnt; i++)
  103. *out++ = ((*diffs++ * 1000 + half_total) / total_change);
  104. /* return the total in case the caller wants to use it */
  105. return total_change;
  106. }
  107. void
  108. init_cpu(struct stream *st)
  109. {
  110. char buf[SYMON_MAX_OBJSIZE];
  111. cp_time_len = CTL_MAXNAME;
  112. if (sysctlnametomib(cp_time_mib_str, cp_time_mib, &cp_time_len) < 0) {
  113. warning("sysctlnametomib for cpu failed");
  114. cp_time_len = 0;
  115. }
  116. cp_size = sizeof(st->parg.cp.time);
  117. get_cpu(buf, sizeof(buf), st);
  118. info("started module cpu(%.200s)", st->arg);
  119. }
  120. void
  121. gets_cpu()
  122. {
  123. /* EMPTY */
  124. }
  125. int
  126. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  127. {
  128. if (!cp_time_len) {
  129. return 0;
  130. }
  131. if (sysctl(cp_time_mib, cp_time_len, &st->parg.cp.time, &cp_size, NULL, 0) < 0) {
  132. warning("%s:%d: sysctl kern.cp_time failed", __FILE__, __LINE__);
  133. return 0;
  134. }
  135. /* convert cp_time counts to percentages */
  136. (void)percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time, st->parg.cp.old, st->parg.cp.diff);
  137. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  138. (double) (st->parg.cp.states[CP_USER] / 10.0),
  139. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  140. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  141. (double) (st->parg.cp.states[CP_INTR] / 10.0),
  142. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  143. }