Class AbstractOSProcess
- All Implemented Interfaces:
OSProcess
- Direct Known Subclasses:
AbstractProcOSProcess, BsdOSProcess, LinuxOSProcess, MacOSProcess, WindowsOSProcess
-
Nested Class Summary
Nested classes/interfaces inherited from interface OSProcess
OSProcess.StateModifier and TypeInterfaceDescriptionstatic enumProcess and Thread Execution States -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longprotected longprotected longprotected Stringprotected intprotected Stringprotected intprotected longprotected OSProcess.Stateprotected intprotected longprotected longprotected long -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractOSProcess(int pid) Creates an AbstractOSProcess for the given process ID. -
Method Summary
Modifier and TypeMethodDescriptionlongGets the bytes read by the process.longGets the bytes written by the process.longGets kernel/system (privileged) time used by the process.getName()Gets the name of the process, often the executable program.intGets the process ID of this process's parent.getPath()Gets the full filesystem path of the executing process.intGets the priority of this process.doublegetProcessCpuLoadBetweenTicks(OSProcess priorSnapshot) Gets CPU usage of this process since a previous snapshot of the same process, provided as a parameter.doubleGets cumulative CPU usage of this process.intGets the process ID.longGets the process start time.getState()Gets the process state.intGets the number of threads being executed by this process.longGets up time / elapsed time since the process started.longGets user time used by the process.longGets the Virtual Memory Size (VSZ).toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface OSProcess
getAffinityMask, getArguments, getBitness, getCommandLine, getContextSwitches, getCurrentWorkingDirectory, getEnvironmentVariables, getGroup, getGroupID, getHardOpenFileLimit, getInvoluntaryContextSwitches, getMajorFaults, getMinorFaults, getOpenFiles, getPrivateResidentMemory, getResidentMemory, getSoftOpenFileLimit, getThreadDetails, getUser, getUserID, getVoluntaryContextSwitches, updateAttributesModifier and TypeMethodDescriptionlongGets the process affinity mask for this process.Makes a best effort attempt to get a list of the the command-line arguments of the process.intAttempts to get the bitness (32 or 64) of the process.Gets the process command line used to start the process, including arguments if available to be determined.default longA snapshot of the context switches the process has done, equal to the sum ofOSProcess.getVoluntaryContextSwitches()andOSProcess.getInvoluntaryContextSwitches()on platforms that provide the split.Makes a best effort attempt to obtain the current working directory for the process.Makes a best effort attempt to obtain the environment variables of the process.getGroup()Gets the group under which the process is executing.Gets the group id under which the process is executing.longGets the hard limit for open file handles (or network connections) that belong to the given process.default longThe number of involuntary context switches the process has made.default longGets the number of major (hard) faults the process has made which have required loading a memory page from disk.default longGets the number of minor (soft) faults the process has made which have not required loading a memory page from disk.longGets the number of open file handles (or network connections) that belongs to the process.default longReturns an estimate of the portion of physical memory (RAM) uniquely attributed to this process.longReturns the total amount of physical memory (RAM) currently mapped to the process's address space.longGets the soft limit for open file handles (or network connections) of the given process.Retrieves the threads of the process and their details.getUser()Gets the user name of the process owner.Gets the user id of the process owner.default longThe number of voluntary context switches the process has made.booleanAttempts to update process attributes.
-
Field Details
-
name
-
path
-
state
-
parentProcessID
protected int parentProcessID -
threadCount
protected int threadCount -
priority
protected int priority -
virtualSize
protected long virtualSize -
kernelTime
protected long kernelTime -
userTime
protected long userTime -
startTime
protected long startTime -
upTime
protected long upTime -
bytesRead
protected long bytesRead -
bytesWritten
protected long bytesWritten
-
-
Constructor Details
-
AbstractOSProcess
protected AbstractOSProcess(int pid) Creates an AbstractOSProcess for the given process ID.- Parameters:
pid- the process ID
-
-
Method Details
-
getProcessID
public int getProcessID()Description copied from interface:OSProcessGets the process ID.While this is a 32-bit value, it is unsigned on Windows and in extremely rare circumstances may return a negative value.
- Specified by:
getProcessIDin interfaceOSProcess- Returns:
- the processID.
-
getName
-
getPath
-
getState
Description copied from interface:OSProcessGets the process state. -
getParentProcessID
public int getParentProcessID()Description copied from interface:OSProcessGets the process ID of this process's parent.- Specified by:
getParentProcessIDin interfaceOSProcess- Returns:
- the parentProcessID, if any; 0 otherwise.
-
getThreadCount
public int getThreadCount()Description copied from interface:OSProcessGets the number of threads being executed by this process. More information is available usingOSProcess.getThreadDetails().- Specified by:
getThreadCountin interfaceOSProcess- Returns:
- the number of threads in this process.
-
getPriority
public int getPriority()Description copied from interface:OSProcessGets the priority of this process.For Linux and Unix, priority is a value in the range -20 to 19 (20 on some systems). The default priority is 0; lower priorities cause more favorable scheduling.
For Windows, priority values can range from 0 (lowest priority) to 31 (highest priority).
macOS has 128 priority levels, ranging from 0 (lowest priority) to 127 (highest priority). They are divided into several major bands: 0 through 51 are the normal levels; the default priority is 31. 52 through 79 are the highest priority regular threads; 80 through 95 are for kernel mode threads; and 96 through 127 correspond to real-time threads, which are treated differently than other threads by the scheduler.
- Specified by:
getPriorityin interfaceOSProcess- Returns:
- the priority of this process.
-
getVirtualSize
public long getVirtualSize()Description copied from interface:OSProcessGets the Virtual Memory Size (VSZ). Includes all memory that the process can access, including memory that is swapped out and memory that is from shared libraries.- Specified by:
getVirtualSizein interfaceOSProcess- Returns:
- the Virtual Memory Size
-
getKernelTime
public long getKernelTime()Description copied from interface:OSProcessGets kernel/system (privileged) time used by the process.- Specified by:
getKernelTimein interfaceOSProcess- Returns:
- the number of milliseconds the process has executed in kernel/system mode.
-
getUserTime
public long getUserTime()Description copied from interface:OSProcessGets user time used by the process.- Specified by:
getUserTimein interfaceOSProcess- Returns:
- the number of milliseconds the process has executed in user mode.
-
getUpTime
-
getStartTime
public long getStartTime()Description copied from interface:OSProcessGets the process start time.- Specified by:
getStartTimein interfaceOSProcess- Returns:
- the start time of the process in number of milliseconds since January 1, 1970 UTC.
-
getBytesRead
public long getBytesRead()Description copied from interface:OSProcessGets the bytes read by the process. This includes all I/O activity generated by the process to include file, network, and device I/Os.On Solaris, includes both bytes read and written.
- Specified by:
getBytesReadin interfaceOSProcess- Returns:
- the number of bytes the process has read.
-
getBytesWritten
public long getBytesWritten()Description copied from interface:OSProcessGets the bytes written by the process. This includes all I/O activity generated by the process to include file, network, and device I/Os.On Solaris, all IO bytes are included read bytes so this value is 0.
- Specified by:
getBytesWrittenin interfaceOSProcess- Returns:
- the number of bytes the process has written to disk.
-
getProcessCpuLoadCumulative
public double getProcessCpuLoadCumulative()Description copied from interface:OSProcessGets cumulative CPU usage of this process.This calculation sums CPU ticks across all processors and may exceed 100% for multi-threaded processes. This is consistent with the cumulative CPU presented by the "top" command on Linux/Unix machines.
- Specified by:
getProcessCpuLoadCumulativein interfaceOSProcess- Returns:
- The proportion of up time that the process was executing in kernel or user mode.
-
getProcessCpuLoadBetweenTicks
Description copied from interface:OSProcessGets CPU usage of this process since a previous snapshot of the same process, provided as a parameter.This calculation sums CPU ticks across all processors and may exceed 100% for multi-threaded processes. This is consistent with process usage calculations on Linux/Unix machines, but should be divided by the number of logical processors to match the value displayed by the Windows Task Manager.
The accuracy of this calculation is dependent on both the number of threads on which the process is executing, and the precision of the Operating System's tick counters. A polling interval of at least a few seconds is recommended.
Usage example:
Map<Integer, OSProcess> priorSnapshot = new HashMap<>(); while (monitoring) { for (OSProcess p : os.getProcesses(null, null, 0)) { double cpu = p.getProcessCpuLoadBetweenTicks(priorSnapshot.get(p.getProcessID())); priorSnapshot.put(p.getProcessID(), p); } Thread.sleep(2000); }- Specified by:
getProcessCpuLoadBetweenTicksin interfaceOSProcess- Parameters:
priorSnapshot- AnOSProcessobject containing statistics for this same process collected at a prior point in time. May be null.- Returns:
- If the prior snapshot is for the same process at a prior point in time, the proportion of elapsed up time between the current process snapshot and the previous one that the process was executing in kernel or user mode. Returns cumulative load otherwise.
-
toString
-