Skip to content

Commit

Permalink
Expose timestamp APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan Jowett committed Nov 14, 2024
1 parent 870b23b commit 273b464
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions cxplat/cxplat_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_executable(cxplat_test
cxplat_processor_test.cpp
cxplat_rundown_test.cpp
cxplat_size_test.cpp
cxplat_time_test.cpp
cxplat_workitem_test.cpp
)

Expand Down
1 change: 1 addition & 0 deletions cxplat/cxplat_test/cxplat_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<ClCompile Include="cxplat_processor_test.cpp" />
<ClCompile Include="cxplat_rundown_test.cpp" />
<ClCompile Include="cxplat_size_test.cpp" />
<ClCompile Include="cxplat_time_test.cpp" />
<ClCompile Include="cxplat_workitem_test.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions cxplat/cxplat_test/cxplat_test.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@
<ClCompile Include="cxplat_processor_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="cxplat_time_test.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
51 changes: 51 additions & 0 deletions cxplat/cxplat_test/cxplat_time_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#if !defined(CMAKE_NUGET)
#include <catch2/catch_all.hpp>
#else
#include <catch2/catch.hpp>
#endif
#include "cxplat.h"

#include <Windows.h>

TEST_CASE("query_time_precise_include_suspend", "[time]")
{
uint64_t time1 = cxplat_query_time_since_boot_precise(true);
Sleep(2);
uint64_t time2 = cxplat_query_time_since_boot_precise(true);

// The time difference should be at least 10000 filetime units (1ms)
REQUIRE(time2 - time1 >= 10000);
}

TEST_CASE("query_time_precise", "[time]")
{
uint64_t time1 = cxplat_query_time_since_boot_precise(false);
Sleep(2);
uint64_t time2 = cxplat_query_time_since_boot_precise(false);

// The time difference should be at least 10000 filetime units (1ms)
REQUIRE(time2 - time1 >= 10000);
}

TEST_CASE("query_time_approximate_include_suspend", "[time]")
{
uint64_t time1 = cxplat_query_time_since_boot_approximate(true);
Sleep(2);
uint64_t time2 = cxplat_query_time_since_boot_approximate(true);

// The time difference should be at least 10000 filetime units (1ms)
REQUIRE(time2 - time1 >= 10000);
}

TEST_CASE("query_time_approximate", "[time]")
{
uint64_t time1 = cxplat_query_time_since_boot_approximate(false);
Sleep(2);
uint64_t time2 = cxplat_query_time_since_boot_approximate(false);

// The time difference should be at least 10000 filetime units (1ms)
REQUIRE(time2 - time1 >= 10000);
}
1 change: 1 addition & 0 deletions cxplat/inc/cxplat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "cxplat_processor.h"
#include "cxplat_rundown.h"
#include "cxplat_size.h"
#include "cxplat_time.h"
#include "cxplat_workitem.h"

CXPLAT_EXTERN_C_BEGIN
Expand Down
15 changes: 15 additions & 0 deletions cxplat/inc/cxplat_time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#pragma once

#include <stdint.h>

CXPLAT_EXTERN_C_BEGIN

uint64_t
cxplat_query_time_since_boot_precise(bool include_suspended_time);

uint64_t
cxplat_query_time_since_boot_approximate(bool include_suspended_time);

CXPLAT_EXTERN_C_END
1 change: 1 addition & 0 deletions cxplat/src/cxplat_winkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_library(cxplat_winkernel STATIC
memory_winkernel.c
processor_winkernel.c
rundown_winkernel.c
time_winkernel.c
workitem_winkernel.c
)

Expand Down
1 change: 1 addition & 0 deletions cxplat/src/cxplat_winkernel/cxplat_winkernel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ClCompile Include="processor_winkernel.c" />
<ClCompile Include="rundown_winkernel.c" />
<ClCompile Include="size_winkernel.c" />
<ClCompile Include="time_winkernel.c" />
<ClCompile Include="workitem_winkernel.c" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions cxplat/src/cxplat_winkernel/cxplat_winkernel.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClCompile Include="processor_winkernel.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="time_winkernel.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\inc\cxplat.h">
Expand Down
37 changes: 37 additions & 0 deletions cxplat/src/cxplat_winkernel/time_winkernel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#include "cxplat.h"
#include "wdm.h"

uint64_t
cxplat_query_time_since_boot_precise(bool include_suspended_time)
{
uint64_t qpc_time;
if (include_suspended_time) {
// KeQueryUnbiasedInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// Unbiased Interrupt time is the total time since boot including time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryunbiasedinterrupttimeprecise
return KeQueryUnbiasedInterruptTimePrecise(&qpc_time);
} else {
// KeQueryInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// (Biased) Interrupt time is the total time since boot excluding time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryinterrupttimeprecise
return KeQueryInterruptTimePrecise(&qpc_time);
}
}

uint64_t
cxplat_query_time_since_boot_approximate(bool include_suspended_time)
{
if (include_suspended_time) {
// KeQueryUnbiasedInterruptTime returns the current interrupt-time count in 100-nanosecond units.
// Unbiased Interrupt time is the total time since boot including time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryunbiasedinterrupttime
return KeQueryUnbiasedInterruptTime();
} else {
// KeQueryInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// (Biased) Interrupt time is the total time since boot excluding time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryinterrupttime
return KeQueryInterruptTime();
}
}
1 change: 1 addition & 0 deletions cxplat/src/cxplat_winuser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_library(cxplat_winuser STATIC
size_winuser.c
workitem_winuser.cpp
symbol_decoder.h
time_winuser.cpp
winuser_internal.h
)

Expand Down
1 change: 1 addition & 0 deletions cxplat/src/cxplat_winuser/cxplat_winuser.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<ClCompile Include="processor_winuser.cpp" />
<ClCompile Include="rundown_winuser.cpp" />
<ClCompile Include="size_winuser.c" />
<ClCompile Include="time_winuser.cpp" />
<ClCompile Include="workitem_winuser.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
3 changes: 3 additions & 0 deletions cxplat/src/cxplat_winuser/cxplat_winuser.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,8 @@
<ClCompile Include="processor_winuser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="time_winuser.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
43 changes: 43 additions & 0 deletions cxplat/src/cxplat_winuser/time_winuser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#include "cxplat.h"
#include "realtimeapiset.h"

#pragma comment(lib, "Mincore.lib")

uint64_t
cxplat_query_time_since_boot_precise(bool include_suspended_time)
{
uint64_t qpc_time;
if (include_suspended_time) {
// KeQueryUnbiasedInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// Unbiased Interrupt time is the total time since boot including time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryunbiasedinterrupttimeprecise
QueryUnbiasedInterruptTimePrecise(&qpc_time);
} else {
// KeQueryInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// (Biased) Interrupt time is the total time since boot excluding time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryinterrupttimeprecise
QueryInterruptTimePrecise(&qpc_time);
}
return qpc_time;
}

uint64_t
cxplat_query_time_since_boot_approximate(bool include_suspended_time)
{
uint64_t qpc_time;
if (include_suspended_time) {
// KeQueryUnbiasedInterruptTime returns the current interrupt-time count in 100-nanosecond units.
// Unbiased Interrupt time is the total time since boot including time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryunbiasedinterrupttime
QueryUnbiasedInterruptTime(&qpc_time);
} else {
// KeQueryInterruptTimePrecise returns the current interrupt-time count in 100-nanosecond units.
// (Biased) Interrupt time is the total time since boot excluding time spent suspended.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-kequeryinterrupttime
QueryInterruptTime(&qpc_time);
}

return qpc_time;
}

0 comments on commit 273b464

Please sign in to comment.