-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeMem.c
202 lines (168 loc) · 4.77 KB
/
TimeMem.c
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
/* TimeMem.c - Windows port of Unix time utility */
//added by dadeniji 2020-05-22
#define NDEBUG 1
#include <Windows.h>
#include <Psapi.h>
#include <stdio.h>
#include <tchar.h>
void displayLastError(void)
{
/*
Aaronontheweb
Win32 Errors: How to Format GetLastError() Output into Readable Strings
https://gist.github.com/Aaronontheweb/7461004#file-getlasterror-cpp
*/
DWORD dLastError = GetLastError();
LPCTSTR strErrorMessage = NULL;
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
dLastError,
0,
//(LPWSTR) &strErrorMessage,
(LPTSTR) &strErrorMessage,
0,
NULL
);
//Prints debug output to the console
_tprintf(_T(strErrorMessage));
//Prints debug output to the console
_tprintf("Error Code #: %ld\n", dLastError);
}
/* Displays usage help for this program. */
static void usage()
{
_tprintf(_T("Usage: TimeMem command [args...]\n"));
}
/* Converts FILETIME to ULONGLONG. */
static ULONGLONG ConvertFileTime(const FILETIME *t)
{
ULARGE_INTEGER i;
CopyMemory(&i, t, sizeof(ULARGE_INTEGER));
return i.QuadPart;
}
/* Displays information about a process. */
static int info(HANDLE hProcess)
{
DWORD dwExitCode;
FILETIME ftCreation, ftExit, ftKernel, ftUser;
double tElapsed, tKernel, tUser;
PROCESS_MEMORY_COUNTERS pmc = { sizeof(PROCESS_MEMORY_COUNTERS) };
_Bool bRC = 0;
/* Exit code */
if (!GetExitCodeProcess(hProcess, &dwExitCode))
return 1;
/* CPU info */
if (!GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser))
{
return 1;
}
tElapsed = 1.0e-7 * (ConvertFileTime(&ftExit) - ConvertFileTime(&ftCreation));
tKernel = 1.0e-7 * ConvertFileTime(&ftKernel);
tUser = 1.0e-7 * ConvertFileTime(&ftUser);
/* Memory info */
// Print information about the memory usage of the process.
bRC = GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc));
/*
Aaronontheweb
Win32 Errors: How to Format GetLastError() Output into Readable Strings
https://gist.github.com/Aaronontheweb/7461004#file-getlasterror-cpp
*/
if (bRC ==0)
{
displayLastError();
return (1);
}
/* Display info. */
_tprintf(_T("Exit code : %u\n"), dwExitCode);
_tprintf(_T("Elapsed time : %.2lf\n"), tElapsed);
_tprintf(_T("Kernel time : %.2lf (%.1lf%%)\n"), tKernel, 100.0*tKernel/tElapsed);
_tprintf(_T("User time : %.2lf (%.1lf%%)\n"), tUser, 100.0*tUser/tElapsed);
_tprintf(_T("page fault # : %u\n"), pmc.PageFaultCount);
_tprintf(_T("Working set : %u KB\n"), pmc.PeakWorkingSetSize/1024);
_tprintf(_T("Paged pool : %u KB\n"), pmc.QuotaPeakPagedPoolUsage/1024);
_tprintf(_T("Non-paged pool : %u KB\n"), pmc.QuotaPeakNonPagedPoolUsage/1024);
_tprintf(_T("Page file size : %u KB\n"), pmc.PeakPagefileUsage/1024);
return 0;
}
/* Todo:
* - mimic linux time utility interface; e.g. see http://linux.die.net/man/1/time
* - build under 64-bit
* - display detailed error message
*/
int _tmain(
#ifndef NDEBUG
int argc, _TCHAR *argv[]
#endif
)
{
LPTSTR szCmdLine;
LPTSTR szBegin;
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi;
int ret;
/* Read the command line. */
szCmdLine = GetCommandLine();
/* Strip the first token from the command line. */
if (szCmdLine[0] == '"')
{
/* The first token is double-quoted. Note that we don't need to
* worry about escaped quote, because a quote is not a valid
* path name under Windows.
*/
LPTSTR p = szCmdLine + 1;
while (*p && *p != '"')
++p;
szBegin = (*p == '"')? p + 1 : p;
}
else
{
/* The first token is deliminated by a space or tab.
* See "Parsing C++ Command Line Arguments" below:
* http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx
*/
LPTSTR p = szCmdLine;
while (*p && *p != ' ' && *p != '\t')
++p;
szBegin = p;
}
/* Skip white spaces. */
while (*szBegin == ' ' || *szBegin == '\t')
++szBegin;
/* If we have no more arguments, display usage info and exit. */
if (*szBegin == 0)
{
usage();
return 1;
}
/* Display argc,argv and command line for debugging purpose. */
#ifndef NDEBUG
{
int i;
for (i = 0; i < argc; i++)
_tprintf(_T("argv[%d]=%s\n"), i, argv[i]);
_tprintf(_T("CmdLine=%s\n"), szCmdLine);
_tprintf(_T("Invoked=%s\n"), szBegin);
}
#endif
/* Create the process. */
if (!CreateProcess(NULL, szBegin, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
_tprintf(_T("Error: Cannot create process.\n"));
displayLastError();
return 1;
}
/* Wait for the process to finish. */
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_OBJECT_0)
{
_tprintf(_T("Error: Cannot wait for process.\n"));
return 1;
}
/* Display process statistics. */
ret = info(pi.hProcess);
/* Close process handles. */
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return ret;
}