From 097e64bff7035d3f7833905e4c6da051359eddfc Mon Sep 17 00:00:00 2001 From: "Takuto NAKAMURA (Kyome)" Date: Sat, 26 Jul 2025 15:40:40 +0900 Subject: [PATCH] Made it possible to display more detailed CPU information. --- RunCat365/CPURepository.cs | 75 +++++++++++++++++++++++++++++----- RunCat365/MemoryRepository.cs | 14 ++----- RunCat365/Program.cs | 15 +++---- RunCat365/StorageRepository.cs | 8 ++-- 4 files changed, 78 insertions(+), 34 deletions(-) diff --git a/RunCat365/CPURepository.cs b/RunCat365/CPURepository.cs index d99a2c73..f6eeebb9 100644 --- a/RunCat365/CPURepository.cs +++ b/RunCat365/CPURepository.cs @@ -12,36 +12,78 @@ // See the License for the specific language governing permissions and // limitations under the License. -global using CPUInfo = float; using System.Diagnostics; namespace RunCat365 { + struct CPUInfo + { + internal float Total { get; set; } + internal float User { get; set; } + internal float Kernel { get; set; } + internal float Idle { get; set; } + } + internal static class CPUInfoExtension { - internal static string GenerateIndicator(this CPUInfo cpuInfo) + internal static string GetDescription(this CPUInfo cpuInfo) { - return $"CPU: {cpuInfo:f1}%"; + return $"CPU: {cpuInfo.Total:f1}%"; + } + + internal static List GenerateIndicator(this CPUInfo cpuInfo) + { + var resultLines = new List + { + $"CPU: {cpuInfo.Total:f1}%", + $" ├─ User: {cpuInfo.User:f1}%", + $" ├─ Kernel: {cpuInfo.Kernel:f1}%", + $" └─ Available: {cpuInfo.Idle:f1}%" + }; + return resultLines; } } internal class CPURepository { - private readonly PerformanceCounter cpuCounter; + private readonly PerformanceCounter totalCounter; + private readonly PerformanceCounter userCounter; + private readonly PerformanceCounter kernelCounter; + private readonly PerformanceCounter idleCounter; private readonly List cpuInfoList = []; private const int CPU_INFO_LIST_LIMIT_SIZE = 5; internal CPURepository() { - cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); - _ = cpuCounter.NextValue(); // discards first return value + totalCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + userCounter = new PerformanceCounter("Processor", "% User Time", "_Total"); + kernelCounter = new PerformanceCounter("Processor", "% Privileged Time", "_Total"); + idleCounter = new PerformanceCounter("Processor", "% Idle Time", "_Total"); + + // Discards first return value + _ = totalCounter.NextValue(); + _ = userCounter.NextValue(); + _ = kernelCounter.NextValue(); + _ = idleCounter.NextValue(); } internal void Update() { - // Range of CPU percentage: 0-100 (%) - var value = Math.Min(100, cpuCounter.NextValue()); - cpuInfoList.Add(value); + // Range of value: 0-100 (%) + var total = Math.Min(100, totalCounter.NextValue()); + var user = Math.Min(100, userCounter.NextValue()); + var kernel = Math.Min(100, kernelCounter.NextValue()); + var idle = Math.Min(100, idleCounter.NextValue()); + + var cpuInfo = new CPUInfo + { + Total = total, + User = user, + Kernel = kernel, + Idle = idle, + }; + + cpuInfoList.Add(cpuInfo); if (CPU_INFO_LIST_LIMIT_SIZE < cpuInfoList.Count) { cpuInfoList.RemoveAt(0); @@ -50,12 +92,23 @@ internal void Update() internal CPUInfo Get() { - return cpuInfoList.Average(); + if (cpuInfoList.Count == 0) return new CPUInfo(); + + return new CPUInfo + { + Total = cpuInfoList.Average(x => x.Total), + User = cpuInfoList.Average(x => x.User), + Kernel = cpuInfoList.Average(x => x.Kernel), + Idle = cpuInfoList.Average(x => x.Idle) + }; } internal void Close() { - cpuCounter.Close(); + totalCounter.Close(); + userCounter.Close(); + kernelCounter.Close(); + idleCounter.Close(); } } } diff --git a/RunCat365/MemoryRepository.cs b/RunCat365/MemoryRepository.cs index 42f08e7d..8fea8f06 100644 --- a/RunCat365/MemoryRepository.cs +++ b/RunCat365/MemoryRepository.cs @@ -17,9 +17,9 @@ internal static List GenerateIndicator(this MemoryInfo memoryInfo) var resultLines = new List { $"Memory: {memoryInfo.MemoryLoad}%", - $" ├─ Total: {memoryInfo.TotalMemory.ToByteFormatted()}", - $" ├─ Used: {memoryInfo.UsedMemory.ToByteFormatted()}", - $" └─ Available: {memoryInfo.AvailableMemory.ToByteFormatted()}" + $" ├─ Total: {memoryInfo.TotalMemory.ToByteFormatted()}", + $" ├─ Used: {memoryInfo.UsedMemory.ToByteFormatted()}", + $" └─ Available: {memoryInfo.AvailableMemory.ToByteFormatted()}" }; return resultLines; } @@ -31,13 +31,7 @@ internal partial class MemoryRepository internal MemoryRepository() { - memoryInfo = new MemoryInfo - { - MemoryLoad = 0, - TotalMemory = 0, - AvailableMemory = 0, - UsedMemory = 0, - }; + memoryInfo = new MemoryInfo(); } internal void Update() diff --git a/RunCat365/Program.cs b/RunCat365/Program.cs index a676d85c..64c0a42e 100644 --- a/RunCat365/Program.cs +++ b/RunCat365/Program.cs @@ -341,22 +341,19 @@ private void FetchSystemInfo( List storageValue ) { - var cpuIndicator = cpuInfo.GenerateIndicator(); - notifyIcon.Text = cpuIndicator; + notifyIcon.Text = cpuInfo.GetDescription(); - var systemInfoValues = new List - { - cpuIndicator - }; + var systemInfoValues = new List(); + systemInfoValues.AddRange(cpuInfo.GenerateIndicator()); systemInfoValues.AddRange(memoryInfo.GenerateIndicator()); systemInfoValues.AddRange(storageValue.GenerateIndicator()); systemInfoMenu.Text = string.Join("\n", [.. systemInfoValues]); } - private int CalculateInterval(CPUInfo cpuInfo) + private int CalculateInterval(float cpuTotalValue) { // Range of interval: 25-500 (ms) = 2-40 (fps) - var speed = (float)Math.Max(1.0f, (cpuInfo / 5.0f) * fpsMaxLimit.GetRate()); + var speed = (float)Math.Max(1.0f, (cpuTotalValue / 5.0f) * fpsMaxLimit.GetRate()); return (int)(500.0f / speed); } @@ -373,7 +370,7 @@ private void FetchTick(object? state, EventArgs e) FetchSystemInfo(cpuInfo, memoryInfo, storageInfo); animateTimer.Stop(); - animateTimer.Interval = CalculateInterval(cpuInfo); + animateTimer.Interval = CalculateInterval(cpuInfo.Total); animateTimer.Start(); } } diff --git a/RunCat365/StorageRepository.cs b/RunCat365/StorageRepository.cs index 710a66d3..801919eb 100644 --- a/RunCat365/StorageRepository.cs +++ b/RunCat365/StorageRepository.cs @@ -66,12 +66,12 @@ internal static List GenerateIndicator(this List storageInf { var info = storageInfoList[i]; var isLastItem = (i == storageInfoList.Count - 1); - var parentPrefix = isLastItem ? " └─ " : " ├─ "; - var childIndent = isLastItem ? " " : " │ "; + var parentPrefix = isLastItem ? " └─ " : " ├─ "; + var childIndent = isLastItem ? " " : " │ "; var percentage = ((double)info.UsedSpaceSize / info.TotalSize) * 100.0; resultLines.Add($"{parentPrefix}{info.Drive.GetString()}: {percentage:f1}%"); - resultLines.Add($"{childIndent} ├─ Used: {info.UsedSpaceSize.ToByteFormatted()}"); - resultLines.Add($"{childIndent} └─ Available: {info.AvailableSpaceSize.ToByteFormatted()}"); + resultLines.Add($"{childIndent} ├─ Used: {info.UsedSpaceSize.ToByteFormatted()}"); + resultLines.Add($"{childIndent} └─ Available: {info.AvailableSpaceSize.ToByteFormatted()}"); } return resultLines;