001: package org.cougaar.core.qos.profile;
002:
003: import java.lang.reflect.*;
004: import java.io.*;
005: import java.text.*;
006: import java.util.*;
007: import java.util.regex.*;
008: import org.cougaar.core.agent.*;
009: import org.cougaar.core.component.*;
010: import org.cougaar.core.mts.*;
011: import org.cougaar.core.node.*;
012: import org.cougaar.core.qos.metrics.*;
013: import org.cougaar.core.service.*;
014: import org.cougaar.core.service.wp.*;
015: import org.cougaar.core.thread.*;
016: import org.cougaar.core.wp.resolver.*;
017: import org.cougaar.util.*;
018:
019: /**
020: * This component profiles the {@link MetricsService}'s load
021: * metrics for the cpu, messaging, and persistence load of the
022: * node aggegate, each agent, and core services (mts & metrics).
023: * <p>
024: * Note that these are the metrics service's thread-based CPU
025: * metrics, as opposed to the operating system's {@link ProcLoadAvg}
026: * "loadavg" metrics.
027: * <p>
028: * This is the same output as the "/metrics/agent/load" servlet
029: * generated by {@link org.cougaar.core.qos.metrics.AgentLoadServlet}.
030: * <p>
031: * Example output:<pre>
032: * load_node_NodeA - #cpu_load, cpu_load_jips,
033: * recv_message_count, sent_message_count,
034: * recv_message_bytes, sent_message_bytes,
035: * persist_size_in_bytes,
036: * load_node_NodeA - 0.88, 1270.32,
037: * 146.00, 145.90,
038: * 132248.03, 186308.48,
039: * 10518528.00,
040: * load_agent_AgentA - 0.31, 183.71,
041: * 145.15, 145.05,
042: * 132090.91, 186105.49,
043: * 0.00,
044: * load_agent_NodeA - 0.01, 19.82,
045: * 0.80, 0.80,
046: * 117.49, 147.17,
047: * 0.00,
048: * load_service_MTS - #cpu_load, cpu_load_jips,
049: * load_service_MTS - 0.49, 533.80,
050: * load_service_Metrics - 0.08, 99.95,
051: * load_service_NodeRoot - 0.00, 1.54,
052: * </pre>
053: *
054: * @see ProfilerCoordinator required coordinator component
055: */
056: public class Load extends ProfilerBase {
057: private static final DecimalFormat FORMAT_DECIMAL = new DecimalFormat(
058: "#0.00");
059: private static final String[] FIELDS = new String[] { "cpu_load",
060: "cpu_load_jips", "recv_message_count",
061: "sent_message_count", "recv_message_bytes",
062: "sent_message_bytes", "persist_size_in_bytes", };
063: private static final String HEADER = toHeader(FIELDS);
064: private static final String[] KEYS = new String[] {
065: Constants.CPU_LOAD_AVG, Constants.CPU_LOAD_MJIPS,
066: Constants.MSG_IN, Constants.MSG_OUT, Constants.BYTES_IN,
067: Constants.BYTES_OUT, };
068:
069: private static final String[] SERVICE_FIELDS = new String[] {
070: "cpu_load", "cpu_load_jips", };
071: private static final String SERVICE_HEADER = toHeader(SERVICE_FIELDS);
072: private static final String[] SERVICE_KEYS = new String[] {
073: Constants.CPU_LOAD_AVG, Constants.CPU_LOAD_MJIPS, };
074:
075: // dummy logLoad keys
076: private static final Object LOAD_NODE = new Object();
077: private static final Object LOAD_MTS = "MTS";
078: private static final Object LOAD_METRICS = "Metrics";
079: private static final Object LOAD_NODE_ROOT = "NodeRoot";
080:
081: private final Map logs = new HashMap();
082:
083: private MessageAddress localNode;
084: private MetricsService ms;
085: public AgentStatusService as;
086:
087: public void load() {
088: super .load();
089: localNode = findLocalNode();
090: ms = (MetricsService) sb.getService(this , MetricsService.class,
091: null);
092: findServiceLater("as",
093: "org.cougaar.core.mts.AgentStatusService");
094: }
095:
096: public void run() {
097: logLoads();
098: }
099:
100: private void logLoads() {
101: logLoad(LOAD_NODE);
102: for (Iterator iter = getLocalAgents().iterator(); iter
103: .hasNext();) {
104: MessageAddress addr = (MessageAddress) iter.next();
105: logLoad(addr);
106: }
107: logLoad(LOAD_MTS);
108: logLoad(LOAD_METRICS);
109: logLoad(LOAD_NODE_ROOT);
110: }
111:
112: private void logLoad(Object o) {
113: String s = getLoad(o);
114: getLog(o).shout(s);
115: }
116:
117: private boolean isService(Object o) {
118: return (o == LOAD_MTS || o == LOAD_METRICS || o == LOAD_NODE_ROOT);
119: }
120:
121: private boolean isNode(Object o) {
122: return o == LOAD_NODE;
123: }
124:
125: private String getLoad(Object o) {
126:
127: if (isService(o)) {
128: String path = "Service(" + o + ")" + Constants.PATH_SEPR;
129: StringBuffer buf = new StringBuffer();
130: for (int i = 0; i < SERVICE_KEYS.length; i++) {
131: String key = path + SERVICE_KEYS[i] + "("
132: + Constants._10_SEC_AVG + ")";
133: String s = getMetric(key);
134: buf.append(s).append(", ");
135: }
136: return buf.toString();
137: }
138:
139: String path = (isNode(o) ? "Node(" + localNode + ")" : "Agent("
140: + o + ")")
141: + Constants.PATH_SEPR;
142:
143: StringBuffer buf = new StringBuffer();
144: for (int i = 0; i <= KEYS.length; i++) {
145: String key = path
146: + (i < KEYS.length ? KEYS[i] + "("
147: + Constants._10_SEC_AVG + ")"
148: : (isNode(o) ? "VMSize"
149: : Constants.PERSIST_SIZE_LAST));
150: String s = getMetric(key);
151: buf.append(s).append(", ");
152: }
153: return buf.toString();
154: }
155:
156: private String getMetric(String key) {
157: Metric metric = ms.getValue(key);
158: String s = (metric == null ? "-1" : FORMAT_DECIMAL
159: .format(metric.doubleValue()));
160: return s;
161: }
162:
163: private LoggingService getLog(Object o) {
164: LoggingService log;
165: synchronized (logs) {
166: log = (LoggingService) logs.get(o);
167: if (log == null) {
168: String key = "load_"
169: + (isService(o) ? "service_" + o
170: : isNode(o) ? "node_" + localNode
171: : "agent_" + o);
172: key = key.replace('.', '_');
173: log = (LoggingService) sb.getService(
174: "org.cougaar.core.qos.profile.agent_load."
175: + key, LoggingService.class, null);
176: logs.put(o, log);
177: if (header) {
178: log.shout(isService(o) ? SERVICE_HEADER : HEADER);
179: }
180: if (align) {
181: int num_fields = (isService(o) ? SERVICE_FIELDS.length
182: : FIELDS.length);
183: for (int i = 0, n = getRunCount(); i < n; i++) {
184: StringBuffer buf = new StringBuffer();
185: for (int j = 0; j < num_fields; j++) {
186: buf.append("0, ");
187: }
188: log.shout(buf.toString());
189: }
190: }
191: }
192: }
193: return log;
194: }
195:
196: private MessageAddress findLocalNode() {
197: NodeIdentificationService nis = (NodeIdentificationService) sb
198: .getService(this , NodeIdentificationService.class, null);
199: if (nis == null) {
200: return null;
201: }
202: MessageAddress ret = nis.getMessageAddress();
203: sb.releaseService(this , NodeIdentificationService.class, nis);
204: return ret;
205: }
206:
207: private Set getLocalAgents() {
208: return as.getLocalAgents();
209: }
210: }
|