netcpu_osx.c
Upload User: kvgkvg
Upload Date: 2015-05-07
Package Size: 1129k
Code Size: 3k
Development Platform:

C/C++

  1. char   netcpu_sysctl_id[]="
  2. @(#)netcpu_osx.c  Version 2.4.3";
  3. #if HAVE_CONFIG_H
  4. # include <config.h>
  5. #endif
  6. #include <stdio.h>
  7. #if HAVE_INTTYPES_H
  8. # include <inttypes.h>
  9. #else
  10. # if HAVE_STDINT_H
  11. #  include <stdint.h>
  12. # endif
  13. #endif
  14. #if TIME_WITH_SYS_TIME
  15. # include <sys/time.h>
  16. # include <time.h>
  17. #else
  18. # if HAVE_SYS_TIME_H
  19. #  include <sys/time.h>
  20. # else
  21. #  include <time.h>
  22. # endif
  23. #endif
  24. #if HAVE_LIMITS_H
  25. # include <limits.h>
  26. # ifndef LONG_LONG_MAX
  27. #  define LONG_LONG_MAX LLONG_MAX
  28. # endif /* LONG_LONG_MAX */
  29. #endif
  30. #include <errno.h>
  31. #include <mach/host_info.h>
  32. #include <mach/mach_types.h>
  33. /* it would seem that on 10.3.9 mach_msg_type_number_t is in
  34.    <mach/message.h> so we'll see about including that one too.
  35.    hopefully it still exists in 10.4. if not, we will need to add some
  36.    .h file checks in configure so we can use "HAVE_mumble" ifdefs
  37.    here */
  38. #include <mach/message.h>
  39. #include "netsh.h"
  40. #include "netlib.h"
  41. #define UNSIGNED_DIFFERENCE(x,y) (x >= y ? x - y : (0 - y) + x )
  42. static host_cpu_load_info_data_t lib_start_ticks;
  43. static host_cpu_load_info_data_t lib_end_ticks;
  44. static mach_port_t lib_host_port;
  45. void
  46. cpu_util_init(void) 
  47. {
  48.   lib_host_port = mach_host_self();
  49.   return;
  50. }
  51. void
  52. cpu_util_terminate(void)
  53. {
  54.   mach_port_deallocate(lib_host_port);
  55.   return;
  56. }
  57. int
  58. get_cpu_method(void)
  59. {
  60.   return OSX;
  61. }
  62. void
  63. get_cpu_idle(uint64_t *res)
  64. {
  65.     return;
  66. }
  67. void
  68. get_host_ticks(host_cpu_load_info_t info)
  69. {
  70.   mach_msg_type_number_t count;
  71.   count = HOST_CPU_LOAD_INFO_COUNT;
  72.   host_statistics(lib_host_port, HOST_CPU_LOAD_INFO, (host_info_t)info, &count);
  73.   return;
  74. }
  75. /* calibrate_sysctl  - perform the idle rate calculation using the
  76.    sysctl call - typically on BSD */
  77. float
  78. calibrate_idle_rate(int iterations, int interval)
  79. {
  80.     return (float)0.0;   
  81. }
  82. float
  83. calc_cpu_util_internal(float elapsed_time)
  84. {
  85.   float correction_factor;
  86.   natural_t userticks, systicks, idleticks, totalticks;
  87.   lib_local_cpu_util = (float)0.0;
  88.   /* It is possible that the library measured a time other than */
  89.   /* the one that the user want for the cpu utilization */
  90.   /* calculations - for example, tests that were ended by */
  91.   /* watchdog timers such as the udp stream test. We let these */
  92.   /* tests tell up what the elapsed time should be. */
  93.   
  94.   if (elapsed_time != 0.0) {
  95.     correction_factor = (float) 1.0 + 
  96.       ((lib_elapsed - elapsed_time) / elapsed_time);
  97.   }
  98.   else {
  99.     correction_factor = (float) 1.0;
  100.   }
  101.   if (debug) {
  102.     fprintf(where, "correction factor: %fn", correction_factor);
  103.   }
  104.   userticks = UNSIGNED_DIFFERENCE((lib_end_ticks.cpu_ticks[CPU_STATE_USER] + lib_end_ticks.cpu_ticks[CPU_STATE_NICE]),
  105.   (lib_start_ticks.cpu_ticks[CPU_STATE_USER] + lib_start_ticks.cpu_ticks[CPU_STATE_NICE]));
  106.   systicks = UNSIGNED_DIFFERENCE(lib_end_ticks.cpu_ticks[CPU_STATE_SYSTEM], lib_start_ticks.cpu_ticks[CPU_STATE_SYSTEM]);
  107.   idleticks = UNSIGNED_DIFFERENCE(lib_end_ticks.cpu_ticks[CPU_STATE_IDLE], lib_start_ticks.cpu_ticks[CPU_STATE_IDLE]);
  108.   totalticks = userticks + systicks + idleticks;
  109.   lib_local_cpu_util = ((float)userticks + (float)systicks)/(float)totalticks * 100.0f;
  110.   lib_local_cpu_util *= correction_factor;
  111.   return lib_local_cpu_util;
  112. }
  113. void
  114. cpu_start_internal(void)
  115. {
  116.     get_host_ticks(&lib_start_ticks);
  117. }
  118. void
  119. cpu_stop_internal(void)
  120. {
  121.     get_host_ticks(&lib_end_ticks);
  122. }