public void TestGetCoreUsageByProcessID(string sourceProcDirectoryPath, int processID, double expectedCoreUsage, double expectedCpuUsageTotal) { const int SAMPLING_TIME_SECONDS = 3; var procDirectory = ValidateLocalProcDirectory(); // Update the cpuinfo file in the local proc directory CopyCPUInfoFile(procDirectory, sourceProcDirectoryPath); // Update the cpu stat file in the local proc directory using sourceProcDirectoryPath var sourceCpuStatFile1 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\CpuStat1\stat")); var sourceCpuStatFile2 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\CpuStat2\stat")); var targetCpuStatFile = CopyCPUStatFile(procDirectory, sourceCpuStatFile1); // Update the process stat file in the local proc directory using sourceProcDirectoryPath var sourceStatFile1 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\ProcStat1\stat")); var sourceStatFile2 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\ProcStat2\stat")); var targetStatFile = CopyProcessStatFile(procDirectory, processID, sourceStatFile1); var linuxSystemInfo = new LinuxSystemInfo { TraceEnabled = true }; linuxSystemInfo.DebugEvent += LinuxSystemInfo_DebugEvent; linuxSystemInfo.ErrorEvent += LinuxSystemInfo_ErrorEvent; var filesToCopy = new List <TestFileCopyInfo> { new TestFileCopyInfo(sourceCpuStatFile2, targetCpuStatFile), new TestFileCopyInfo(sourceStatFile2, targetStatFile) }; // Start a timer to replace the stat file in 2 seconds var fileReplacerTimer = new Timer(ReplaceFiles, filesToCopy, (SAMPLING_TIME_SECONDS - 1) * 1000, -1); var coreUsage = linuxSystemInfo.GetCoreUsageByProcessID(processID, out var cpuUsageTotal, SAMPLING_TIME_SECONDS); fileReplacerTimer.Dispose(); // Delay 1 second to allow threads to finish up var startTime = DateTime.UtcNow; while (true) { Thread.Sleep(250); if (DateTime.UtcNow.Subtract(startTime).TotalMilliseconds >= 1000) { break; } } Console.WriteLine("Core usage: {0:F2}", coreUsage); Console.WriteLine("Total CPU usage: {0:F1}%", cpuUsageTotal); Assert.AreEqual(expectedCoreUsage, coreUsage, 0.01, "Core usage mismatch"); Assert.AreEqual(expectedCpuUsageTotal, cpuUsageTotal, 0.1, "Total CPU usage mismatch"); }
static SystemInfo() { var c = new OSVersionInfo(); if (c.GetOSVersion().ToLower().Contains("windows")) { SystemInfoObject = new WindowsSystemInfo(); } else { SystemInfoObject = new LinuxSystemInfo(); } }
public void TestGetCoreCount(string sourceProcDirectoryPath, int expectedCoreCount, int expectedProcessorPackages) { var procDirectory = ValidateLocalProcDirectory(); // Update the cpuinfo file in the local proc directory CopyCPUInfoFile(procDirectory, sourceProcDirectoryPath); var linuxSystemInfo = new LinuxSystemInfo(); var coreCount = linuxSystemInfo.GetCoreCount(); Console.WriteLine("Core count: {0}", coreCount); Assert.AreEqual(expectedCoreCount, coreCount); Console.WriteLine("Processor Packages: {0}", linuxSystemInfo.GetProcessorPackageCount()); Assert.AreEqual(expectedProcessorPackages, linuxSystemInfo.GetProcessorPackageCount()); }
public void TestGetTotalMemory(string sourceProcDirectoryPath, float expectedTotalMemoryMB) { var procDirectory = ValidateLocalProcDirectory(); // Update the meminfo file in the local proc directory using sourceProcDirectoryPath var sourceMemInfoFile = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, LinuxSystemInfo.MEMINFO_FILE)); var targetMemInfoFile = new FileInfo(Path.Combine(procDirectory.FullName, LinuxSystemInfo.MEMINFO_FILE)); if (targetMemInfoFile.Exists) { try { targetMemInfoFile.Delete(); } catch (Exception ex) { Assert.Fail("Could not replace the local meminfo file at " + targetMemInfoFile.FullName + ": " + ex.Message); } } try { sourceMemInfoFile.CopyTo(targetMemInfoFile.FullName); } catch (Exception ex) { Assert.Fail("Could not copy the meminfo file to " + targetMemInfoFile.FullName + ": " + ex.Message); } var linuxSystemInfo = new LinuxSystemInfo(); var totalMemoryMB = linuxSystemInfo.GetTotalMemoryMB(); Console.WriteLine("Total memory: {0:F0} MB", totalMemoryMB); Assert.AreEqual(expectedTotalMemoryMB, totalMemoryMB, 1); }
public void TestGetCoreUsageByProcessName(string sourceProcDirectoryPath, int processID, string processName, string arguments, double expectedCoreUsageTotal) { const int SAMPLING_TIME_SECONDS = 3; var procDirectory = ValidateLocalProcDirectory(); // Update the cpuinfo file in the local proc directory CopyCPUInfoFile(procDirectory, sourceProcDirectoryPath); // Update the cpu stat file in the local proc directory using sourceProcDirectoryPath var sourceCpuStatFile1 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\CpuStat1\stat")); var sourceCpuStatFile2 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\CpuStat2\stat")); var targetCpuStatFile = CopyCPUStatFile(procDirectory, sourceCpuStatFile1); // Update the process stat file in the local proc directory using sourceProcDirectoryPath var sourceStatFile1 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\ProcStat1\stat")); var sourceStatFile2 = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\ProcStat2\stat")); var targetStatFile = CopyProcessStatFile(procDirectory, processID, sourceStatFile1); // Update the process cmdline file in the local proc directory using sourceProcDirectoryPath var sourceCmdLineFile = VerifyTestFile(Path.Combine(sourceProcDirectoryPath, processID + @"\ProcStat1\cmdline")); var targetCmdLineFile = new FileInfo(Path.Combine(procDirectory.FullName, processID + @"\cmdline")); if (targetCmdLineFile.Exists) { try { targetCmdLineFile.Delete(); } catch (Exception ex) { Assert.Fail("Could not replace the local process cmdline file at " + targetCmdLineFile.FullName + ": " + ex.Message); } } try { sourceCmdLineFile.CopyTo(targetCmdLineFile.FullName, true); } catch (Exception ex) { Assert.Fail("Could not copy the cmdline file to " + targetCmdLineFile.FullName + ": " + ex.Message); } var linuxSystemInfo = new LinuxSystemInfo(); var filesToCopy = new List <TestFileCopyInfo> { new TestFileCopyInfo(sourceCpuStatFile2, targetCpuStatFile), new TestFileCopyInfo(sourceStatFile2, targetStatFile) }; // Start a timer to replace the stat file in 2 seconds var fileReplacerTimer = new Timer(ReplaceFiles, filesToCopy, (SAMPLING_TIME_SECONDS - 1) * 1000, -1); var coreUsage = linuxSystemInfo.GetCoreUsageByProcessName(processName, arguments, out var processIDs, SAMPLING_TIME_SECONDS); fileReplacerTimer.Dispose(); // Delay 1 second to allow threads to finish up var startTime = DateTime.UtcNow; while (true) { Thread.Sleep(250); if (DateTime.UtcNow.Subtract(startTime).TotalMilliseconds >= 1000) { break; } } Console.WriteLine("Core usage: {0:F2}", coreUsage); Assert.AreEqual(expectedCoreUsageTotal, coreUsage, 0.1, "Core usage mismatch"); if (processIDs.Count == 1) { Console.WriteLine("Process ID: " + processIDs.First()); } else { Console.WriteLine("Process IDs: " + string.Join(", ", processIDs)); } }