1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.util.hostmetric; 17 18 import com.android.ddmlib.Log.LogLevel; 19 import com.android.tradefed.log.ILogRegistry.EventType; 20 21 import com.google.common.annotations.VisibleForTesting; 22 23 import java.util.HashMap; 24 import java.util.Map; 25 26 import com.android.tradefed.log.LogRegistry; 27 28 /** 29 * {@link AbstractHostMonitor} implementation that monitors the heap memory on the host and log it 30 * periodically to the history log. 31 */ 32 public class HeapHostMonitor extends AbstractHostMonitor { 33 34 protected static final String HEAP_KEY = "heap_memory_Mbytes"; 35 36 public HeapHostMonitor() { 37 super(); 38 setName("HeapHostMonitor"); 39 } 40 41 /** {@inheritDoc} */ 42 @Override 43 public void dispatch() { 44 // This host monitor does not care about events, so we flush them out. 45 mHostEvents.clear(); 46 // Collect the current JVM memory usage in bytes 47 Runtime rt = Runtime.getRuntime(); 48 long totalJvmMemory = (rt.totalMemory() - rt.freeMemory()) / (1024l * 1024l); 49 Map<String, String> args = new HashMap<>(); 50 args.put(HEAP_KEY, Long.toString(totalJvmMemory)); 51 logEvent(args); 52 } 53 54 /** Log the event to the history log. */ 55 @VisibleForTesting 56 void logEvent(Map<String, String> args) { 57 LogRegistry.getLogRegistry().logEvent(LogLevel.INFO, EventType.HEAP_MEMORY, args); 58 } 59 } 60