sm_cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* $Id: sm_cpu.c,v 1.16 2003/12/20 16:30:44 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 of the
  5. * 'top' utility. His copyright statement is below.
  6. *
  7. * Copyright (c) 2001-2003 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 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 <sys/dkstat.h>
  56. #include <sys/param.h>
  57. #include <sys/sysctl.h>
  58. #include "error.h"
  59. #include "symon.h"
  60. __BEGIN_DECLS
  61. int percentages(int, int *, long *, long *, long *);
  62. __END_DECLS
  63. /* Globals for this module all start with cp_ */
  64. static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
  65. static size_t cp_size;
  66. static long cp_time[CPUSTATES];
  67. static long cp_old[CPUSTATES];
  68. static long cp_diff[CPUSTATES];
  69. static int cp_states[CPUSTATES];
  70. /*
  71. * percentages(cnt, out, new, old, diffs) - calculate percentage change
  72. * between array "old" and "new", putting the percentages i "out".
  73. * "cnt" is size of each array and "diffs" is used for scratch space.
  74. * The array "old" is updated on each call.
  75. * The routine assumes modulo arithmetic. This function is especially
  76. * useful on BSD mchines for calculating cpu state percentages.
  77. */
  78. int
  79. percentages(int cnt, int *out, register long *new, register long *old, long *diffs)
  80. {
  81. register int i;
  82. register long change;
  83. register long total_change;
  84. register long *dp;
  85. long half_total;
  86. /* initialization */
  87. total_change = 0;
  88. dp = diffs;
  89. /* calculate changes for each state and the overall change */
  90. for (i = 0; i < cnt; i++) {
  91. if ((change = *new - *old) < 0) {
  92. /* this only happens when the counter wraps */
  93. change = ((unsigned int) *new - (unsigned int) *old);
  94. }
  95. total_change += (*dp++ = change);
  96. *old++ = *new++;
  97. }
  98. /* avoid divide by zero potential */
  99. if (total_change == 0)
  100. total_change = 1;
  101. /* calculate percentages based on overall change, rounding up */
  102. half_total = total_change / 2l;
  103. for (i = 0; i < cnt; i++)
  104. *out++ = ((*diffs++ * 1000 + half_total) / total_change);
  105. /* return the total in case the caller wants to use it */
  106. return total_change;
  107. }
  108. /* Prepare cpu module for use */
  109. void
  110. init_cpu(char *s)
  111. {
  112. char buf[_POSIX2_LINE_MAX];
  113. cp_size = sizeof(cp_time);
  114. /* Call get_cpu once to fill the cp_old structure */
  115. get_cpu(buf, sizeof(buf), NULL);
  116. info("started module cpu(%.200s)", s);
  117. }
  118. /* Get new cpu measurements */
  119. int
  120. get_cpu(char *symon_buf, int maxlen, char *s)
  121. {
  122. int total;
  123. if (sysctl(cp_time_mib, 2, &cp_time, &cp_size, NULL, 0) < 0) {
  124. warning("%s:%d: sysctl kern.cp_time failed", __FILE__, __LINE__);
  125. return 0;
  126. }
  127. /* convert cp_time counts to percentages */
  128. total = percentages(CPUSTATES, cp_states, cp_time, cp_old, cp_diff);
  129. return snpack(symon_buf, maxlen, s, MT_CPU,
  130. (double) (cp_states[CP_USER] / 10.0),
  131. (double) (cp_states[CP_NICE] / 10.0),
  132. (double) (cp_states[CP_SYS] / 10.0),
  133. (double) (cp_states[CP_INTR] / 10.0),
  134. (double) (cp_states[CP_IDLE] / 10.0));
  135. }