-
Notifications
You must be signed in to change notification settings - Fork 141
/
Console.cpp
273 lines (228 loc) · 8.27 KB
/
Console.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (C) 2019-2021 Intel Corporation
// SPDX-License-Identifier: MIT
#include "PresentMon.hpp"
static HANDLE gConsoleHandle = INVALID_HANDLE_VALUE;
static char gConsoleWriteBuffer[8 * 1024] = {};
static uint32_t gConsoleWriteBufferIndex = 0;
static uint32_t gConsolePrevWriteBufferSize = 0;
static SHORT gConsoleTop = 0;
static SHORT gConsoleWidth = 0;
static SHORT gConsoleBufferHeight = 0;
static bool gConsoleFirstCommit = true;
bool IsConsoleInitialized()
{
return gConsoleHandle != INVALID_HANDLE_VALUE;
}
bool InitializeConsole()
{
if (!IsConsoleInitialized()) {
gConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (gConsoleHandle == INVALID_HANDLE_VALUE) {
return false;
}
CONSOLE_SCREEN_BUFFER_INFO info = {};
if (GetConsoleScreenBufferInfo(gConsoleHandle, &info) == 0) {
gConsoleHandle = INVALID_HANDLE_VALUE;
return false;
}
gConsoleTop = info.dwCursorPosition.Y;
gConsoleWidth = info.srWindow.Right - info.srWindow.Left + 1;
gConsoleBufferHeight = info.dwSize.Y;
gConsoleWriteBufferIndex = 0;
gConsolePrevWriteBufferSize = 0;
gConsoleFirstCommit = true;
}
return true;
}
static void vConsolePrint(char const* format, va_list args)
{
auto s = gConsoleWriteBuffer + gConsoleWriteBufferIndex;
auto n = sizeof(gConsoleWriteBuffer) - gConsoleWriteBufferIndex;
int r = vsnprintf(s, n, format, args);
if (r > 0) {
gConsoleWriteBufferIndex = min((uint32_t) (n - 1), gConsoleWriteBufferIndex + r);
}
}
void ConsolePrint(char const* format, ...)
{
va_list args;
va_start(args, format);
vConsolePrint(format, args);
va_end(args);
}
void ConsolePrintLn(char const* format, ...)
{
va_list args;
va_start(args, format);
vConsolePrint(format, args);
va_end(args);
auto x = gConsoleWriteBufferIndex % gConsoleWidth;
auto s = gConsoleWidth - x;
memset(gConsoleWriteBuffer + gConsoleWriteBufferIndex, ' ', s);
gConsoleWriteBufferIndex += s;
}
void CommitConsole()
{
auto sizeWritten = gConsoleWriteBufferIndex;
auto linesWritten = (SHORT) (sizeWritten / gConsoleWidth);
// Reset gConsoleTop on the first commit so we don't overwrite any warning
// messages.
auto size = sizeWritten;
if (gConsoleFirstCommit) {
gConsoleFirstCommit = false;
CONSOLE_SCREEN_BUFFER_INFO info = {};
GetConsoleScreenBufferInfo(gConsoleHandle, &info);
gConsoleTop = info.dwCursorPosition.Y;
} else {
// Write some extra empty lines to make sure we clear anything from
// last time.
if (size < gConsolePrevWriteBufferSize) {
memset(gConsoleWriteBuffer + size, ' ', gConsolePrevWriteBufferSize - size);
size = gConsolePrevWriteBufferSize;
}
}
// If we're at the end of the console buffer, issue some new lines to make
// some space.
auto maxCursorY = gConsoleBufferHeight - linesWritten;
if (gConsoleTop > maxCursorY) {
COORD bottom = { 0, gConsoleBufferHeight - 1 };
SetConsoleCursorPosition(gConsoleHandle, bottom);
printf("\n");
for (--gConsoleTop; gConsoleTop > maxCursorY; --gConsoleTop) {
printf("\n");
}
}
// Write to the console.
DWORD dwCharsWritten = 0;
COORD cursor = { 0, gConsoleTop };
WriteConsoleOutputCharacterA(gConsoleHandle, gConsoleWriteBuffer, (DWORD) size, cursor, &dwCharsWritten);
// Put the cursor at the end of the written text.
cursor.Y += linesWritten;
SetConsoleCursorPosition(gConsoleHandle, cursor);
// Update console info in case it was resized.
CONSOLE_SCREEN_BUFFER_INFO info = {};
GetConsoleScreenBufferInfo(gConsoleHandle, &info);
gConsoleWidth = info.srWindow.Right - info.srWindow.Left + 1;
gConsoleBufferHeight = info.dwSize.Y;
gConsoleWriteBufferIndex = 0;
gConsolePrevWriteBufferSize = sizeWritten;
}
void UpdateConsole(uint32_t processId, ProcessInfo const& processInfo)
{
auto const& args = GetCommandLineArgs();
// Don't display non-target or empty processes
if (!processInfo.mTargetProcess ||
processInfo.mModuleName.empty() ||
processInfo.mSwapChain.empty()) {
return;
}
auto empty = true;
for (auto const& pair : processInfo.mSwapChain) {
auto address = pair.first;
auto const& chain = pair.second;
// Only show swapchain data if there at least two presents in the
// history.
if (chain.mPresentHistoryCount < 2) {
continue;
}
auto const& present0 = *chain.mPresentHistory[(chain.mNextPresentIndex - chain.mPresentHistoryCount) % SwapChainData::PRESENT_HISTORY_MAX_COUNT];
auto const& presentN = *chain.mPresentHistory[(chain.mNextPresentIndex - 1) % SwapChainData::PRESENT_HISTORY_MAX_COUNT];
auto cpuAvg = QpcDeltaToSeconds(presentN.PresentStartTime - present0.PresentStartTime) / (chain.mPresentHistoryCount - 1);
auto dspAvg = 0.0;
auto latAvg = 0.0;
PresentEvent* displayN = nullptr;
if (args.mTrackDisplay) {
uint64_t display0ScreenTime = 0;
uint64_t latSum = 0;
uint32_t displayCount = 0;
for (uint32_t i = 0; i < chain.mPresentHistoryCount; ++i) {
auto const& p = chain.mPresentHistory[(chain.mNextPresentIndex - chain.mPresentHistoryCount + i) % SwapChainData::PRESENT_HISTORY_MAX_COUNT];
if (p->FinalState == PresentResult::Presented) {
if (displayCount == 0) {
display0ScreenTime = p->ScreenTime;
}
displayN = p.get();
latSum += p->ScreenTime - p->PresentStartTime;
displayCount += 1;
}
}
if (displayCount >= 2) {
dspAvg = QpcDeltaToSeconds(displayN->ScreenTime - display0ScreenTime) / (displayCount - 1);
}
if (displayCount >= 1) {
latAvg = QpcDeltaToSeconds(latSum) / displayCount;
}
}
if (empty) {
empty = false;
ConsolePrintLn("%s[%d]:", processInfo.mModuleName.c_str(), processId);
}
ConsolePrint(" %016llX (%s): SyncInterval=%d Flags=%d CPU%s=%.2lf",
address,
RuntimeToString(presentN.Runtime),
presentN.SyncInterval,
presentN.PresentFlags,
dspAvg > 0.0 ? "/Display" : "",
1000.0 * cpuAvg);
if (dspAvg > 0.0) ConsolePrint("/%.2lf", 1000.0 * dspAvg);
ConsolePrint("ms (%.1lf", 1.0 / cpuAvg);
if (dspAvg > 0.0) ConsolePrint("/%.1lf", 1.0 / dspAvg);
ConsolePrint(" fps)");
if (latAvg > 0.0) {
ConsolePrint(" latency=%.2lfms", 1000.0 * latAvg);
}
if (displayN != nullptr) {
ConsolePrint(" %s", PresentModeToString(displayN->PresentMode));
}
ConsolePrintLn("");
}
if (!empty) {
ConsolePrintLn("");
}
}
namespace {
int PrintColor(WORD color, char const* format, va_list val)
{
#ifndef NDEBUG
{
char buffer[256];
if (_vsnprintf_s(buffer, _TRUNCATE, format, val) == -1) {
buffer[252] = '.';
buffer[253] = '.';
buffer[254] = '.';
}
OutputDebugStringA(buffer);
}
#endif
CONSOLE_SCREEN_BUFFER_INFO info = {};
auto setColor = IsConsoleInitialized() && GetConsoleScreenBufferInfo(gConsoleHandle, &info) != 0;
if (setColor) {
auto bg = info.wAttributes & (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
if (bg == 0) {
color |= FOREGROUND_INTENSITY;
}
SetConsoleTextAttribute(gConsoleHandle, WORD(bg | color));
}
int c = vfprintf(stderr, format, val);
if (setColor) {
SetConsoleTextAttribute(gConsoleHandle, info.wAttributes);
}
return c;
}
}
int PrintWarning(char const* format, ...)
{
va_list val;
va_start(val, format);
int c = PrintColor(FOREGROUND_RED | FOREGROUND_GREEN, format, val);
va_end(val);
return c;
}
int PrintError(char const* format, ...)
{
va_list val;
va_start(val, format);
int c = PrintColor(FOREGROUND_RED, format, val);
va_end(val);
return c;
}