From 1f49dfe4a7bb4aaffb6c8e22ea85afc55376c998 Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Tue, 6 Sep 2016 20:32:25 +0000
Subject: [PATCH] -cpu command line option to report CPU/memory for all
 messages (same as in getdp)

---
 Common/CommandLine.cpp |  5 ++++
 Common/GmshMessage.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++
 Common/GmshMessage.h   |  7 +++++
 Common/OS.cpp          | 13 ++++++++++
 Common/OS.h            |  1 +
 5 files changed, 85 insertions(+)

diff --git a/Common/CommandLine.cpp b/Common/CommandLine.cpp
index 0083b29fec..f53fdae39d 100644
--- a/Common/CommandLine.cpp
+++ b/Common/CommandLine.cpp
@@ -149,6 +149,7 @@ std::vector<std::pair<std::string, std::string> > GetUsage()
   s.push_back(mp("-setstring name value", "Set constant string name=value"));
   s.push_back(mp("-option file",       "Parse option file at startup"));
   s.push_back(mp("-convert files",     "Convert files into latest binary formats, then exit"));
+  s.push_back(mp("-cpu",               "Report CPU times for all operations"));
   s.push_back(mp("-version",           "Show version number"));
   s.push_back(mp("-info",              "Show detailed version information"));
   s.push_back(mp("-help",              "Show command line usage"));
@@ -364,6 +365,10 @@ void GetOptions(int argc, char *argv[])
         CTX::instance()->batch = 4;
         i++;
       }
