From d16c3a864883eade51aac931db1ff403e90ac959 Mon Sep 17 00:00:00 2001
From: z30010524 <zhangyunbo7@huawei.com>
Date: Tue, 7 Feb 2023 15:31:05 +0000
Subject: [PATCH 08/15] jcmd mnt add start time and end time

DTS/AR: AR.SR.02ce7e6e.001
Summary: <JDK> :jcmd mnt add start time and end time
LLT: NA
Patch Type:huawei
Bug url: NA
---
 hotspot/src/share/vm/services/memReporter.cpp | 15 ++++++++++++++-
 hotspot/src/share/vm/services/nmtDCmd.cpp     | 14 +++++++++++++-
 hotspot/src/share/vm/services/nmtDCmd.hpp     |  9 +++++++++
 3 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/hotspot/src/share/vm/services/memReporter.cpp b/hotspot/src/share/vm/services/memReporter.cpp
index a324890d3..8ea363805 100644
--- a/hotspot/src/share/vm/services/memReporter.cpp
+++ b/hotspot/src/share/vm/services/memReporter.cpp
@@ -24,6 +24,7 @@
 #include "precompiled.hpp"
 
 #include "memory/allocation.hpp"
+#include "services/nmtDCmd.hpp"
 #include "services/mallocTracker.hpp"
 #include "services/memReporter.hpp"
 #include "services/virtualMemoryTracker.hpp"
@@ -294,7 +295,19 @@ void MemDetailReporter::report_virtual_memory_region(const ReservedMemoryRegion*
 void MemSummaryDiffReporter::report_diff() {
   const char* scale = current_scale();
   outputStream* out = output();
-  out->print_cr("\nNative Memory Tracking:\n");
+  time_t startTime = NMTDCmd::get_start_time();
+  time_t endTime = time(0);
+  struct tm endTimeTm = {0};
+  if (localtime_r(&endTime, &endTimeTm) == NULL) {
+    out->print_cr("\nNative Memory Tracking:\n");
+  } else {
+    out->print_cr("\nNative Memory Tracking: end time is %d-%02d-%02d %02d:%02d:%02d, elapsed time is %d secs\n",
+    static_cast<int>(endTimeTm.tm_year) + START_YEAR,
+    static_cast<int>(endTimeTm.tm_mon) + 1,
+    static_cast<int>(endTimeTm.tm_mday), static_cast<int>(endTimeTm.tm_hour),
+    static_cast<int>(endTimeTm.tm_min), static_cast<int>(endTimeTm.tm_sec),
+    static_cast<int>(endTime - startTime));
+  }
 
   // Overall diff
   out->print("Total: ");
diff --git a/hotspot/src/share/vm/services/nmtDCmd.cpp b/hotspot/src/share/vm/services/nmtDCmd.cpp
index 2635bbb6e..417a58c59 100644
--- a/hotspot/src/share/vm/services/nmtDCmd.cpp
+++ b/hotspot/src/share/vm/services/nmtDCmd.cpp
@@ -73,6 +73,7 @@ size_t NMTDCmd::get_scale(const char* scale) const {
   return NMTUtil::scale_from_name(scale);
 }
 
+time_t NMTDCmd::_start_time = 0;
 void NMTDCmd::execute(DCmdSource source, TRAPS) {
   // Check NMT state
   //  native memory tracking has to be on
@@ -128,7 +129,18 @@ void NMTDCmd::execute(DCmdSource source, TRAPS) {
     if (!baseline.baseline(MemTracker::tracking_level() != NMT_detail)) {
       output()->print_cr("Baseline failed");
     } else {
-      output()->print_cr("Baseline succeeded");
+      NMTDCmd::set_start_time(time(0));
+      time_t startTime = NMTDCmd::get_start_time();
+      struct tm startTimeTm = {0};
+      if (localtime_r(&startTime, &startTimeTm) == NULL) {
+        output()->print_cr("Baseline succeeded");
+      } else {
+        output()->print_cr("Baseline succeeded, start time is %d-%02d-%02d %02d:%02d:%02d",
+        static_cast<int>(startTimeTm.tm_year) + START_YEAR,
+        static_cast<int>(startTimeTm.tm_mon) + 1,
+        static_cast<int>(startTimeTm.tm_mday), static_cast<int>(startTimeTm.tm_hour),
+        static_cast<int>(startTimeTm.tm_min), static_cast<int>(startTimeTm.tm_sec));
+      }
     }
   } else if (_summary_diff.value()) {
     MemBaseline& baseline = MemTracker::get_baseline();
diff --git a/hotspot/src/share/vm/services/nmtDCmd.hpp b/hotspot/src/share/vm/services/nmtDCmd.hpp
index df1ab367f..fc7af5c8d 100644
--- a/hotspot/src/share/vm/services/nmtDCmd.hpp
+++ b/hotspot/src/share/vm/services/nmtDCmd.hpp
@@ -32,6 +32,8 @@
 #include "services/memBaseline.hpp"
 #include "services/mallocTracker.hpp"
 
+const int START_YEAR = 1900; // tm struct, the number of years since 1900.
+
 /**
  * Native memory tracking DCmd implementation
  */
@@ -61,9 +63,16 @@ class NMTDCmd: public DCmdWithParser {
     return p;
   }
   static int num_arguments();
+  static void set_start_time(const time_t &time) {
+    _start_time = time;
+  }
+  static time_t get_start_time() {
+    return _start_time;
+  }
   virtual void execute(DCmdSource source, TRAPS);
 
  private:
+  static time_t _start_time;
   void report(bool summaryOnly, size_t scale);
   void report_diff(bool summaryOnly, size_t scale);
 
-- 
2.19.0

