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.tools.csmart.runtime.jni;
028:
029: import java.util.*;
030: import java.io.*;
031:
032: /**
033: * This class attempts to read the cpu load stats from the /proc filesystem.
034: * It totals up the user and kernel cpu time for the process group that includes
035: * the specified process. This way it can accumulate stats for all of the java threads
036: * in an application.
037: **/
038: class ProcCpu {
039: public static void main(String args[]) {
040: ProcCpu p = new ProcCpu(args[0]);
041: System.out.println("Collecting CPU stats for " + args[0]);
042: int jiffies = p.get();
043: System.out.println("Jiffies = " + jiffies);
044: }
045:
046: /**
047: * This method returs true iff this system supports the /proc filesystem.
048: */
049: public static boolean isOK() {
050: File self = new File("/proc/self/stat");
051: return self.exists();
052: }
053:
054: private String pid = null;
055:
056: /**
057: * Monitor the specified pid.
058: */
059: ProcCpu(String pid) {
060: this .pid = pid;
061: }
062:
063: /**
064: * Monitor the current pid.
065: */
066: ProcCpu() {
067: }
068:
069: /**
070: * Return the (cumulative) number of jiffies (1/100 sec) used
071: * by this process.
072: * (Actually all of the processes in the process group)
073: */
074: int get() {
075:
076: File self;
077: BufferedReader f;
078: String l;
079: Proc p;
080: int pgroup = -1;
081:
082: try {
083: if (pid == null)
084: self = new File("/proc/self/stat");
085: else
086: self = new File("/proc/" + pid + "/stat");
087: f = new BufferedReader(new InputStreamReader(
088: new FileInputStream(self)));
089: l = f.readLine();
090: p = new Proc(l);
091: pgroup = p.pgrp;
092: } catch (Exception ex) {
093: ex.printStackTrace();
094: }
095:
096: int jiffies = 0;
097: File pd = new File("/proc");
098: if (pd.isDirectory()) {
099: File pfs[] = pd.listFiles();
100: for (int i = 0; i < pfs.length; i++) {
101: File pf = pfs[i];
102: if (pf.isDirectory()
103: && "0123456789".indexOf(pf.getName().charAt(0)) >= 0) {
104: try {
105: File sf = new File(pf, "stat");
106: f = new BufferedReader(new InputStreamReader(
107: new FileInputStream(sf)));
108: l = f.readLine();
109: p = new Proc(l);
110: // accumulate totals for the proces group
111: if (p.pgrp == pgroup) {
112: jiffies += p.utime + p.stime;
113: }
114: f.close();
115: } catch (IOException ioe) {
116: //ioe.printStackTrace();
117: }
118: }
119: }
120: }
121: return jiffies;
122: }
123:
124: /**
125: * This is a struct for holding CPU performance data.
126: */
127: static class Proc {
128: int pid;
129: String cmd;
130: String state;
131: int ppid;
132: int pgrp;
133: int session;
134: int utime;
135: int stime;
136:
137: Proc(String l) {
138: Vector v = explode(l);
139: pid = Integer.parseInt((String) v.elementAt(0));
140: cmd = (String) v.elementAt(1);
141: state = (String) v.elementAt(2);
142: ppid = Integer.parseInt((String) v.elementAt(3));
143: pgrp = Integer.parseInt((String) v.elementAt(4));
144: session = Integer.parseInt((String) v.elementAt(5));
145:
146: utime = Integer.parseInt((String) v.elementAt(13));
147: stime = Integer.parseInt((String) v.elementAt(14));
148: // ignore the rest
149: }
150: }
151:
152: public static Vector explode(String s) {
153: Vector v = new Vector();
154: int j = 0; // non-white
155: int k = 0; // char after last white
156: int l = s.length();
157: int i = 0;
158: while (i < l) {
159: if (Character.isWhitespace(s.charAt(i))) {
160: // is white - what do we do?
161: if (i == k) { // skipping contiguous white
162: k++;
163: } else { // last char wasn't white - word boundary!
164: v.addElement(s.substring(k, i));
165: k = i + 1;
166: }
167: } else { // nonwhite
168: // let it advance
169: }
170: i++;
171: }
172: if (k != i) { // leftover non-white chars
173: v.addElement(s.substring(k, i));
174: }
175: return v;
176: }
177:
178: }
|