• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
无忧岛主
实践是检验真理的唯一标准
博客园    首页    新随笔       管理    订阅  订阅
8、.NET Core 实践 2024-03-29 11:44 CPU过高

Windbg 指令记录

0:008> !runaway
 User Mode Time
  Thread       Time
    7:35c0     0 days 0:03:04.953
    8:111c     0 days 0:03:01.640
    6:4d30     0 days 0:02:57.281
    5:8424     0 days 0:02:52.640
    0:6fe8     0 days 0:00:00.031
    2:6c28     0 days 0:00:00.015
    4:6430     0 days 0:00:00.000
    3:7188     0 days 0:00:00.000
    1:4d0      0 days 0:00:00.000
0:008> ~8
.  8  Id: 6b50.111c Suspend: 0 Teb: 000000d0`f1d77000 Unfrozen
      Start: coreclr!Thread::intermediateThreadProc (00007ffb`e0c63440)
      Priority: 0  Priority class: 32  Affinity: fff
0:008> !clrstack
OS Thread Id: 0x111c (8)
        Child SP               IP Call Site
000000D0F2E7EDA0 00007ffb8117631b Sx.WinDbg.Program.StressCPU()
000000D0F2E7EE30 00007ffbe04a20eb System.Threading.ThreadHelper.ThreadStart_Context(System.Object) [/_/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @ 44]
000000D0F2E7EE60 00007ffbe0499296 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) [/_/src/System.Private.CoreLib/shared/System/Threading/ExecutionContext.cs @ 172]
000000D0F2E7EEE0 00007ffbe048553b System.Threading.ThreadHelper.ThreadStart() [/_/src/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs @ 93]
000000D0F2E7F0F8 00007ffbe0c76ca3 [GCFrame: 000000d0f2e7f0f8] 
000000D0F2E7F360 00007ffbe0c76ca3 [DebuggerU2MCatchHandlerFrame: 000000d0f2e7f360] 

对应的测试代码

using System;
using System.Diagnostics;
using System.Threading;

namespace Sx.WinDbg
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("CPU Stress Test");
            int numThreads = 4; // 可以根据需要调整线程数
            Thread[] threads = new Thread[numThreads];

            for (int i = 0; i < numThreads; i++)
            {
                threads[i] = new Thread(new ThreadStart(ThreadMatch.StressCPU));
                threads[i].Start();
            }

            Console.ReadLine(); // 等待输入以防止程序立即退出
        }

       
    }
}

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace Sx.WinDbg
{
    internal class ThreadMatch
    {
        internal static void StressCPU()
        {
            double result = 0;
            Stopwatch sw = new Stopwatch();

            while (true)
            {
                sw.Start();
                for (int i = 0; i < 1000000; i++)
                {
                    result += Math.Sqrt(i);
                }
                sw.Stop();

                // 大约计算1秒钟,以保持大约100%的CPU使用率
                if (sw.ElapsedMilliseconds < 1000)
                {
                    Thread.Sleep(1000 - (int)sw.ElapsedMilliseconds);
                }
            }
        }
    }
}

 

如果本文引用了你的文章而未注明,请及时联系我。
posted on 2024-03-29 11:44  无忧岛主  阅读(42)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3