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 agent traffic matrix for all
021: * local-to-local and local-to-remote agent messaging.
022: * <p>
023: * Example output:<pre>
024: * tm_AgentA__to__AgentB - #count, bytes,
025: * tm_AgentA__to__AgentB - 4783.0, 6136339.0
026: * tm_NodeA__to__NodeB - 8.0, 8167.0
027: * tm_NodeA__to__NodeA - 38.0, 0.0
028: * </pre>
029: * <p>
030: * Node traffic is usually naming service related.
031: *
032: * @see ProfilerCoordinator required coordinator component
033: */
034: public class TrafficMatrix extends ProfilerBase {
035: private static final String TMS_CLASS_NAME = "org.cougaar.core.qos.tmatrix.TrafficMatrixStatisticsService";
036: private static final String[] FIELDS = new String[] { "count",
037: "bytes", };
038: private static final String HEADER = toHeader(FIELDS);
039: private static final String ALIGN = "0, 0";
040: private final Map logs = new HashMap();
041: public Object tms;
042:
043: public void load() {
044: super .load();
045: findServiceLater("tms", TMS_CLASS_NAME);
046: }
047:
048: public void run() {
049: logMatrix();
050: }
051:
052: private void logMatrix() {
053: try {
054: if (tms == null) {
055: throw new RuntimeException("Unable to obtain "
056: + TMS_CLASS_NAME + " service");
057: }
058: Class tmsCl = Class.forName(TMS_CLASS_NAME);
059: Method snapM = tmsCl.getMethod("snapshotMatrix", null);
060: Object tm = snapM.invoke(tms, null);
061:
062: Class tmCl = Class
063: .forName("org.cougaar.core.qos.tmatrix.TrafficMatrix");
064: Method iterM = tmCl.getMethod("getIterator", null);
065: Iterator itr = (Iterator) iterM.invoke(tm, null);
066:
067: Class tiCl = Class
068: .forName("org.cougaar.core.qos.tmatrix.TrafficMatrix$TrafficIterator");
069: Method origM = tiCl.getMethod("getOrig", null);
070: Method targetM = tiCl.getMethod("getTarget", null);
071:
072: Class trCl = Class
073: .forName("org.cougaar.core.qos.tmatrix.TrafficMatrix$TrafficRecord");
074: Method msgCountM = trCl.getMethod("getMsgCount", null);
075: Method byteCountM = trCl.getMethod("getByteCount", null);
076:
077: while (itr.hasNext()) {
078: Object o = itr.next();
079: Object orig = origM.invoke(itr, null);
080: Object target = targetM.invoke(itr, null);
081: Object msgCount = msgCountM.invoke(o, null);
082: Object byteCount = byteCountM.invoke(o, null);
083: getLog(orig, target).shout(msgCount + ", " + byteCount);
084: }
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: }
089:
090: private LoggingService getLog(Object orig, Object target) {
091: LoggingService log;
092: synchronized (logs) {
093: Map m = (Map) logs.get(orig);
094: if (m == null) {
095: m = new HashMap();
096: logs.put(orig, m);
097: }
098: log = (LoggingService) m.get(target);
099: if (log == null) {
100: String os = orig.toString().replace('.', '_');
101: String ts = target.toString().replace('.', '_');
102: String key = "tm_" + os + "__to__" + ts;
103: log = (LoggingService) sb.getService(
104: "org.cougaar.core.qos.profile.traffic_matrix."
105: + key, LoggingService.class, null);
106: m.put(target, log);
107: if (header) {
108: log.shout(HEADER);
109: }
110: if (align) {
111: for (int i = 0, n = getRunCount(); i < n; i++) {
112: log.shout(ALIGN);
113: }
114: }
115: }
116: }
117: return log;
118: }
119: }
|