001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.util;
030:
031: import java.util.logging.Level;
032: import java.util.logging.Logger;
033:
034: public class CpuUsage {
035: static Logger log = Log.open(CpuUsage.class);
036:
037: private static CauchoNative jni;
038: private static boolean triedLoading;
039:
040: private int pid;
041:
042: private long time;
043: private long interval;
044:
045: private long userTime;
046: private long systemTime;
047:
048: private CpuUsage() {
049: }
050:
051: public static synchronized CpuUsage create() {
052: if (jni == null && !triedLoading) {
053: triedLoading = true;
054: try {
055: jni = CauchoNative.create();
056: } catch (Throwable e) {
057: log.log(Level.FINE, e.toString(), e);
058: }
059: }
060:
061: CpuUsage usage = new CpuUsage();
062:
063: if (jni == null)
064: return usage;
065:
066: usage.update(Alarm.getCurrentTime());
067:
068: return usage;
069: }
070:
071: public void update(long now) {
072: time = now;
073:
074: if (jni == null)
075: return;
076:
077: synchronized (jni) {
078: jni.calculateUsage();
079: pid = jni.getPid();
080: userTime = (long) (jni.getUserTime() * 1000);
081: systemTime = (long) (jni.getSystemTime() * 1000);
082: }
083: }
084:
085: public void copy(CpuUsage source) {
086: pid = source.pid;
087: time = source.time;
088:
089: userTime = source.userTime;
090: systemTime = source.systemTime;
091: }
092:
093: public void clear() {
094: pid = -1;
095: time = 0;
096: interval = 0;
097: userTime = 0;
098: systemTime = 0;
099: }
100:
101: public void add(CpuUsage base, CpuUsage source) {
102: if (pid == -1) {
103: pid = source.getPid();
104: time = source.getTime();
105: interval = time - base.getTime();
106: } else if (pid == source.getPid())
107: return;
108:
109: userTime += source.getUserTime() - base.getUserTime();
110: systemTime += source.getSystemTime() - base.getSystemTime();
111: }
112:
113: public int getPid() {
114: return pid;
115: }
116:
117: public long getTime() {
118: return time;
119: }
120:
121: public long getInterval() {
122: return interval;
123: }
124:
125: public long getUserTime() {
126: return userTime;
127: }
128:
129: public long getSystemTime() {
130: return systemTime;
131: }
132:
133: boolean setUser(String user, String group) throws Exception {
134: if (jni == null)
135: return false;
136:
137: else if (user != null) {
138: synchronized (jni) {
139: return jni.setUser(user, group);
140: }
141: } else
142: return false;
143: }
144: }
|