3

Docker不做虚拟化内核,对.NET有什么影响? - Broder

 1 year ago
source link: https://www.cnblogs.com/Z7TS/p/17098398.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

前两天刷抖音,看见了这样一个问题。
问题:容器化不做虚拟内核,会有什么弊端?Java很多方法会跟CPU的核数有关,这个时候调用系统函数,读到的是宿主机信息,而不是我们限制资源的大小。
思考:在我们.NET中是否也会出现这种问题呢?

1. 准备程序

在我们.NET中,并行编程(Parallel)或者线程池(ThreedPool)中,默认会根据CPU数量对我们进行线程分配。
于是我就从Parallel中,找到TaskReplicator类(该类主要用于同时在一个或多个Task中运行委托)下的GenerateCooperativeMultitaskingTaskTimeout方法。

private static int GenerateCooperativeMultitaskingTaskTimeout()
{
    // This logic ensures that we have a diversity of timeouts across worker tasks (100, 150, 200, 250, 100, etc)
    // Otherwise all worker will try to timeout at precisely the same point, which is bad if the work is just about to finish.
    int period = Environment.ProcessorCount;
    int pseudoRnd = Environment.TickCount;
    return CooperativeMultitaskingTaskTimeout_Min + (pseudoRnd % period) * CooperativeMultitaskingTaskTimeout_Increment;
}

抽取获取处理器数量方法Environment.ProcessorCount,放到控制台中。

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("获取宿主机器处理器数量:"+Environment.ProcessorCount);
        Console.ReadLine();
    }
}

2. 环境准备

  1. 本机CPU--6个

1148127-20230207143323240-483408757.png
  1. 虚拟机分配CPU--4个

1148127-20230207143358407-1208189966.png
  1. Docker分配CPU--1个

1148127-20230207143533518-1205718161.png

程序最终获取CPU数量是虚拟机的数量

采用cpus结果:

1148127-20230207143614672-1384109667.png

采用–cpuset-cpus命令结果:

1148127-20230207155507804-2029211229.png

Docker和虚拟机的区别:

1148127-20230207143939773-553188493.png

__EOF__

本文作者: Broder 本文链接: https://www.cnblogs.com/Z7TS/p/17098398.html 关于博主: 评论和私信会在第一时间回复。或者直接私信我。 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处! 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK