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 per-agent blackboard size and
021: * add/change/remove counts for standard blackboard object types
022: * (UniqueObject, Task, etc).
023: * <p>
024: * Example output:<pre>
025: * bb_AgentA__Task - #current, added, changed, removed
026: * bb_AgentA__Task - 5807, 7615, 3514, 1806
027: * bb_AgentA__UniqueObject - 11853, 18841, 46569, 6988
028: * bb_AgentA__transactions - 192, 192, 0, 0
029: * </pre>
030: * In this example, 5807 tasks are on the blackboard.
031: * <p>
032: * "transactions" is used to measure the number of plugin
033: * "execute()" cycles.
034: *
035: * @see ProfilerCoordinator required coordinator component
036: * @see BlackboardSizeInAgent required per-agent component
037: */
038: public class BlackboardSize extends ProfilerBase {
039: private/*static*/final String HEADER = "#" + "current, "
040: + "added, " + "changed, " + "removed";
041: private ServiceBroker rootsb;
042: private ServiceProvider sp;
043: private Map logs = new HashMap();
044: private Map clients = new HashMap();
045:
046: public void load() {
047: super .load();
048: NodeControlService ncs = (NodeControlService) sb.getService(
049: this , NodeControlService.class, null);
050: rootsb = ncs.getRootServiceBroker();
051: sb.releaseService(this , NodeControlService.class, ncs);
052:
053: sp = new BlackboardSizeSP();
054: rootsb.addService(BlackboardSizeService.class, sp);
055: }
056:
057: public void run() {
058: logBB();
059: }
060:
061: private void logBB() {
062: synchronized (clients) {
063: for (Iterator iter = clients.entrySet().iterator(); iter
064: .hasNext();) {
065: Map.Entry me = (Map.Entry) iter.next();
066: String name = (String) me.getKey();
067: BlackboardSizeService.Client c = (BlackboardSizeService.Client) me
068: .getValue();
069: Map m = c.getData();
070: String[] keys = (String[]) m.keySet().toArray(
071: new String[m.size()]);
072: Arrays.sort(keys);
073: for (int i = 0; i < keys.length; i++) {
074: int[] values = (int[]) m.get(keys[i]);
075: getLog(name, keys[i]).shout(
076: values[0] + ", " + values[1] + ", "
077: + values[2] + ", " + values[3]);
078: }
079: }
080: }
081: }
082:
083: private LoggingService getLog(String name, String id) {
084: LoggingService log;
085: synchronized (logs) {
086: Map m = (Map) logs.get(name);
087: if (m == null) {
088: m = new HashMap();
089: logs.put(name, m);
090: }
091: log = (LoggingService) m.get(id);
092: if (log == null) {
093: String sn = name.replace('.', '_');
094: String key = sn + "__" + id;
095: log = (LoggingService) sb.getService(
096: "org.cougaar.core.qos.profile.bb.bb_" + key,
097: LoggingService.class, null);
098: m.put(id, log);
099: if (header) {
100: log.shout(HEADER);
101: }
102: if (align) {
103: for (int i = 0, n = getRunCount(); i < n; i++) {
104: log.shout("0, 0, 0, 0");
105: }
106: }
107: }
108: }
109: return log;
110: }
111:
112: private void register(BlackboardSizeService.Client c) {
113: synchronized (clients) {
114: clients.put(c.getName(), c);
115: }
116: }
117:
118: private void unregister(BlackboardSizeService.Client c) {
119: synchronized (clients) {
120: clients.remove(c.getName());
121: }
122: }
123:
124: private class BlackboardSizeSP implements ServiceProvider {
125: private final BlackboardSizeService SVC = new BlackboardSizeService() {
126: };
127:
128: public Object getService(ServiceBroker sb, Object requestor,
129: Class serviceClass) {
130: if (!BlackboardSizeService.class
131: .isAssignableFrom(serviceClass)) {
132: return null;
133: }
134: register((BlackboardSizeService.Client) requestor);
135: return SVC;
136: }
137:
138: public void releaseService(ServiceBroker sb, Object requestor,
139: Class serviceClass, Object service) {
140: unregister((BlackboardSizeService.Client) requestor);
141: }
142: }
143: }
|