Skip to content

Commit ba4c5c6

Browse files
Goes with: Improve --datetime accuracy
1 parent 0c6b5fc commit ba4c5c6

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

PresentData/TraceSession.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,7 @@ ULONG TraceSession::Start(
481481
PMTraceConsumer* pmConsumer,
482482
MRTraceConsumer* mrConsumer,
483483
wchar_t const* etlPath,
484-
wchar_t const* sessionName,
485-
TimestampType timestampType)
484+
wchar_t const* sessionName)
486485
{
487486
assert(mSessionHandle == 0);
488487
assert(mTraceHandle == INVALID_PROCESSTRACE_HANDLE);
@@ -529,7 +528,7 @@ ULONG TraceSession::Start(
529528

530529
TraceProperties sessionProps = {};
531530
sessionProps.Wnode.BufferSize = (ULONG) sizeof(TraceProperties);
532-
sessionProps.Wnode.ClientContext = timestampType; // Clock resolution to use when logging the timestamp for each event
531+
sessionProps.Wnode.ClientContext = mTimestampType; // Clock resolution to use when logging the timestamp for each event
533532
sessionProps.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; // We have a realtime consumer, not writing to a log file
534533
sessionProps.LogFileNameOffset = 0; // 0 means no output log file
535534
sessionProps.LoggerNameOffset = offsetof(TraceProperties, mSessionName); // Location of session name; will be written by StartTrace()
@@ -584,7 +583,8 @@ ULONG TraceSession::Start(
584583
// time of the first event, which matches GPUVIEW usage, and realtime
585584
// captures are based off the timestamp here.
586585

587-
switch (traceProps.LogfileHeader.ReservedFlags) {
586+
mTimestampType = (TimestampType) traceProps.LogfileHeader.ReservedFlags;
587+
switch (mTimestampType) {
588588
case TIMESTAMP_TYPE_SYSTEM_TIME:
589589
mTimestampFrequency.QuadPart = 10000000ull;
590590
break;

PresentData/TraceSession.hpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ struct PMTraceConsumer;
55
struct MRTraceConsumer;
66

77
struct TraceSession {
8+
enum TimestampType {
9+
TIMESTAMP_TYPE_QPC = 1,
10+
TIMESTAMP_TYPE_SYSTEM_TIME = 2,
11+
TIMESTAMP_TYPE_CPU_CYCLE_COUNTER = 3,
12+
};
13+
814
LARGE_INTEGER mStartTimestamp = {};
915
LARGE_INTEGER mTimestampFrequency = {};
1016
uint64_t mStartFileTime = 0;
17+
TimestampType mTimestampType = TIMESTAMP_TYPE_QPC;
18+
1119
PMTraceConsumer* mPMConsumer = nullptr;
1220
MRTraceConsumer* mMRConsumer = nullptr;
1321
TRACEHANDLE mSessionHandle = 0; // invalid session handles are 0
1422
TRACEHANDLE mTraceHandle = INVALID_PROCESSTRACE_HANDLE; // invalid trace handles are INVALID_PROCESSTRACE_HANDLE
1523
ULONG mContinueProcessingBuffers = TRUE;
1624

17-
enum TimestampType {
18-
TIMESTAMP_TYPE_QPC = 1,
19-
TIMESTAMP_TYPE_SYSTEM_TIME = 2,
20-
TIMESTAMP_TYPE_CPU_CYCLE_COUNTER = 3,
21-
};
22-
2325
ULONG Start(
2426
PMTraceConsumer* pmConsumer, // Required PMTraceConsumer instance
2527
MRTraceConsumer* mrConsumer, // If nullptr, no WinMR tracing
2628
wchar_t const* etlPath, // If nullptr, live/realtime tracing session
27-
wchar_t const* sessionName, // Required session name
28-
TimestampType timestampType); // Which timestamp type to use
29+
wchar_t const* sessionName); // Required session name
2930

3031
void Stop();
3132

PresentMon/TraceSession.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ bool StartTraceSession()
3434
gMRConsumer = new MRTraceConsumer(args.mTrackDisplay);
3535
}
3636

37+
if (args.mOutputDateTime) {
38+
gSession.mTimestampType = TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME;
39+
} else {
40+
gSession.mTimestampType = TraceSession::TIMESTAMP_TYPE_QPC;
41+
}
42+
3743
// Start the session;
3844
// If a session with this same name is already running, we either exit or
3945
// stop it and start a new session. This is useful if a previous process
4046
// failed to properly shut down the session for some reason.
41-
auto timestampType = args.mOutputDateTime ? TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME : TraceSession::TIMESTAMP_TYPE_QPC;
42-
auto status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName, timestampType);
47+
auto status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName);
4348
if (status == ERROR_ALREADY_EXISTS) {
4449
if (args.mStopExistingSession) {
4550
PrintWarning(
@@ -61,7 +66,7 @@ bool StartTraceSession()
6166

6267
status = TraceSession::StopNamedSession(args.mSessionName);
6368
if (status == ERROR_SUCCESS) {
64-
status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName, timestampType);
69+
status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName);
6570
}
6671
}
6772

@@ -164,12 +169,13 @@ double TimestampToSeconds(uint64_t timestamp)
164169

165170
void TimestampToLocalSystemTime(uint64_t timestamp, SYSTEMTIME* st, uint64_t* ns)
166171
{
167-
/* if not TIMESTAMP_TYPE_SYSTEM_TIME
168-
auto tns = (timestamp - gSession.mStartTimestamp.QuadPart) * 1000000000ull / gSession.mTimestampFrequency.QuadPart;
169-
auto ft = gSession.mStartFileTime + (tns / 100);
170-
FileTimeToSystemTime((FILETIME const*) &ft, st);
171-
*ns = tns % 1000000000;
172-
*/
173-
FileTimeToSystemTime((FILETIME const*) &timestamp, st);
172+
if (gSession.mTimestampType != TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME) {
173+
auto delta100ns = (timestamp - gSession.mStartTimestamp.QuadPart) * 10000000ull / gSession.mTimestampFrequency.QuadPart;
174+
timestamp = gSession.mStartFileTime + delta100ns;
175+
}
176+
177+
FILETIME lft{};
178+
FileTimeToLocalFileTime((FILETIME*) &timestamp, &lft);
179+
FileTimeToSystemTime(&lft, st);
174180
*ns = (timestamp % 10000000) * 100;
175181
}

0 commit comments

Comments
 (0)