001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2007 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.qos.qrs.sysstat;
028:
029: import java.io.FileReader;
030: import java.io.BufferedReader;
031: import java.util.Map;
032:
033: import org.cougaar.qos.qrs.DataValue;
034:
035: public class LinuxCPUInfo extends SysStatHandler {
036: private static String CacheLineKey, ClockLineKey, BogomipsLineKey;
037:
038: static {
039: BogomipsLineKey = "bogomips";
040: String arch = System.getProperty("os.arch");
041: if (arch.equals("ppc")) {
042: CacheLineKey = "L2 cache";
043: ClockLineKey = "clock";
044: } else if (arch.equals("i386") || arch.equals("amd64")) {
045: CacheLineKey = "cache size";
046: ClockLineKey = "cpu MHz";
047: }
048:
049: }
050:
051: private FileReader fr;
052: private BufferedReader br;
053: private String bogomips_key, cache_key, clock_key;
054:
055: public void initialize(String host, int pid) {
056: String prefix = "Host" + KEY_SEPR + host + KEY_SEPR + "CPU"
057: + KEY_SEPR;
058: bogomips_key = prefix + "bogomips";
059: cache_key = prefix + "cache";
060: clock_key = prefix + "MHz";
061: }
062:
063: private void close() {
064: try {
065: br.close();
066: } catch (java.io.IOException ioe_ex) {
067: }
068: }
069:
070: public void getData(Map<String, DataValue> map) {
071: fr = null;
072: try {
073: fr = new FileReader("/proc/cpuinfo");
074: } catch (java.io.FileNotFoundException fnf_ex) {
075: return;
076: }
077:
078: br = new BufferedReader(fr);
079: String line = null;
080: while (true) {
081: try {
082: line = br.readLine();
083: if (line == null) {
084: break;
085: }
086: if (line.startsWith(BogomipsLineKey)) {
087: map.put(bogomips_key, extractBogomips(line));
088: } else if (line.startsWith(CacheLineKey)) {
089: map.put(cache_key, extractCache(line));
090: } else if (line.startsWith(ClockLineKey)) {
091: map.put(clock_key, extractClock(line));
092: }
093: } catch (java.io.IOException io_ex) {
094: close();
095: return;
096: }
097: }
098: close();
099: }
100:
101: private DataValue extractBogomips(String line) {
102: int index = line.indexOf(':');
103: if (index == -1) {
104: return DataValue.NO_VALUE;
105: }
106: String doub = line.substring(index + 2);
107: try {
108: return new DataValue(parseValue(doub),
109: SECOND_MEAS_CREDIBILITY, "", PROVENANCE);
110: } catch (NumberFormatException nf_ex) {
111: return DataValue.NO_VALUE;
112: }
113: }
114:
115: private DataValue extractCache(String line) {
116: int index = line.indexOf(':');
117: if (index == -1) {
118: return DataValue.NO_VALUE;
119: }
120: // String doub = line.substring(index+2);
121: int start_index = index + 2;
122: int end_index = start_index;
123: try {
124: while (Character.isDigit(line.charAt(end_index))) {
125: ++end_index;
126: }
127: double cache = Double.parseDouble(line.substring(
128: start_index, end_index));
129:
130: return new DataValue(cache, SECOND_MEAS_CREDIBILITY, "",
131: PROVENANCE);
132: } catch (NumberFormatException nf_ex) {
133: return DataValue.NO_VALUE;
134: }
135: }
136:
137: private DataValue extractClock(String line) {
138: int index = line.indexOf(':');
139: if (index == -1) {
140: return DataValue.NO_VALUE;
141: }
142: double clock = 0.0;
143: String arch = System.getProperty("os.arch");
144: try {
145: if (arch.equals("ppc")) {
146: // get rid of the MHz at the end...
147: int start_index = index + 2;
148: int end_index = line.indexOf("M");
149: clock = Double.parseDouble(line.substring(start_index,
150: end_index));
151: } else {
152: String doub = line.substring(index + 2);
153: clock = parseValue(doub);
154: }
155:
156: return new DataValue(clock, SECOND_MEAS_CREDIBILITY, "",
157: PROVENANCE);
158: } catch (NumberFormatException nf_ex) {
159: return DataValue.NO_VALUE;
160: }
161: }
162:
163: }
|