/** * Constructor which allows assigning the /proc/ directories. This will be * used only in unit tests. * @param procfsMemFile fake file for /proc/meminfo * @param procfsCpuFile fake file for /proc/cpuinfo * @param procfsStatFile fake file for /proc/stat * @param procfsNetFile fake file for /proc/net/dev * @param procfsDisksFile fake file for /proc/diskstats * @param jiffyLengthInMillis fake jiffy length value */ @VisibleForTesting public SysInfoLinux(String procfsMemFile, String procfsCpuFile, String procfsStatFile, String procfsNetFile, String procfsDisksFile, long jiffyLengthInMillis) { this.procfsMemFile = procfsMemFile; this.procfsCpuFile = procfsCpuFile; this.procfsStatFile = procfsStatFile; this.procfsNetFile = procfsNetFile; this.procfsDisksFile = procfsDisksFile; this.jiffyLengthInMillis = jiffyLengthInMillis; this.cpuTimeTracker = new CpuTimeTracker(jiffyLengthInMillis); this.perDiskSectorSize = new HashMap<String, Integer>(); }
/** {@inheritDoc} */ @Override public float getNumVCoresUsed() { readProcStatFile(); float overallVCoresUsage = cpuTimeTracker.getCpuTrackerUsagePercent(); if (overallVCoresUsage != CpuTimeTracker.UNAVAILABLE) { overallVCoresUsage = overallVCoresUsage / 100F; } return overallVCoresUsage; }
long nTime = Long.parseLong(mat.group(2)); long sTime = Long.parseLong(mat.group(3)); cpuTimeTracker.updateElapsedJiffies( BigInteger.valueOf(uTime + nTime + sTime), getCurrentTime());
/** * Get the CPU usage by all the processes in the process-tree in Windows. * Note: UNAVAILABLE will be returned in case when CPU usage is not * available. It is NOT advised to return any other error code. * * @return percentage CPU usage since the process-tree was created, * {@link #UNAVAILABLE} if CPU usage cannot be calculated or not available. */ @Override public float getCpuUsagePercent() { BigInteger processTotalMs = getTotalProcessMs(); cpuTimeTracker.updateElapsedJiffies(processTotalMs, clock.getTime()); return cpuTimeTracker.getCpuTrackerUsagePercent(); }
/** {@inheritDoc} */ @Override public long getCumulativeCpuTime() { readProcStatFile(); return cpuTimeTracker.getCumulativeCpuTime(); }
/** * Get the CPU usage by all the processes in the process-tree in Unix. * Note: UNAVAILABLE will be returned in case when CPU usage is not * available. It is NOT advised to return any other error code. * * @return percentage CPU usage since the process-tree was created, * {@link #UNAVAILABLE} if CPU usage cannot be calculated or not available. */ @Override public float getCpuUsagePercent() { BigInteger processTotalJiffies = getTotalProcessJiffies(); if (LOG.isDebugEnabled()) { LOG.debug("Process " + pid + " jiffies:" + processTotalJiffies); } cpuTimeTracker.updateElapsedJiffies(processTotalJiffies, clock.getTime()); return cpuTimeTracker.getCpuTrackerUsagePercent(); }
/** {@inheritDoc} */ @Override public long getCumulativeCpuTime() { readProcStatFile(); return cpuTimeTracker.getCumulativeCpuTime(); }
/** {@inheritDoc} */ @Override public float getCpuUsagePercentage() { readProcStatFile(); float overallCpuUsage = cpuTimeTracker.getCpuTrackerUsagePercent(); if (overallCpuUsage != CpuTimeTracker.UNAVAILABLE) { overallCpuUsage = overallCpuUsage / getNumProcessors(); } return overallCpuUsage; }
/** * Create a monitor for a Windows process tree. * @param pid Identifier of the job object. * @param pClock Clock to keep track of time for CPU utilization. */ public WindowsBasedProcessTree(final String pid, final Clock pClock) { super(pid); this.taskProcessId = pid; this.clock = pClock; // Instead of jiffies, Windows uses milliseconds directly; 1ms = 1 jiffy this.cpuTimeTracker = new CpuTimeTracker(1L); }
@Override public void updateProcessTree() { try { this.processTotalJiffies = readTotalProcessJiffies(); cpuTimeTracker.updateElapsedJiffies(processTotalJiffies, clock.getTime()); } catch (YarnException e) { LOG.warn("Failed to parse " + pid, e); } processPhysicalMemory = getMemorySize(memStat); if (memswStat.exists()) { processVirtualMemory = getMemorySize(memswStat); } else if(LOG.isDebugEnabled()) { LOG.debug("Swap cgroups monitoring is not compiled into the kernel " + memswStat.getAbsolutePath().toString()); } }
@Override public float getCpuUsagePercent() { if (LOG.isDebugEnabled()) { LOG.debug("Process " + pid + " jiffies:" + processTotalJiffies); } return cpuTimeTracker.getCpuTrackerUsagePercent(); }
/** * Constructor which allows assigning the /proc/ directories. This will be * used only in unit tests. * @param procfsMemFile fake file for /proc/meminfo * @param procfsCpuFile fake file for /proc/cpuinfo * @param procfsStatFile fake file for /proc/stat * @param procfsNetFile fake file for /proc/net/dev * @param procfsDisksFile fake file for /proc/diskstats * @param jiffyLengthInMillis fake jiffy length value */ @VisibleForTesting public SysInfoLinux(String procfsMemFile, String procfsCpuFile, String procfsStatFile, String procfsNetFile, String procfsDisksFile, long jiffyLengthInMillis) { this.procfsMemFile = procfsMemFile; this.procfsCpuFile = procfsCpuFile; this.procfsStatFile = procfsStatFile; this.procfsNetFile = procfsNetFile; this.procfsDisksFile = procfsDisksFile; this.jiffyLengthInMillis = jiffyLengthInMillis; this.cpuTimeTracker = new CpuTimeTracker(jiffyLengthInMillis); this.perDiskSectorSize = new HashMap<String, Integer>(); }
long nTime = Long.parseLong(mat.group(2)); long sTime = Long.parseLong(mat.group(3)); cpuTimeTracker.updateElapsedJiffies( BigInteger.valueOf(uTime + nTime + sTime), getCurrentTime());
/** {@inheritDoc} */ @Override public float getNumVCoresUsed() { readProcStatFile(); float overallVCoresUsage = cpuTimeTracker.getCpuTrackerUsagePercent(); if (overallVCoresUsage != CpuTimeTracker.UNAVAILABLE) { overallVCoresUsage = overallVCoresUsage / 100F; } return overallVCoresUsage; }
/** * Create resource calculator for testing. * @param pid A pid from the cgroup or null for all containers * @param procfsDir Path to /proc or a mock /proc directory * @param cGroupsHandler Initialized cgroups handler object * @param clock A clock object * @param jiffyLengthMs0 Jiffy length in milliseconds */ @VisibleForTesting CGroupsResourceCalculator(String pid, String procfsDir, CGroupsHandler cGroupsHandler, Clock clock, long jiffyLengthMs0) { super(pid); this.procfsDir = procfsDir; this.cGroupsHandler = cGroupsHandler; this.pid = pid != null && pid.equals("0") ? "1" : pid; this.jiffyLengthMs = jiffyLengthMs0; this.cpuTimeTracker = new CpuTimeTracker(this.jiffyLengthMs); this.clock = clock; this.processTotalJiffies = BigInteger.ZERO; this.processPhysicalMemory = UNAVAILABLE; this.processVirtualMemory = UNAVAILABLE; }
/** {@inheritDoc} */ @Override public float getCpuUsagePercentage() { readProcStatFile(); float overallCpuUsage = cpuTimeTracker.getCpuTrackerUsagePercent(); if (overallCpuUsage != CpuTimeTracker.UNAVAILABLE) { overallCpuUsage = overallCpuUsage / getNumProcessors(); } return overallCpuUsage; }
/** * Build a new process tree rooted at the pid. * * This method is provided mainly for testing purposes, where * the root of the proc file system can be adjusted. * * @param pid root of the process tree * @param procfsDir the root of a proc file system - only used for testing. * @param clock clock for controlling time for testing */ public ProcfsBasedProcessTree(String pid, String procfsDir, Clock clock) { super(pid); this.clock = clock; this.pid = getValidPID(pid); this.procfsDir = procfsDir; this.cpuTimeTracker = new CpuTimeTracker(JIFFY_LENGTH_IN_MILLIS); }