@ -53,6 +53,12 @@ public class OSUtils {
private static final SystemInfo SI = new SystemInfo ( ) ;
private static final SystemInfo SI = new SystemInfo ( ) ;
public static final String TWO_DECIMAL = "0.00" ;
public static final String TWO_DECIMAL = "0.00" ;
/ * *
* return - 1 when the function can not get hardware env info
* e . g { @link OSUtils # loadAverage ( ) } { @link OSUtils # cpuUsage ( ) }
* /
public static final double NEGATIVE_ONE = - 1 ;
private static HardwareAbstractionLayer hal = SI . getHardware ( ) ;
private static HardwareAbstractionLayer hal = SI . getHardware ( ) ;
private OSUtils ( ) { }
private OSUtils ( ) { }
@ -118,9 +124,11 @@ public class OSUtils {
* /
* /
public static double loadAverage ( ) {
public static double loadAverage ( ) {
double loadAverage = hal . getProcessor ( ) . getSystemLoadAverage ( ) ;
double loadAverage = hal . getProcessor ( ) . getSystemLoadAverage ( ) ;
if ( Double . isNaN ( loadAverage ) ) {
return NEGATIVE_ONE ;
}
DecimalFormat df = new DecimalFormat ( TWO_DECIMAL ) ;
DecimalFormat df = new DecimalFormat ( TWO_DECIMAL ) ;
df . setRoundingMode ( RoundingMode . HALF_UP ) ;
df . setRoundingMode ( RoundingMode . HALF_UP ) ;
return Double . parseDouble ( df . format ( loadAverage ) ) ;
return Double . parseDouble ( df . format ( loadAverage ) ) ;
}
}
@ -133,10 +141,12 @@ public class OSUtils {
public static double cpuUsage ( ) {
public static double cpuUsage ( ) {
CentralProcessor processor = hal . getProcessor ( ) ;
CentralProcessor processor = hal . getProcessor ( ) ;
double cpuUsage = processor . getSystemCpuLoad ( ) ;
double cpuUsage = processor . getSystemCpuLoad ( ) ;
if ( Double . isNaN ( cpuUsage ) ) {
return NEGATIVE_ONE ;
}
DecimalFormat df = new DecimalFormat ( TWO_DECIMAL ) ;
DecimalFormat df = new DecimalFormat ( TWO_DECIMAL ) ;
df . setRoundingMode ( RoundingMode . HALF_UP ) ;
df . setRoundingMode ( RoundingMode . HALF_UP ) ;
return Double . parseDouble ( df . format ( cpuUsage ) ) ;
return Double . parseDouble ( df . format ( cpuUsage ) ) ;
}
}
@ -393,7 +403,6 @@ public class OSUtils {
return null ;
return null ;
}
}
/ * *
/ * *
* whether is macOS
* whether is macOS
* @return true if mac
* @return true if mac