changeset 195:61e865edcf55

Bug 3308: Valgrind reports some memory issues Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/81
author Yasumasa Suenaga <yasuenag@gmail.com>
date Sun, 29 Jan 2017 21:49:39 +0900
parents e761d94faf34
children 25dbcd27c512
files agent/src/heapstats-engines/classContainer.cpp agent/src/heapstats-engines/configuration.hpp agent/src/heapstats-engines/globals.hpp agent/src/heapstats-engines/overrider.cpp agent/src/heapstats-engines/snapShotMain.cpp agent/src/heapstats-engines/snapShotMain.hpp agent/src/heapstats-engines/trapSender.hpp agent/src/heapstats-engines/util.cpp
diffstat 8 files changed, 69 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/heapstats-engines/classContainer.cpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/classContainer.cpp	Sun Jan 29 21:49:39 2017 +0900
@@ -221,6 +221,8 @@
     free(cur->className);
     free(cur);
   }
+
+  atomic_inc(&result->numRefs, 1);
   return result;
 }
 
@@ -279,7 +281,6 @@
       try {
         /* Append class data. */
         (*classMap)[klassOop] = objData;
-        atomic_inc(&objData->numRefs, 1);
       } catch (...) {
         /*
          * Maybe failed to allocate memory at "std::map::operator[]".
@@ -384,9 +385,14 @@
          ++cur) {
       TObjectData *pos = (*cur).second;
 
-      if ((pos != NULL) && (atomic_get(&pos->numRefs) == 0)) {
-        free(pos->className);
-        free(pos);
+      if (likely(pos != NULL)) {
+        /* Decrement reference from this class map. */
+        atomic_inc(&pos->numRefs, -1);
+
+        if (atomic_get(&pos->numRefs) == 0) {
+          free(pos->className);
+          free(pos);
+        }
       }
     }
 
--- a/agent/src/heapstats-engines/configuration.hpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/configuration.hpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file configuration.hpp
  * \brief This file treats HeapStats configuration.
- * Copyright (C) 2014-2016 Yasumasa Suenaga
+ * Copyright (C) 2014-2017 Yasumasa Suenaga
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -84,7 +84,7 @@
   /*!
    * \brief Configuration name in heapstats.conf .
    */
-  const char *configName;
+  char *configName;
 
   /*!
    * \brief Setter of this configuration.
@@ -128,6 +128,8 @@
   }
 
   virtual ~TConfigElement() {
+    free(configName);
+
     if (finalizer != NULL) {
       finalizer(value);
     }
--- a/agent/src/heapstats-engines/globals.hpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/globals.hpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file globals.hpp
  * \brief Definitions of global variables.
- * Copyright (C) 2015 Yasumasa Suenaga
+ * Copyright (C) 2015-2017 Yasumasa Suenaga
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -64,9 +64,6 @@
 #include "gcWatcher.hpp"
 extern TGCWatcher *gcWatcher;
 
-#include "snapShotContainer.hpp"
-extern TSnapShotContainer *snapshotByGC;
-
 /*!
  * \brief Mutex of working directory.
  */
--- a/agent/src/heapstats-engines/overrider.cpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/overrider.cpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file overrider.cpp
  * \brief Controller of overriding functions in HotSpot VM.
- * Copyright (C) 2014-2015 Yasumasa Suenaga
+ * Copyright (C) 2014-2017 Yasumasa Suenaga
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -26,6 +26,7 @@
 #include "vmVariables.hpp"
 #include "vmFunctions.hpp"
 #include "overrider.hpp"
+#include "snapShotMain.hpp"
 
 #if PROCESSOR_ARCH == X86
 #include "arch/x86/x86BitMapMarker.hpp"
@@ -1221,9 +1222,6 @@
     switchOverrideFunction(cms_new_hook, enable);
     list = cms_sweep_hook;
   } else if (vmVal->getUseG1()) {
-    /* If users select G1, we prepare TSnapShotContainer NOW! */
-    snapshotByGC = TSnapShotContainer::getInstance();
-
     /* Switch G1GC event hooking. */
     switchOverrideFunction(g1Event_hook, enable);
 
@@ -1553,7 +1551,7 @@
   switchOverrideFunction(g1_hook, false);
 
   /* Discard existed snapshot data */
-  snapshotByGC->clear(false);
+  clearCurrentSnapShot();
   checkObjectMap->clear();
 }
 
--- a/agent/src/heapstats-engines/snapShotMain.cpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/snapShotMain.cpp	Sun Jan 29 21:49:39 2017 +0900
@@ -758,6 +758,14 @@
         gcWatcher->stop();
       }
 
