-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasure.cpp
More file actions
131 lines (111 loc) · 3.52 KB
/
measure.cpp
File metadata and controls
131 lines (111 loc) · 3.52 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
//
// measure.cpp
// -----------
// Runs a child process with stdin/stdout redirected and measures:
// - wall clock time (ms)
// - peak working set memory (MB)
// - exit code
//
// Usage:
// measure.exe <executable> <input_file> <output_file> <stats_file>
//
// Writes one line to stats_file:
// <elapsed_ms> <peak_mb> <exit_code>
//
// Compile once (handled automatically by perf_test.bat):
// g++ -std=gnu++23 -O2 measure.cpp -o build\measure -lpsapi
//
#include <windows.h>
#include <psapi.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#pragma comment(lib, "psapi.lib")
int main(int argc, char* argv[])
{
if (argc != 5)
{
std::cerr << "Usage: measure <executable> <input_file> <output_file> <stats_file>\n";
return 1;
}
std::string executable = argv[1];
std::string input_file = argv[2];
std::string output_file = argv[3];
std::string stats_file = argv[4];
SECURITY_ATTRIBUTES sa{};
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = nullptr;
HANDLE h_in = CreateFileA(
input_file.c_str(),
GENERIC_READ, FILE_SHARE_READ,
&sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr
);
if (h_in == INVALID_HANDLE_VALUE)
{
std::cerr << "[measure] Cannot open input file: " << input_file << "\n";
return 1;
}
HANDLE h_out = CreateFileA(
output_file.c_str(),
GENERIC_WRITE, 0,
&sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr
);
if (h_out == INVALID_HANDLE_VALUE)
{
std::cerr << "[measure] Cannot open output file: " << output_file << "\n";
CloseHandle(h_in);
return 1;
}
STARTUPINFOA si{};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = h_in;
si.hStdOutput = h_out;
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
PROCESS_INFORMATION pi{};
auto start = std::chrono::steady_clock::now();
BOOL ok = CreateProcessA(
nullptr,
const_cast<char*>(executable.c_str()),
nullptr, nullptr,
TRUE,
0, nullptr, nullptr,
&si, &pi
);
if (!ok)
{
std::cerr << "[measure] Failed to launch: " << executable
<< " (error " << GetLastError() << ")\n";
CloseHandle(h_in);
CloseHandle(h_out);
return 1;
}
// Poll peak memory every 10ms while process runs
SIZE_T peak_bytes = 0;
while (WaitForSingleObject(pi.hProcess, 10) == WAIT_TIMEOUT)
{
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(pi.hProcess, &pmc, sizeof(pmc)))
if (pmc.WorkingSetSize > peak_bytes)
peak_bytes = pmc.WorkingSetSize;
}
auto end = std::chrono::steady_clock::now();
// Final sample after exit — process may flush memory on exit
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(pi.hProcess, &pmc, sizeof(pmc)))
if (pmc.WorkingSetSize > peak_bytes)
peak_bytes = pmc.WorkingSetSize;
DWORD exit_code = 0;
GetExitCodeProcess(pi.hProcess, &exit_code);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(h_in);
CloseHandle(h_out);
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
double peak_mb = static_cast<double>(peak_bytes) / (1024.0 * 1024.0);
std::ofstream stats(stats_file);
stats << elapsed_ms << " " << peak_mb << " " << exit_code << "\n";
return 0;
}