CPU 2022-02-21
时钟周期
时钟频率(又译:时脉速度,英语:clock rate)是指同步电路中时钟的基础频率,它以“每秒时钟周期”(clock cycles per second)来度量,量度单位采用SI单位赫兹(Hz)。时钟频率测量 CPU 每秒执行的周期数,以 GHz(千兆赫)为单位。在每个周期中,处理器内数十亿个晶体管会打开和关闭,也就是逻辑零状态与逻辑一状态切换一次
CPU 每秒要处理来自不同程序的众多指令(如算术等低级计算)。有时,多个指令可在一个时钟周期内完成;而在其他情况下,一条指令可能需要多个时钟周期来处理。由于不同的 CPU 设计处理指令的方式不同,所以最好比较同一品牌和同一代 CPU 的时钟速度。
例如,5 年前时钟速度更高的 CPU,其性能可能还不如时钟速度更低的新 CPU,因为新架构可能可以更高效地处理指令
超线程、超频和睿频
Hyperthreading lets the processor run multiple threads simultaneously, making better use of its Execution resources. One core poses as two cores and starts fetching instructions from two addresses alternatively.超线程一个物理core产生两个逻辑core
Turbo boost automatically pushes the frequency of individual cores over the standard limit, as long as it's under the power, voltage and temperature threshold.
- 注意,turbo(睿频)和Over-clocking(超频)的不同
Turbo modes are essentially over-clocks for the masses, but you don’t choose the speed; the system does. Turbo是system根据任务需要、外部运行环境(cpu温度等)等动态调整cpu时钟频率的一种技术。Intel cpu的turbo(睿频)默认打开,无需额外配置
Over-clocking is a process that takes a capable processor and changes its clock multiplier. 超频后的cpu时钟速度是人为指定的。Every CPU has a low-level clock that is multiplied in order to reach the number we all know. A CPU with a 300 MHz low-level clock and an 11x multiplier has an effective clock speed of 3.3 GHz.
cpu使用率(占用率)、利用率和负载
使用率(占用率)
称占用率更好理解
简单的说所谓的进程(process) CPU 占用率的定义是在一定时间内占用时间的比例
一个 Chrome 进程在 1s 内占用了一个 CPU 线程(即占用一个core) 0.5s 的时间, 则占用率50%
一个 Chrome 进程在 1s 内占用了一个 CPU 线程(即占用core1) 0.5s 的时间, 占用了另一个 CPU 线程(占用core2) 0.8s 的时间, 则占用率50% + 80% = 130%
mac活动监视器上的cpu%就是指占用率
利用率
利用率 = cpu真正执行指令的时间/cpu被进程占用时间
进程占用cpu,意味着进程拥有cpu的使用权,但并非每时每刻进程都总在使用cpu,俗称占着茅坑不拉屎
load
负载 = cpu正在执行的进程数 + 等待cpu执行的进程数
CPU load is the number of processes which are being executed by CPU or waiting to be executed by CPU.
High load average sometimes implies CPU is overloaded with too many processes.
However, this can be a different case depending on how many CPU cores are installed.
One single CPU core can only handle one task at a time.
The more cores system has, the more tasks system can handle in parallel.
Below is an example to understand the relationship between load average and CPU cores:
23:16:49 up 10:49, 5 user, load average: 1.00, 0.40, 3.35
On single core system this would mean:
- The CPU was fully (100%) utilized on average; 1 process was running on the CPU (1.00) over the last 1 minute.
- The CPU was idle by 60% on average; no processes were waiting for CPU time (0.40) over the last 5 minutes.
- The CPU was overloaded by 235% on average; 2.35 processes were waiting for CPU time (3.35) over the last 15 minutes.
On a dual-core system this would mean: - The one CPU was 100% idle on average, one CPU was being used; no processes were waiting for CPU time(1.00) over the last 1 minute.
- The CPUs were idle by 160% on average; no processes were waiting for CPU time. (0.40) over the last 5 minutes.
- The CPUs were overloaded by 135% on average; 1.35 processes were waiting for CPU time. (3.35) over the last 15 minutes.
我们讨论的是cpu负载而不是core负载,当cpu包含n个core的时候,cpu的100%utilization就是n
负载高,CPU利用率低: 负载高说明等待cpu执行的进程越来越多,利用率低说明当前占用cpu的进程在“空转”,可能是当前占用cpu的进程僵死了
cpu位数
数据总线
CPU的位数指CPU能够一次寄存和处理二进制数码的位数,和CPU寄存器的位数对应,本质上是cpu数据总线的位数,与地址总线的位数无关
16位的CPU的数据线是16条,所以每次内存访问是16位,也就是两个字节,因此每次访问一定是从偶地址开始。如果访问单字节,或偶地址上的字(双字节),只要一次访问。如果访问奇地址的字,则需要取前一个字,与后一个字,再经组合得到要访问的奇地址上的字。一句话,物理上,16位CPU访问内存时总是偶地址访问
32位的CPU的数据线为32条。访问内存数据时总是双字(32位)存取。所以DWORD型数据按双字地址存放是最佳的。WORD要按偶地址存放,免得它跨越两个双字,需要读2次才能读完
这里提一嘴操作系统位数和cpu位数的关系
cpu指令集是一切程序最底层的依赖。操作系统位数就是其依赖的cpu的指令集位数
程序(操作系统、软件等)载入内存后是依赖cpu具体执行的,就操作系统而言,它依赖32位长度的cpu指令集,就是32位操作系统;依赖64位长度的cpu指令集,就是64位操作系统
通常情况下,cpu是向后兼容的,比如64位cpu也支持32位的指令,因此可以在64位CPU上运行32位的操作系统,但是不可在32位cpu上运行64位操作系统
地址总线
地址总线只是一条设计给CPU进行外部寻址的通路
一般64位CPU的地址总线宽度都不到64位
Intel 64 architecture defines a 64-bit linear address. Implementations can support less. The first implementation of IA-32 processors with Intel 64 architecture supports a 48-bit linear address. This means a canonical address must have bits 63 through 48 set to zeros or ones (depending on whether bit 47 is a zero or one)