trace2: report peak memory usage of the process

Teach Windows version of git to report peak memory usage during exit() processing. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Apr 15, 2019 at 13:39 UTC 26c6f251d754a33c188ce7ad8f5ea6cb0bc740d7
4 files changed +60 -8
common-main.c
+1 -1
@@ -41,7 +41,7 @@ int main(int argc, const char **argv)
41
42 trace2_initialize();
43 trace2_cmd_start(argv);
44 - trace2_collect_process_info();
44 + trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);
45
46 git_setup_gettext();
47
compat/win32/trace2_win32_process_info.c
+47 -3
@@ -1,5 +1,6 @@
1 #include "../../cache.h"
2 #include "../../json-writer.h"
3 +#include "lazyload.h"
4 #include <Psapi.h>
5 #include <tlHelp32.h>
6
@@ -137,11 +138,54 @@ static void get_is_being_debugged(void)
138 "windows/debugger_present", 1);
139 }
140
140 -void trace2_collect_process_info(void)
141 +/*
142 + * Emit JSON data with the peak memory usage of the current process.
143 + */
144 +static void get_peak_memory_info(void)
145 +{
146 + DECLARE_PROC_ADDR(psapi.dll, BOOL, GetProcessMemoryInfo, HANDLE,
147 + PPROCESS_MEMORY_COUNTERS, DWORD);
148 +
149 + if (INIT_PROC_ADDR(GetProcessMemoryInfo)) {
150 + PROCESS_MEMORY_COUNTERS pmc;
151 +
152 + if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc,
153 + sizeof(pmc))) {
154 + struct json_writer jw = JSON_WRITER_INIT;
155 +
156 + jw_object_begin(&jw, 0);
157 +
158 +#define KV(kv) #kv, (intmax_t)pmc.kv
159 +
160 + jw_object_intmax(&jw, KV(PageFaultCount));
161 + jw_object_intmax(&jw, KV(PeakWorkingSetSize));
162 + jw_object_intmax(&jw, KV(PeakPagefileUsage));
163 +
164 + jw_end(&jw);
165 +
166 + trace2_data_json("process", the_repository,
167 + "windows/memory", &jw);
168 + jw_release(&jw);
169 + }
170 + }
171 +}
172 +
173 +void trace2_collect_process_info(enum trace2_process_info_reason reason)
174 {
175 if (!trace2_is_enabled())
176 return;
177
145 - get_is_being_debugged();
146 - get_ancestry();
178 + switch (reason) {
179 + case TRACE2_PROCESS_INFO_STARTUP:
180 + get_is_being_debugged();
181 + get_ancestry();
182 + return;
183 +
184 + case TRACE2_PROCESS_INFO_EXIT:
185 + get_peak_memory_info();
186 + return;
187 +
188 + default:
189 + BUG("trace2_collect_process_info: unknown reason '%d'", reason);
190 + }
191 }
trace2.c
+2
@@ -213,6 +213,8 @@ int trace2_cmd_exit_fl(const char *file, int line, int code)
213 if (!trace2_enabled)
214 return code;
215
216 + trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT);
217 +
218 tr2main_exit_code = code;
219
220 us_now = getnanotime() / 1000;
trace2.h
+10 -4
@@ -391,13 +391,19 @@ void trace2_printf(const char *fmt, ...);
391 * Optional platform-specific code to dump information about the
392 * current and any parent process(es). This is intended to allow
393 * post-processors to know who spawned this git instance and anything
394 - * else the platform may be able to tell us about the current process.
394 + * else that the platform may be able to tell us about the current process.
395 */
396 +
397 +enum trace2_process_info_reason {
398 + TRACE2_PROCESS_INFO_STARTUP,
399 + TRACE2_PROCESS_INFO_EXIT,
400 +};
401 +
402 #if defined(GIT_WINDOWS_NATIVE)
397 -void trace2_collect_process_info(void);
403 +void trace2_collect_process_info(enum trace2_process_info_reason reason);
404 #else
399 -#define trace2_collect_process_info() \
400 - do { \
405 +#define trace2_collect_process_info(reason) \
406 + do { \
407 } while (0)
408 #endif
409