Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Runtime.ConstrainedExecution;
- using System.Runtime.InteropServices;
- using System.Security;
- using System.Text;
- namespace Core.Diagnostics {
- internal sealed class PhysicalMemoryMonitor:MemoryMonitor {
- private const int MIN_TOTAL_MEMORY_TRIM_PERCENT = 10;
- private static readonly long TARGET_TOTAL_MEMORY_TRIM_INTERVAL_TICKS = 0xb2d05e00L;
- private PhysicalMemoryMonitor() {
- }
- internal PhysicalMemoryMonitor(int physicalMemoryLimitPercentage) {
- long totalPhysical = MemoryMonitor.TotalPhysical;
- if(totalPhysical >= 0x100000000L) {
- base._pressureHigh = 0x63;
- }
- else if(totalPhysical >= 0x80000000L) {
- base._pressureHigh = 0x62;
- }
- else if(totalPhysical >= 0x40000000L) {
- base._pressureHigh = 0x61;
- }
- else if(totalPhysical >= 0x30000000L) {
- base._pressureHigh = 0x60;
- }
- else {
- base._pressureHigh = 0x5f;
- }
- base._pressureLow = base._pressureHigh - 9;
- this.SetLimit(physicalMemoryLimitPercentage);
- base.InitHistory();
- }
- [SecuritySafeCritical]
- protected override int GetCurrentPressure() {
- MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX();
- memoryStatusEx.Init();
- if(UnsafeNativeMethods.GlobalMemoryStatusEx(ref memoryStatusEx) == 0) {
- return 0;
- }
- return memoryStatusEx.dwMemoryLoad;
- }
- internal override int GetPercentToTrim(DateTime lastTrimTime,int lastTrimPercent) {
- int num = 0;
- if(base.IsAboveHighPressure()) {
- long ticks = DateTime.UtcNow.Subtract(lastTrimTime).Ticks;
- if(ticks > 0L) {
- num = Math.Min(50,(int)((lastTrimPercent * TARGET_TOTAL_MEMORY_TRIM_INTERVAL_TICKS) / ticks));
- num = Math.Max(10,num);
- }
- }
- return num;
- }
- internal void SetLimit(int physicalMemoryLimitPercentage) {
- if(physicalMemoryLimitPercentage != 0) {
- base._pressureHigh = Math.Max(3,physicalMemoryLimitPercentage);
- base._pressureLow = Math.Max(1,base._pressureHigh - 9);
- }
- }
- internal long MemoryLimit {
- get {
- return (long)base._pressureHigh;
- }
- }
- }
- internal abstract class MemoryMonitor {
- protected int _i0;
- protected int _pressureHigh;
- protected int[] _pressureHist;
- protected int _pressureLow;
- protected int _pressureTotal;
- protected const long GIGABYTE = 0x40000000L;
- protected const int GIGABYTE_SHIFT = 30;
- protected const int HISTORY_COUNT = 6;
- protected const long KILOBYTE = 0x400L;
- protected const int KILOBYTE_SHIFT = 10;
- protected const long MEGABYTE = 0x100000L;
- protected const int MEGABYTE_SHIFT = 20;
- private static long s_totalPhysical;
- private static long s_totalVirtual;
- protected const long TERABYTE = 0x10000000000L;
- protected const int TERABYTE_SHIFT = 40;
- [SecuritySafeCritical]
- static MemoryMonitor() {
- MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX();
- memoryStatusEx.Init();
- if(UnsafeNativeMethods.GlobalMemoryStatusEx(ref memoryStatusEx) != 0) {
- s_totalPhysical = memoryStatusEx.ullTotalPhys;
- s_totalVirtual = memoryStatusEx.ullTotalVirtual;
- }
- }
- protected MemoryMonitor() {
- }
- protected abstract int GetCurrentPressure();
- internal abstract int GetPercentToTrim(DateTime lastTrimTime,int lastTrimPercent);
- protected void InitHistory() {
- int currentPressure = this.GetCurrentPressure();
- this._pressureHist = new int[6];
- for(int i = 0;i < 6;i++) {
- this._pressureHist[i] = currentPressure;
- this._pressureTotal += currentPressure;
- }
- }
- internal bool IsAboveHighPressure() {
- return (this.PressureLast >= this.PressureHigh);
- }
- internal void Update() {
- int currentPressure = this.GetCurrentPressure();
- this._i0 = (this._i0 + 1) % 6;
- this._pressureTotal -= this._pressureHist[this._i0];
- this._pressureTotal += currentPressure;
- this._pressureHist[this._i0] = currentPressure;
- }
- internal int PressureHigh {
- get {
- return this._pressureHigh;
- }
- }
- internal int PressureLast {
- get {
- return this._pressureHist[this._i0];
- }
- }
- internal int PressureLow {
- get {
- return this._pressureLow;
- }
- }
- internal static long TotalPhysical {
- get {
- return s_totalPhysical;
- }
- }
- internal static long TotalVirtual {
- get {
- return s_totalVirtual;
- }
- }
- }
- [StructLayout(LayoutKind.Sequential,CharSet = CharSet.Unicode)]
- internal struct MEMORYSTATUSEX {
- internal int dwLength;
- internal int dwMemoryLoad;
- internal long ullTotalPhys;
- internal long ullAvailPhys;
- internal long ullTotalPageFile;
- internal long ullAvailPageFile;
- internal long ullTotalVirtual;
- internal long ullAvailVirtual;
- internal long ullAvailExtendedVirtual;
- internal void Init() {
- this.dwLength = Marshal.SizeOf(typeof(MEMORYSTATUSEX));
- }
- }
- [SuppressUnmanagedCodeSecurity,SecurityCritical]
- internal static class UnsafeNativeMethods {
- private const string ADVAPI32 = "ADVAPI32.DLL";
- private const string KERNEL32 = "KERNEL32.DLL";
- [DllImport("KERNEL32.DLL",CharSet = CharSet.Unicode)]
- internal static extern int GetModuleFileName(IntPtr module,StringBuilder filename,int size);
- [DllImport("KERNEL32.DLL",CharSet = CharSet.Unicode)]
- internal static extern int GlobalMemoryStatusEx(ref MEMORYSTATUSEX memoryStatusEx);
- [ReliabilityContract(Consistency.WillNotCorruptState,Cer.Success),DllImport("ADVAPI32.DLL")]
- internal static extern int RegCloseKey(IntPtr hKey);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment