浏览代码

优化状态日志的产生,对系统状态持续跟踪平均值

yuzhengyang 7 年之前
父节点
当前提交
17ae0f427e

二进制
Fork.Net/.vs/Fork.Net/v15/Server/sqlite3/storage.ide


二进制
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide


+ 4 - 2
Fork.Net/Azylee.Utils/Azylee.Core/AppUtils/AppInfoTool.cs

@@ -59,8 +59,10 @@ namespace Azylee.Core.AppUtils
             //当前时间
             var current = process.TotalProcessorTime;
             //间隔时间内的CPU运行时间除以逻辑CPU数量
-            double value = (current - begin).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
-            if (value < 0 || 100 < value) return 0;
+            var minus = current - begin;
+            double value = minus.TotalMilliseconds / Environment.ProcessorCount / interval * 100;
+            if (value < 0) return 0;
+            if (100 < value) return 100;
             return value;
         }
         /// <summary>

+ 67 - 23
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLog.cs

@@ -44,15 +44,16 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
 
         #region 基础属性
         const string LOG_PATH = @"azylee.log";//存储路径
+        const int Interval = 1000;//监测间隔时间
+        const int WriteInterval = 60 * Interval;//写出间隔时间
 
         private int CACHE_DAYS = 30;//缓存天数
         private string LogPath = AppDomain.CurrentDomain.BaseDirectory + LOG_PATH;//存储路径
         private DateTime Time = DateTime.Now;//标记当前时间
-        private int Interval = 60 * 1000;//监测间隔时间
         private Task Listener = null;//监测任务
+        private Process AppProcess = Process.GetCurrentProcess();
         private CancellationTokenSource CancelToken = new CancellationTokenSource();//监测取消Token
         private PerformanceCounter ComputerProcessor = ComputerStatusTool.Processor();//电脑CPU监控
-        private PerformanceCounter AppProcessor = AppInfoTool.Processor();//程序CPU监控
         #endregion
 
         public void SetLogPath(string path)
@@ -87,11 +88,27 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
                     try
                     {
                         WriteConfig();
+                        int runtime = 0;//运行时间(毫秒)
+                        long afk = WindowsAPI.GetLastInputTime();//空闲时间缓存
+                        TimeSpan pin = TimeSpan.Zero;//程序运行时间戳
+                        StatusLogModel status = null;//运行状态信息模型
                         while (!CancelToken.IsCancellationRequested)
                         {
-                            Time = DateTime.Now;
-                            Thread.Sleep(Interval);
-                            WriteStatus();
+                            pin = AppProcess.TotalProcessorTime;//程序运行时间戳
+                            runtime += Interval;//增加运行时间
+                            Thread.Sleep(Interval);//等待间隔时间
+
+                            //每秒钟都会执行的操作
+                            CollectStatus(ref status, runtime, afk, pin);//收集数据
+                            afk = WindowsAPI.GetLastInputTime();//空闲时间缓存
+
+                            //每分钟进行汇总输出
+                            if (runtime >= WriteInterval)
+                            {
+                                WriteStatus(status);//写出数据
+                                runtime = 0;//重置运行时间
+                                status = null;//重置数据
+                            }
                         }
                     }
                     catch { }
@@ -127,31 +144,58 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
             IniTool.WriteValue(file, "system", "drive", ComputerInfoTool.GetSystemDriveTotalSize().ToString());
         }
         /// <summary>
