Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 64 additions & 11 deletions RunCat365/CPURepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GenerateIndicator(this CPUInfo cpuInfo)
{
var resultLines = new List<string>
{
$"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<CPUInfo> 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);
Expand All @@ -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();
}
}
}
14 changes: 4 additions & 10 deletions RunCat365/MemoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal static List<string> GenerateIndicator(this MemoryInfo memoryInfo)
var resultLines = new List<string>
{
$"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;
}
Expand All @@ -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()
Expand Down
15 changes: 6 additions & 9 deletions RunCat365/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,19 @@ private void FetchSystemInfo(
List<StorageInfo> storageValue
)
{
var cpuIndicator = cpuInfo.GenerateIndicator();
notifyIcon.Text = cpuIndicator;
notifyIcon.Text = cpuInfo.GetDescription();

var systemInfoValues = new List<string>
{
cpuIndicator
};
var systemInfoValues = new List<string>();
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);
}

Expand All @@ -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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions RunCat365/StorageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ internal static List<string> GenerateIndicator(this List<StorageInfo> 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;
Expand Down