sm_cpu.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2004 Matthew Gream
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get current cpu statistics in percentages (total of all counts = 100.0)
  32. * and returns them in symon_buf as
  33. *
  34. * user : nice : system : interrupt : idle
  35. *
  36. * This code is not re-entrant and UP only.
  37. *
  38. * This module uses the sysctl interface and can run as any user.
  39. */
  40. #include "conf.h"
  41. #include <sys/param.h>
  42. #include <sys/dkstat.h>
  43. #include <sys/sysctl.h>
  44. #include "error.h"
  45. #include "percentages.h"
  46. #include "symon.h"
  47. /* Globals for this module all start with cp_ */
  48. static char cp_time_mib_str[] = "kern.cp_time";
  49. static int cp_time_mib[CTL_MAXNAME];
  50. static size_t cp_time_len = 0;
  51. static size_t cp_size;
  52. void
  53. init_cpu(struct stream *st)
  54. {
  55. char buf[SYMON_MAX_OBJSIZE];
  56. cp_time_len = CTL_MAXNAME;
  57. if (sysctlnametomib(cp_time_mib_str, cp_time_mib, &cp_time_len) < 0) {
  58. warning("sysctlnametomib for cpu failed");
  59. cp_time_len = 0;
  60. }
  61. cp_size = sizeof(st->parg.cp.time1);
  62. get_cpu(buf, sizeof(buf), st);
  63. info("started module cpu(%.200s)", st->arg);
  64. }
  65. void
  66. gets_cpu()
  67. {
  68. /* EMPTY */
  69. }
  70. int
  71. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  72. {
  73. int i;
  74. if (!cp_time_len) {
  75. return 0;
  76. }
  77. if (sysctl(cp_time_mib, cp_time_len, &st->parg.cp.time1, &cp_size, NULL, 0) < 0) {
  78. warning("%s:%d: sysctl kern.cp_time failed", __FILE__, __LINE__);
  79. return 0;
  80. }
  81. /* convert cp_time counts to percentages */
  82. for (i = 0; i < CPUSTATES; i++)
  83. st->parg.cp.time2[i] = (int64_t) st->parg.cp.time1[i];
  84. (void)percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time2, st->parg.cp.old, st->parg.cp.diff);
  85. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  86. (double) (st->parg.cp.states[CP_USER] / 10.0),
  87. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  88. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  89. (double) (st->parg.cp.states[CP_INTR] / 10.0),
  90. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  91. }