Ejemplo n.º 1
0
 public UseSysInfoWatch()
 {
     mProcessorCount          = Environment.ProcessorCount;
     _mLastTime               = RunTimeWatch.GetRunTimeMilliseconds();
     _mCpuMaxTime             = mProcessorCount * 1000;
     mProcess                 = Process.GetCurrentProcess();
     _mLastTotalProcessorTime = mProcess.TotalProcessorTime.Milliseconds;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取服务状态
        /// </summary>
        /// <returns></returns>

        public ServerStatus GetServerStatus()
        {
            if (RunTimeWatch.GetRunTimeMilliseconds() - _mLastGetTime > 1000)
            {
                if (System.Threading.Interlocked.CompareExchange(ref _mGetStatus, 1, 0) == 0)
                {
                    _mLastGetTime = RunTimeWatch.GetRunTimeMilliseconds();
                    ServerStatus result = new ServerStatus();
                    TimeSpan     ts     = (DateTime.Now - RunTimeWatch.StartTime);
                    result.RunTime = $"{(long)ts.Days}:{(long)ts.Hours}:{(long)ts.Minutes}:{(long)ts.Seconds}";

                    long   time   = RunTimeWatch.GetRunTimeMilliseconds();
                    double second = (double)(time - _mLastTime) / 1000d;
                    _mLastTime = time;
                    double cpuTime     = mProcess.TotalProcessorTime.TotalMilliseconds;
                    long   cpuFullTime = (long)(second * _mCpuMaxTime);
                    double useTime     = cpuTime - _mLastTotalProcessorTime;
                    _mLastTotalProcessorTime = cpuTime;
                    result.Cpu = (int)((useTime / cpuFullTime) * 10000) / 100d;

                    if (result.Cpu > 100)
                    {
                        result.Cpu = 100;
                    }
                    if (result.Cpu < 0)
                    {
                        result.Cpu = 0;
                    }
                    result.Memory = (Environment.WorkingSet / 1024) / 1024;
                    _mInfo        = result;
                    System.Threading.Interlocked.Exchange(ref _mGetStatus, 0);
                }
            }
            var metrics = memoryMetricsClient.GetMetrics();

            _mInfo.MemoryTotal    = metrics.Total;
            _mInfo.MemoryTotalUse = metrics.Used;
            _mInfo.CpuTotalUse    = GetCpuTotalUse();
            _mInfo.Drives         = AnnoDrives.GetDrivesInfo();
            if (_mInfo.Cpu > _mInfo.CpuTotalUse)
            {
                _mInfo.CpuTotalUse = _mInfo.Cpu;
            }

            _mInfo.EngineCounter = Engine.EngineCounter;
            return(_mInfo);
        }