sm_cpu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* $Id: sm_cpu.c,v 1.4 2005/10/21 14:58: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
  5. * of the 'top' utility. His copyright statement is below.
  6. *
  7. * Copyright (c) 2001-2005 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. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <fcntl.h>
  54. #include <ctype.h>
  55. #include <errno.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <strings.h>
  59. #include <unistd.h>
  60. #include "conf.h"
  61. #include "error.h"
  62. #include "symon.h"
  63. #include "xmalloc.h"
  64. __BEGIN_DECLS
  65. int percentages(int, int *, long *, long *, long *);
  66. __END_DECLS
  67. /* Globals for this module all start with cp_ */
  68. static void *cp_buf = NULL;
  69. static int cp_size = 0;
  70. static int cp_maxsize = 0;
  71. /*
  72. * percentages(cnt, out, new, old, diffs) - calculate percentage change
  73. * between array "old" and "new", putting the percentages i "out".
  74. * "cnt" is size of each array and "diffs" is used for scratch space.
  75. * The array "old" is updated on each call.
  76. * The routine assumes modulo arithmetic. This function is especially
  77. * useful on BSD mchines for calculating cpu state percentages.
  78. */
  79. int
  80. percentages(int cnt, int *out, register long *new, register long *old, long *diffs)
  81. {
  82. register int i;
  83. register long change;
  84. register long total_change;
  85. register long *dp;
  86. long half_total;
  87. /* initialization */
  88. total_change = 0;
  89. dp = diffs;
  90. /* calculate changes for each state and the overall change */
  91. for (i = 0; i < cnt; i++) {
  92. if ((change = *new - *old) < 0) {
  93. /* this only happens when the counter wraps */
  94. change = ((unsigned int) *new - (unsigned int) *old);
  95. }
  96. total_change += (*dp++ = change);
  97. *old++ = *new++;
  98. }
  99. /* avoid divide by zero potential */
  100. if (total_change == 0)
  101. total_change = 1;
  102. /* calculate percentages based on overall change, rounding up */
  103. half_total = total_change / 2l;
  104. for (i = 0; i < cnt; i++)
  105. *out++ = ((*diffs++ * 1000 + half_total) / total_change);
  106. /* return the total in case the caller wants to use it */
  107. return total_change;
  108. }
  109. void
  110. init_cpu(struct stream *st)
  111. {
  112. char buf[SYMON_MAX_OBJSIZE];
  113. if (cp_buf == NULL) {
  114. cp_maxsize = SYMON_MAX_OBJSIZE;
  115. cp_buf = xmalloc(cp_maxsize);
  116. }
  117. if (st->arg != NULL && isdigit(*st->arg)) {
  118. snprintf(st->parg.cp.name, sizeof(st->parg.cp.name), "cpu%s", st->arg);
  119. } else {
  120. snprintf(st->parg.cp.name, sizeof(st->parg.cp.name), "cpu");
  121. }
  122. gets_cpu();
  123. get_cpu(buf, sizeof(buf), st);
  124. info("started module cpu(%.200s)", st->arg);
  125. }
  126. void
  127. gets_cpu()
  128. {
  129. int fd;
  130. if ((fd = open("/proc/stat", O_RDONLY)) < 0) {
  131. warning("cannot access /proc/stat: %.200s", strerror(errno));
  132. return;
  133. }
  134. bzero(cp_buf, cp_maxsize);
  135. cp_size = read(fd, cp_buf, cp_maxsize);
  136. close(fd);
  137. if (cp_size == cp_maxsize) {
  138. /* buffer is too small to hold all interface data */
  139. cp_maxsize += SYMON_MAX_OBJSIZE;
  140. if (cp_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  141. fatal("%s:%d: dynamic object limit (%d) exceeded for cp data",
  142. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  143. }
  144. cp_buf = xrealloc(cp_buf, cp_maxsize);
  145. gets_cpu();
  146. return;
  147. }
  148. if (cp_size == -1) {
  149. warning("could not read if statistics from /proc/stat: %.200s", strerror(errno));
  150. }
  151. }
  152. int
  153. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  154. {
  155. char *line;
  156. if (cp_size <= 0) {
  157. return 0;
  158. }
  159. if ((line = strstr(cp_buf, st->parg.cp.name)) == NULL) {
  160. warning("could not find %s", st->parg.cp.name);
  161. return 0;
  162. }
  163. line += strlen(st->parg.cp.name);
  164. if (4 > sscanf(line, "%lu %lu %lu %lu\n",
  165. &st->parg.cp.time[CP_USER],
  166. &st->parg.cp.time[CP_NICE],
  167. &st->parg.cp.time[CP_SYS],
  168. &st->parg.cp.time[CP_IDLE])) {
  169. warning("could not parse cpu statistics for %.200s", &st->parg.cp.name);
  170. return 0;
  171. }
  172. percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time,
  173. st->parg.cp.old, st->parg.cp.diff);
  174. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  175. (double) (st->parg.cp.states[CP_USER] / 10.0),
  176. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  177. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  178. (double) (0),
  179. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  180. }