private static int getAvailableProcessorsFromFilesystem ( ) { boolean isTraceOn = TraceComponent . isAnyTracingEnabled ( ) ; Double availableProcessorsDouble = null ; int availableProcessorsInt = - 1 ; //Check for docker files String periodFileLocation = File . separator + "sys" + File . separator + "fs" + File . separator + "cgroup" + File . separator + "cpu" + File . separator + "cpu.cfs_period_us" ; String quotaFileLocation = File . separator + "sys" + File . separator + "fs" + File . separator + "cgroup" + File . separator + "cpu" + File . separator + "cpu.cfs_quota_us" ; File cfsPeriod = new File ( periodFileLocation ) ; File cfsQuota = new File ( quotaFileLocation ) ; if ( cfsPeriod . exists ( ) && cfsQuota . exists ( ) ) { //Found docker files //Read quota try { String quotaContents = readFile ( cfsQuota ) ; double quotaFloat = new Double ( quotaContents ) ; if ( isTraceOn && tc . isDebugEnabled ( ) ) Tr . debug ( tc , "quotaFloat = " + quotaFloat ) ; if ( quotaFloat >= 0 ) { //Read period String periodContents = readFile ( cfsPeriod ) ; double periodFloat = new Double ( periodContents ) ; if ( isTraceOn && tc . isDebugEnabled ( ) ) Tr . debug ( tc , "periodFloat = " + periodFloat ) ; if ( periodFloat != 0 ) { availableProcessorsDouble = quotaFloat / periodFloat ; availableProcessorsDouble = roundToTwoDecimalPlaces ( availableProcessorsDouble ) ; if ( isTraceOn && tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Calculated availableProcessors: " + availableProcessorsDouble + ". period=" + periodFloat + ", quota=" + quotaFloat ) ; } } } catch ( Throwable e ) { if ( isTraceOn && tc . isDebugEnabled ( ) ) Tr . debug ( tc , "Caught exception: " + e . getMessage ( ) + ". Using number of processors reported by java" ) ; availableProcessorsDouble = null ; } } else { if ( isTraceOn && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Files " + quotaFileLocation + " : " + cfsQuota . exists ( ) ) ; Tr . debug ( tc , "Files " + periodFileLocation + " : " + cfsPeriod . exists ( ) ) ; } } availableProcessorsInt = ( availableProcessorsDouble == null ) ? - 1 : availableProcessorsDouble . intValue ( ) ; // make sure any z.xy cpu quota was not rounded down (especially to 0 ...) during int conversion if ( availableProcessorsDouble != null && availableProcessorsDouble > availableProcessorsInt ) availableProcessorsInt ++ ; return availableProcessorsInt ; }
utility below parses cpu limits info from Docker files