/libgtop-2.28.4/sysdeps/aix/cpu.c

# · C · 135 lines · 87 code · 26 blank · 22 comment · 9 complexity · 49903ed2aec67b517c5e73e016a0bbc2 MD5 · raw file

  1. /* Copyright (C) 1998-99 Martin Baulig
  2. This file is part of LibGTop 1.0.
  3. Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
  4. LibGTop is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License,
  7. or (at your option) any later version.
  8. LibGTop is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with LibGTop; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include <stdlib.h>
  19. #include <sys/systemcfg.h>
  20. #include <sys/sysinfo.h>
  21. #include <glibtop.h>
  22. #include <glibtop/error.h>
  23. #include <glibtop/cpu.h>
  24. #include <utils.h>
  25. static const unsigned long _glibtop_sysdeps_cpu =
  26. (1L << GLIBTOP_CPU_TOTAL) + (1L << GLIBTOP_CPU_USER) +
  27. (1L << GLIBTOP_CPU_SYS) + (1L << GLIBTOP_CPU_IDLE) +
  28. (1L << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_FREQUENCY) +
  29. (1L << GLIBTOP_XCPU_TOTAL) + (1L << GLIBTOP_XCPU_USER) +
  30. (1L << GLIBTOP_XCPU_SYS) + (1L << GLIBTOP_XCPU_IDLE) +
  31. (1L << GLIBTOP_XCPU_NICE);
  32. /* Init function. */
  33. void
  34. _glibtop_init_cpu_p (glibtop *server)
  35. {
  36. off_t result;
  37. server->ncpu = _system_configuration.ncpus;
  38. if (server->ncpu == 1)
  39. {
  40. server->ncpu = 0; /* means single-processor, see glibtop.h */
  41. }
  42. result = _glibtop_get_kmem_offset(server, "sysinfo");
  43. if (result == -1)
  44. {
  45. return;
  46. }
  47. server->machine.sysinfo_offset = result;
  48. result = _glibtop_get_kmem_offset(server, "cpuinfo");
  49. if (result == -1)
  50. {
  51. server->sysdeps.cpu = 0;
  52. return;
  53. }
  54. server->machine.cpuinfo_offset = result;
  55. server->machine.cpuinfo = (struct cpuinfo*)calloc(_system_configuration.ncpus, sizeof(struct cpuinfo));
  56. server->sysdeps.cpu = _glibtop_sysdeps_cpu;
  57. }
  58. /* Provides information about cpu usage. */
  59. void
  60. glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf)
  61. {
  62. int result;
  63. int cpu;
  64. struct sysinfo sysinfo;
  65. glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_CPU), 0);
  66. memset (buf, 0, sizeof (glibtop_cpu));
  67. result = _glibtop_get_kmem_info(server, server->machine.sysinfo_offset,
  68. &sysinfo, sizeof(struct sysinfo));
  69. if (result <= 0)
  70. {
  71. glibtop_error_io_r (server, "Cannot read sysinfo");
  72. return;
  73. }
  74. buf->idle = sysinfo.cpu[CPU_IDLE];
  75. buf->user = sysinfo.cpu[CPU_USER];
  76. buf->sys = sysinfo.cpu[CPU_KERNEL];
  77. buf->nice = sysinfo.cpu[CPU_WAIT];
  78. buf->total = buf->idle + buf->user + buf->sys + buf->nice ;
  79. result = _glibtop_get_kmem_info(server, server->machine.cpuinfo_offset,
  80. server->machine.cpuinfo,
  81. _system_configuration.ncpus
  82. * sizeof(struct cpuinfo));
  83. if (result <= 0)
  84. {
  85. glibtop_error_io_r (server, "Cannot read cpuinfo");
  86. return;
  87. }
  88. for (cpu = 0; cpu < MIN(GLIBTOP_NCPU, _system_configuration.ncpus); cpu++)
  89. {
  90. buf->xcpu_idle[cpu] =
  91. server->machine.cpuinfo[cpu].cpu[CPU_IDLE];
  92. buf->xcpu_user[cpu] =
  93. server->machine.cpuinfo[cpu].cpu[CPU_USER];
  94. buf->xcpu_sys[cpu] =
  95. server->machine.cpuinfo[cpu].cpu[CPU_KERNEL];
  96. buf->xcpu_nice[cpu] =
  97. server->machine.cpuinfo[cpu].cpu[CPU_WAIT];
  98. buf->xcpu_total[cpu] = buf->xcpu_idle[cpu] +
  99. buf->xcpu_user[cpu] +
  100. buf->xcpu_sys[cpu] +
  101. buf->xcpu_nice[cpu];
  102. }
  103. buf->frequency = sysconf(_SC_CLK_TCK);
  104. buf->flags = _glibtop_sysdeps_cpu;
  105. }