Class MacOSProcess

java.lang.Object
oshi.software.common.AbstractOSProcess
oshi.software.common.os.mac.MacOSProcess
All Implemented Interfaces:
OSProcess

@ThreadSafe public abstract class MacOSProcess extends AbstractOSProcess
Abstract base shared by the macOS OSProcess implementations (JNA and FFM). Holds the field storage, the trivial accessors, the command-line/argument/environment memoization, thread enumeration, affinity mask, and the pbi_status state mapping. The native attribute reads (proc_pidinfo, the KERN_PROCARGS2 sysctl, the getrlimit file limits, and the logical-processor sysctl) are provided by the per-backend subclasses.
  • Field Details

    • LOG_MAC_SYSCTL_WARNING

      protected static final boolean LOG_MAC_SYSCTL_WARNING
    • MAC_RLIMIT_NOFILE

      protected static final int MAC_RLIMIT_NOFILE
      See Also:
    • P_LP64

      protected static final int P_LP64
      See Also:
    • majorVersion

      protected final int majorVersion
    • minorVersion

      protected final int minorVersion
    • os

      protected final MacOperatingSystem os
    • currentWorkingDirectory

      protected String currentWorkingDirectory
    • user

      protected String user
    • userID

      protected String userID
    • group

      protected String group
    • groupID

      protected String groupID
    • residentSetSize

      protected long residentSetSize
    • memoryFootprint

      protected long memoryFootprint
    • openFiles

      protected long openFiles
    • bitness

      protected int bitness
    • minorFaults

      protected long minorFaults
    • majorFaults

      protected long majorFaults
    • contextSwitches

      protected long contextSwitches
    • voluntaryContextSwitches

      protected long voluntaryContextSwitches
    • involuntaryContextSwitches

      protected long involuntaryContextSwitches
  • Constructor Details

    • MacOSProcess

      protected MacOSProcess(int pid, int major, int minor, MacOperatingSystem os)
  • Method Details

    • getCommandLine

      public String getCommandLine()
      Description copied from interface: OSProcess
      Gets the process command line used to start the process, including arguments if available to be determined. This method generally returns the same information as OSProcess.getArguments() in a more user-readable format, and is more robust to non-elevated access.

      The format of this string is platform-dependent, may be truncated, and may require the end user to parse the result. Users should generally prefer OSProcess.getArguments() which already parses the results, and use this method as a backup.

      On AIX and Solaris, the string may be truncated to 80 characters if there was insufficient permission to read the process memory.

      On Windows, attempts to retrieve the value from process memory, which requires that the process be owned by the same user as the executing process, or elevated permissions, and additionally requires the target process to have the same bitness (e.g., this will fail on a 32-bit process if queried by 64-bit and vice versa). If reading process memory fails, by default, performs a single WMI query for this process, with some latency. If this method will be frequently called for multiple processes, see the configuration file to enable a batch query mode to improve performance via caching, or configure the option via GlobalConfig before instantiating any OSProcess object.

      Returns:
      the process command line.
    • getArguments

      public List<String> getArguments()
      Description copied from interface: OSProcess
      Makes a best effort attempt to get a list of the the command-line arguments of the process. Returns the same information as OSProcess.getCommandLine() but parsed to a list. May require elevated permissions or same-user ownership.
      Returns:
      A list of Strings representing the arguments. May return an empty list if there was a failure (for example, because the process is already dead or permission was denied).
    • getEnvironmentVariables

      public Map<String,String> getEnvironmentVariables()
      Description copied from interface: OSProcess
      Makes a best effort attempt to obtain the environment variables of the process. May require elevated permissions or same-user ownership.
      Returns:
      A map representing the environment variables and their values. May return an empty map if there was a failure (for example, because the process is already dead or permission was denied).
    • getCurrentWorkingDirectory

      public String getCurrentWorkingDirectory()
      Description copied from interface: OSProcess
      Makes a best effort attempt to obtain the current working directory for the process.
      Returns:
      the process current working directory.
    • getUser

      public String getUser()
      Description copied from interface: OSProcess
      Gets the user name of the process owner.
      Returns:
      the user name. On Windows systems, also returns the domain prepended to the username.
    • getUserID

      public String getUserID()
      Description copied from interface: OSProcess
      Gets the user id of the process owner.
      Returns:
      the userID. On Windows systems, returns the Security ID (SID)
    • getGroup

      public String getGroup()
      Description copied from interface: OSProcess
      Gets the group under which the process is executing.

      On Windows systems, populating this value for processes other than the current user requires administrative privileges (and still may fail for some system processes) and can incur significant latency. When successful, returns a the default primary group with access to this process, corresponding to the SID in OSProcess.getGroupID().

      Returns:
      the group.
    • getGroupID

      public String getGroupID()
      Description copied from interface: OSProcess
      Gets the group id under which the process is executing.

      On Windows systems, populating this value for processes other than the current user requires administrative privileges (and still may fail for some system processes) and can incur significant latency. When successful, returns the default primary group SID with access to this process, corresponding to the name in OSProcess.getGroup().

      Returns:
      the groupID.
    • getThreadDetails

      public List<OSThread> getThreadDetails()
      Description copied from interface: OSProcess
      Retrieves the threads of the process and their details.

      The amount of returned information is operating-system dependent and may incur some latency.

      Returns:
      a list of threads
    • getResidentMemory

      public long getResidentMemory()
      Description copied from interface: OSProcess
      Returns the total amount of physical memory (RAM) currently mapped to the process's address space. This value represents the Resident Set Size (RSS) and includes both private memory and memory shared with other processes (such as shared libraries). This aligns with the reporting behavior of standard command-line utilities like ps and top.

      On Linux, returns the RSS value from /proc/[pid]/stat, which may be inaccurate because of a kernel-internal scalability optimization. If accurate values are required, read /proc/[pid]/smaps using FileUtil#getKeyValueMapFromFile(String, String).

      Returns:
      The resident set size in bytes.
    • getPrivateResidentMemory

      public long getPrivateResidentMemory()
      Description copied from interface: OSProcess
      Returns an estimate of the portion of physical memory (RAM) uniquely attributed to this process. This metric excludes shared libraries and other memory segments mapped by multiple processes, providing a more accurate representation of the process's individual impact on system resources. This value aligns with the primary "Memory" column in graphical system monitors, such as Windows Task Manager, macOS Activity Monitor, and the GNOME System Monitor.
      Returns:
      The private or "footprint" resident memory in bytes if available. Defaults to OSProcess.getResidentMemory() otherwise.
    • getOpenFiles

      public long getOpenFiles()
      Description copied from interface: OSProcess
      Gets the number of open file handles (or network connections) that belongs to the process.

      On FreeBSD and Solaris, this value is only populated if information for a single process id is requested.

      Returns:
      open files or -1 if unknown or not supported
    • getBitness

      public int getBitness()
      Description copied from interface: OSProcess
      Attempts to get the bitness (32 or 64) of the process.
      Returns:
      The bitness, if able to be determined, 0 otherwise.
    • getAffinityMask

      public long getAffinityMask()
      Description copied from interface: OSProcess
      Gets the process affinity mask for this process.

      On Windows systems with more than 64 processors, if the threads of the calling process are in a single processor group, returns the process affinity mask for that group (which may be zero if the specified process is running in a different group). If the calling process contains threads in multiple groups, returns zero.

      Because macOS does not export interfaces that identify processors or control thread placement, explicit thread to processor binding is not supported and this method will return a bitmask of all logical processors.

      If the Operating System fails to retrieve an affinity mask (e.g., the process has terminated), returns zero.

      Returns:
      a bit vector in which each bit represents the processors that a process is allowed to run on.
    • getMinorFaults

      public long getMinorFaults()
      Description copied from interface: OSProcess
      Gets the number of minor (soft) faults the process has made which have not required loading a memory page from disk. Sometimes called reclaims.

      On Windows, this includes the total of major and minor faults.

      Not available on AIX.

      Returns:
      minor page faults (reclaims).
    • getMajorFaults

      public long getMajorFaults()
      Description copied from interface: OSProcess
      Gets the number of major (hard) faults the process has made which have required loading a memory page from disk.

      Windows does not distinguish major and minor faults at the process level, so this value returns 0 and major faults are included in OSProcess.getMinorFaults().

      Not available on AIX.

      Returns:
      major page faults.
    • getContextSwitches

      public long getContextSwitches()
      Description copied from interface: OSProcess
      A snapshot of the context switches the process has done, equal to the sum of OSProcess.getVoluntaryContextSwitches() and OSProcess.getInvoluntaryContextSwitches() on platforms that provide the split. On Windows, this returns the total from performance counters but the split is unavailable (returns 0). On macOS for non-current processes, this returns the combined total from the kernel but the split is unavailable.

      For the current process on supported POSIX platforms, this aggregates across all threads via getrusage(RUSAGE_SELF). For other processes, platform-specific sources are used.

      Not available on AIX (returns 0).

      Returns:
      sum of both voluntary and involuntary context switches if available, 0 otherwise.
    • getVoluntaryContextSwitches

      public long getVoluntaryContextSwitches()
      Description copied from interface: OSProcess
      The number of voluntary context switches the process has made. A voluntary context switch occurs when a process gives up the CPU before its time slice expires (e.g., waiting for I/O).

      For the current process, getrusage(RUSAGE_SELF) is used on supported POSIX platforms, which aggregates across all threads. For other processes, platform-specific sources are used (/proc/[pid]/status on Linux, ps on FreeBSD/OpenBSD, /proc/[pid]/usage on Solaris). On macOS, the split is only available for the current process; for other processes this returns 0.

      Returns:
      voluntary context switches if available, 0 otherwise.
    • getInvoluntaryContextSwitches

      public long getInvoluntaryContextSwitches()
      Description copied from interface: OSProcess
      The number of involuntary context switches the process has made. An involuntary context switch occurs when the scheduler preempts the process (e.g., time slice expired).

      For the current process, getrusage(RUSAGE_SELF) is used on supported POSIX platforms, which aggregates across all threads. For other processes, platform-specific sources are used (/proc/[pid]/status on Linux, ps on FreeBSD/OpenBSD, /proc/[pid]/usage on Solaris). On macOS, the split is only available for the current process; for other processes this returns 0.

      Returns:
      involuntary context switches if available, 0 otherwise.
    • stateFromStatus

      protected static OSProcess.State stateFromStatus(int status)
      Maps a macOS pbi_status value to an OSProcess.State.
      Parameters:
      status - the pbi_status value
      Returns:
      the corresponding process state
    • queryArgsAndEnvironment

      protected abstract Pair<List<String>, Map<String,String>> queryArgsAndEnvironment()
      Queries this process's arguments and environment via the backend-specific KERN_PROCARGS2 sysctl.
      Returns:
      a pair of the argument list and the environment map
    • queryLogicalProcessorCount

      protected abstract int queryLogicalProcessorCount()
      Returns the number of logical processors via the backend-specific hw.logicalcpu sysctl.
      Returns:
      the logical processor count