+        /// 收集数据
+        /// </summary>
+        /// <returns></returns>
+        private bool CollectStatus(ref StatusLogModel status, int runtime, long afk, TimeSpan pin)
+        {
+            try
+            {
+                int count = runtime / Interval;//收集次数,用来帮助平均值计算
+
+                if (status == null) status = new StatusLogModel() { Time = DateTime.Now };
+                //固定值数据
+                status.Long = runtime;//运行时长
+                //累计值数据
+                long afktemp = WindowsAPI.GetLastInputTime() - afk;
+                if (afktemp > 0) status.AFK = status.AFK + afktemp;
+                //计算平均值数据
+                int cpu = (int)ComputerProcessor.NextValue();//CPU占用
+                long ram = (long)ComputerInfoTool.AvailablePhysicalMemory();//系统可用内存
+                int appcpu = (int)AppInfoTool.CalcCpuRate(AppProcess, pin, Interval);//程序CPU占用
+                long appram = AppInfoTool.RAM();//程序内存占用
+                long sysdisk = ComputerInfoTool.GetSystemDriveAvailableSize();//系统盘可用空间
+
+                status.CpuPer = ((count - 1) * status.CpuPer + cpu) / count;//CPU占用
+                status.RamFree = ((count - 1) * status.RamFree + ram) / count;//系统可用内存
+                status.AppCpuPer = ((count - 1) * status.AppCpuPer + appcpu) / count;//程序CPU占用
+                status.AppRamUsed = ((count - 1) * status.AppRamUsed + appram) / count;//程序内存占用
+                status.SysDriveFree = ((count - 1) * status.SysDriveFree + sysdisk) / count;//系统盘可用空间
+                return true;
+            }
+            catch { return false; }
+        }
+        /// <summary>
         /// 写出运行时状态信息
         /// </summary>
-        private void WriteStatus()
+        private void WriteStatus(StatusLogModel status)
         {
             try
             {
-                StatusLogModel status = new StatusLogModel()
+                if (status != null)
                 {
-                    Time = Time,
-                    Long = Interval / 1000,
-                    AFK = WindowsAPI.GetLastInputTime() / 1000,
-                    CpuPer = (int)ComputerProcessor.NextValue(),
-                    RamFree = (long)ComputerInfoTool.AvailablePhysicalMemory(),
-                    SysDriveFree = ComputerInfoTool.GetSystemDriveAvailableSize(),
-                    AppCpuPer = (int)AppProcessor.NextValue(),
-                    AppRamUsed = AppInfoTool.RAM(),
-                };
-                //设置日志目录和日志文件
-                string path = DirTool.Combine(LogPath, "status");
-                string file = DirTool.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
-                //创建日志目录
-                DirTool.Create(path);
-                //写出日志
-                TxtTool.Append(file, status.ToString());
+                    //处理比率
+                    status.Long = status.Long / 1000;
+                    status.AFK = status.AFK / 1000;
 
+                    //设置日志目录和日志文件
+                    string path = DirTool.Combine(LogPath, "status");
+                    string file = DirTool.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
+                    //创建日志目录
+                    DirTool.Create(path);
+                    //写出日志
+                    TxtTool.Append(file, status.ToString());
+                }
                 Cleaner();
             }
             catch { }

+ 2 - 2
Fork.Net/Azylee.Utils/Azylee.Core/LogUtils/StatusLogUtils/StatusLogModel.cs

@@ -43,7 +43,7 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
         /// <summary>
         /// 应用程序占用内存
         /// </summary>
-        public double AppRamUsed { get; set; }
+        public long AppRamUsed { get; set; }
 
         public override string ToString()
         {
@@ -64,7 +64,7 @@ namespace Azylee.Core.LogUtils.StatusLogUtils
                 try { if (elements.Length > 5) model.RamFree = long.Parse(elements[5]); } catch { }
                 try { if (elements.Length > 7) model.SysDriveFree = long.Parse(elements[7]); } catch { }
                 try { if (elements.Length > 8) model.AppCpuPer = int.Parse(elements[8]); } catch { }
-                try { if (elements.Length > 9) model.AppRamUsed = double.Parse(elements[9]); } catch { }
+                try { if (elements.Length > 9) model.AppRamUsed = long.Parse(elements[9]); } catch { }
             }
             return model;
         }

+ 10 - 0
Fork.Net/Test/Test.BlackBox/Form1.cs

@@ -2,6 +2,7 @@
 using Azylee.Core.LogUtils.StatusLogUtils;
 using Azylee.Core.WindowsUtils.InfoUtils;
 using System;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 
 namespace Test.BlackBox
@@ -38,6 +39,15 @@ namespace Test.BlackBox
             Log.d("yoyoyoyoyo");
             Log.i("yoyoyoyoyo");
             Log.v("yoyoyoyoyo");
+            Task.Factory.StartNew(() =>
+            {
+                while (true)
+                {
+                    string ceshi = "cpu占用";
+                    ceshi = ceshi + ceshi;
+                    int count = ceshi.Length + ceshi.Length;
+                }
+            });
         }
     }
 }