Windbg程序调试系列5-高CPU问题分析

 上篇博客中给大家分享了使用Windbg进行Live Debugging:

当然,这个CPU使用率是整个所有核心CPU的使用率。比如我本机是8核心的CPU。整体的CPU使用率 在某一瞬间是14%。

这个CPU使用率是如何计算出来的,有两个重要的时间sysTime和idleTime:

sysTime:表示该时间段内总的CPU时间=CPU处于用户态和内核态CPU时间的总和,即sysTime =kerneTimel + userTime

(注:这里并不包括idleTime,因为当CPU处于空闲状态时,是在内核模式下运行System Idle Process这个进程,所以kernelTime实际上已经包含了idleTime);

idleTime:表示在该时间段内CPU处于空闲状态的时间;

1
CPU% = 1 – idleTime / sysTime * 100

 说到这里,我们分析一个应用的高cpu问题,更多的是:分析用户态的CPU耗时。即,我们应用程序本身运行时消耗的CPU时间片总和。

然后,进入今天的正题,使用Windbg分析高CPU问题:

一、首先我们用C#写一个Any CPU架构的Console模拟程序

复制代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks; namespace HighCpuDemo{    class Program    {        static void Main(string[] args)        {            var normalThread = new Thread(NormalMethod);            normalThread.Start();             var longRunningThread = new Thread(LongRunningMethod);            longRunningThread.Start();             Console.ReadKey();        }         private static void NormalMethod()        {            int a = 0;            int b = 100000;            var list = new List<int>();            for (int i = 0; i < b; i++)            {                a += i;                list.Add(a);                var max = list.Max();                var min = list.Min();                var avg = list.Average();                Console.WriteLine(string.Format("Thread:{0}, writeline:{1}", Thread.CurrentThread.ManagedThreadId, a));                 //休息一下                Thread.Sleep(100);            }        }         private static void LongRunningMethod()        {            for (int c = 0; c < 100000; c++)            {                int a = 0;                int b = 100000;                var list = new List<int>();                for (int i = 0; i < b; i++)                {                    a += i;                    list.Add(a);                &nb
                    
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信