Skip to content

Commit 26c6f25

Browse files
jeffhostetlergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bce9db6 commit 26c6f25

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

common-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, const char **argv)
4141

4242
trace2_initialize();
4343
trace2_cmd_start(argv);
44-
trace2_collect_process_info();
44+
trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP);
4545

4646
git_setup_gettext();
4747

compat/win32/trace2_win32_process_info.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../../cache.h"
22
#include "../../json-writer.h"
3+
#include "lazyload.h"
34
#include <Psapi.h>
45
#include <tlHelp32.h>
56

@@ -137,11 +138,54 @@ static void get_is_being_debugged(void)
137138
"windows/debugger_present", 1);
138139
}
139140

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)
141174
{
142175
if (!trace2_is_enabled())
143176
return;
144177

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+
}
147191
}

trace2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ int trace2_cmd_exit_fl(const char *file, int line, int code)
213213
if (!trace2_enabled)
214214
return code;
215215

216+
trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT);
217+
216218
tr2main_exit_code = code;
217219

218220
us_now = getnanotime() / 1000;

trace2.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,19 @@ void trace2_printf(const char *fmt, ...);
391391
* Optional platform-specific code to dump information about the
392392
* current and any parent process(es). This is intended to allow
393393
* 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.
395395
*/
396+
397+
enum trace2_process_info_reason {
398+
TRACE2_PROCESS_INFO_STARTUP,
399+
TRACE2_PROCESS_INFO_EXIT,
400+
};
401+
396402
#if defined(GIT_WINDOWS_NATIVE)
397-
void trace2_collect_process_info(void);
403+
void trace2_collect_process_info(enum trace2_process_info_reason reason);
398404
#else
399-
#define trace2_collect_process_info() \
400-
do { \
405+
#define trace2_collect_process_info(reason) \
406+
do { \
401407
} while (0)
402408
#endif
403409

0 commit comments

Comments
 (0)