win_clock.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2023 Amazon Inc. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_api_extension.h"
  6. #include <winternl.h>
  7. #include "win_util.h"
  8. #define NANOSECONDS_PER_SECOND 1000000000ULL
  9. #define NANOSECONDS_PER_TICK 100
  10. #if WINAPI_PARTITION_DESKTOP
  11. extern NTSTATUS
  12. NtQueryTimerResolution(PULONG MinimumResolution, PULONG MaximumResolution,
  13. PULONG CurrentResolution);
  14. #endif
  15. static __wasi_errno_t
  16. calculate_monotonic_clock_frequency(uint64 *out_frequency)
  17. {
  18. LARGE_INTEGER frequency;
  19. if (!QueryPerformanceFrequency(&frequency))
  20. return convert_windows_error_code(GetLastError());
  21. *out_frequency = (uint64)frequency.QuadPart;
  22. return __WASI_ESUCCESS;
  23. }
  24. static __wasi_errno_t
  25. get_performance_counter_value(uint64 *out_counter)
  26. {
  27. LARGE_INTEGER counter;
  28. if (!QueryPerformanceCounter(&counter))
  29. return convert_windows_error_code(GetLastError());
  30. *out_counter = counter.QuadPart;
  31. return __WASI_ESUCCESS;
  32. }
  33. __wasi_errno_t
  34. os_clock_res_get(__wasi_clockid_t clock_id, __wasi_timestamp_t *resolution)
  35. {
  36. __wasi_errno_t error = __WASI_ESUCCESS;
  37. switch (clock_id) {
  38. case __WASI_CLOCK_MONOTONIC:
  39. {
  40. uint64 frequency;
  41. error = calculate_monotonic_clock_frequency(&frequency);
  42. if (error != __WASI_ESUCCESS)
  43. return error;
  44. const uint64 result = (uint64)NANOSECONDS_PER_SECOND / frequency;
  45. *resolution = result;
  46. return error;
  47. }
  48. case __WASI_CLOCK_REALTIME:
  49. case __WASI_CLOCK_PROCESS_CPUTIME_ID:
  50. case __WASI_CLOCK_THREAD_CPUTIME_ID:
  51. {
  52. #if WINAPI_PARTITION_DESKTOP && WASM_ENABLE_WAMR_COMPILER == 0
  53. ULONG maximum_time;
  54. ULONG minimum_time;
  55. ULONG current_time;
  56. NTSTATUS
  57. status = NtQueryTimerResolution(&maximum_time, &minimum_time,
  58. &current_time);
  59. uint64 result = (uint64)current_time * NANOSECONDS_PER_TICK;
  60. *resolution = result / (uint64)NANOSECONDS_PER_SECOND;
  61. return error;
  62. #else
  63. return __WASI_ENOTSUP;
  64. #endif
  65. }
  66. default:
  67. return __WASI_EINVAL;
  68. }
  69. }
  70. __wasi_errno_t
  71. os_clock_time_get(__wasi_clockid_t clock_id, __wasi_timestamp_t precision,
  72. __wasi_timestamp_t *time)
  73. {
  74. __wasi_errno_t error = __WASI_ESUCCESS;
  75. switch (clock_id) {
  76. case __WASI_CLOCK_REALTIME:
  77. {
  78. FILETIME sys_now;
  79. #if NTDDI_VERSION >= NTDDI_WIN8
  80. GetSystemTimePreciseAsFileTime(&sys_now);
  81. #else
  82. GetSystemTimeAsFileTime(&sys_now);
  83. #endif
  84. *time = convert_filetime_to_wasi_timestamp(&sys_now);
  85. return BHT_OK;
  86. }
  87. case __WASI_CLOCK_MONOTONIC:
  88. {
  89. uint64 frequency;
  90. error = calculate_monotonic_clock_frequency(&frequency);
  91. if (error != __WASI_ESUCCESS)
  92. return error;
  93. uint64 counter;
  94. error = get_performance_counter_value(&counter);
  95. if (error != __WASI_ESUCCESS)
  96. return error;
  97. if (NANOSECONDS_PER_SECOND % frequency == 0) {
  98. *time = counter * NANOSECONDS_PER_SECOND / frequency;
  99. }
  100. else {
  101. uint64 seconds = counter / frequency;
  102. uint64 fractions = counter % frequency;
  103. *time = seconds * NANOSECONDS_PER_SECOND
  104. + (fractions * NANOSECONDS_PER_SECOND) / frequency;
  105. }
  106. return error;
  107. }
  108. case __WASI_CLOCK_PROCESS_CPUTIME_ID:
  109. case __WASI_CLOCK_THREAD_CPUTIME_ID:
  110. {
  111. FILETIME creation_time;
  112. FILETIME exit_time;
  113. FILETIME kernel_time;
  114. FILETIME user_time;
  115. HANDLE handle = (clock_id == __WASI_CLOCK_PROCESS_CPUTIME_ID)
  116. ? GetCurrentProcess()
  117. : GetCurrentThread();
  118. if (!GetProcessTimes(handle, &creation_time, &exit_time,
  119. &kernel_time, &user_time))
  120. return convert_windows_error_code(GetLastError());
  121. *time = convert_filetime_to_wasi_timestamp(&kernel_time)
  122. + convert_filetime_to_wasi_timestamp(&user_time);
  123. return error;
  124. }
  125. default:
  126. return __WASI_EINVAL;
  127. }
  128. }