Lesson 07 虚拟化
在上一讲我们通过 强制模块化 实现了不同设备间的隔离。但物理设备是昂贵的,我们不可能为每一个小任务都买一台新服务器。
这一讲的主题是 虚拟化。它利用“间接层+保持接口”的组合,将有限的物理资源幻化为无限的逻辑资源。
1. 什么是虚拟化?
虚拟化的本质是资源的灵活重组。它通过增加一个间接层,将底层的物理实体(计算、存储、通信)包装成更高层的抽象资源。
- 核心功能:
- 复用:1 变多。让多个任务共享同一个物理资源(如 CPU 分时调度)。
- 聚合:多合 1。将多个物理资源合并为一个巨大的逻辑资源(如分布式存储)。
- 模拟:A 变 B。让一种资源看起来像另一种资源(如在 Mac 上运行 Windows 虚拟机)。
- 保持接口一致。虚拟化后的资源必须提供与原物理资源相同的接口,这样上层软件才不需要任何修改就能直接运行。
2. 三大支柱的虚拟化实现
为了构建一台虚拟计算机,我们需要对硬件的三大件进行全方位改造:
处理器 $\rightarrow$ 线程:
- 手段:分时。
- 核心:为了切换执行流,系统需要保存和恢复任务的状态——包括指令引用(PC)和环境引用(栈指针 SP、寄存器)。
- 收益:多线程可以交叠执行,隐藏 I/O 等待的延时,极大提高了 CPU 利用率。
内存 $\rightarrow$ 虚拟内存:
- 手段:间接层映射。
- 实现:程序访问的是虚拟地址,通过名称解析层映射到真实的物理地址。
- 收益:实现了延迟绑定和隔离。每个程序都觉得自己拥有从 0 开始的连续内存空间,而实际上它们在物理内存中可能是支离破碎的。
链路 $\rightarrow$ 虚拟链路:
- 手段:分组发送。
- 实现:动态分配带宽,而不是物理上的硬连接。
3. 虚拟化的大一统:虚拟计算机
线程 + 虚拟内存 = 虚拟计算机
虚拟计算机 + 虚拟链路 = 强制模块化
4. 对照
Lesson 07 Virtualization
In the previous lesson, we achieved isolation across different devices through Hard Modularity. However, physical equipment is expensive, and it is impossible for us to purchase a new server for every small task.
The theme of this lesson is Virtualization. By utilizing the combination of “indirection + maintaining interfaces,” it transforms finite physical resources into seemingly infinite logical resources.
1. What is Virtualization?
The essence of virtualization is the flexible reorganization of resources. By adding an indirection layer, it wraps underlying physical entities (computing, storage, communication) into higher-level abstract resources.
- Core Functions:
- Multiplexing: Turning 1 into many. Allowing multiple tasks to share the same physical resource (e.g., CPU time-sharing).
- Aggregation: Turning many into 1. Combining multiple physical resources into one giant logical resource (e.g., distributed storage).
- Emulation: Turning A into B. Making one type of resource look like another (e.g., running a Windows virtual machine on a Mac).
- Maintaining Interface Consistency: Virtualized resources must provide the same interface as the original physical resources. This ensures that upper-level software can run directly without any modification.
2. Virtualizing the Three Pillars
To construct a virtual computer, we must transform the three primary hardware components:
Processor $\rightarrow$ Thread:
- Mechanism: Time-sharing (Multiplexing).
- Core: To switch execution flows, the system needs to save and restore task states—including the Instruction Reference (PC) and Environment Reference (Stack Pointer SP, Registers).
- Benefits: Multi-threading allows tasks to overlap execution, hiding the latency of I/O waits and significantly improving CPU utilization.
Memory $\rightarrow$ Virtual Memory:
- Mechanism: Indirection mapping.
- Implementation: Programs access virtual addresses, which are mapped to real physical addresses through a name resolution layer.
- Benefits: Achieves late binding and isolation. Every program perceives itself as possessing a continuous memory space starting from zero, while in reality, they may be fragmented across physical memory.
Link $\rightarrow$ Virtual Link:
- Mechanism: Packet switching.
- Implementation: Dynamically allocating bandwidth rather than relying on rigid physical connections.
3. The Grand Unified Theory of Virtualization: The Virtual Computer
Thread + Virtual Memory = Virtual Computer
Virtual Computer + Virtual Link = Hard Modularity