sm_cpuiow.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* $Id: sm_cpuiow.c,v 1.2 2008/01/30 14:29:31 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-2008 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 : iowait
  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. static int percentages(int, int64_t *, int64_t *, int64_t *, int64_t *);
  66. __END_DECLS
  67. /* Globals for this module all start with cpw_ */
  68. static void *cpw_buf = NULL;
  69. static int cpw_size = 0;
  70. static int cpw_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. static int
  80. percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
  81. {
  82. int64_t change, total_change, *dp, half_total;
  83. int i;
  84. /* initialization */
  85. total_change = 0;
  86. dp = diffs;
  87. /* calculate changes for each state and the overall change */
  88. for (i = 0; i < cnt; i++) {
  89. if ((change = *new - *old) < 0) {
  90. /* this only happens when the counter wraps */
  91. change = (QUAD_MAX - *old) + *new;
  92. }
  93. total_change += (*dp++ = change);
  94. *old++ = *new++;
  95. }
  96. /* avoid divide by zero potential */
  97. if (total_change == 0)
  98. total_change = 1;
  99. /* calculate percentages based on overall change, rounding up */
  100. half_total = total_change / 2l;
  101. for (i = 0; i < cnt; i++)
  102. *out++ = ((*diffs++ * 1000 + half_total) / total_change);
  103. /* return the total in case the caller wants to use it */
  104. return (total_change);
  105. }
  106. void
  107. init_cpuiow(struct stream *st)
  108. {
  109. char buf[SYMON_MAX_OBJSIZE];
  110. if (cpw_buf == NULL) {
  111. cpw_maxsize = SYMON_MAX_OBJSIZE;
  112. cpw_buf = xmalloc(cpw_maxsize);
  113. }
  114. if (st->arg != NULL && isdigit(*st->arg)) {
  115. snprintf(st->parg.cpw.name, sizeof(st->parg.cpw.name), "cpu%s", st->arg);
  116. } else {
  117. snprintf(st->parg.cpw.name, sizeof(st->parg.cpw.name), "cpu");
  118. }
  119. gets_cpuiow();
  120. get_cpuiow(buf, sizeof(buf), st);
  121. info("started module cpuiow(%.200s)", st->arg);
  122. }
  123. void
  124. gets_cpuiow()
  125. {
  126. int fd;
  127. if ((fd = open("/proc/stat", O_RDONLY)) < 0) {
  128. warning("cannot access /proc/stat: %.200s", strerror(errno));
  129. return;
  130. }
  131. bzero(cpw_buf, cpw_maxsize);
  132. cpw_size = read(fd, cpw_buf, cpw_maxsize);
  133. close(fd);
  134. if (cpw_size == cpw_maxsize) {
  135. /* buffer is too small to hold all interface data */
  136. cpw_maxsize += SYMON_MAX_OBJSIZE;
  137. if (cpw_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  138. fatal("%s:%d: dynamic object limit (%d) exceeded for cp data",
  139. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  140. }
  141. cpw_buf = xrealloc(cpw_buf, cpw_maxsize);
  142. gets_cpuiow();
  143. return;
  144. }
  145. if (cpw_size == -1) {
  146. warning("could not read if statistics from /proc/stat: %.200s", strerror(errno));
  147. }
  148. }
  149. int
  150. get_cpuiow(char *symon_buf, int maxlen, struct stream *st)
  151. {
  152. char *line;
  153. if (cpw_size <= 0) {
  154. return 0;
  155. }
  156. if ((line = strstr(cpw_buf, st->parg.cpw.name)) == NULL) {
  157. warning("could not find %s", st->parg.cpw.name);
  158. return 0;
  159. }
  160. line += strlen(st->parg.cpw.name);
  161. if (CPUSTATES > sscanf(line, "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  162. &st->parg.cpw.time[CP_USER],
  163. &st->parg.cpw.time[CP_NICE],
  164. &st->parg.cpw.time[CP_SYS],
  165. &st->parg.cpw.time[CP_IDLE],
  166. &st->parg.cpw.time[CP_IOWAIT],
  167. &st->parg.cpw.time[CP_HARDIRQ],
  168. &st->parg.cpw.time[CP_SOFTIRQ],
  169. &st->parg.cpw.time[CP_STEAL])) {
  170. /* /proc/stat might not support steal */
  171. st->parg.cpw.time[CP_STEAL] = 0;
  172. if ((CPUSTATES - 1) > sscanf(line, "%llu %llu %llu %llu %llu %llu %llu\n",
  173. &st->parg.cpw.time[CP_USER],
  174. &st->parg.cpw.time[CP_NICE],
  175. &st->parg.cpw.time[CP_SYS],
  176. &st->parg.cpw.time[CP_IDLE],
  177. &st->parg.cpw.time[CP_IOWAIT],
  178. &st->parg.cpw.time[CP_HARDIRQ],
  179. &st->parg.cpw.time[CP_SOFTIRQ])) {
  180. warning("could not parse cpu statistics for %.200s", &st->parg.cpw.name);
  181. return 0;
  182. }
  183. }
  184. percentages(CPUSTATES, st->parg.cpw.states, st->parg.cpw.time,
  185. st->parg.cpw.old, st->parg.cpw.diff);
  186. return snpack(symon_buf, maxlen, st->arg, MT_CPUIOW,
  187. (double) (st->parg.cpw.states[CP_USER] / 10.0),
  188. (double) (st->parg.cpw.states[CP_NICE] / 10.0),
  189. (double) (st->parg.cpw.states[CP_SYS] / 10.0),
  190. (double) (st->parg.cpw.states[CP_HARDIRQ] +
  191. st->parg.cpw.states[CP_SOFTIRQ] +
  192. st->parg.cpw.states[CP_STEAL]) / 10.0,
  193. (double) (st->parg.cpw.states[CP_IDLE] / 10.0),
  194. (double) (st->parg.cpw.states[CP_IOWAIT] / 10.0));
  195. }