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: package org.cougaar.tools.csmart.runtime.jni;
027:
028: import java.util.*;
029:
030: public class CpuClock {
031: private static boolean needLibrary = true;
032: private static boolean haveLibrary = false;
033: private static ProcCpu cpu;
034:
035: /**
036: * Determine whether we can use the /proc filesystem for stats.
037: * If not, use the JNI code.
038: */
039: static {
040: if (ProcCpu.isOK()) {
041: System.out.println("Using Java CPU measurment");
042: cpu = new ProcCpu();
043: } else {
044: System.out.println("Using JNI CPU measurment");
045: cpu = null;
046: }
047: }
048:
049: /**
050: * Load library if necessary. Check for loading errors and disable
051: * cpu clock if library can't be loaded.
052: * @return true if the libary is available.
053: **/
054: private static synchronized boolean checkLibrary() {
055: if (needLibrary) {
056: try {
057: System.loadLibrary("mylib");
058: haveLibrary = true;
059: } catch (UnsatisfiedLinkError e) {
060: System.err.println("CpuClock exception: " + e);
061: }
062: needLibrary = false;
063: }
064: return haveLibrary;
065: }
066:
067: //Native method declaration
068: private static native int clock();
069:
070: private static int checkedClock() {
071: if (checkLibrary()) {
072: return clock();
073: } else {
074: return 0;
075: }
076: }
077:
078: /**
079: * clock() returns deltas, we accumulate them so we can report total cpu time.
080: **/
081: private static long cpuAccumulator = 0L;
082:
083: /**
084: * Get total cpu time in milliseconds. This is the basis for every
085: * other use of cpu time.
086: **/
087: public static synchronized long cpuTimeMillis() {
088: if (cpu != null) {
089: cpuAccumulator = cpu.get() * 10; // convert from jiffies to millis
090: } else {
091: cpuAccumulator += checkedClock();
092: }
093:
094: return cpuAccumulator;
095: }
096:
097: long lastTime = System.currentTimeMillis();
098: long lastCPU = cpuTimeMillis();
099:
100: /**
101: * Get ratio of cpu time to real time.
102: **/
103: public double cpuLoad() {
104: return cpuLoad(true);
105: }
106:
107: /**
108: * Get the cpu utilization -- the ratio of cpu time to real time. If
109: * no real time has elapsed, returns 0.0.
110: * @param reset resets the bases for the next call to this method to
111: * zero.
112: **/
113: public double cpuLoad(boolean reset) {
114: long this CPU = cpuTimeMillis();
115: long this Time = System.currentTimeMillis();
116: long elapsed = this Time - lastTime;
117: long cpu = Math.min(elapsed, this CPU - lastCPU);
118: if (elapsed == 0)
119: return 0.0;
120: if (reset) {
121: lastCPU += cpu;
122: lastTime += elapsed;
123: }
124: return (double) cpu / (double) elapsed;
125: }
126:
127: public static void main(String args[]) {
128: //Create class instance
129: CpuClock c = new CpuClock();
130:
131: //Call native method to load ReadFile.java
132: int time = c.checkedClock();
133: //Print contents of ReadFile.java
134: for (int loops = 0; loops < 3; loops++) {
135: System.out.println("LOOP: Processor load is: "
136: + c.cpuLoad());
137: for (int i = 0; i < 40000000; i++) {
138: int j = i / 3;
139: Math.sqrt(j);
140: }
141: }
142: System.out.println("Processor load is: " + c.cpuLoad());
143: System.out.println("Processor load is: " + c.cpuLoad());
144: try {
145: Thread.sleep(2000);
146: } catch (Exception e) {
147: }
148: System.out.println("SLEEP: Processor load is: " + c.cpuLoad());
149: }
150: }
|