Class AbstractOSProcess

java.lang.Object
oshi.software.common.AbstractOSProcess
All Implemented Interfaces:
OSProcess
Direct Known Subclasses:
AbstractProcOSProcess, BsdOSProcess, LinuxOSProcess, MacOSProcess, WindowsOSProcess

@ThreadSafe public abstract class AbstractOSProcess extends Object implements OSProcess
A process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
  • Field Details

    • name

      protected String name
    • path

      protected String path
    • state

      protected OSProcess.State 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: OSProcess
      Gets 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:
      getProcessID in interface OSProcess
      Returns:
      the processID.
    • getName

      public String getName()
      Description copied from interface: OSProcess
      Gets the name of the process, often the executable program.
      Specified by:
      getName in interface OSProcess
      Returns:
      the name of the process.
    • getPath

      public String getPath()
      Description copied from interface: OSProcess
      Gets the full filesystem path of the executing process.
      Specified by:
      getPath in interface OSProcess
      Returns:
      the full path of the executing process.
    • getState

      public OSProcess.State getState()
      Description copied from interface: OSProcess
      Gets the process state.
      Specified by:
      getState in interface OSProcess
      Returns:
      the execution state of the process.
    • getParentProcessID

      public int getParentProcessID()
      Description copied from interface: OSProcess
      Gets the process ID of this process's parent.
      Specified by:
      getParentProcessID in interface OSProcess
      Returns:
      the parentProcessID, if any; 0 otherwise.
    • getThreadCount

      public int getThreadCount()
      Description copied from interface: OSProcess
      Gets the number of threads being executed by this process. More information is available using OSProcess.getThreadDetails().
      Specified by:
      getThreadCount in interface OSProcess
      Returns:
      the number of threads in this process.
    • getPriority

      public int getPriority()
      Description copied from interface: OSProcess
      Gets 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:
      getPriority in interface OSProcess
      Returns:
      the priority of this process.
    • getVirtualSize

      public long getVirtualSize()
      Description copied from interface: OSProcess
      Gets 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:
      getVirtualSize in interface OSProcess
      Returns:
      the Virtual Memory Size
    • getKernelTime

      public long getKernelTime()
      Description copied from interface: OSProcess
      Gets kernel/system (privileged) time used by the process.
      Specified by:
      getKernelTime in interface OSProcess
      Returns:
      the number of milliseconds the process has executed in kernel/system mode.
    • getUserTime

      public long getUserTime()
      Description copied from interface: OSProcess
      Gets user time used by the process.
      Specified by:
      getUserTime in interface OSProcess
      Returns:
      the number of milliseconds the process has executed in user mode.
    • getUpTime

      public long getUpTime()
      Description copied from interface: OSProcess
      Gets up time / elapsed time since the process started.
      Specified by:
      getUpTime in interface OSProcess
      Returns:
      the number of milliseconds since the process started.
    • getStartTime

      public long getStartTime()
      Description copied from interface: OSProcess
      Gets the process start time.
      Specified by:
      getStartTime in interface OSProcess
      Returns:
      the start time of the process in number of milliseconds since January 1, 1970 UTC.
    • getBytesRead

      public long getBytesRead()
      Description copied from interface: OSProcess
      Gets 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:
      getBytesRead in interface OSProcess
      Returns:
      the number of bytes the process has read.
    • getBytesWritten

      public long getBytesWritten()
      Description copied from interface: OSProcess
      Gets 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:
      getBytesWritten in interface OSProcess
      Returns:
      the number of bytes the process has written to disk.
    • getProcessCpuLoadCumulative

      public double getProcessCpuLoadCumulative()
      Description copied from interface: OSProcess
      Gets 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:
      getProcessCpuLoadCumulative in interface OSProcess
      Returns:
      The proportion of up time that the process was executing in kernel or user mode.
    • getProcessCpuLoadBetweenTicks

      public double getProcessCpuLoadBetweenTicks(OSProcess priorSnapshot)
      Description copied from interface: OSProcess
      Gets 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:
      getProcessCpuLoadBetweenTicks in interface OSProcess
      Parameters:
      priorSnapshot - An OSProcess object 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

      public String toString()
      Overrides:
      toString in class Object