+      else if(!strcmp(argv[i] + 1, "cpu")) {
+        Msg::SetInfoCpu(true);
+        i++;
+      }
       else if(!strcmp(argv[i] + 1, "refine")) {
         CTX::instance()->batch = 5;
         i++;
diff --git a/Common/GmshMessage.cpp b/Common/GmshMessage.cpp
index aa85b962ab..4ca490c90e 100644
--- a/Common/GmshMessage.cpp
+++ b/Common/GmshMessage.cpp
@@ -51,6 +51,8 @@ int Msg::_verbosity = 5;
 int Msg::_progressMeterStep = 20;
 int Msg::_progressMeterCurrent = 0;
 std::map<std::string, double> Msg::_timers;
+bool Msg::_infoCpu = false;
+double Msg::_startTime = 0.;
 int Msg::_warningCount = 0;
 int Msg::_errorCount = 0;
 int Msg::_atLeastOneErrorInRun = 0;
@@ -106,6 +108,7 @@ static void addGmshPathToEnvironmentVar(const std::string &name)
 
 void Msg::Init(int argc, char **argv)
 {
+  _startTime = TimeOfDay();
 #if defined(HAVE_MPI)
   int flag;
   MPI_Initialized(&flag);
@@ -252,6 +255,52 @@ static int streamIsVT100(FILE* stream)
   return 1;
 }
 
+std::string Msg::PrintResources(bool printDate, bool printWallTime,
+                                bool printCpu, bool printMem)
+{
+  long mem = GetMemoryUsage();
+
+  std::string pdate = "";
+  if(printDate){
+    time_t now;
+    time(&now);
+    pdate = ctime(&now);
+    pdate.resize(pdate.size() - 1);
+    if(printWallTime || printCpu || (printMem && mem))
+      pdate += ", ";
+  }
+
+  std::string pwall = "";
+  if(printWallTime){
+    char tmp[128];
+    sprintf(tmp, "Wall = %gs", TimeOfDay() - _startTime);
+    pwall = tmp;
+    if(printCpu || (printMem && mem))
+      pwall += ", ";
+  }
+
+  std::string pcpu = "";
+  if(printCpu){
+    char tmp[128];
+    sprintf(tmp, "CPU = %gs", Cpu());
+    pcpu = tmp;
+    if(printMem && mem)
+      pcpu += ", ";
+  }
+
+  std::string pmem = "";
+  if(mem && printMem){
+    char tmp[128];
+    sprintf(tmp, "Mem = %gMb", (double)mem / 1024. / 1024.);
+    pmem = tmp;
+  }
+
+  std::string str;
+  if(pdate.size() || pwall.size() || pcpu.size() || pmem.size())
+    str += " (" + pdate +  pwall +  pcpu +  pmem + ")";
+  return str;
+}
+
 void Msg::Fatal(const char *fmt, ...)
 {
   _errorCount++;
@@ -388,6 +437,11 @@ void Msg::Info(const char *fmt, ...)
   vsnprintf(str, sizeof(str), fmt, args);
   va_end(args);
 
+  if(_infoCpu){
+    std::string res = PrintResources(false, true, true, true);
+    strcat(str, res.c_str());
+  }
+
   if(_callback) (*_callback)("Info", str);
   if(_client) _client->Info(str);
 
@@ -468,6 +522,11 @@ void Msg::StatusBar(bool log, const char *fmt, ...)
   vsnprintf(str, sizeof(str), fmt, args);
   va_end(args);
 
+  if(_infoCpu){
+    std::string res = PrintResources(false, true, true, true);
+    strcat(str, res.c_str());
+  }
+
   if(_callback && log) (*_callback)("Info", str);
   if(_client && log) _client->Info(str);
 
diff --git a/Common/GmshMessage.h b/Common/GmshMessage.h
index 11eced45c4..0e3c68504a 100644
--- a/Common/GmshMessage.h
+++ b/Common/GmshMessage.h
@@ -36,6 +36,10 @@ class Msg {
   static int _progressMeterStep, _progressMeterCurrent;
   // timers
   static std::map<std::string, double> _timers;
+  // report cpu time for each info message?
+  static bool _infoCpu;
+  // starting time (gettimeofday at startup)
+  static double _startTime;
   // counters
   static int _warningCount, _errorCount, _atLeastOneErrorInRun;
   static std::string _firstWarning, _firstError;
@@ -80,6 +84,8 @@ class Msg {
   {
     return _commandLineStrings;
   }
+  static std::string PrintResources(bool printDate, bool printWallTime,
+                                    bool printCpu, bool printMem);
   static void Fatal(const char *fmt, ...);
   static void Error(const char *fmt, ...);
   static void Warning(const char *fmt, ...);
@@ -94,6 +100,7 @@ class Msg {
   static void SetProgressMeterStep(int step){ _progressMeterStep = step; }
   static int GetProgressMeterStep(){ return _progressMeterStep; }
   static void ResetProgressMeter(){ if(!_commRank) _progressMeterCurrent = 0; }
+  static void SetInfoCpu(bool val){ _infoCpu = val; }
   static double &Timer(std::string str){ return _timers[str]; }
   static void PrintTimers();
   static void ResetErrorCounter();
diff --git a/Common/OS.cpp b/Common/OS.cpp
index 61c0be3630..7acaed7b5c 100644
--- a/Common/OS.cpp
+++ b/Common/OS.cpp
@@ -365,6 +365,19 @@ double TotalRam()
   return ram;
 }
 
+double TimeOfDay()
+{
+#if defined(WIN32) && !defined(__CYGWIN__)
+  struct _timeb  localTime;
+  _ftime(&localTime);
+  return localTime.time + 1.e-3 * localTime.millitm;
+#else
+  struct timeval t;
+  gettimeofday(&t, NULL);
+  return t.tv_sec + 1.e-6 * t.tv_usec;
+#endif
+}
+
 long GetMemoryUsage()
 {
   long mem = 0;
diff --git a/Common/OS.h b/Common/OS.h
index 2fae7ff237..88db4bd090 100644
--- a/Common/OS.h
+++ b/Common/OS.h
@@ -17,6 +17,7 @@ void SleepInSeconds(double s);
 void CheckResources();
 double Cpu();
 double TotalRam();
+double TimeOfDay();
 long GetMemoryUsage();
 int GetProcessId();
 std::string GetExecutableFileName();
-- 
GitLab