/** * Get the maximum number of threads allowed by OS for the machine. * This returns smaller of threads-max and pid_max from /proc/sys/kernel. */ private static long getSystemThreadLimit() { String line = getLineFromFile("/proc/sys/kernel/threads-max"); long threadsMax = 0; if (line != null) { try { threadsMax = Long.parseLong(line); } catch (NumberFormatException ignored) { } } line = getLineFromFile("/proc/sys/kernel/pid_max"); if (line != null) { try { long pidMax = Long.parseLong(line); threadsMax = threadsMax > 0 ? Math.min(threadsMax, pidMax) : pidMax; } catch (NumberFormatException ignored) { } } return threadsMax; }
/** * Get the current total number of threads across system and maximum number * of file descriptors threads allowed by OS for the machine. */ private static void getSystemFileDescriptorStats(long[] longs) { String line = getLineFromFile("/proc/sys/fs/file-nr"); if (line != null) { String[] parts = line.split("[ \t]"); if (parts.length > 0) { try { longs[LinuxSystemStats.totalFileDescriptorsLONG] = Long.parseLong(parts[0]); } catch (NumberFormatException ignored) { } if (parts.length > 2) { try { longs[LinuxSystemStats.fileDescriptorsSystemMaxLONG] = Long.parseLong(parts[2]); } catch (NumberFormatException ignored) { } } } } }
private static void getLoadAvg(double[] doubles, long[] longs) { try { String line = getLineFromFile("/proc/loadavg"); if ( line == null ) { return; } st.setString(line); doubles[LinuxSystemStats.loadAverage1DOUBLE] = st.nextTokenAsDouble(); doubles[LinuxSystemStats.loadAverage5DOUBLE] = st.nextTokenAsDouble(); doubles[LinuxSystemStats.loadAverage15DOUBLE] = st.nextTokenAsDouble(); // also read the total number of threads in the system String threadLoad = st.nextToken(); if (threadLoad != null) { int slashIndex = threadLoad.indexOf('/'); if (slashIndex >= 0) { threadLoad = threadLoad.substring(slashIndex + 1); } try { longs[LinuxSystemStats.totalThreadsLONG] = Long.parseLong(threadLoad); } catch (NumberFormatException ignored) { } } } catch (NoSuchElementException ignored) { } finally { st.releaseResources(); } }