Skip to content

[MemProf] Add interface for reseting the profile file descriptor #73714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler-rt/include/sanitizer/memprof_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ const char *SANITIZER_CDECL __memprof_default_options(void);
/// \returns 0 on success.
int SANITIZER_CDECL __memprof_profile_dump(void);

/// Closes the existing file descriptor, if it is valid and not stdout or
/// stderr, and resets the internal state such that the profile filename is
/// reopened on the next profile dump attempt. This can be used to enable
/// multiple rounds of profiling on the same binary.
void SANITIZER_CDECL __memprof_profile_reset(void);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
8 changes: 8 additions & 0 deletions compiler-rt/lib/memprof/memprof_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,11 @@ int __memprof_profile_dump() {
// detected during the dumping process.
return 0;
}

void __memprof_profile_reset() {
if (report_file.fd != kInvalidFd && report_file.fd != kStdoutFd &&
report_file.fd != kStderrFd) {
CloseFile(report_file.fd);
report_file.fd = kInvalidFd;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment why we need to set this to kInvalidFd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
1 change: 1 addition & 0 deletions compiler-rt/lib/memprof/memprof_interface_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern uptr __memprof_shadow_memory_dynamic_address;
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
__memprof_profile_filename[1];
SANITIZER_INTERFACE_ATTRIBUTE int __memprof_profile_dump();
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_profile_reset();

SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);
Expand Down
39 changes: 39 additions & 0 deletions compiler-rt/test/memprof/TestCases/profile_reset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Test to ensure that multiple rounds of dumping, using the
// __memprof_profile_reset interface to close the initial file
// and cause the profile to be reopened, works as expected.

// RUN: %clangxx_memprof %s -o %t

// RUN: rm -f %t.log.*
// RUN: %env_memprof_opts=print_text=true:log_path=%t.log %run %t

// Check both outputs, starting with the renamed initial dump, then remove it so
// that the second glob matches a single file.
// RUN: FileCheck %s --dump-input=always < %t.log.*.sv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the dump-input left over from debugging? The default is on fail which seems fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this came in when I copied from another test, where it is presumably leftover debugging. I removed from here.

// RUN: rm -f %t.log.*.sv
// RUN: FileCheck %s --dump-input=always < %t.log.*
// CHECK: Memory allocation stack id

#include <sanitizer/memprof_interface.h>
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <string>
int main(int argc, char **argv) {
char *x = (char *)malloc(10);
memset(x, 0, 10);
free(x);
__memprof_profile_dump();
// Save the initial dump in a different file.
std::string origname = __sanitizer_get_report_path();
std::string svname = origname + ".sv";
rename(origname.c_str(), svname.c_str());
// This should cause the current file descriptor to be closed and the
// the internal state reset so that the profile filename is reopened
// on the next write.
__memprof_profile_reset();
// This will dump to origname again.
__memprof_profile_dump();
return 0;
}