[compiler-rt] e8101f2 - tsan: move mem profile initialization into separate function
Dmitry Vyukov via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 22 01:18:12 PDT 2021
Author: Dmitry Vyukov
Date: 2021-09-22T10:18:08+02:00
New Revision: e8101f2149dfcd6a915b975a1f83ac09a5cd04b9
URL: https://github.com/llvm/llvm-project/commit/e8101f2149dfcd6a915b975a1f83ac09a5cd04b9
DIFF: https://github.com/llvm/llvm-project/commit/e8101f2149dfcd6a915b975a1f83ac09a5cd04b9.diff
LOG: tsan: move mem profile initialization into separate function
BackgroundThread function is quite large,
move mem profile initialization into a separate function.
Depends on D110151.
Reviewed By: melver, vitalybuka
Differential Revision: https://reviews.llvm.org/D110152
Added:
Modified:
compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
compiler-rt/lib/tsan/rtl/tsan_rtl.h
Removed:
################################################################################
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
index 507a318ad371..294f3d31a057 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.cpp
@@ -149,10 +149,35 @@ ThreadState::ThreadState(Context *ctx, Tid tid, int unique_id, u64 epoch,
}
#if !SANITIZER_GO
-static void MemoryProfiler(Context *ctx, fd_t fd, int i) {
+void MemoryProfiler() {
+ if (ctx->memprof_fd == kInvalidFd)
+ return;
InternalMmapVector<char> buf(4096);
WriteMemoryProfile(buf.data(), buf.size());
- WriteToFile(fd, buf.data(), internal_strlen(buf.data()));
+ WriteToFile(ctx->memprof_fd, buf.data(), internal_strlen(buf.data()));
+}
+
+void InitializeMemoryProfiler() {
+ ctx->memprof_fd = kInvalidFd;
+ const char *fname = flags()->profile_memory;
+ if (!fname || !fname[0])
+ return;
+ if (internal_strcmp(fname, "stdout") == 0) {
+ ctx->memprof_fd = 1;
+ } else if (internal_strcmp(fname, "stderr") == 0) {
+ ctx->memprof_fd = 2;
+ } else {
+ InternalScopedString filename;
+ filename.append("%s.%d", fname, (int)internal_getpid());
+ ctx->memprof_fd = OpenFile(filename.data(), WrOnly);
+ if (ctx->memprof_fd == kInvalidFd) {
+ Printf("ThreadSanitizer: failed to open memory profile file '%s'\n",
+ filename.data());
+ return;
+ }
+ }
+ MemoryProfiler();
+ MaybeSpawnBackgroundThread();
}
static void *BackgroundThread(void *arg) {
@@ -164,25 +189,6 @@ static void *BackgroundThread(void *arg) {
cur_thread()->ignore_interceptors++;
const u64 kMs2Ns = 1000 * 1000;
- fd_t mprof_fd = kInvalidFd;
- if (flags()->profile_memory && flags()->profile_memory[0]) {
- if (internal_strcmp(flags()->profile_memory, "stdout") == 0) {
- mprof_fd = 1;
- } else if (internal_strcmp(flags()->profile_memory, "stderr") == 0) {
- mprof_fd = 2;
- } else {
- InternalScopedString filename;
- filename.append("%s.%d", flags()->profile_memory, (int)internal_getpid());
- fd_t fd = OpenFile(filename.data(), WrOnly);
- if (fd == kInvalidFd) {
- Printf("ThreadSanitizer: failed to open memory profile file '%s'\n",
- filename.data());
- } else {
- mprof_fd = fd;
- }
- }
- }
-
u64 last_flush = NanoTime();
uptr last_rss = 0;
for (int i = 0;
@@ -215,9 +221,7 @@ static void *BackgroundThread(void *arg) {
last_rss = rss;
}
- // Write memory profile if requested.
- if (mprof_fd != kInvalidFd)
- MemoryProfiler(ctx, mprof_fd, i);
+ MemoryProfiler();
// Flush symbolizer cache if requested.
if (flags()->flush_symbolizer_ms > 0) {
@@ -403,6 +407,7 @@ void Initialize(ThreadState *thr) {
#if !SANITIZER_GO
Symbolizer::LateInitialize();
+ InitializeMemoryProfiler();
#endif
if (flags()->stop_on_start) {
diff --git a/compiler-rt/lib/tsan/rtl/tsan_rtl.h b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
index af149460e06b..2090093767fc 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_rtl.h
+++ b/compiler-rt/lib/tsan/rtl/tsan_rtl.h
@@ -558,6 +558,7 @@ struct Context {
ClockAlloc clock_alloc;
Flags flags;
+ fd_t memprof_fd;
Mutex slot_mtx;
};
More information about the llvm-commits
mailing list