sm_cpu.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <sys/dkstat.h>
  41. #include <sys/param.h>
  42. #include <sys/sysctl.h>
  43. #include <sys/sched.h>
  44. #include "error.h"
  45. #include "percentages.h"
  46. #include "symon.h"
  47. /* Globals for this module all start with cp_ */
  48. static int cp_time_mib[] = {CTL_KERN, KERN_CP_TIME};
  49. static size_t cp_size;
  50. void
  51. init_cpu(struct stream *st)
  52. {
  53. char buf[SYMON_MAX_OBJSIZE];
  54. cp_size = sizeof(st->parg.cp.time);
  55. get_cpu(buf, sizeof(buf), st);
  56. info("started module cpu(%.200s)", st->arg);
  57. }
  58. void
  59. gets_cpu()
  60. {
  61. /* EMPTY */
  62. }
  63. int
  64. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  65. {
  66. if (sysctl(cp_time_mib, 2, &st->parg.cp.time, &cp_size, NULL, 0) < 0) {
  67. warning("%s:%d: sysctl kern.cp_time failed", __FILE__, __LINE__);
  68. return 0;
  69. }
  70. /* convert cp_time counts to percentages */
  71. (void)percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time, st->parg.cp.old, st->parg.cp.diff);
  72. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  73. (double) (st->parg.cp.states[CP_USER] / 10.0),
  74. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  75. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  76. (double) (st->parg.cp.states[CP_INTR] / 10.0),
  77. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  78. }