public class JavaSysMon extends java.lang.Object implements Monitor
When instantiated for the first time, JavaSysMon will discover which operating system it is running on and attempt to load the appropriate OS-specific extensions. If JavaSysMon doesn't support the OS you're running on, all calls to the API will return null or zero values. Probably the best one to test is osName.
You can run JavaSysMon directly as a jar file, using the command "java -jar javasysmon.jar", in which case it will display output similar to the UNIX "top" command. You can optionally specify a process id as an argument, in which case JavaSysMon will attempt to kill the process.
Constructor and Description |
---|
JavaSysMon()
Creates a new JavaSysMon object through which to access
the JavaSysMon API.
|
Modifier and Type | Method and Description |
---|---|
long |
cpuFrequencyInHz()
Get the CPU frequency in Hz
|
CpuTimes |
cpuTimes()
Gets a snapshot which contains the total amount
of time the CPU has spent in user mode, kernel mode,
and idle.
|
int |
currentPid()
Gets the pid of the process that is calling this method
(assuming it is running in the same process).
|
void |
infanticide()
Attempts to kill all the descendants of the currently running process.
|
void |
killProcess(int pid)
Attempts to kill the process identified by the integer id supplied.
|
void |
killProcessTree(int pid,
boolean descendantsOnly)
Kills the process tree starting at the process identified by pid.
|
static void |
main(java.lang.String[] params)
This is the main entry point when running the jar directly.
|
int |
numCpus()
Get the number of CPU cores.
|
java.lang.String |
osName()
Get the operating system name.
|
MemoryStats |
physical()
Gets the physical memory installed, and the amount free.
|
ProcessInfo[] |
processTable()
Get the current process table.
|
OsProcess |
processTree()
Gets the current process table in the form of a process tree.
|
static void |
setMonitor(Monitor myMonitor)
Allows you to register your own implementation of
Monitor . |
boolean |
supportedPlatform()
Whether or not JavaSysMon is running on a supported platform.
|
MemoryStats |
swap()
Gets the amount of swap available to the operating system,
and the amount that is free.
|
long |
uptimeInSeconds()
How long the system has been up in seconds.
|
void |
visitProcessTree(int pid,
ProcessVisitor processVisitor)
Allows you to visit the process tree, starting at the node identified by pid.
|
public JavaSysMon()
public static void setMonitor(Monitor myMonitor)
Monitor
.myMonitor
- An implementation of the Monitor interface that all API calls will be delegated topublic static void main(java.lang.String[] params) throws java.lang.Exception
java.lang.Exception
public boolean supportedPlatform()
true
if the platform is supported,
false
if it isn't.public java.lang.String osName()
public int numCpus()
public long cpuFrequencyInHz()
cpuFrequencyInHz
in interface Monitor
public long uptimeInSeconds()
uptimeInSeconds
in interface Monitor
public int currentPid()
currentPid
in interface Monitor
public CpuTimes cpuTimes()
CpuTimes.getCpuUsage(com.jezhumble.javasysmon.CpuTimes)
public MemoryStats physical()
public MemoryStats swap()
public ProcessInfo[] processTable()
processTree()
instead.processTable
in interface Monitor
public OsProcess processTree()
public void killProcess(int pid)
killProcess
in interface Monitor
pid
- The id of the process to killpublic void visitProcessTree(int pid, ProcessVisitor processVisitor)
pid
- The identifier of the node to start visitingprocessVisitor
- The visitorpublic void killProcessTree(int pid, boolean descendantsOnly)
This method uses visitProcessTree(int, com.jezhumble.javasysmon.ProcessVisitor)
.
pid
- The identifier of the process at which to start killing the tree.descendantsOnly
- Whether or not to kill the process you start at,
or only its descendantspublic void infanticide()
Copyright © 2009 ThoughtWorks. All Rights Reserved.