summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDean Michael Berris <dberris@google.com>2018-07-19 09:20:19 +0000
committerDean Michael Berris <dberris@google.com>2018-07-19 09:20:19 +0000
commitc1d00864fe9408b0cdffb89f4631f6f5e9c8bcc2 (patch)
tree65cb92c7873432a9b34db3c0901e6fd2bff0193f
parent8caa0e5cb1bc0b8cbe3e6d3fdfa85db28d226b1c (diff)
[XRay][compiler-rt] Profiling: No files when empty
This change makes it so that the profiling mode implementation will only write files when there are buffers to write. Before this change, we'd always open a file even if there were no profiles collected when flushing. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@337443 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/xray/xray_profiling.cc50
-rw-r--r--test/xray/TestCases/Posix/profiling-multi-threaded.cc2
-rw-r--r--test/xray/TestCases/Posix/profiling-single-threaded.cc2
3 files changed, 29 insertions, 25 deletions
diff --git a/lib/xray/xray_profiling.cc b/lib/xray/xray_profiling.cc
index a11fddcc2..786084c77 100644
--- a/lib/xray/xray_profiling.cc
+++ b/lib/xray/xray_profiling.cc
@@ -132,31 +132,35 @@ XRayLogFlushStatus profilingFlush() XRAY_NEVER_INSTRUMENT {
// At this point, we'll create the file that will contain the profile, but
// only if the options say so.
if (!profilingFlags()->no_flush) {
- int Fd = -1;
- Fd = getLogFD();
- if (Fd == -1) {
- if (__sanitizer::Verbosity())
- Report(
- "profiler: Failed to acquire a file descriptor, dropping data.\n");
+ // First check whether we have data in the profile collector service
+ // before we try and write anything down.
+ XRayBuffer B = profileCollectorService::nextBuffer({nullptr, 0});
+ if (B.Data == nullptr) {
+ if (Verbosity())
+ Report("profiling: No data to flush.\n");
} else {
- XRayProfilingFileHeader Header;
- Header.Timestamp = NanoTime();
- Header.PID = internal_getpid();
- retryingWriteAll(Fd, reinterpret_cast<const char *>(&Header),
- reinterpret_cast<const char *>(&Header) +
- sizeof(Header));
-
- // Now for each of the threads, write out the profile data as we would see
- // it in memory, verbatim.
- XRayBuffer B = profileCollectorService::nextBuffer({nullptr, 0});
- while (B.Data != nullptr && B.Size != 0) {
- retryingWriteAll(Fd, reinterpret_cast<const char *>(B.Data),
- reinterpret_cast<const char *>(B.Data) + B.Size);
- B = profileCollectorService::nextBuffer(B);
+ int Fd = getLogFD();
+ if (Fd == -1) {
+ if (Verbosity())
+ Report("profiling: Failed to flush to file, dropping data.\n");
+ } else {
+ XRayProfilingFileHeader Header;
+ Header.Timestamp = NanoTime();
+ Header.PID = internal_getpid();
+ retryingWriteAll(Fd, reinterpret_cast<const char *>(&Header),
+ reinterpret_cast<const char *>(&Header) +
+ sizeof(Header));
+
+ // Now for each of the threads, write out the profile data as we would
+ // see it in memory, verbatim.
+ while (B.Data != nullptr && B.Size != 0) {
+ retryingWriteAll(Fd, reinterpret_cast<const char *>(B.Data),
+ reinterpret_cast<const char *>(B.Data) + B.Size);
+ B = profileCollectorService::nextBuffer(B);
+ }
+ // Then we close out the file.
+ internal_close(Fd);
}
-
- // Then we close out the file.
- internal_close(Fd);
}
}
diff --git a/test/xray/TestCases/Posix/profiling-multi-threaded.cc b/test/xray/TestCases/Posix/profiling-multi-threaded.cc
index f4caadb28..7ccad1bac 100644
--- a/test/xray/TestCases/Posix/profiling-multi-threaded.cc
+++ b/test/xray/TestCases/Posix/profiling-multi-threaded.cc
@@ -8,7 +8,7 @@
// RUN: XRAY_PROFILING_OPTIONS=no_flush=1 %run %t
// RUN: XRAY_OPTIONS=verbosity=1 %run %t
// RUN: PROFILES=`ls xray-log.profiling-multi-* | wc -l`
-// RUN: [ $PROFILES -ge 1 ]
+// RUN: [ $PROFILES -eq 1 ]
// RUN: rm -f xray-log.profiling-multi-*
//
// REQUIRES: x86_64-target-arch
diff --git a/test/xray/TestCases/Posix/profiling-single-threaded.cc b/test/xray/TestCases/Posix/profiling-single-threaded.cc
index 32002c699..fd508b1ac 100644
--- a/test/xray/TestCases/Posix/profiling-single-threaded.cc
+++ b/test/xray/TestCases/Posix/profiling-single-threaded.cc
@@ -8,7 +8,7 @@
// RUN: XRAY_PROFILING_OPTIONS=no_flush=true %run %t
// RUN: XRAY_OPTIONS=verbosity=1 %run %t
// RUN: PROFILES=`ls xray-log.profiling-single-* | wc -l`
-// RUN: [ $PROFILES -ge 2 ]
+// RUN: [ $PROFILES -eq 2 ]
// RUN: rm -f xray-log.profiling-single-*
//
// REQUIRES: x86_64-target-arch