无限循环和 CPU 在单核和多核上的使用
Infinite loop and CPU usage on single core and multicore
面试官问了我这个问题,我想了解概念,即 CPU 用法如何变化:
1)如果在单核机器单线程设计中无限循环运行
2) 如果死循环是运行多核机器(4核)单线程设计
3)如果在单核机器的多线程设计中死循环运行
4)多核机(4核)多线程设计中如果无限循环运行
5) 当应用程序拥有比硬件核心更多的线程时会发生什么。例如:应用程序在 4 核机器上创建 30 个线程。它会提高应用程序的性能还是降低性能?
6) 当应用程序的线程少于硬件核心时会发生什么。例如:应用程序在 4 核机器上创建 5 个线程。它会提高应用程序的性能还是降低性能?
请您解释一下概念,使事情一目了然。我有很多困惑。
首先,正如评论所述,完整的答案将取决于各种因素,包括:
- OS 及其线程调度程序
- 调整参数/资源限制
- 系统是运行在裸机上还是在管理程序下
- 由于应用程序
的其他原因导致系统负载
- 未提及的应用程序的各个方面...
但是,如果我们做一些简化的假设,我们可以给出一些 "first approximation" 的答案。
1) If infinite loop is running in single-threaded design with single core machine.
我们称之为 base-line 案例;即下面描述的加速比 相对于 这种情况。
2) If infinite loop is running in single-threaded design with multi-core machine ( 4 core )
没有加速。即使有多个内核,应用程序也无法使用它们。一个线程 运行 一次在单个核心上。
3) If infinite loop is running in multi-threaded design with single core machine.
没有加速。虽然有 N 个线程,但每个线程在单个可用内核上平均获得可用执行时间的 1/N。一个核心一次只能运行一个线程。
4)If infinite loop is running in multi-threaded design with multi-core machine (4 core )
加速高达 four-fold。
5) What will happen when application is having more threads then hardware core. For ex : Application is creating 30 threads in 4 core machine. Will it increase the performance of application or decrease the performance ?
如果应用程序是 CPU 绑定的,您将获得不超过 four-fold 的加速。参见上面的 2) 和 3)。
6) What will happen when application is having less threads then hardware core. For ex : Application is creating 5 threads in 4 core machine. Will it increase the performance of application or decrease the performance ?
你的例子没有意义。 5 个线程超过 4 个内核。如果您的意思是 4 个内核上的 3 个线程,那么 CPU 绑定应用程序的最大加速比是 three-fold。
以下是我针对上述内容所做的主要简化假设:
- 除被测应用程序外,系统处于空闲状态。
- 未使用管理程序。
- 足够的物理内存
- 应用程序 CPU 绑定(不是网络或磁盘 I/O 绑定)
- 申请时间长运行宁
- 应用程序对内存的使用并未对可用内存带宽造成压力
- 应用程序线程未争用锁等
- 物理内核,而不是 "HT" 内核
- 应用程序没有相关的OS-imposed资源限制
面试官问了我这个问题,我想了解概念,即 CPU 用法如何变化:
1)如果在单核机器单线程设计中无限循环运行
2) 如果死循环是运行多核机器(4核)单线程设计
3)如果在单核机器的多线程设计中死循环运行
4)多核机(4核)多线程设计中如果无限循环运行
5) 当应用程序拥有比硬件核心更多的线程时会发生什么。例如:应用程序在 4 核机器上创建 30 个线程。它会提高应用程序的性能还是降低性能?
6) 当应用程序的线程少于硬件核心时会发生什么。例如:应用程序在 4 核机器上创建 5 个线程。它会提高应用程序的性能还是降低性能?
请您解释一下概念,使事情一目了然。我有很多困惑。
首先,正如评论所述,完整的答案将取决于各种因素,包括:
- OS 及其线程调度程序
- 调整参数/资源限制
- 系统是运行在裸机上还是在管理程序下
- 由于应用程序 的其他原因导致系统负载
- 未提及的应用程序的各个方面...
但是,如果我们做一些简化的假设,我们可以给出一些 "first approximation" 的答案。
1) If infinite loop is running in single-threaded design with single core machine.
我们称之为 base-line 案例;即下面描述的加速比 相对于 这种情况。
2) If infinite loop is running in single-threaded design with multi-core machine ( 4 core )
没有加速。即使有多个内核,应用程序也无法使用它们。一个线程 运行 一次在单个核心上。
3) If infinite loop is running in multi-threaded design with single core machine.
没有加速。虽然有 N 个线程,但每个线程在单个可用内核上平均获得可用执行时间的 1/N。一个核心一次只能运行一个线程。
4)If infinite loop is running in multi-threaded design with multi-core machine (4 core )
加速高达 four-fold。
5) What will happen when application is having more threads then hardware core. For ex : Application is creating 30 threads in 4 core machine. Will it increase the performance of application or decrease the performance ?
如果应用程序是 CPU 绑定的,您将获得不超过 four-fold 的加速。参见上面的 2) 和 3)。
6) What will happen when application is having less threads then hardware core. For ex : Application is creating 5 threads in 4 core machine. Will it increase the performance of application or decrease the performance ?
你的例子没有意义。 5 个线程超过 4 个内核。如果您的意思是 4 个内核上的 3 个线程,那么 CPU 绑定应用程序的最大加速比是 three-fold。
以下是我针对上述内容所做的主要简化假设:
- 除被测应用程序外,系统处于空闲状态。
- 未使用管理程序。
- 足够的物理内存
- 应用程序 CPU 绑定(不是网络或磁盘 I/O 绑定)
- 申请时间长运行宁
- 应用程序对内存的使用并未对可用内存带宽造成压力
- 应用程序线程未争用锁等
- 物理内核,而不是 "HT" 内核
- 应用程序没有相关的OS-imposed资源限制