Merge "Create a host monitor for heap memory"
diff --git a/src/com/android/tradefed/log/ILogRegistry.java b/src/com/android/tradefed/log/ILogRegistry.java
index afd91e1..da660bd 100644
--- a/src/com/android/tradefed/log/ILogRegistry.java
+++ b/src/com/android/tradefed/log/ILogRegistry.java
@@ -34,6 +34,7 @@
         DEVICE_DISCONNECTED,
         INVOCATION_START,
         INVOCATION_END,
+        HEAP_MEMORY,
     }
 
     /**
diff --git a/src/com/android/tradefed/util/hostmetric/HeapHostMonitor.java b/src/com/android/tradefed/util/hostmetric/HeapHostMonitor.java
new file mode 100644
index 0000000..41b839f
--- /dev/null
+++ b/src/com/android/tradefed/util/hostmetric/HeapHostMonitor.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.util.hostmetric;
+
+import com.android.ddmlib.Log.LogLevel;
+import com.android.tradefed.log.ILogRegistry.EventType;
+
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.android.tradefed.log.LogRegistry;
+
+/**
+ * {@link AbstractHostMonitor} implementation that monitors the heap memory on the host and log it
+ * periodically to the history log.
+ */
+public class HeapHostMonitor extends AbstractHostMonitor {
+
+    protected static final String HEAP_KEY = "heap_memory_Mbytes";
+
+    public HeapHostMonitor() {
+        super();
+        setName("HeapHostMonitor");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void dispatch() {
+        // This host monitor does not care about events, so we flush them out.
+        mHostEvents.clear();
+        // Collect the current JVM memory usage in bytes
+        Runtime rt = Runtime.getRuntime();
+        long totalJvmMemory = (rt.totalMemory() - rt.freeMemory()) / (1024l * 1024l);
+        Map<String, String> args = new HashMap<>();
+        args.put(HEAP_KEY, Long.toString(totalJvmMemory));
+        logEvent(args);
+    }
+
+    /** Log the event to the history log. */
+    @VisibleForTesting
+    void logEvent(Map<String, String> args) {
+        LogRegistry.getLogRegistry().logEvent(LogLevel.INFO, EventType.HEAP_MEMORY, args);
+    }
+}
diff --git a/tests/src/com/android/tradefed/UnitTests.java b/tests/src/com/android/tradefed/UnitTests.java
index 2efc216..c1d393c 100644
--- a/tests/src/com/android/tradefed/UnitTests.java
+++ b/tests/src/com/android/tradefed/UnitTests.java
@@ -189,6 +189,7 @@
 import com.android.tradefed.util.ZipUtil2Test;
 import com.android.tradefed.util.ZipUtilTest;
 import com.android.tradefed.util.hostmetric.AbstractHostMonitorTest;
+import com.android.tradefed.util.hostmetric.HeapHostMonitorTest;
 import com.android.tradefed.util.keystore.JSONFileKeyStoreClientTest;
 import com.android.tradefed.util.net.HttpHelperTest;
 import com.android.tradefed.util.net.HttpMultipartPostTest;
@@ -418,6 +419,7 @@
 
     //util/hostmetric
     AbstractHostMonitorTest.class,
+    HeapHostMonitorTest.class,
 
     // util subdirs
     AndroidManifestWriterTest.class,
diff --git a/tests/src/com/android/tradefed/util/hostmetric/HeapHostMonitorTest.java b/tests/src/com/android/tradefed/util/hostmetric/HeapHostMonitorTest.java
new file mode 100644
index 0000000..c9e2d75
--- /dev/null
+++ b/tests/src/com/android/tradefed/util/hostmetric/HeapHostMonitorTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tradefed.util.hostmetric;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.Map;
+
+/** Unit tests for {@link HeapHostMonitor}. */
+@RunWith(JUnit4.class)
+public class HeapHostMonitorTest {
+
+    private HeapHostMonitor mMonitor;
+    private Map<String, String> mArgsLogged = null;
+
+    @Before
+    public void setUp() {
+        mArgsLogged = null;
+        mMonitor =
+                new HeapHostMonitor() {
+                    @Override
+                    void logEvent(Map<String, String> args) {
+                        mArgsLogged = args;
+                    }
+                };
+    }
+
+    /** Test that an event is logged with the memory key in it. */
+    @Test
+    public void testDispatchOfHeap() {
+        assertNull(mArgsLogged);
+        mMonitor.dispatch();
+        assertNotNull(mArgsLogged);
+        assertNotNull(mArgsLogged.get(HeapHostMonitor.HEAP_KEY));
+        // make sure it's a positive number
+        long mem = Long.parseLong(mArgsLogged.get(HeapHostMonitor.HEAP_KEY));
+        assertTrue(mem > 0l);
+    }
+}