Package org.agrona
Class SystemUtil
- java.lang.Object
-
- org.agrona.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 aSystem.getProperties()
can be set to so thatnull
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 aSystem.getProperty(String)
with the exception that if the value isNULL_PROPERTY_VALUE
then returnnull
.static java.lang.String
getProperty(java.lang.String propertyName, java.lang.String defaultValue)
Get the value of aSystem.getProperty(String, String)
with the exception that if the value isNULL_PROPERTY_VALUE
then returnnull
, 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 onosName()
.static boolean
isWindows()
Is the operating system likely to be Windows based onosName()
.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 toPropertyAction.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 toPropertyAction.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 providedStringBuilder
.static java.lang.String
tmpDirName()
The system property forjava.io.tmpdir
and ensuring aFile.separator
is at the end.
-
-
-
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 aSystem.getProperties()
can be set to so thatnull
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
-
MAX_G_VALUE
private static final long MAX_G_VALUE
- See Also:
- Constant Field Values
-
MAX_M_VALUE
private static final long MAX_M_VALUE
- See Also:
- Constant Field Values
-
MAX_K_VALUE
private static final long MAX_K_VALUE
- 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
-
-
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 onosName()
.- Returns:
true
if the operating system is likely to be Windows based onosName()
.
-
isLinux
public static boolean isLinux()
Is the operating system likely to be Linux based onosName()
.- Returns:
true
if the operating system is likely to be Linux based onosName()
.
-
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 forjava.io.tmpdir
and ensuring aFile.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 providedStringBuilder
.- 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 toPropertyAction.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 toPropertyAction.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 aSystem.getProperty(String)
with the exception that if the value isNULL_PROPERTY_VALUE
then returnnull
.- Parameters:
propertyName
- to get the value for.- Returns:
- the value of a
System.getProperty(String)
with the exception that if the value isNULL_PROPERTY_VALUE
then returnnull
.
-
getProperty
public static java.lang.String getProperty(java.lang.String propertyName, java.lang.String defaultValue)
Get the value of aSystem.getProperty(String, String)
with the exception that if the value isNULL_PROPERTY_VALUE
then returnnull
, 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 isNULL_PROPERTY_VALUE
then returnnull
, 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
thenLong.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
thenLong.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
-
-