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.tradefed.config.GlobalConfiguration; 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.util.IRunUtil; 22 import com.android.tradefed.util.RunUtil; 23 import com.android.tradefed.util.VersionParser; 24 25 import java.net.InetAddress; 26 import java.util.HashMap; 27 import java.util.Map; 28 import java.util.Queue; 29 import java.util.concurrent.LinkedBlockingQueue; 30 31 /** 32 * Typical class for Host Health Monitoring. implementing dispatch() with specifics of the agent. 33 */ 34 public abstract class AbstractHostMonitor extends Thread implements IHostMonitor { 35 @Option( 36 name = "dispatch-interval", 37 description = "the time interval between dispatches in ms", 38 isTimeVal = true 39 ) 40 private long mDispatchInterval = 15 * 1000; 41 42 @Option(name = "agent-name", description = "the name of the agent object to be used") 43 private String mAgentName = "host_metric_agent"; 44 45 @Option(name = "event-tag", description = "Event Tag that will be accepted by the Monitor.") 46 private HostMetricType mTag = HostMetricType.NONE; 47 48 protected Queue<HostDataPoint> mHostEvents = new LinkedBlockingQueue<HostDataPoint>(); 49 50 protected Map<String, String> mHostData = new HashMap<>(); 51 52 private boolean mIsCanceled = false; 53 54 public AbstractHostMonitor() { 55 super("AbstractHostMonitor"); 56 this.setDaemon(true); 57 } 58 59 /** 60 * Collect and Emits the current host data values. Should emits the Events of the Queue if any. 61 */ 62 public abstract void dispatch(); 63 64 /** Return the tag identifying which 'class' of {@link IHostMonitor} to reach. */ 65 public HostMetricType getTag() { 66 return mTag; 67 } 68 69 @Override 70 public void run() { 71 try { 72 mHostData.put("hostname", InetAddress.getLocalHost().getHostName()); 73 mHostData.put("tradefed_version", VersionParser.fetchVersion()); 74 75 while (!mIsCanceled) { 76 dispatch(); 77 getRunUtil().sleep(mDispatchInterval); 78 } 79 } catch (Exception e) { 80 CLog.e(e); 81 } 82 } 83 84 /** {@inheritDoc} */ 85 @Override 86 public synchronized void addHostEvent(HostMetricType tag, HostDataPoint event) { 87 if (getTag().equals(tag)) { 88 mHostEvents.add(event); 89 } 90 } 91 92 /** {@inheritDoc} */ 93 @Override 94 public void terminate() { 95 mIsCanceled = true; 96 } 97 98 IHostHealthAgent getMetricAgent() { 99 IHostHealthAgent agent = 100 (IHostHealthAgent) 101 GlobalConfiguration.getInstance().getConfigurationObject(mAgentName); 102 return agent; 103 } 104 105 IRunUtil getRunUtil() { 106 return RunUtil.getDefault(); 107 } 108 109 int getQueueSize() { 110 return mHostEvents.size(); 111 } 112 } 113