Repository navigation
Expand file tree
/
Copy pathwtime.cpp
More file actions
179 lines (140 loc) · 4.26 KB
/
Copy pathwtime.cpp
File metadata and controls
179 lines (140 loc) · 4.26 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
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
// Software version (major.minor.path date time)
static const char VERSION[] = "0.0.1 " __DATE__ " " __TIME__;
static void ShowErrorMessage(DWORD errorCode)
{
char *buffer = nullptr;
FormatMessageA
(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &buffer, 0, nullptr
);
if (buffer != nullptr)
{
printf_s("%s", buffer);
LocalFree(buffer);
}
}
static unsigned long long SubtractTime(FILETIME ftA, FILETIME ftB)
{
ULARGE_INTEGER a, b;
a.LowPart = ftA.dwLowDateTime;
a.HighPart = ftA.dwHighDateTime;
b.LowPart = ftB.dwLowDateTime;
b.HighPart = ftB.dwHighDateTime;
return a.QuadPart - b.QuadPart;
}
static double ToMilliseconds(unsigned long long t)
{
unsigned long long TICKS_PER_MS = 10000;
return (double)t / (double)TICKS_PER_MS;
}
static double ToMilliseconds(FILETIME ft)
{
ULARGE_INTEGER r;
r.LowPart = ft.dwLowDateTime;
r.HighPart = ft.dwHighDateTime;
return ToMilliseconds(r.QuadPart);
}
static void PrintTime(const char *prefix, double milliseconds)
{
int d = 0, h = 0, m = 0, s = 0;
auto ms = static_cast<unsigned long long>(milliseconds);
if (ms >= 1000)
{
s = (int)(ms / 1000); // seconds
ms %= 1000; // remaining milliseconds
m = s / 60; // minutes
s %= 60; // remaining seconds
h = m / 60; // hours
m %= 60; // remaining minutes
d = h / 24; // days
h %= 24; // remaining hours
}
printf_s("%s % 4dd % 2dh % 2dm % 2ds % 3dms\n", prefix, d, h, m, s, (int)ms);
}
static void ShowExecutionTime(HANDLE hProcess)
{
FILETIME creationTime = { 0 }, exitTime = { 0 };
FILETIME kernelTime = { 0 }, userTime = { 0 };
if (!GetProcessTimes(hProcess, &creationTime, &exitTime, &kernelTime, &userTime))
{
ShowErrorMessage(GetLastError());
return;
}
ULARGE_INTEGER r;
r.QuadPart = SubtractTime(exitTime, creationTime);
putchar('\n');
putchar('\n');
double time = ToMilliseconds(r.QuadPart);
PrintTime("real:", time);
time = ToMilliseconds(userTime);
PrintTime("user:", time);
time = ToMilliseconds(kernelTime);
PrintTime("sys :", time);
}
static void RunProgram(const std::string &program, const std::string &arguments)
{
STARTUPINFOA si = {};
PROCESS_INFORMATION pi = {};
si.cb = (DWORD)sizeof(si);
si.wShowWindow = SW_NORMAL;
si.dwFlags |= STARTF_USESHOWWINDOW;
std::string commandLine(program + " " + arguments);
char *cmd = const_cast<char*>(commandLine.c_str());
BOOL oK = CreateProcessA
(
nullptr, cmd, nullptr, nullptr, FALSE, CREATE_NEW_PROCESS_GROUP,
nullptr, nullptr, &si, &pi
);
if (oK)
{
WaitForSingleObject(pi.hProcess, INFINITE);
ShowExecutionTime(pi.hProcess);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
DWORD err = GetLastError();
ShowErrorMessage(err);
}
}
static void ShowHelp()
{
{
printf_s("Usage:\n");
printf_s(" wtime command [arguments ...]\n\n");
}
{
printf_s("VERSION:\n");
printf_s(" %s\n\n", VERSION);
}
{
printf_s("OUTPUT TIPS:\n");
printf_s(" real: the amount of time between the the process has exit and created\n\n");
printf_s(" user: amount of time that the process has executed in user mode.\n");
printf_s(" This value can exceed the amount of real time if the process \n");
printf_s(" executes across multiple CPU cores.\n\n");
printf_s(" sys : amount of time that the process has executed in kernel mode.\n");
}
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
ShowHelp();
return EXIT_FAILURE;
}
std::string arguments;
arguments.reserve(1024);
for (int i = 2; i < argc; ++i)
{
arguments.append(argv[i]);
arguments.append(i < argc - 1 ? " " : "");
}
RunProgram(argv[1], arguments);
return EXIT_SUCCESS;
}