Skip to content

Commit

Permalink
Goes with: Improve --datetime accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffersonMontgomery-Intel committed Feb 21, 2024
1 parent 0c6b5fc commit ba4c5c6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
8 changes: 4 additions & 4 deletions PresentData/TraceSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ ULONG TraceSession::Start(
PMTraceConsumer* pmConsumer,
MRTraceConsumer* mrConsumer,
wchar_t const* etlPath,
wchar_t const* sessionName,
TimestampType timestampType)
wchar_t const* sessionName)
{
assert(mSessionHandle == 0);
assert(mTraceHandle == INVALID_PROCESSTRACE_HANDLE);
Expand Down Expand Up @@ -529,7 +528,7 @@ ULONG TraceSession::Start(

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

switch (traceProps.LogfileHeader.ReservedFlags) {
mTimestampType = (TimestampType) traceProps.LogfileHeader.ReservedFlags;
switch (mTimestampType) {
case TIMESTAMP_TYPE_SYSTEM_TIME:
mTimestampFrequency.QuadPart = 10000000ull;
break;
Expand Down
17 changes: 9 additions & 8 deletions PresentData/TraceSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@ struct PMTraceConsumer;
struct MRTraceConsumer;

struct TraceSession {
enum TimestampType {
TIMESTAMP_TYPE_QPC = 1,
TIMESTAMP_TYPE_SYSTEM_TIME = 2,
TIMESTAMP_TYPE_CPU_CYCLE_COUNTER = 3,
};

LARGE_INTEGER mStartTimestamp = {};
LARGE_INTEGER mTimestampFrequency = {};
uint64_t mStartFileTime = 0;
TimestampType mTimestampType = TIMESTAMP_TYPE_QPC;

PMTraceConsumer* mPMConsumer = nullptr;
MRTraceConsumer* mMRConsumer = nullptr;
TRACEHANDLE mSessionHandle = 0; // invalid session handles are 0
TRACEHANDLE mTraceHandle = INVALID_PROCESSTRACE_HANDLE; // invalid trace handles are INVALID_PROCESSTRACE_HANDLE
ULONG mContinueProcessingBuffers = TRUE;

enum TimestampType {
TIMESTAMP_TYPE_QPC = 1,
TIMESTAMP_TYPE_SYSTEM_TIME = 2,
TIMESTAMP_TYPE_CPU_CYCLE_COUNTER = 3,
};

ULONG Start(
PMTraceConsumer* pmConsumer, // Required PMTraceConsumer instance
MRTraceConsumer* mrConsumer, // If nullptr, no WinMR tracing
wchar_t const* etlPath, // If nullptr, live/realtime tracing session
wchar_t const* sessionName, // Required session name
TimestampType timestampType); // Which timestamp type to use
wchar_t const* sessionName); // Required session name

void Stop();

Expand Down
26 changes: 16 additions & 10 deletions PresentMon/TraceSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ bool StartTraceSession()
gMRConsumer = new MRTraceConsumer(args.mTrackDisplay);
}

if (args.mOutputDateTime) {
gSession.mTimestampType = TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME;
} else {
gSession.mTimestampType = TraceSession::TIMESTAMP_TYPE_QPC;
}

// Start the session;
// If a session with this same name is already running, we either exit or
// stop it and start a new session. This is useful if a previous process
// failed to properly shut down the session for some reason.
auto timestampType = args.mOutputDateTime ? TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME : TraceSession::TIMESTAMP_TYPE_QPC;
auto status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName, timestampType);
auto status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName);
if (status == ERROR_ALREADY_EXISTS) {
if (args.mStopExistingSession) {
PrintWarning(
Expand All @@ -61,7 +66,7 @@ bool StartTraceSession()

status = TraceSession::StopNamedSession(args.mSessionName);
if (status == ERROR_SUCCESS) {
status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName, timestampType);
status = gSession.Start(gPMConsumer, gMRConsumer, args.mEtlFileName, args.mSessionName);
}
}

Expand Down Expand Up @@ -164,12 +169,13 @@ double TimestampToSeconds(uint64_t timestamp)

void TimestampToLocalSystemTime(uint64_t timestamp, SYSTEMTIME* st, uint64_t* ns)
{
/* if not TIMESTAMP_TYPE_SYSTEM_TIME
auto tns = (timestamp - gSession.mStartTimestamp.QuadPart) * 1000000000ull / gSession.mTimestampFrequency.QuadPart;
auto ft = gSession.mStartFileTime + (tns / 100);
FileTimeToSystemTime((FILETIME const*) &ft, st);
*ns = tns % 1000000000;
*/
FileTimeToSystemTime((FILETIME const*) &timestamp, st);
if (gSession.mTimestampType != TraceSession::TIMESTAMP_TYPE_SYSTEM_TIME) {
auto delta100ns = (timestamp - gSession.mStartTimestamp.QuadPart) * 10000000ull / gSession.mTimestampFrequency.QuadPart;
timestamp = gSession.mStartFileTime + delta100ns;
}

FILETIME lft{};
FileTimeToLocalFileTime((FILETIME*) &timestamp, &lft);
FileTimeToSystemTime(&lft, st);
*ns = (timestamp % 10000000) * 100;
}

0 comments on commit ba4c5c6

Please sign in to comment.