From cde523870a4ffcc64ed4d8a89a379435f0df7231 Mon Sep 17 00:00:00 2001 From: Ted Date: Sat, 11 Jun 2016 15:02:45 -0700 Subject: [PATCH] Added Overall CPU and RAM resource monitors for Windows Profile. --- .../WindowsProfile/WindowsProfileDataModel.cs | 8 ++ .../WindowsProfile/WindowsProfileModel.cs | 79 +++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs index de9bc9206..c44c5a36a 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileDataModel.cs @@ -8,9 +8,11 @@ namespace Artemis.Modules.Effects.WindowsProfile { Spotify = new Spotify(); Cpu = new CpuDataModel(); + Performance = new PerformanceDataModel(); } public CpuDataModel Cpu { get; set; } + public PerformanceDataModel Performance { get; set; } public Spotify Spotify { get; set; } } @@ -26,6 +28,12 @@ namespace Artemis.Modules.Effects.WindowsProfile public int Core8Usage { get; set; } } + public class PerformanceDataModel + { + public int CPUUsage { get; set; } + public int RAMUsage { get; set; } + } + public class Spotify { public bool Running { get; set; } diff --git a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs index a1537c89b..b701db0cc 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs @@ -7,12 +7,68 @@ using Artemis.Managers; using Artemis.Models; using Artemis.Models.Profiles; using SpotifyAPI.Local; +using System.Runtime.InteropServices; namespace Artemis.Modules.Effects.WindowsProfile { + static class PerformanceInfo + { + [DllImport("psapi.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetPerformanceInfo([Out] out PerformanceInformation PerformanceInformation, [In] int Size); + + [StructLayout(LayoutKind.Sequential)] + public struct PerformanceInformation + { + public int Size; + public IntPtr CommitTotal; + public IntPtr CommitLimit; + public IntPtr CommitPeak; + public IntPtr PhysicalTotal; + public IntPtr PhysicalAvailable; + public IntPtr SystemCache; + public IntPtr KernelTotal; + public IntPtr KernelPaged; + public IntPtr KernelNonPaged; + public IntPtr PageSize; + public int HandlesCount; + public int ProcessCount; + public int ThreadCount; + } + + public static Int64 GetPhysicalAvailableMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalAvailable.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + + public static Int64 GetTotalMemoryInMiB() + { + PerformanceInformation pi = new PerformanceInformation(); + if (GetPerformanceInfo(out pi, Marshal.SizeOf(pi))) + { + return Convert.ToInt64((pi.PhysicalTotal.ToInt64() * pi.PageSize.ToInt64() / 1048576)); + } + else + { + return -1; + } + + } + } + public class WindowsProfileModel : EffectModel { private List _cores; + private PerformanceCounter _overallCPU; private int _cpuFrames; private SpotifyLocalAPI _spotify; private bool _spotifySetupBusy; @@ -57,6 +113,8 @@ namespace Artemis.Modules.Effects.WindowsProfile _cores.Add(null); coreCount++; } + + _overallCPU = GetOverallPerformanceCounter(); } private void UpdateCpu(WindowsProfileDataModel dataModel) @@ -85,6 +143,16 @@ namespace Artemis.Modules.Effects.WindowsProfile dataModel.Cpu.Core7Usage = (int) _cores[6].NextValue(); if (_cores[7] != null) dataModel.Cpu.Core8Usage = (int) _cores[7].NextValue(); + + //From Ted - Let's get overall RAM and CPU usage here + dataModel.Performance.CPUUsage = (int)_overallCPU.NextValue(); + + Int64 phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB(); + Int64 tot = PerformanceInfo.GetTotalMemoryInMiB(); + decimal percentFree = ((decimal)phav / (decimal)tot) * 100; + decimal percentOccupied = 100 - percentFree; + + dataModel.Performance.RAMUsage = (int)percentOccupied; } public override List GetRenderLayers(bool renderMice, bool renderHeadsets) @@ -92,6 +160,17 @@ namespace Artemis.Modules.Effects.WindowsProfile return Profile.GetRenderLayers(DataModel, renderMice, renderHeadsets, false); } + public static PerformanceCounter GetOverallPerformanceCounter() + { + PerformanceCounter cpuCounter = new PerformanceCounter(); + + cpuCounter.CategoryName = "Processor"; + cpuCounter.CounterName = "% Processor Time"; + cpuCounter.InstanceName = "_Total"; + + return cpuCounter; + } + public static List GetPerformanceCounters() { var performanceCounters = new List();