Package org.agrona

Class SystemUtil


  • public final class SystemUtil
    extends java.lang.Object
    Utilities for inspecting the system.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long MAX_G_VALUE  
      private static long MAX_K_VALUE  
      private static long MAX_M_VALUE  
      static java.lang.String NULL_PROPERTY_VALUE
      Value a System.getProperties() can be set to so that null will be returned as if the property was not set.
      private static java.lang.String OS_ARCH  
      private static java.lang.String OS_NAME  
      private static long PID  
      static long PID_NOT_FOUND
      PID value if a process id could not be determined.
      private static java.lang.String SUN_PID_PROP_NAME  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private SystemUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long getDurationInNanos​(java.lang.String propertyName, long defaultValue)
      Get a string representation of a time duration with an optional suffix of 's', 'ms', 'us', or 'ns' suffix to indicate seconds, milliseconds, microseconds, or nanoseconds respectively.
      static long getPid()
      Return the current process id from the OS.
      static java.lang.String getProperty​(java.lang.String propertyName)
      Get the value of a System.getProperty(String) with the exception that if the value is NULL_PROPERTY_VALUE then return null.
      static java.lang.String getProperty​(java.lang.String propertyName, java.lang.String defaultValue)
      Get the value of a System.getProperty(String, String) with the exception that if the value is NULL_PROPERTY_VALUE then return null, otherwise if the value is not set then return the default value.
      static int getSizeAsInt​(java.lang.String propertyName, int defaultValue)
      Get a size value as an int from a system property.
      static long getSizeAsLong​(java.lang.String propertyName, long defaultValue)
      Get a size value as a long from a system property.
      static boolean isDebuggerAttached()
      Is a debugger attached to the JVM?
      static boolean isLinux()
      Is the operating system likely to be Linux based on osName().
      static boolean isWindows()
      Is the operating system likely to be Windows based on osName().
      static boolean isX64Arch()
      Is the operating system architecture (osArch()) represents an x86-based system.
      (package private) static boolean isX64Arch​(java.lang.String arch)  
      private static void loadProperties​(PropertyAction propertyAction, java.io.InputStream in)  
      static void loadPropertiesFile​(java.lang.String filenameOrUrl)
      Load system properties from a given filename or url with default to PropertyAction.REPLACE.
      static void loadPropertiesFile​(PropertyAction propertyAction, java.lang.String filenameOrUrl)
      Load system properties from a given filename or url.
      static void loadPropertiesFiles​(java.lang.String... filenamesOrUrls)
      Load system properties from a given set of filenames or URLs with default to PropertyAction.REPLACE.
      static void loadPropertiesFiles​(PropertyAction propertyAction, java.lang.String... filenamesOrUrls)
      Load system properties from a given set of filenames or URLs.
      static java.lang.String osArch()
      Returns the name of the operating system architecture.
      static java.lang.String osName()
      Get the name of the operating system as a lower case String.
      static long parseDuration​(java.lang.String propertyName, java.lang.String propertyValue)
      Parse a string representation of a time duration with an optional suffix of 's', 'ms', 'us', or 'ns' to indicate seconds, milliseconds, microseconds, or nanoseconds respectively.
      static long parseSize​(java.lang.String propertyName, java.lang.String propertyValue)
      Parse a string representation of a value with optional suffix of 'g', 'm', and 'k' suffix to indicate gigabytes, megabytes, or kilobytes respectively.
      static java.lang.String threadDump()
      Get a formatted dump of all threads with associated state and stack traces.
      static void threadDump​(java.lang.StringBuilder sb)
      Write a formatted dump of all threads with associated state and stack traces to a provided StringBuilder.
      static java.lang.String tmpDirName()
      The system property for java.io.tmpdir and ensuring a File.separator is at the end.
      • Methods inherited from class java.lang.Object

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

      • PID_NOT_FOUND

        public static final long PID_NOT_FOUND
        PID value if a process id could not be determined. This value should be equal to a kernel only process id for the platform so that it does not indicate a real process id.
        See Also:
        Constant Field Values
      • NULL_PROPERTY_VALUE

        public static final java.lang.String NULL_PROPERTY_VALUE
        Value a System.getProperties() can be set to so that null will be returned as if the property was not set.
        See Also:
        getProperty(String), Constant Field Values
      • SUN_PID_PROP_NAME

        private static final java.lang.String SUN_PID_PROP_NAME
        See Also:
        Constant Field Values
      • OS_NAME

        private static final java.lang.String OS_NAME
      • OS_ARCH

        private static final java.lang.String OS_ARCH
      • PID

        private static final long PID
    • Constructor Detail

      • SystemUtil

        private SystemUtil()
    • Method Detail

      • osName

        public static java.lang.String osName()
        Get the name of the operating system as a lower case String.

        This is what is returned from System.getProperty("os.name").toLowerCase().

        Returns:
        the name of the operating system as a lower case String.
      • osArch

        public static java.lang.String osArch()
        Returns the name of the operating system architecture.

        This is the same a calling the System.getProperty("os.arch", "unknown").

        Returns:
        name of the operating system architecture or unknown.
      • getPid

        public static long getPid()
        Return the current process id from the OS.
        Returns:
        current process id or PID_NOT_FOUND if PID was not found.
        See Also:
        PID_NOT_FOUND
      • isWindows

        public static boolean isWindows()
        Is the operating system likely to be Windows based on osName().
        Returns:
        true if the operating system is likely to be Windows based on osName().
      • isLinux

        public static boolean isLinux()
        Is the operating system likely to be Linux based on osName().
        Returns:
        true if the operating system is likely to be Linux based on osName().
      • isX64Arch

        public static boolean isX64Arch()
        Is the operating system architecture (osArch()) represents an x86-based system.
        Returns:
        true if the operating system architecture represents an x86-based system.
      • isDebuggerAttached

        public static boolean isDebuggerAttached()
        Is a debugger attached to the JVM?
        Returns:
        true if attached otherwise false.
      • tmpDirName

        public static java.lang.String tmpDirName()
        The system property for java.io.tmpdir and ensuring a File.separator is at the end.
        Returns:
        tmp directory for the runtime.
      • threadDump

        public static java.lang.String threadDump()
        Get a formatted dump of all threads with associated state and stack traces.
        Returns:
        a formatted dump of all threads with associated state and stack traces.
      • threadDump

        public static void threadDump​(java.lang.StringBuilder sb)
        Write a formatted dump of all threads with associated state and stack traces to a provided StringBuilder.
        Parameters:
        sb - to write the thread dump to.
      • loadPropertiesFile

        public static void loadPropertiesFile​(java.lang.String filenameOrUrl)
        Load system properties from a given filename or url with default to PropertyAction.REPLACE.

        File is first searched for in resources using the system ClassLoader, then file system, then URL. All are loaded if multiples found.

        Parameters:
        filenameOrUrl - that holds properties.
      • loadPropertiesFile

        public static void loadPropertiesFile​(PropertyAction propertyAction,
                                              java.lang.String filenameOrUrl)
        Load system properties from a given filename or url.

        File is first searched for in resources using the system ClassLoader, then file system, then URL. All are loaded if multiples found.

        Parameters:
        propertyAction - to take with each loaded property.
        filenameOrUrl - that holds properties.
      • loadPropertiesFiles

        public static void loadPropertiesFiles​(java.lang.String... filenamesOrUrls)
        Load system properties from a given set of filenames or URLs with default to PropertyAction.REPLACE.
        Parameters:
        filenamesOrUrls - that holds properties.
        See Also:
        loadPropertiesFile(String)
      • loadPropertiesFiles

        public static void loadPropertiesFiles​(PropertyAction propertyAction,
                                               java.lang.String... filenamesOrUrls)
        Load system properties from a given set of filenames or URLs.
        Parameters:
        propertyAction - to take with each loaded property.
        filenamesOrUrls - that holds properties.
        See Also:
        loadPropertiesFile(String)
      • getProperty

        public static java.lang.String getProperty​(java.lang.String propertyName)
        Get the value of a System.getProperty(String) with the exception that if the value is NULL_PROPERTY_VALUE then return null.
        Parameters:
        propertyName - to get the value for.
        Returns:
        the value of a System.getProperty(String) with the exception that if the value is NULL_PROPERTY_VALUE then return null.
      • getProperty

        public static java.lang.String getProperty​(java.lang.String propertyName,
                                                   java.lang.String defaultValue)
        Get the value of a System.getProperty(String, String) with the exception that if the value is NULL_PROPERTY_VALUE then return null, otherwise if the value is not set then return the default value.
        Parameters:
        propertyName - to get the value for.
        defaultValue - to use if the property is not set.
        Returns:
        the value of a System.getProperty(String, String) with the exception that if the value is NULL_PROPERTY_VALUE then return null, otherwise if the value is not set then return the default value.
      • getSizeAsInt

        public static int getSizeAsInt​(java.lang.String propertyName,
                                       int defaultValue)
        Get a size value as an int from a system property. Supports a 'g', 'm', and 'k' suffix to indicate gigabytes, megabytes, or kilobytes respectively.
        Parameters:
        propertyName - to lookup.
        defaultValue - to be applied if the system property is not set.
        Returns:
        the int value.
        Throws:
        java.lang.NumberFormatException - if the value is out of range or mal-formatted.
      • getSizeAsLong

        public static long getSizeAsLong​(java.lang.String propertyName,
                                         long defaultValue)
        Get a size value as a long from a system property. Supports a 'g', 'm', and 'k' suffix to indicate gigabytes, megabytes, or kilobytes respectively.
        Parameters:
        propertyName - to lookup.
        defaultValue - to be applied if the system property is not set.
        Returns:
        the long value.
        Throws:
        java.lang.NumberFormatException - if the value is out of range or mal-formatted.
      • parseSize

        public static long parseSize​(java.lang.String propertyName,
                                     java.lang.String propertyValue)
        Parse a string representation of a value with optional suffix of 'g', 'm', and 'k' suffix to indicate gigabytes, megabytes, or kilobytes respectively.
        Parameters:
        propertyName - that associated with the size value.
        propertyValue - to be parsed.
        Returns:
        the long value.
        Throws:
        java.lang.NumberFormatException - if the value is out of range or mal-formatted.
      • getDurationInNanos

        public static long getDurationInNanos​(java.lang.String propertyName,
                                              long defaultValue)
        Get a string representation of a time duration with an optional suffix of 's', 'ms', 'us', or 'ns' suffix to indicate seconds, milliseconds, microseconds, or nanoseconds respectively.

        If the resulting duration is greater than Long.MAX_VALUE then Long.MAX_VALUE is used.

        Parameters:
        propertyName - associated with the duration value.
        defaultValue - to be used if the property is not present.
        Returns:
        the long value.
        Throws:
        java.lang.NumberFormatException - if the value is negative or malformed.
      • parseDuration

        public static long parseDuration​(java.lang.String propertyName,
                                         java.lang.String propertyValue)
        Parse a string representation of a time duration with an optional suffix of 's', 'ms', 'us', or 'ns' to indicate seconds, milliseconds, microseconds, or nanoseconds respectively.

        If the resulting duration is greater than Long.MAX_VALUE then Long.MAX_VALUE is used.

        Parameters:
        propertyName - associated with the duration value.
        propertyValue - to be parsed.
        Returns:
        the long value.
        Throws:
        java.lang.NumberFormatException - if the value is negative or malformed.
      • isX64Arch

        static boolean isX64Arch​(java.lang.String arch)
      • loadProperties

        private static void loadProperties​(PropertyAction propertyAction,
                                           java.io.InputStream in)
                                    throws java.io.IOException
        Throws:
        java.io.IOException