From 5f281480e1b125c200b44ee62b23f676f41b178c 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 | 77 +++++++++++++++++++ 2 files changed, 85 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 b709dcde3..906ed42e1 100644 --- a/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs +++ b/Artemis/Artemis/Modules/Effects/WindowsProfile/WindowsProfileModel.cs @@ -8,13 +8,69 @@ using Artemis.Models; using Artemis.Models.Profiles; using Ninject.Extensions.Logging; 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 readonly ILogger _logger; private List _cores; + private PerformanceCounter _overallCPU; private int _cpuFrames; private SpotifyLocalAPI _spotify; private bool _spotifySetupBusy; @@ -100,6 +156,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) @@ -107,6 +173,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();