protected void fillRemainingMemoryPercent ( final ExecutorInfo stats ) { if ( exists_Bash && exists_Cat && exists_Grep && exists_Meminfo ) { try { final ArrayList < String > output = Utils . runProcess ( "/bin/bash" , "-c" , "/bin/cat /proc/meminfo | grep -E \"^MemTotal:|^MemFree:|^Buffers:|^Cached:|^SwapCached:\"" ) ; long totalMemory = 0 ; long totalFreeMemory = 0 ; Long parsedResult = ( long ) 0 ; // process the output from bash call. // we expect the result from the bash call to be something like following - // MemTotal: 65894264 kB // MemFree: 57753844 kB // Buffers: 305552 kB // Cached: 3802432 kB // SwapCached: 0 kB // Note : total free memory = freeMemory + cached + buffers + swapCached // TODO : think about merging the logic in systemMemoryInfo as the logic is similar if ( output . size ( ) == 5 ) { for ( final String result : output ) { // find the total memory and value the variable. parsedResult = extractMemoryInfo ( "MemTotal" , result ) ; if ( null != parsedResult ) { totalMemory = parsedResult ; continue ; } // find the free memory. parsedResult = extractMemoryInfo ( "MemFree" , result ) ; if ( null != parsedResult ) { totalFreeMemory += parsedResult ; continue ; } // find the Buffers. parsedResult = extractMemoryInfo ( "Buffers" , result ) ; if ( null != parsedResult ) { totalFreeMemory += parsedResult ; continue ; } // find the Cached. parsedResult = extractMemoryInfo ( "SwapCached" , result ) ; if ( null != parsedResult ) { totalFreeMemory += parsedResult ; continue ; } // find the Cached. parsedResult = extractMemoryInfo ( "Cached" , result ) ; if ( null != parsedResult ) { totalFreeMemory += parsedResult ; continue ; } } } else { logger . error ( "failed to get total/free memory info as the bash call returned invalid result." + String . format ( " Output from the bash call - %s " , output . toString ( ) ) ) ; } // the number got from the proc file is in KBs we want to see the number in MBs so we are dividing it by 1024. stats . setRemainingMemoryInMB ( totalFreeMemory / 1024 ) ; stats . setRemainingMemoryPercent ( totalMemory == 0 ? 0 : ( ( double ) totalFreeMemory / ( double ) totalMemory ) * 100 ) ; } catch ( final Exception ex ) { logger . error ( "failed fetch system memory info " + "as exception is captured when fetching result from bash call. Ex -" + ex . getMessage ( ) ) ; } } else { logger . error ( "failed fetch system memory info, one or more files from the following list are missing - " + "'/bin/bash'," + "'/bin/cat'," + "'/proc/loadavg'" ) ; } }
fill the result set with the percent of the remaining system memory on the server .