+      if (TVMVariables::getInstance()->getUseG1()) {
+        if (snapshotByGC == NULL) {
+          snapshotByGC = TSnapShotContainer::getInstance();
+        } else {
+          snapshotByGC->clear(false);
+        }
+      }
+
       /* Switch GC hooking state. */
       setGCHookState(enable);
     }
@@ -783,6 +791,13 @@
   }
 }
 
+ /*!
+  * \brief Clear current SnapShot.
+  */
+void clearCurrentSnapShot() {
+  snapshotByGC->clear(false);
+}
+
 /*!
  * \brief JVM initialization event for snapshot function.
  * \param jvmti [in] JVMTI environment object.
@@ -844,6 +859,7 @@
   TSnapShotContainer *snapshot = popSnapShotQueue();
   /* Output all waiting snapshot. */
   while (snapshot != NULL) {
+    snapshot->setTotalSize(jvmInfo->getTotalMemory());
     notifySnapShot(snapshot);
     snapshot = popSnapShotQueue();
   }
@@ -908,16 +924,27 @@
  * \param env [in] JNI environment object.
  */
 void onAgentFinalForSnapShot(JNIEnv *env) {
+  /* Destroy SnapShot Processor instance. */
+  delete snapShotProcessor;
+  snapShotProcessor = NULL;
+
+  /*
+   * Delete snapshot instances
+   */
+  if (snapshotByCMS != NULL) {
+    TSnapShotContainer::releaseInstance(snapshotByCMS);
+  }
+  if (snapshotByGC != NULL) {
+    TSnapShotContainer::releaseInstance(snapshotByGC);
+  }
+
+  /* Finalize and deallocate old snapshot containers. */
+  TSnapShotContainer::globalFinalize();
+
   /* Destroy object that is for snapshot. */
   delete clsContainer;
   clsContainer = NULL;
 
-  delete snapShotProcessor;
-  snapShotProcessor = NULL;
-
-  /* Finalize and deallocate old snapshot containers. */
-  TSnapShotContainer::globalFinalize();
-
   /* Destroy object that is each snapshot trigger. */
   delete gcWatcher;
   gcWatcher = NULL;
--- a/agent/src/heapstats-engines/snapShotMain.hpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/snapShotMain.hpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file snapShotMain.hpp
  * \brief This file is used to take snapshot.
- * Copyright (C) 2011-2015 Nippon Telegraph and Telephone Corporation
+ * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -58,6 +58,11 @@
 void setThreadEnableForSnapShot(jvmtiEnv *jvmti, JNIEnv *env, bool enable);
 
 /*!
+ * \brief Clear current SnapShot.
+ */
+void clearCurrentSnapShot();
+
+/*!
  * \brief JVM initialization event for snapshot function.
  * \param jvmti  [in] JVMTI environment object.
  * \param env    [in] JNI environment object.
--- a/agent/src/heapstats-engines/trapSender.hpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/trapSender.hpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file trapSender.hpp
  * \brief This file is used to send SNMP trap.
- * Copyright (C) 2011-2015 Nippon Telegraph and Telephone Corporation
+ * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -227,6 +227,8 @@
       }
       /* Close and free SNMP session. */
       snmp_close(&session);
+      free(session.peername);
+      free(session.community);
     }
     /* Unlock to use in multi-thread. */
     EXIT_PTHREAD_SECTION(&senderMutex)
--- a/agent/src/heapstats-engines/util.cpp	Tue Jan 24 11:46:41 2017 +0900
+++ b/agent/src/heapstats-engines/util.cpp	Sun Jan 29 21:49:39 2017 +0900
@@ -1,7 +1,7 @@
 /*!
  * \file util.cpp
  * \brief This file is utilities.
- * Copyright (C) 2011-2015 Nippon Telegraph and Telephone Corporation
+ * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -169,7 +169,14 @@
   }
 
   /* Clean up. */
-
+  for (int Cnt = 0; Cnt < count; Cnt++) {
+    jvmti->Deallocate((unsigned char *)events[Cnt].id);
+    jvmti->Deallocate((unsigned char *)events[Cnt].short_description);
+    for (int Cnt2 = 0; Cnt2 < events[Cnt].param_count; Cnt2++) {
+      jvmti->Deallocate((unsigned char *)events[Cnt].params[Cnt2].name);
+    }
+    jvmti->Deallocate((unsigned char *)events[Cnt].params);
+  }
   jvmti->Deallocate((unsigned char *)events);
   return ret;
 }