Class OS


  • public final class OS
    extends java.lang.Object
    Utility class to gather statistics about the running application.
    Initially this class was created to collect information about a long-running server application that should be reported via a status-service.
    Most features are only supported by unix operating systems. But for some values a fallback can be used, e.g. we can use the system-time instead of real thread-time.
    Needs to be extends for a more general use.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  OS.Info
      Utility class to collect all values at once.
      A bit too specific for general usage, but good for demonstration.
    • Constructor Summary

      Constructors 
      Constructor Description
      OS()  
    • Method Summary

      Modifier and Type Method Description
      static long getMaxOpenFileHandles()
      Gets the maximal number of open files of this process - if possible.
      static long getOpenFileHandles()
      Gets the number of open files of this process - if possible.
      Possible means running inside some "unix".
      static java.lang.String getOSName()  
      static double getSystemLoadAverage()
      Gets the current CPU Load average - if possible.
      Is this is not possible, -1 is returned.
      The value depends on implementation in the OS.
      static long getThreadExecutionTimeNS()
      Get execution time of current thread, if possible.
      If this is not possible or enabled, system-wide time will be used.
      static void init()
      Initialize this tool class.
      static boolean isThreadTimeMeasurementUsed()
      Returns true if getThreadExecutionTimeNS is based on real thread-time.
      • Methods inherited from class java.lang.Object

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

      • OS

        public OS()
    • Method Detail

      • getOpenFileHandles

        public static long getOpenFileHandles()
        Gets the number of open files of this process - if possible.
        Possible means running inside some "unix".
        Returns:
        the amount of open file handles or -1 if value is not available.
      • getMaxOpenFileHandles

        public static long getMaxOpenFileHandles()
        Gets the maximal number of open files of this process - if possible.
        Possible means running, that we are running inside some "unix".
        Returns:
        The maximal amount of open file handles or -1 if value is not available.
      • getSystemLoadAverage

        public static double getSystemLoadAverage()
        Gets the current CPU Load average - if possible.
        Is this is not possible, -1 is returned.
        The value depends on implementation in the OS.
        Returns:
        The average system load.
      • getOSName

        public static java.lang.String getOSName()
      • getThreadExecutionTimeNS

        public static long getThreadExecutionTimeNS()
        Get execution time of current thread, if possible.
        If this is not possible or enabled, system-wide time will be used.
        Returns:
        the elapsed time in nanoseconds.
      • isThreadTimeMeasurementUsed

        public static boolean isThreadTimeMeasurementUsed()
        Returns true if getThreadExecutionTimeNS is based on real thread-time.
        Returns:
        True if thread-time-measurement is used.
      • init

        public static void init()
        Initialize this tool class. Should be called once before usage, because otherwise it will not work ;)