首页 > 代码库 > 系统数据监控
系统数据监控
系统数据监控。
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading;using Arch.CFramework.CLoggingAdapter;using Ctrip.Mobile.AppDownload.Utility;namespace Ctrip.Mobile.AppDownload.Service{ public class MonitorService : RunningService { public MonitorService() { ElapsedMiliseconds = 3 * 60 * 60 * 1000;//每3小时记录一次。 s_stopwatch.Start(); } public override void LoopLogic() { string info = GetSystemInfo(); //Dashboard is better. LoggingManager.Instance().Info("MoniterService", info, new Dictionary<string, string>() { { "SystemInfo", "SystemInfo" } }); } Stopwatch s_stopwatch = new Stopwatch(); private PerformanceCounter _pc; private static TimeSpan s_lastTotalProcessTime = TimeSpan.Zero; public string GetSystemInfo() { int timeout; int totalThreads; int memory; int workingSetExecludePrivate; int workerThreads; int completionPortThreads; int maxThreadPoolThreads; int maxIOCPThreads; string split = ", "; StringBuilder sbInfo = new StringBuilder(); sbInfo.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + split); s_stopwatch.Stop(); Process process = Process.GetCurrentProcess(); if (_pc == null) { _pc = new PerformanceCounter("Process", "Working Set - Private", process.ProcessName); } process.Refresh(); totalThreads = process.Threads.Count; memory = (int)((process.WorkingSet64) / (1024 * 1024)); workingSetExecludePrivate = (int)(_pc.NextValue() / (1024 * 1024)); double elapsedProcessTime = (process.TotalProcessorTime - s_lastTotalProcessTime).TotalMilliseconds; double elapsedSystemTime = s_stopwatch.ElapsedMilliseconds; int cpu = (int)(Math.Round(elapsedProcessTime * 100.0 / elapsedSystemTime / Environment.ProcessorCount)); s_lastTotalProcessTime = process.TotalProcessorTime; s_stopwatch.Restart(); ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads); ThreadPool.GetMaxThreads(out maxThreadPoolThreads, out maxIOCPThreads); sbInfo.AppendFormat("CPU: {0}, ", cpu < 100 ? cpu : 100); sbInfo.AppendFormat("worker thread: {0}, ", maxThreadPoolThreads - workerThreads); sbInfo.AppendFormat("cpio : {0}, ", maxIOCPThreads - completionPortThreads); sbInfo.AppendFormat("total Threads: {0}, ", totalThreads); sbInfo.AppendFormat("memory(working): {0}MB, ", memory); sbInfo.AppendFormat("memory(private): {0}MB, ", workingSetExecludePrivate); return sbInfo.ToString(); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。