云计算之路-阿里云上:消灭“黑色n秒”第一招——不让CPU空闲
2014-05-19 17:18
471 查看
class DistributedThread { [DllImport("kernel32.dll")] public static extern int GetCurrentThreadId(); [DllImport("kernel32.dll")] public static extern int GetCurrentProcessorNumber(); private ThreadStart threadStart; private ParameterizedThreadStart parameterizedThreadStart; private Thread thread; public int ProcessorAffinity { get; set; } public Thread ManagedThread { get { return thread; } } private DistributedThread() { thread = new Thread(DistributedThreadStart); } public DistributedThread(ThreadStart threadStart) : this() { this.threadStart = threadStart; } public DistributedThread(ParameterizedThreadStart threadStart) : this() { this.parameterizedThreadStart = threadStart; } public void Start() { if (this.threadStart == null) throw new InvalidOperationException(); thread.Start(null); } public void Start(object parameter) { if (this.parameterizedThreadStart == null) throw new InvalidOperationException(); thread.Start(parameter); } private void DistributedThreadStart(object parameter) { try { // fix to OS thread Thread.BeginThreadAffinity(); // set affinity if (ProcessorAffinity != 0) { CurrentThread.ProcessorAffinity = new IntPtr(ProcessorAffinity); } // call real thread if (this.threadStart != null) { this.threadStart(); } else if (this.parameterizedThreadStart != null) { this.parameterizedThreadStart(parameter); } else { throw new InvalidOperationException(); } } finally { // reset affinity CurrentThread.ProcessorAffinity = new IntPtr(0xFFFF); Thread.EndThreadAffinity(); } } private ProcessThread CurrentThread { get { int id = GetCurrentThreadId(); return (from ProcessThread th in Process.GetCurrentProcess().Threads where th.Id == id select th).Single(); } } }
DistributedThread
接下来,出招——KeepAllCpuCoresAlive!
![](http://images.cnitblog.com/i/35695/201405/191701371214203.png)
结果。。。这一招以失败告终!
![](http://images.cnitblog.com/i/35695/201405/191703530907375.png)
(上图是“黑色1秒”发生时性能监视器监测到的ASP.NET Requests/Sec为0的情况)
失败又如何,就如同代码编译不通过一般不值一提。那为什么还要写博客出来呢?分享的就是过程!
接下来呢?准备第二招。。。
【参考资料】
Running .NET threads on selected processor cores
Threading in C#
Keep Alive the Machine - No Sleep
相关文章推荐
- 云计算之路-阿里云上:对“黑色n秒”问题的最终猜想——CPU C-states引起的
- 云计算之路-阿里云上:消灭“黑色n秒”第二招——给w3wp进程指定CPU核
- 云计算之路-阿里云上:读取缓存时的“黑色0.1秒”
- 云计算之路-阿里云上:节点 CPU 波动引发 docker swarm 集群故障
- 云计算之路-阿里云上: RDS实例CPU跑满引发的故障
- 云计算之路-阿里云上:结合IIS日志分析“黑色30秒”问题
- 云计算之路-阿里云上-幸福没那么容易:“黑色1秒”又出现了
- 云计算之路-阿里云上:为什么看不见CPU在坐过山车,磁盘IO在蹦极
- 云计算之路-阿里云上:消灭“黑色n秒”第三招——禁用网卡的TCP/IP Offload
- 云计算之路-阿里云上:基于Xen的IO模型进一步分析“黑色0.1秒”问题
- 云计算之路-阿里云上:“黑色10秒钟”的新进展
- 云计算之路-阿里云上:从ASP.NET线程角度对“黑色30秒”问题的全新分析
- 云计算之路-阿里云上:14:20-14:55博客后台2台服务器都CPU 100%引发的故障
- 云计算之路-阿里云上:4000IOPS的RDS+16核CPU的负载均衡
- 云计算之路-阿里云上:神奇的“黑色30秒”再次出现,究竟是谁的错?
- 云计算之路-阿里云上:启用Windows虚拟内存引发的CPU 100%故障
- 云计算之路-阿里云上:Linux内核bug引起的“黑色10秒钟”
- 云计算之路:用阿里云 vs Azure的对比测试揭开乌云的面纱
- 云计算之路-阿里云上:数据库写入超时的问题终于真相大白