forked from GameTechDev/PresentMon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (163 loc) · 6.05 KB
/
Copy pathmain.cpp
File metadata and controls
181 lines (163 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//--------------------------------------------------------------------------------------
// Copyright 2015 Intel Corporation
// All Rights Reserved
//
// Permission is granted to use, copy, distribute and prepare derivative works of this
// software for any purpose and without fee, provided, that the above copyright notice
// and this statement appear in all copies. Intel makes no representations about the
// suitability of this software for any purpose. THIS SOFTWARE IS PROVIDED "AS IS."
// INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, AND ALL LIABILITY,
// INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THIS SOFTWARE,
// INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS, AND INCLUDING THE
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Intel does not
// assume any responsibility for any errors which may appear in this software nor any
// responsibility to update it.
//--------------------------------------------------------------------------------------
#define BUILDNUM 104
#include <windows.h>
#include <thread>
#include <cstdio>
#include "PresentMon.hpp"
#include "Util.hpp"
#include <mutex>
bool g_Quit = false;
static std::thread *g_PresentMonThread;
static std::mutex *g_ExitMutex;
BOOL WINAPI HandlerRoutine(
_In_ DWORD dwCtrlType
)
{
std::lock_guard<std::mutex> lock(*g_ExitMutex);
g_Quit = true;
if (g_PresentMonThread) {
g_PresentMonThread->join();
}
return TRUE;
}
void printHelp()
{
printf("PresentMon Build %d\n",
BUILDNUM);
printf(
"\nCommand line options:\n"
" -captureall: record ALL processes (default).\n"
" -process_name [exe name]: record specific process.\n"
" -process_id [integer]: record specific process ID.\n"
" -output_file [path]: override the default output path.\n"
" -etl_file [path]: consume events from an ETL file instead of real-time.\n"
" -delay [seconds]: wait before starting to consume events.\n"
" -timed [seconds]: stop listening and exit after a set amount of time.\n"
" -no_csv: do not create any output file.\n"
" -exclude_dropped: exclude dropped presents from the csv output.\n"
" -scroll_toggle: only record events while scroll lock is enabled.\n"
);
printf("\nCSV columns explained (self explanatory columns omitted):\n"
" Dropped: boolean indicator. 1 = dropped, 0 = displayed.\n"
" MsBetweenPresents: time between this Present() API call and the previous one.\n"
" MsBetweenDisplayChange: time between when this frame was displayed, and previous was displayed.\n"
" MsInPresentAPI: time spent inside the Present() API call.\n"
" MsUntilRenderComplete: time between present start and GPU work completion.\n"
" MsUntilDisplayed: time between present start and frame display.\n"
);
}
int main(int argc, char ** argv)
{
--argc;
++argv;
if (argc == 0) {
printHelp();
return 0;
}
int waitpid = -1;
PresentMonArgs args;
std::string title_string = "PresentMon";
args.mTargetProcessName = "*";
for (int i = 0; i < argc; ++i)
{
// 2-component arguments
if (i + 1 < argc)
{
if (!strcmp(argv[i], "-waitpid"))
{
waitpid = atoi(argv[++i]);
continue;
}
else if (!strcmp(argv[i], "-process_name"))
{
args.mTargetProcessName = argv[++i];
}
else if (!strcmp(argv[i], "-process_id"))
{
args.mTargetPid = atoi(argv[++i]);
}
else if (!strcmp(argv[i], "-output_file"))
{
args.mOutputFileName = argv[++i];
}
else if (!strcmp(argv[i], "-etl_file"))
{
args.mEtlFileName = argv[++i];
}
else if (!strcmp(argv[i], "-delay"))
{
args.mDelay = atoi(argv[++i]);
}
else if (!strcmp(argv[i], "-timed"))
{
args.mTimer = atoi(argv[++i]);
}
}
// 1-component args
{
if (!strcmp(argv[i], "-no_csv"))
{
args.mOutputFileName = "*";
}
else if (!strcmp(argv[i], "-exclude_dropped"))
{
args.mExcludeDropped = true;
}
else if (!strcmp(argv[i], "-scroll_toggle"))
{
args.mScrollLockToggle = true;
}
else if (!strcmp(argv[i], "-?") || !strcmp(argv[i], "-help"))
{
printHelp();
return 0;
}
}
title_string += ' ';
title_string += argv[i];
}
if (waitpid >= 0) {
WaitForProcess(waitpid);
if (!HaveAdministratorPrivileges()) {
printf("Elevation process failed. Aborting.\n");
return 0;
}
}
if (!args.mEtlFileName && !HaveAdministratorPrivileges()) {
printf("Process is not running as admin. Attempting to elevate.\n");
RestartAsAdministrator(argc, argv);
return 0;
}
SetConsoleCtrlHandler(HandlerRoutine, TRUE);
SetConsoleTitleA(title_string.c_str());
std::mutex exit_mutex;
g_ExitMutex = &exit_mutex;
// Run PM in a separate thread so we can join it in the CtrlHandler (can't join the main thread)
std::thread pm(PresentMonEtw, args);
g_PresentMonThread = ±
while (!g_Quit)
{
Sleep(100);
}
// Wait for tracing to finish, to ensure the PM thread closes the session correctly
// Prevent races on joining the PM thread between the control handler and the main thread
std::lock_guard<std::mutex> lock(exit_mutex);
if (g_PresentMonThread->joinable()) {
g_PresentMonThread->join();
}
return 0;
}