Advertisement
XConquer

Code CpuUsage By Pezzi Tomas

Dec 3rd, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using System.Diagnostics;
  8.  
  9. namespace Pezzi
  10. {
  11.     public class CpuUsage
  12.     {
  13.  
  14.         public static void CpuUsageThread(object state)
  15.         {
  16.             CpuUsage usage = new CpuUsage();
  17.             while (true)
  18.             {
  19.                 if (!Server.CpuUsageTimer)
  20.                 {
  21.                     System.Threading.Thread.CurrentThread.Abort();
  22.                 }
  23.                 short cpuUsage = usage.GetUsage();
  24.                 Server.CpuUse = cpuUsage;
  25.                 System.Threading.Thread.Sleep(500);
  26.             }
  27.         }
  28.  
  29.         [DllImport("kernel32.dll", SetLastError = true)]
  30.         private static extern bool GetSystemTimes(
  31.                     out System.Runtime.InteropServices.ComTypes.FILETIME lpIdleTime,
  32.                     out System.Runtime.InteropServices.ComTypes.FILETIME lpKernelTime,
  33.                     out System.Runtime.InteropServices.ComTypes.FILETIME lpUserTime
  34.                     );
  35.  
  36.         System.Runtime.InteropServices.ComTypes.FILETIME _prevSysKernel;
  37.         System.Runtime.InteropServices.ComTypes.FILETIME _prevSysUser;
  38.  
  39.         TimeSpan _prevProcTotal;
  40.  
  41.         Int16 _cpuUsage;
  42.         DateTime _lastRun;
  43.         long _runCount;
  44.  
  45.         public CpuUsage()
  46.         {
  47.             _cpuUsage = -1;
  48.             _lastRun = DateTime.MinValue;
  49.             _prevSysUser.dwHighDateTime = _prevSysUser.dwLowDateTime = 0;
  50.             _prevSysKernel.dwHighDateTime = _prevSysKernel.dwLowDateTime = 0;
  51.             _prevProcTotal = TimeSpan.MinValue;
  52.             _runCount = 0;
  53.         }
  54.  
  55.         public short GetUsage()
  56.         {
  57.             short cpuCopy = _cpuUsage;
  58.             if (Interlocked.Increment(ref _runCount) == 1)
  59.             {
  60.                 if (!EnoughTimePassed)
  61.                 {
  62.                     Interlocked.Decrement(ref _runCount);
  63.                     return cpuCopy;
  64.                 }
  65.  
  66.                 System.Runtime.InteropServices.ComTypes.FILETIME sysIdle, sysKernel, sysUser;
  67.                 TimeSpan procTime;
  68.  
  69.                 Process process = Process.GetCurrentProcess();
  70.                 procTime = process.TotalProcessorTime;
  71.  
  72.                 if (!GetSystemTimes(out sysIdle, out sysKernel, out sysUser))
  73.                 {
  74.                     Interlocked.Decrement(ref _runCount);
  75.                     return cpuCopy;
  76.                 }
  77.  
  78.                 if (!IsFirstRun)
  79.                 {
  80.                     UInt64 sysKernelDiff = SubtractTimes(sysKernel, _prevSysKernel);
  81.                     UInt64 sysUserDiff = SubtractTimes(sysUser, _prevSysUser);
  82.  
  83.                     UInt64 sysTotal = sysKernelDiff + sysUserDiff;
  84.  
  85.                     Int64 procTotal = procTime.Ticks - _prevProcTotal.Ticks;
  86.  
  87.                     if (sysTotal > 0)
  88.                     {
  89.                         _cpuUsage = (short)((100.0 * procTotal) / sysTotal);
  90.                     }
  91.                 }
  92.  
  93.                 _prevProcTotal = procTime;
  94.                 _prevSysKernel = sysKernel;
  95.                 _prevSysUser = sysUser;
  96.  
  97.                 _lastRun = DateTime.Now;
  98.  
  99.                 cpuCopy = _cpuUsage;
  100.             }
  101.             Interlocked.Decrement(ref _runCount);
  102.  
  103.             return cpuCopy;
  104.         }
  105.         private UInt64 SubtractTimes(System.Runtime.InteropServices.ComTypes.FILETIME a, System.Runtime.InteropServices.ComTypes.FILETIME b)
  106.         {
  107.             UInt64 aInt = ((UInt64)(a.dwHighDateTime << 32)) | (UInt64)a.dwLowDateTime;
  108.             UInt64 bInt = ((UInt64)(b.dwHighDateTime << 32)) | (UInt64)b.dwLowDateTime;
  109.  
  110.             return aInt - bInt;
  111.         }
  112.  
  113.         private bool EnoughTimePassed
  114.         {
  115.             get
  116.             {
  117.                 const int minimumElapsedMS = 250;
  118.                 TimeSpan sinceLast = DateTime.Now - _lastRun;
  119.                 return sinceLast.TotalMilliseconds > minimumElapsedMS;
  120.             }
  121.         }
  122.  
  123.         private bool IsFirstRun
  124.         {
  125.             get
  126.             {
  127.                 return (_lastRun == DateTime.MinValue);
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement