Class ProcessStat


  • @ThreadSafe
    public final class ProcessStat
    extends java.lang.Object
    Utility to read process statistics from /proc/[pid]/stat
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ProcessStat.PidStat
      Enum corresponding to the fields in the output of /proc/[pid]/stat
      static class  ProcessStat.PidStatM
      Enum corresponding to the fields in the output of /proc/[pid]/statm
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int PROC_PID_STAT_LENGTH
      Constant defining the number of integer values in /proc/pid/stat.
      private static java.util.regex.Pattern SOCKET  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ProcessStat()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.File[] getFileDescriptorFiles​(int pid)
      Gets an array of files in the /proc/{pid}/fd directory.
      static java.io.File[] getPidFiles()
      Gets an array of files in the /proc directory with only numeric digit filenames, corresponding to processes
      static java.util.Map<ProcessStat.PidStatM,​java.lang.Long> getPidStatM​(int pid)
      Reads the statistics in /proc/[pid]/statm and returns the results.
      static Triplet<java.lang.String,​java.lang.Character,​java.util.Map<ProcessStat.PidStat,​java.lang.Long>> getPidStats​(int pid)
      Reads the statistics in /proc/[pid]/stat and returns the results.
      static OSProcess.State getState​(char stateValue)
      Returns Enum STATE for the state value obtained from status file of any process/thread.
      static java.util.List<java.lang.Integer> getThreadIds​(int pid)
      Gets a List of thread ids for a process from the /proc/[pid]/task/ directory with only numeric digit filenames, corresponding to the threads.
      private static java.io.File[] listNumericFiles​(java.lang.String path)  
      static java.util.Map<java.lang.Long,​java.lang.Integer> querySocketToPidMap()
      Gets a map of sockets and their corresponding process ID
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SOCKET

        private static final java.util.regex.Pattern SOCKET
      • PROC_PID_STAT_LENGTH

        public static final int PROC_PID_STAT_LENGTH
        Constant defining the number of integer values in /proc/pid/stat. 2.6 Kernel has 44 elements, 3.3 has 47, and 3.5 has 52.
    • Constructor Detail

      • ProcessStat

        private ProcessStat()
    • Method Detail

      • getPidStats

        public static Triplet<java.lang.String,​java.lang.Character,​java.util.Map<ProcessStat.PidStat,​java.lang.Long>> getPidStats​(int pid)
        Reads the statistics in /proc/[pid]/stat and returns the results.
        Parameters:
        pid - The process ID for which to fetch stats
        Returns:
        A triplet containing the process name as the first element, a character representing the process state as the second element, and an EnumMap as the third element, where the numeric values in ProcessStat.PidStat are mapped to a Long value.

        If the process doesn't exist, returns null.

      • getPidStatM

        public static java.util.Map<ProcessStat.PidStatM,​java.lang.Long> getPidStatM​(int pid)
        Reads the statistics in /proc/[pid]/statm and returns the results.
        Parameters:
        pid - The process ID for which to fetch stats
        Returns:
        An EnumMap where the numeric values in ProcessStat.PidStatM are mapped to a Long value.

        If the process doesn't exist, returns null.

      • getFileDescriptorFiles

        public static java.io.File[] getFileDescriptorFiles​(int pid)
        Gets an array of files in the /proc/{pid}/fd directory.
        Parameters:
        pid - id of process to read file descriptors for
        Returns:
        An array of File objects representing opened file descriptors of the process
      • getPidFiles

        public static java.io.File[] getPidFiles()
        Gets an array of files in the /proc directory with only numeric digit filenames, corresponding to processes
        Returns:
        An array of File objects for the process files
      • querySocketToPidMap

        public static java.util.Map<java.lang.Long,​java.lang.Integer> querySocketToPidMap()
        Gets a map of sockets and their corresponding process ID
        Returns:
        a map with socket as the key and pid as the value
      • getThreadIds

        public static java.util.List<java.lang.Integer> getThreadIds​(int pid)
        Gets a List of thread ids for a process from the /proc/[pid]/task/ directory with only numeric digit filenames, corresponding to the threads.
        Parameters:
        pid - process id
        Returns:
        A list of thread id.
      • listNumericFiles

        private static java.io.File[] listNumericFiles​(java.lang.String path)
      • getState

        public static OSProcess.State getState​(char stateValue)
        Returns Enum STATE for the state value obtained from status file of any process/thread.
        Parameters:
        stateValue - state value from the status file
        Returns:
        OSProcess.State