protected override void OnStart(string[] args) { int count = Environment.ProcessorCount; try { CpuIdle = new PerformanceCounter("Processor", "% Idle Time", "_Total", true); } catch (Exception) { } if (count < 1 || CpuIdle == null) { WriteLogEntry(2001, "无法监控CPU", true); ExitCode = 2001; Stop(); return; } if (count > 31) { WriteLogEntry(2001, "CPU太多,此服务不支持拥有超过31个逻辑处理器的系统", true); ExitCode = 2002; Stop(); return; } CpuIdle.NextValue(); RestrainThreshold = (float)(100.0 - 90.0 / Environment.ProcessorCount); IdleThreshold = (float)(100.0 - 50.0 / Environment.ProcessorCount); if (Lagfree.MyPid < 0) { using (var me = Process.GetCurrentProcess()) Lagfree.MyPid = me.Id; } Lagfree.SetupCategory(); RestrainedCount = new PerformanceCounter(Lagfree.CounterCategoryName, Lagfree.CpuRestrainedCounterName, false); LastCounts = new SortedDictionary <int, double>(); Restrained = new Dictionary <int, CpuRestrainedProcess>(); UsageCheckTimer = new Timer(UsageCheck, null, CheckInterval, CheckInterval); }
protected override void OnStart(string[] args) { if (Lagfree.MyPid < 0) { using (var me = Process.GetCurrentProcess()) Lagfree.MyPid = me.Id; } const string PhysicalDiskCategoryName = "PhysicalDisk"; var phyDisks = new PerformanceCounterCategory(PhysicalDiskCategoryName); var instDisks = phyDisks.GetInstanceNames(); string SysDrive = Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0, 2).ToLowerInvariant(); foreach (var inst in instDisks) { if (inst == "_Total") { continue; } if (inst.ToLowerInvariant().Contains(SysDrive)) { try { if (!HasNominalMediaRotationRate(int.Parse(inst.Split(' ')[0]))) { WriteLogEntry(3001, "注意:检测到OS安装在非旋转存储设备上"); } } catch (Win32Exception) { WriteLogEntry(3002, "警告:无法检测到OS安装所在存储设备的旋转速度", true); } Disk = new PerformanceCounter(PhysicalDiskCategoryName, "% Idle Time", inst, true); Disk.NextValue(); break; } } if (Disk == null) { WriteLogEntry(2001, "无法监控磁盘", true); ExitCode = 2001; Stop(); } else { Lagfree.SetupCategory(); RestrainedCount = new PerformanceCounter(Lagfree.CounterCategoryName, Lagfree.IoRestrainedCounterName, false); Restrained = new Dictionary <int, HddRestrainedProcess>(); LastCounts = new SortedDictionary <int, IO_COUNTERS>(); UsageCheckTimer = new Timer(UsageCheck, null, CheckInterval, CheckInterval); } }