adding disk io per process and bug fixes
This commit is contained in:
1 parent
449de48cea
commit
797d8a9edf
24 files changed
+779
-105
No files matched your search
@@ -292,74 +292,58 @@ public class FileInfo extends FileAttrs implements java.io.Serializable {
|
||||
}
|
||||
|
||||
public String diff(FileInfo info) {
|
||||
ArrayList changes = new ArrayList();
|
||||
ArrayList changes = new ArrayList();
|
||||
|
||||
if (this.getMtime() != info.getMtime()) {
|
||||
changes.add(new Diff("Mtime",
|
||||
formatDate(info.getMtime()),
|
||||
formatDate(this.getMtime())));
|
||||
if (this.getMtime() != info.getMtime()) {
|
||||
changes.add(new Diff("Mtime", formatDate(info.getMtime()),
|
||||
formatDate(this.getMtime())));
|
||||
} else if (this.getCtime() != info.getCtime()) {
|
||||
changes.add(new Diff("Ctime", formatDate(info.getCtime()),
|
||||
formatDate(this.getCtime())));
|
||||
}
|
||||
|
||||
if (this.getPermissions() != info.getPermissions()) {
|
||||
changes.add(new Diff("Perms", info.getPermissionsString(), this
|
||||
.getPermissionsString()));
|
||||
}
|
||||
|
||||
if (this.getType() != info.getType()) {
|
||||
changes.add(new Diff("Type", info.getTypeString(), this
|
||||
.getTypeString()));
|
||||
}
|
||||
|
||||
if (this.getUid() != info.getUid()) {
|
||||
changes.add(new Diff("Uid", info.getUid(), this.getUid()));
|
||||
}
|
||||
|
||||
if (this.getGid() != info.getGid()) {
|
||||
changes.add(new Diff("Gid", info.getGid(), this.getGid()));
|
||||
}
|
||||
|
||||
if (this.getSize() != info.getSize()) {
|
||||
changes.add(new Diff("Size", info.getSize(), this.getSize()));
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IS_WIN32) {
|
||||
if (this.getInode() != info.getInode()) {
|
||||
changes.add(new Diff("Inode", info.getInode(), this.getInode()));
|
||||
}
|
||||
|
||||
if (this.getDevice() != info.getDevice()) {
|
||||
changes.add(new Diff("Device", info.getDevice(), this
|
||||
.getDevice()));
|
||||
}
|
||||
|
||||
if (this.getNlink() != info.getNlink()) {
|
||||
changes.add(new Diff("Nlink", info.getNlink(), this.getNlink()));
|
||||
}
|
||||
}
|
||||
else if (this.getCtime() != info.getCtime()) {
|
||||
changes.add(new Diff("Ctime",
|
||||
formatDate(info.getCtime()),
|
||||
formatDate(this.getCtime())));
|
||||
}
|
||||
else {
|
||||
//no point in checking the rest if all times are the same.
|
||||
//or should we include atime in the diff?
|
||||
|
||||
/* if changes were not detected then return empty String */
|
||||
if (changes.isEmpty()){
|
||||
return "";
|
||||
}
|
||||
|
||||
if (this.getPermissions() != info.getPermissions()) {
|
||||
changes.add(new Diff("Perms",
|
||||
info.getPermissionsString(),
|
||||
this.getPermissionsString()));
|
||||
}
|
||||
|
||||
if (this.getType() != info.getType()) {
|
||||
changes.add(new Diff("Type",
|
||||
info.getTypeString(),
|
||||
this.getTypeString()));
|
||||
}
|
||||
|
||||
if (this.getUid() != info.getUid()) {
|
||||
changes.add(new Diff("Uid",
|
||||
info.getUid(),
|
||||
this.getUid()));
|
||||
}
|
||||
|
||||
if (this.getGid() != info.getGid()) {
|
||||
changes.add(new Diff("Gid",
|
||||
info.getGid(),
|
||||
this.getGid()));
|
||||
}
|
||||
|
||||
if (this.getSize() != info.getSize()) {
|
||||
changes.add(new Diff("Size",
|
||||
info.getSize(),
|
||||
this.getSize()));
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IS_WIN32) {
|
||||
if (this.getInode() != info.getInode()) {
|
||||
changes.add(new Diff("Inode",
|
||||
info.getInode(),
|
||||
this.getInode()));
|
||||
}
|
||||
|
||||
if (this.getDevice() != info.getDevice()) {
|
||||
changes.add(new Diff("Device",
|
||||
info.getDevice(),
|
||||
this.getDevice()));
|
||||
}
|
||||
|
||||
if (this.getNlink() != info.getNlink()) {
|
||||
changes.add(new Diff("Nlink",
|
||||
info.getNlink(),
|
||||
this.getNlink()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
StringBuffer sb = format(changes);
|
||||
if (this.dirStatEnabled) {
|
||||
sb.append(diff(info.stat));
|
||||
@@ -389,7 +373,9 @@ public class FileInfo extends FileAttrs implements java.io.Serializable {
|
||||
|
||||
stat();
|
||||
|
||||
return this.mtime != oldInfo.mtime;
|
||||
boolean isModified = isModified(this.oldInfo);
|
||||
|
||||
return isModified;
|
||||
}
|
||||
|
||||
public boolean changed()
|
||||
@@ -455,4 +441,49 @@ public class FileInfo extends FileAttrs implements java.io.Serializable {
|
||||
|
||||
return fetchInfo(sigar, name, false);
|
||||
}
|
||||
|
||||
private boolean isModified(FileInfo info){
|
||||
/* Check modified time */
|
||||
if (this.getMtime() != info.getMtime()) {
|
||||
return true;
|
||||
} else if (this.getCtime() != info.getCtime()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getPermissions() != info.getPermissions()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getType() != info.getType()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getUid() != info.getUid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getGid() != info.getGid()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getSize() != info.getSize()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IS_WIN32) {
|
||||
if (this.getInode() != info.getInode()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getDevice() != info.getDevice()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.getNlink() != info.getNlink()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,8 @@ public class Sigar implements SigarProxy {
|
||||
private static String loadError = null;
|
||||
|
||||
public static final long FIELD_NOTIMPL = -1;
|
||||
public static final int PID_PROC_CPU_CACHE = 1;
|
||||
public static final int PID_PROC_IO_CACHE = 2;
|
||||
|
||||
/**
|
||||
* The Sigar java version.
|
||||
@@ -435,7 +437,7 @@ public class Sigar implements SigarProxy {
|
||||
return this.processFinder.findSingleProcess(pid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get process memory info.
|
||||
* @param pid The process id.
|
||||
@@ -633,6 +635,57 @@ public class Sigar implements SigarProxy {
|
||||
Integer.parseInt(port));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get process disk IO info.
|
||||
* @param pid THe process id.
|
||||
* @exception SigarException on failure.
|
||||
*/
|
||||
public ProcDiskIO getProcDiskIO(long pid) throws SigarException {
|
||||
try {
|
||||
return ProcDiskIO.fetch(this, pid);
|
||||
} catch (UnsatisfiedLinkError linkErrorException) {
|
||||
// We want to handle exceptions gracefully even if the linked
|
||||
// shared library is older and isn't compiled with the ProcDiskIO APIs.
|
||||
// The downside of this is that we throw SigarNotImplemented exception
|
||||
// also when the shared library can't be loaded.
|
||||
SigarException sigarException = new SigarNotImplementedException();
|
||||
sigarException.initCause(linkErrorException);
|
||||
throw sigarException;
|
||||
}
|
||||
}
|
||||
|
||||
public ProcDiskIO getProcDiskIO(String pid) throws SigarException {
|
||||
return getProcDiskIO(convertPid(pid));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get process cumulative disk IO info.
|
||||
* @param pid THe process id.
|
||||
* @exception SigarException on failure.
|
||||
*/
|
||||
public ProcCumulativeDiskIO getProcCumulativeDiskIO(long pid) throws SigarException {
|
||||
try {
|
||||
return ProcCumulativeDiskIO.fetch(this, pid);
|
||||
} catch (UnsatisfiedLinkError linkErrorException) {
|
||||
// We want to handle exceptions gracefully even if the linked
|
||||
// shared library is older and isn't compiled with the ProcDiskIO APIs.
|
||||
// The downside of this is that we throw SigarNotImplemented exception
|
||||
// also when the shared library can't be loaded.
|
||||
SigarException sigarException = new SigarNotImplementedException();
|
||||
sigarException.initCause(linkErrorException);
|
||||
throw sigarException;
|
||||
}
|
||||
}
|
||||
|
||||
public ProcCumulativeDiskIO getProcCumulativeDiskIO(String pid) throws SigarException {
|
||||
return getProcCumulativeDiskIO(convertPid(pid));
|
||||
}
|
||||
|
||||
public DumpPidCache dumpPidCache() throws SigarException {
|
||||
return DumpPidCache.fetch(this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the cumulative cpu time for the calling thread.
|
||||
*/
|
||||
|
||||
@@ -106,6 +106,16 @@ public interface SigarProxy {
|
||||
|
||||
public long getProcPort(String protocol, String port) throws SigarException;
|
||||
|
||||
public ProcDiskIO getProcDiskIO(long pid) throws SigarException;
|
||||
|
||||
public ProcDiskIO getProcDiskIO(String pid) throws SigarException;
|
||||
|
||||
public ProcCumulativeDiskIO getProcCumulativeDiskIO(long pid) throws SigarException;
|
||||
|
||||
public ProcCumulativeDiskIO getProcCumulativeDiskIO(String pid) throws SigarException;
|
||||
|
||||
public DumpPidCache dumpPidCache() throws SigarException;
|
||||
|
||||
public FileSystem[] getFileSystemList() throws SigarException;
|
||||
|
||||
public FileSystemMap getFileSystemMap() throws SigarException;
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2007 Hyperic, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.hyperic.sigar.cmd;
|
||||
|
||||
import org.hyperic.sigar.SigarException;
|
||||
import org.hyperic.sigar.SigarPermissionDeniedException;
|
||||
|
||||
/**
|
||||
* Display all pid cache information.
|
||||
*/
|
||||
public class PidCacheInfo extends SigarCommandBase {
|
||||
|
||||
|
||||
public PidCacheInfo(Shell shell) {
|
||||
super(shell);
|
||||
}
|
||||
|
||||
public PidCacheInfo() {
|
||||
super();
|
||||
}
|
||||
|
||||
protected boolean validateArgs(String[] args) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getUsageShort() {
|
||||
return "Display cache info for CPU cache and for IO cache";
|
||||
}
|
||||
|
||||
public boolean isPidCompleter() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void output(String[] args) throws SigarException {
|
||||
sigar.dumpPidCache();
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,14 @@ public class ProcInfo extends SigarCommandBase {
|
||||
try {
|
||||
println("credname=" + sigar.getProcCredName(pid));
|
||||
} catch (SigarException e) {}
|
||||
try {
|
||||
println("diskio=" + sigar.getProcDiskIO(pid));
|
||||
} catch (SigarException e) {}
|
||||
|
||||
try {
|
||||
println("cumulative diskio=" + sigar.getProcCumulativeDiskIO(pid));
|
||||
} catch (SigarException e) {}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
@@ -102,6 +102,8 @@ public class Shell extends ShellBase {
|
||||
registerCommandHandler("time", new Time(this));
|
||||
registerCommandHandler("ulimit", new Ulimit(this));
|
||||
registerCommandHandler("who", new Who(this));
|
||||
registerCommandHandler("pid_cache_info", new PidCacheInfo(this));
|
||||
|
||||
if (SigarLoader.IS_WIN32) {
|
||||
registerCommandHandler("service", new Win32Service(this));
|
||||
registerCommandHandler("fversion", new FileVersionInfo(this));
|
||||
|
||||
@@ -19,6 +19,8 @@ package org.hyperic.sigar.jmx;
|
||||
import org.hyperic.sigar.ProcCpu;
|
||||
import org.hyperic.sigar.ProcFd;
|
||||
import org.hyperic.sigar.ProcMem;
|
||||
import org.hyperic.sigar.ProcUtil;
|
||||
import org.hyperic.sigar.ProcDiskIO;
|
||||
import org.hyperic.sigar.Sigar;
|
||||
import org.hyperic.sigar.SigarException;
|
||||
import org.hyperic.sigar.SigarProxy;
|
||||
@@ -31,20 +33,24 @@ import org.hyperic.sigar.SigarProxyCache;
|
||||
|
||||
public class SigarProcess implements SigarProcessMBean {
|
||||
|
||||
private static final Long NOTIMPL = new Long(Sigar.FIELD_NOTIMPL);
|
||||
private Sigar sigarImpl;
|
||||
private SigarProxy sigar;
|
||||
private long pid = -1;
|
||||
|
||||
public SigarProcess() {
|
||||
this(new Sigar());
|
||||
}
|
||||
|
||||
public SigarProcess(Sigar sigar) {
|
||||
this.sigarImpl = sigar;
|
||||
this.sigarImpl = new Sigar();
|
||||
this.sigar = SigarProxyCache.newInstance(sigarImpl);
|
||||
}
|
||||
|
||||
public SigarProcess(SigarProxy sigar) {
|
||||
this.sigar = sigar;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.sigarImpl.close();
|
||||
if (this.sigarImpl != null) {
|
||||
this.sigarImpl.close();
|
||||
}
|
||||
}
|
||||
|
||||
private RuntimeException unexpectedError(String type,
|
||||
@@ -57,8 +63,7 @@ public class SigarProcess implements SigarProcessMBean {
|
||||
|
||||
private synchronized ProcMem getMem() {
|
||||
try {
|
||||
long pid = this.sigar.getPid();
|
||||
return this.sigar.getProcMem(pid);
|
||||
return this.sigar.getProcMem(getPid());
|
||||
} catch (SigarException e) {
|
||||
throw unexpectedError("Mem", e);
|
||||
}
|
||||
@@ -66,22 +71,40 @@ public class SigarProcess implements SigarProcessMBean {
|
||||
|
||||
private synchronized ProcCpu getCpu() {
|
||||
try {
|
||||
long pid = this.sigar.getPid();
|
||||
return this.sigar.getProcCpu(pid);
|
||||
return this.sigar.getProcCpu(getPid());
|
||||
} catch (SigarException e) {
|
||||
throw unexpectedError("Cpu", e);
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized ProcFd getFd() {
|
||||
try {
|
||||
long pid = this.sigar.getPid();
|
||||
return this.sigar.getProcFd(pid);
|
||||
|
||||
private synchronized ProcDiskIO getDiskIO() {
|
||||
try {
|
||||
return this.sigar.getProcDiskIO(getPid());
|
||||
} catch (SigarException e) {
|
||||
throw unexpectedError("Fd", e);
|
||||
}
|
||||
throw unexpectedError("DiskIO", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private synchronized ProcFd getFd() throws SigarException {
|
||||
return this.sigar.getProcFd(getPid());
|
||||
}
|
||||
|
||||
|
||||
public long getPid() {
|
||||
if (this.pid < 0) {
|
||||
return this.sigar.getPid();
|
||||
}
|
||||
else {
|
||||
return this.pid;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPid(long pid) {
|
||||
this.pid = pid;
|
||||
}
|
||||
|
||||
public Long getMemSize() {
|
||||
return new Long(getMem().getSize());
|
||||
}
|
||||
@@ -119,7 +142,11 @@ public class SigarProcess implements SigarProcessMBean {
|
||||
}
|
||||
|
||||
public Long getOpenFd() {
|
||||
return new Long(getFd().getTotal());
|
||||
try {
|
||||
return new Long(getFd().getTotal());
|
||||
} catch (SigarException e) {
|
||||
return NOTIMPL;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
@@ -132,4 +159,9 @@ public class SigarProcess implements SigarProcessMBean {
|
||||
System.out.println("TimeSys=" + proc.getTimeSys());
|
||||
System.out.println("OpenFd=" + proc.getOpenFd());
|
||||
}
|
||||
|
||||
public Double getBytesReadWriteTotal() {
|
||||
return new Double(getDiskIO().getBytesTotal());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,4 +44,6 @@ public interface SigarProcessMBean {
|
||||
public Double getCpuUsage();
|
||||
|
||||
public Long getOpenFd();
|
||||
|
||||
public Double getBytesReadWriteTotal();
|
||||
}
|
||||
@@ -133,11 +133,11 @@ public class TestFileInfo extends SigarTestCase {
|
||||
tmp.deleteOnExit();
|
||||
traceln("TMP=" + file);
|
||||
|
||||
try {
|
||||
// try {
|
||||
//stat() mtime is in seconds, this happens to quick to detect change.
|
||||
Thread.sleep(1000 * 1);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
//Thread.sleep(1000 * 1);
|
||||
// } catch (InterruptedException e) {
|
||||
// }
|
||||
|
||||
try {
|
||||
FileInfo info = sigar.getFileInfo(file);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package org.hyperic.sigar.test;
|
||||
|
||||
import org.hyperic.sigar.ProcDiskIO;
|
||||
import org.hyperic.sigar.Sigar;
|
||||
import org.hyperic.sigar.SigarException;
|
||||
|
||||
public class TestProcDiskIO extends SigarTestCase {
|
||||
|
||||
public TestProcDiskIO(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
|
||||
private void traceDiskIO(Sigar sigar, long pid) throws Exception {
|
||||
ProcDiskIO procDiskIO;
|
||||
|
||||
try {
|
||||
procDiskIO = sigar.getProcDiskIO(pid);
|
||||
} catch (SigarException e) {
|
||||
traceln("pid " + pid + ": " + e.getMessage());
|
||||
// throw e;
|
||||
return;
|
||||
}
|
||||
|
||||
long bytesRead = procDiskIO.getBytesRead();
|
||||
long bytesWritten = procDiskIO.getBytesWritten();
|
||||
long bytesTotal = procDiskIO.getBytesTotal();
|
||||
|
||||
traceln("Pid=" + pid);
|
||||
traceln("Bytes Read=" + Sigar.formatSize(bytesRead));
|
||||
traceln("Bytes Written=" + Sigar.formatSize(bytesWritten));
|
||||
traceln("Bytes Total=" + Sigar.formatSize(bytesTotal));
|
||||
|
||||
if (bytesRead != -1 && bytesWritten != -1 && bytesTotal != -1) {
|
||||
assertTrue("Bytes total should equal bytesRead + bytesWritten",
|
||||
(bytesTotal == bytesRead + bytesWritten));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCreate() throws Exception {
|
||||
Sigar sigar = getSigar();
|
||||
|
||||
boolean caughtException = false;
|
||||
try {
|
||||
sigar.getProcDiskIO(getInvalidPid());
|
||||
} catch (SigarException e) {
|
||||
caughtException = true;
|
||||
}
|
||||
assertTrue("Test on invalid PID should have thrown an exception.", caughtException);
|
||||
|
||||
long[] pids = sigar.getProcList();
|
||||
for (int i=0; i<pids.length; i++) {
|
||||
traceDiskIO(sigar, pids[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user