From 1860d030e51ad4ee318d6ae2f74341c82d837308 Mon Sep 17 00:00:00 2001
From: Jonathan Peyton <jonathan.l.peyton@intel.com>
Date: Tue, 21 Jun 2016 15:20:33 +0000
Subject: [PATCH] [STATS] Adding process id to output filename

This change appends the process id to the KMP_STATS_FILE (if specified) which
enables MPI processes to output their stats to separate files.

Differential Revision: http://reviews.llvm.org/D21386


git-svn-id: https://llvm.org/svn/llvm-project/openmp/trunk@273273 91177308-0d34-0410-b5e6-96231b3b80d8
---
 runtime/src/kmp_stats.cpp | 22 +++++++++++++++++++---
 runtime/src/kmp_stats.h   |  2 +-
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/runtime/src/kmp_stats.cpp b/runtime/src/kmp_stats.cpp
index 8100f3b..a65a481 100644
--- a/runtime/src/kmp_stats.cpp
+++ b/runtime/src/kmp_stats.cpp
@@ -356,7 +356,6 @@ kmp_stats_list* kmp_stats_list::iterator::operator*() const {
 /* *************************************************************** */
 /* *************  kmp_stats_output_module functions ************** */
 
-const char* kmp_stats_output_module::outputFileName = NULL;
 const char* kmp_stats_output_module::eventsFileName = NULL;
 const char* kmp_stats_output_module::plotFileName   = NULL;
 int kmp_stats_output_module::printPerThreadFlag       = 0;
@@ -372,7 +371,24 @@ void kmp_stats_output_module::init()
     char * threadEvents   = getenv("KMP_STATS_EVENTS");
 
     // set the stats output filenames based on environment variables and defaults
-    outputFileName = statsFileName;
+    if(statsFileName) {
+        // append the process id to the output filename
+        // events.csv --> events-pid.csv
+        size_t index;
+        std::string baseFileName, pid, suffix;
+        std::stringstream ss;
+        outputFileName = std::string(statsFileName);
+        index = outputFileName.find_last_of('.');
+        if(index == std::string::npos) {
+            baseFileName = outputFileName;
+        } else {
+            baseFileName = outputFileName.substr(0, index);
+            suffix = outputFileName.substr(index);
+        }
+        ss << getpid();
+        pid = ss.str();
+        outputFileName = baseFileName + "-" + pid + suffix;
+    }
     eventsFileName = eventsFileName ? eventsFileName : "events.dat";
     plotFileName   = plotFileName   ? plotFileName   : "events.plt";
 
@@ -573,7 +589,7 @@ void kmp_stats_output_module::outputStats(const char* heading)
     statistic totalStats[TIMER_LAST];           /* Synthesized, cross threads versions of normal timer stats */
     statistic allCounters[COUNTER_LAST];
 
-    FILE * statsOut = outputFileName ? fopen (outputFileName, "a+") : stderr;
+    FILE * statsOut = !outputFileName.empty() ? fopen (outputFileName.c_str(), "a+") : stderr;
     if (!statsOut)
         statsOut = stderr;
 
diff --git a/runtime/src/kmp_stats.h b/runtime/src/kmp_stats.h
index 5450f59..b767fdf 100644
--- a/runtime/src/kmp_stats.h
+++ b/runtime/src/kmp_stats.h
@@ -687,7 +687,7 @@ class kmp_stats_output_module {
     };
 
  private:
-    static const char* outputFileName;
+    std::string outputFileName;
     static const char* eventsFileName;
     static const char* plotFileName;
     static int printPerThreadFlag;
-- 
GitLab