From 4487764a6a09fcdfef4dbd957146b646ea256676 Mon Sep 17 00:00:00 2001
From: Tomonori Hashimoto <tomonori.hashimoto@sharp.co.jp>
Date: Fri, 13 May 2022 03:33:27 +0000
Subject: [PATCH] JVET-Z0150: Memory usage print

---
 source/App/EncoderApp/encmain.cpp | 36 +++++++++++++++++++++++++++++++
 source/Lib/CommonLib/TypeDef.h    |  1 +
 2 files changed, 37 insertions(+)

diff --git a/source/App/EncoderApp/encmain.cpp b/source/App/EncoderApp/encmain.cpp
index d5865cb31..83cb4ced2 100644
--- a/source/App/EncoderApp/encmain.cpp
+++ b/source/App/EncoderApp/encmain.cpp
@@ -77,6 +77,33 @@ static void printMacroSettings()
   }
 }
 
+#if JVET_Z0150_MEMORY_USAGE_PRINT
+#ifdef __linux
+#include <cstdlib>
+#include <cstdio>
+#include <cstring>
+
+int getProcStatusValue(const char* key)
+{
+  FILE* file = fopen("/proc/self/status", "r");
+  int result = -1;
+  char line[128];
+
+  int len = strlen(key);
+  while (fgets(line, 128, file) != nullptr)
+  {
+    if (strncmp(line, key, len) == 0)
+    {
+      result = atoi(line+len);
+      break;
+    }
+  }
+  fclose(file);
+  return result;
+}
+#endif
+#endif
+
 // ====================================================================================================================
 // Main function
 // ====================================================================================================================
@@ -290,6 +317,15 @@ int main(int argc, char* argv[])
       }
     }
   }
+
+#if JVET_Z0150_MEMORY_USAGE_PRINT
+#ifdef __linux
+  int vm = getProcStatusValue("VmPeak:");
+  int rm = getProcStatusValue("VmHWM:");
+  printf("\nMemory Usage: VmPeak= %d KB ( %.1f GiB ),  VmHWM= %d KB ( %.1f GiB )\n", vm, (double)vm/(1024*1024), rm, (double)rm/(1024*1024));
+#endif
+#endif
+
   // ending time
   clock_t endClock = clock();
   auto endTime = std::chrono::steady_clock::now();
diff --git a/source/Lib/CommonLib/TypeDef.h b/source/Lib/CommonLib/TypeDef.h
index 4d3396bd2..b1b8d015a 100644
--- a/source/Lib/CommonLib/TypeDef.h
+++ b/source/Lib/CommonLib/TypeDef.h
@@ -262,6 +262,7 @@
 #define JVET_Y0128_NON_CTC                                1 // JVET-Y0128: Fixing issues for RPR enabling and non-CTC configuration in ECM
 #define JVET_Z0067_RPR_ENABLE                             1 // JVET_Z0067: Fixes for RPR
 #define JVET_Z0118_GDR                                    1 // JVET_Z0118: GDR for ECM
+#define JVET_Z0150_MEMORY_USAGE_PRINT                     1 // JVET-Z0150: print memory usage
 
 #if JVET_Z0118_GDR
 #define GDR_LEAK_TEST  1
-- 
GitLab