001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.thread;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.node.NodeControlService;
031: import org.cougaar.core.plugin.ComponentPlugin;
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.core.service.ThreadControlService;
034: import org.cougaar.util.UnaryPredicate;
035:
036: /**
037: * This node-level Plugin shows examples of limiting the top-level
038: * thread service in two ways: it sets the global max to 2, and it
039: * qualifies rights selection for children so that no child ever uses
040: * more than half of the available rights.
041: */
042: public class RootControlPlugin extends ComponentPlugin {
043: private static final int MAX_THREADS = 2;
044:
045: public RootControlPlugin() {
046: super ();
047: }
048:
049: private static class ExampleChildQualifier implements
050: UnaryPredicate {
051: private LoggingService lsvc;
052:
053: ExampleChildQualifier(LoggingService lsvc) {
054: this .lsvc = lsvc;
055: }
056:
057: public boolean execute(Object x) {
058: if (!(x instanceof Scheduler))
059: return false;
060:
061: Scheduler child = (Scheduler) x;
062: int lane = child.getLane();
063: Scheduler parent = child.getTreeNode().getParent()
064: .getScheduler(lane);
065: float count = child.runningThreadCount();
066: float max = parent.maxRunningThreadCount();
067: // Random test - don't let any one child use more than half
068: // the slots
069: if (count / max <= .5) {
070: return true;
071: } else {
072: if (lsvc.isWarnEnabled())
073: lsvc.warn("Attempted to use too many rights Child="
074: + child);
075: return false;
076: }
077: }
078: }
079:
080: public void load() {
081: super .load();
082:
083: ServiceBroker sb = getServiceBroker();
084: LoggingService lsvc = (LoggingService) sb.getService(this ,
085: LoggingService.class, null);
086: NodeControlService ncs = (NodeControlService) sb.getService(
087: this , NodeControlService.class, null);
088: sb = ncs.getRootServiceBroker();
089: ThreadControlService tcs = (ThreadControlService) sb
090: .getService(this , ThreadControlService.class, null);
091: // RightsSelector selector = new PercentageLoadSelector(sb);
092: //tcs.setRightsSelector(selector);
093: if (tcs != null) {
094: tcs.setMaxRunningThreadCount(MAX_THREADS);
095: tcs.setChildQualifier(new ExampleChildQualifier(lsvc));
096: }
097:
098: }
099:
100: protected void setupSubscriptions() {
101: }
102:
103: protected void execute() {
104: System.out.println("Uninteresting");
105: }
106:
107: }
|