CpuInfo.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "os/c-api/il2cpp-config-platforms.h"
  2. #if IL2CPP_PLATFORM_SUPPORTS_CPU_INFO
  3. #if IL2CPP_TARGET_WINDOWS
  4. #include "os/CpuInfo.h"
  5. #include "utils/Memory.h"
  6. #include "WindowsHeaders.h"
  7. struct Il2CppCpuUsageState
  8. {
  9. uint64_t kernel_time;
  10. uint64_t user_time;
  11. uint64_t idle_time;
  12. };
  13. namespace il2cpp
  14. {
  15. namespace os
  16. {
  17. void* CpuInfo::Create()
  18. {
  19. return IL2CPP_MALLOC_ZERO(sizeof(Il2CppCpuUsageState));
  20. }
  21. int32_t CpuInfo::Usage(void* previous)
  22. {
  23. Il2CppCpuUsageState* prev = (Il2CppCpuUsageState*)previous;
  24. int32_t cpu_usage = 0;
  25. int64_t cpu_total_time;
  26. int64_t cpu_busy_time;
  27. uint64_t idle_time;
  28. uint64_t kernel_time;
  29. uint64_t user_time;
  30. ::GetSystemTimes((FILETIME*)&idle_time, (FILETIME*)&kernel_time, (FILETIME*)&user_time);
  31. cpu_total_time = (int64_t)((user_time - (prev ? prev->user_time : 0)) + (kernel_time - (prev ? prev->kernel_time : 0)));
  32. cpu_busy_time = (int64_t)(cpu_total_time - (idle_time - (prev ? prev->idle_time : 0)));
  33. if (prev)
  34. {
  35. prev->idle_time = idle_time;
  36. prev->kernel_time = kernel_time;
  37. prev->user_time = user_time;
  38. }
  39. if (cpu_total_time > 0 && cpu_busy_time > 0)
  40. cpu_usage = (int32_t)(cpu_busy_time * 100 / cpu_total_time);
  41. return cpu_usage;
  42. }
  43. }
  44. }
  45. #endif
  46. #endif