chapter01 Introduction 中文
chapter01 Introduction 中文
A Programmer’s Perspective
计算机系统
周学海
[email protected]
0551-63492149
中国科学技术大学
Welcome to ……
• 主讲:周学海([email protected])
• 地点1: 高新区1号学科楼A430A(周五)
• 地点2:西区(高能效智能计算实验室)
智能计算系统
基于NPU的异构计 高性能实时计算
算系统 系统
异构计 时间可预测的异
构计算系统
算系统
可重构计算系统
基于FPGA的异构计
算系统
9/14/2024 xhzhou@USTC 2
Welcome to ……
• 助教:
姓名 电子邮件
张亦博 [email protected]
陈兴焱 [email protected]
• 课程主页:
– bb.ustc.edu.cn
• QQ群号:564480202
9/14/2024 xhzhou@USTC 3
第1章 Introduction
• 计算机系统?
– 系统、模型、物理世界
– 模型vs.现实,抽象vs.具体
– 深入理解计算机系统的重要性
• 举例:5方面具体问题
• 本课程主要内容
– Builder-Centric vs. Programming-Centric
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 4
System
• 系统的概念:
– ISO/IEC/IEEE15288:为了达到一个或多个所描述的目标由相互作
用的要素构成的集合。是 、用于在所定义的环境中提供
产品或服务,以造福于用户和其他利益相关者
– The International Council on Systems Engineering (INCOSE):
完成所定义目标的要素、子系统或组件的集合,这些要素包括产品、
过程、人员、信息、技术、设施、服务和其他支持要素
• In Summary: 系统并不仅仅是一些要素的简单集合,是一个由
一组相互关联的要素构成的、能够实现某个(些)目标的整体。
Which of the following would be considered a system?
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 5
Definition of System
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 6
What are the properties that characterize a system?
• 系统具有层次结构:
– 由若干相互作用要素(部件)组成,
• 系统是一个整体:
– 其性质不仅是它的 ,而且也是 称为emergency(涌现)
• 系统在由其运行环境和其他系统组成的 中运行:
系统的外部视图包含不属于系统但确实与系统交
互的元素。这些元素的集合称为”操作环境或上下文“(包括系统的使用者)
– system boundary (系统边界):系统内部和外部视图产生了系统边界的概念。
• 系统的功能(functionality):
– 通常用系统与其操作环境(特别是用户)的交互来表示。
• 系统结构(System Architecture):
– 系统在其环境中的基本概念或性质。体现在其要素、关系以及其设计和进化的原则中。
• 与系统使用相关的质量属性:
– 例如可维护、可靠、互操作性等。系统有生命周期,从它的初步和概念阶段到它的(退出应用)消亡
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 7
Model
• 我们所认识的系统:物理世界的实际系统的“心
里表征”
• Model:现实世界系统的一种抽象
– 模型是在物理世界中可以实现的
– 模型是为某一目的而开发的感兴趣系统的
– 一个模型应该解决 的关切/需求,
并对系统的工程具有明确的有用性
• Model的特性:
– Mapping:是实体的一种 。
– Reduction:反映了实体的 特性。
– Pragmatic:模型(模拟)要能完成某些特定的功
能,以反应实体的特性。
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 8
Computer System
• 计算机系统:自动进行信息存储、处理和交换
的系统
• 设计和理解复杂的计算机系统:模块化、抽象
化、层次化、分级化
• MBSE:Model-based System
Engineering
• 计算机系统可以有不同层次(视角)的抽象,
– 不同层级的抽象
– 不同视角的抽象
现代计算机系统的抽象层次
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 9
Computer Systems: A Programmer’s
Perspective
• 大多数计算机课程都强调抽象
– Abstract data types
– Asymptotic analysis
• 抽象是有局限性的
– Especially in the presence of bugs
– Need to understand underlying implementations
– Sometimes the abstract interfaces don’t provide the level of control or
performance you need
• 通过学习这门课程,我们可以:
– Become more effective programmers
• Able to find and eliminate bugs efficiently
• Able to understand and tune program performance
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 10
Great Reality #1
#1: Int,Float并不等价于数学中的整数和实数
• Example1:Is x2 ≥ 0?
– Float’s: Yes!
– Int’s:
• 40000 * 40000 --> 1600000000
• 50000 * 50000 -->
• Example2: Is (x + y) + z = x + (y + z)?
– Unsigned & Signed Int’s: Yes!
– Float’s:
• (1e20 + -1e20) + 3.14 --> 3.14
• 1e20 + (-1e20 + 3.14) --> 0
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 11
Computer Arithmetic
• 计算机算术运算是离散量间的(有模)运算
– Arithmetic operations have important mathematical properties
• 不能假定计算机的算术运算满足“通常”的数学属性
– Due to finiteness of representations
– Integer operations
• satisfy “ring” properties:Commutativity, associativity, distributivity
• 给我们带来的启示:
– Need to understand which abstractions apply in which contexts
– Important issues for compiler writers and serious application
programmers
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 12
Code Security Example
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];
• 类似于在FreeBSD的getpeername实现中的代码
• 有许多人都在试图找出程序中的漏洞
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 13
Typical Usage
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 14
Malicious Usage
/* Kernel memory region holding user-accessible data */
#define KSIZE 1024
char kbuf[KSIZE];
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 15
Great Reality #2
#2: 你必须理解熟悉汇编语言
• 虽然可能您永远不会用汇编语言编写程序
– Compilers are much better & more patient than you are
• 但是熟悉汇编代码是理解机器底层执行模型(machine-level execution
model)的关键
– Behavior of programs in presence of bugs
• High-level language model breaks down
– Tuning program performance
• Understand optimizations done/not done by the compiler
• Understanding sources of program inefficiency
– Implementing system software
• Compiler has machine code as target
• Operating systems must manage process state
– Creating/fighting malware
• x86 assembly is the language of choice!
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 16
Assembly Code Example
• Time Stamp Counter
– Special 64-bit register in Intel-compatible machines
– Incremented every clock cycle
– Read with rdtsc instruction (Read Time-Stamp Counter)
• Application
– Measure time required by procedure
– In units of clock cycles
double t;
start_counter();
P();
t = get_counter();
printf("P required %f clock cycles\n", t);
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 17
Code to Read Counter
• 使用GCC的内联asm编写少量的汇编代码
• 编译器将汇编代码插入生成的机器代码中
/* Set *hi and *lo to the high and low order bits
of the cycle counter.
*/
void access_counter(unsigned *hi, unsigned *lo)
{
asm("rdtsc; movl %%edx,%0; movl %%eax,%1"
: "=r" (*hi), "=r" (*lo)
:
: "%edx", "%eax");
}
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 18
Code to Read Counter
/* Record the current value of the cycle counter. */
void start_counter()
{
access_counter(&cyc_hi, &cyc_lo);
}
n cycle Cycles/n
100 961 9.61
1000 8,407 8.41
1,000 8,426 8.43
10,000 82,861 8.29
10,000 82,876 8.29
1,000,000 8,419,907 8.42
1,000,000 8,425,181 8.43
1,000,000,000 8,371,2305,591 8.37
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 20
Great Reality #3 Memory Matters
#3: 计算机中随机读写存储器是一种非物理的抽象概念
(与现实世界有差距)
• 内存不是非受限的(unbounded)
– It must be allocated and managed
– Many applications are memory dominated
• 内存性能不是统一一致的
– Cache and virtual memory effects can greatly affect program
performance
– Adapting program to characteristics of memory system can
lead to major speed improvements
• 尤其内存引用错误非常有害
– Effects are distant in both time and space
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 21
Memory Referencing Bug Example
double fun(int i)
{
volatile double d[1] = {3.14};
volatile long int a[2];
a[i] = 1073741824; /* Possibly out of bounds */
return d[0];
}
fun(0) ➙ 3.14
fun(1) ➙ 3.14
fun(2) ➙ 3.1399998664856
fun(3) ➙ 2.00000061035156
fun(4) ➙ 3.14, then segmentation fault
• 其结果与特定的体系结构相关
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 22
Memory Referencing Bug Example
double fun(int i)
{
volatile double d[1] = {3.14};
volatile long int a[2];
a[i] = 1073741824; /* Possibly out of bounds */
return d[0];
}
fun(0) ➙ 3.14
fun(1) ➙ 3.14
fun(2) ➙ 3.1399998664856
fun(3) ➙ 2.00000061035156
fun(4) ➙ 3.14, then segmentation fault
Explanation: Saved State 4
d7 ... d4 3
d3 ... d0 2 Location accessed by
fun(i)
a[1] 1
a[0] 0
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 23
Memory Referencing Errors
• C和c++不提供任何内存保护
– Out of bounds array references
– Invalid pointer values
– Abuses of malloc/free
• 内存不当引用会导致bug(nasty bugs)
– Whether or not bug has any effect depends on system and compiler
– Action at a distance
• Corrupted object logically unrelated to one being accessed
• Effect of bug may be first observed long after it is generated
• 我们该如何应对?
– Program in Java,Ruby, or ML
– Understand what possible interactions may occur
– Use or develop tools to detect referencing errors (e.g. Valgrind)
– .....
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 24
Great Reality #4:
#4:算法的不同实现,其性能也会有较大差别
• 渐进复杂度分析中的常数因子也很重要
• 甚至精确的操作数量也不能精确预测性能
– Easily see 10:1 performance range depending on how code written
– Must optimize at multiple levels: algorithm, data representations,
procedures, and loops
• 必须了解系统才能更好地优化性能
– How programs compiled and executed
– How to measure program performance and identify bottlenecks
– How to improve performance without destroying code modularity and
generality
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 25
Memory System Performance Example
4.3ms 81.8ms
• 存储按照分层结构组织
• 性能取决于访问模式
– Including how step through multi-dimensional array
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 26
Why The Performance Differs
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 27
Memory Performance Example
• 矩阵-矩阵乘法的实现
– Multiple ways to nest loops
/* ijk */ /* ikj */
for (i=0; i<n; i++) { for (i=0; i<n; i++) {
for (j=0; j<n; j++) { for (k=0; k<n; k++) {
sum = 0.0; r = a[i][k];
for (k=0; k<n; k++) for (j=0; j<n; j++)
sum += a[i][k] * b[k][j]; c[i][j] += r * b[k][j];
c[i][j] += sum; }
} }
}
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 28
Example Matrix Multiplication
Matrix-Matrix Multiplication (MMM) on 2 x Core 2 Duo 3 GHz (double precision)
Gflop/s
160x
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 30
Great Reality #5
#5: 计算机不仅仅是执行计算任务
• 计算机系统需要输入和输出数据
– I/O系统对程序的可靠性和性能至关重要
• 计算机系统通过网络互联并相互通信
– 许多系统级问题出现在网络环境中
• Concurrent operations by autonomous processes
• Coping with unreliable media
• Cross platform compatibility
• Complex performance issues
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 31
Hardware Organization (Naïve)
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 32
第1章 Introduction
• 计算机系统?
– 系统、模型、物理世界
– 模型vs.现实,抽象vs.具体
– 深入理解计算机系统的重要性:
• 举例:5方面具体问题
• 本课程主要内容
– Builder-Centric vs. Programming-Centric
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 33
Course Perspective
• 大多数系统课程是Builder-Centric
• Computer Architecture
– Design pipelined processor in Verilog
• Operating Systems
– Implement large portions of operating system
• Compilers
– Write compiler for simple language
• Networking
– Implement and simulate network protocols
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 34
Course Perspective (Cont.)
• 这门课: Programmer-Centric
• 通过更多地了解底层系统,您可以成为更有效率的程
序员
– 编写更可靠和高效的程序
– 在操作系统中加入需要使用钩子的特性
• 允许软件开发者能够更深入地与操作系统交互,定制和
扩展其功能。
• E.g., concurrency, signal handlers
• ......
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 35
关于这门课……
• 上课地点:G3-113
• 上课时间:2-18周 周五(3,4,5)
• 参考书:
– Randal E. Bryant and David R. O’Hallaron,
“Computer Systems: A Programmer’s
Perspective”, 2015第3版
(http://csapp.cs.cmu.edu/
– 中译本,深入理解计算机系统,龚亦利等译,机
械工业出版社(第3版,2016)
– Samuel P. Harbison III and Guy L. Steele Jr., “C
A Reference Manual 5th Edition”,Prentice Hall,
2002
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 36
Grading
• 实验 : 50%
• 平时作业: 10%
• 期末考试: 40%
• ......
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 37
About the Textbook
• 作者:
– Randal E. Bryant: CMU, Professor, ACM Fellow & IEEE Fellow
– O’Hallaron: CMU,Professor
• 教材结构:
1. Introduction
2. Representing and Manipulating Information
3. Machine-Level Representing of Programs
4. Processor Architecture 程序结构和运行
5. Optimizing Program Performance
6. The Memory Hierarchy
7. Linking
8. Exceptional Control Flow 在系统上运行
9. Virtual Memory
10. System-Level I/O
11. Network Programming 程序间的交互和通信
12. Concurrent Programming
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 38
Course Structure
• Lectures:
– Introduction (chap. 1)
– Information Representing (chap. 2)
– Machine Language (chap. 3)
– Processor Architecture (chap. 4)
– Code Optimization (chap. 5)
– Memory Hierarchy (chap. 6)
– Linking (chap.7)
– Exception Control(chap.8)
– Virtual Memory(Chap.9)
– I/O(Chap.10)
– Network Programming(Chap.11)
– Concurrent Programming(Chap.12)
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 39
Lecture topics and assignments
• 程序和数据
• 主要内容
– Bits operations, arithmetic, assembly language programs
– Representation of C control and data structures
• 实验
– L1: Manipulating bits
– L2: Defusing a binary bomb
– L3: Hacking a buffer bomb
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 40
The Memory Hierarchy
• 存储层次
• 主要内容
– Memory technology, memory hierarchy, caches, disks,
locality
• 实验
– L4 (Perflab): Optimize the performance of an application
kernel function.
• Learn how to exploit locality in your programs.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 41
Linking and Exceptional Control Flow
• 链接和异常控制流
• 主要内容
– Object files, static and dynamic linking, libraries,
loading
– Hardware exceptions, processes, process control,
Unix signals, nonlocal jumps
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 42
Virtual memory
• 虚拟存储
• 主要内容
– Virtual memory, address translation, dynamic storage
allocation
• 实验
– L5: Writing your own malloc package
• Get a real feel for systems programming
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 43
I/O, Networking, and Concurrency
• 系统I/O、网络、并发
• 主要内容
– High level and low-level I/O, network
programming
– Internet services, Web servers
– concurrency, concurrent server design, threads
– I/O multiplexing with select.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 44
L1:Data Lab
• 实验要求:
– Students implement simple logical and
arithmetic functions, but using a highly restricted
subset of C.
– For example, they must compute the absolute
value of a number using only bit-level operations.
• 实验目的:
– This lab helps students understand the bit-level
representations of C data types and the bit-level
behavior of the operations on data.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 45
L2:Bomb Lab
• 实验要求:
– A "binary bomb" is a program provided to students as an object
code file.
– When run, it prompts the user to type in 6 different strings. If any
of these is incorrect, the bomb ``explodes,'' printing an error
message and logging the event on a grading server.
– Students must ``defuse'' their own unique bomb by
disassembling and reverse engineering the program to
determine what the 6 strings should be.
• 实验目的:
– The lab teaches students to understand assembly language, and
also forces them to learn how to use a debugger. It's also great
fun. A legendary lab among the CMU undergrads.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 46
L3: Buffer Lab
• 实验要求:
– Students modify the run-time behavior of a
binary executable by exploiting a buffer overflow
bug.
• 实验目的:
– This lab teaches the students about the stack
discipline and teaches them about the danger of
writing code that is vulnerable to buffer overflow
attacks.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 47
L4:Performance Lab
• 实验要求:
– Students must optimize the performance of an
application kernel function such as convolution
or matrix transposition.
• 实验目的:
– This lab provides a very clear demonstration of
the properties of cache memories, and gives
students experience with low-level program
optimization.
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 48
L5: Malloc Lab
• 实验要求:
– Students implement their own versions of malloc,
free, and realloc.
• 实验目的:
– This lab gives students a clear understanding of
data layout and organization, and requires them
to evaluate different trade-offs between space
and time efficiency.
– One of our favorite labs. When students finish
this one, they really understand pointers!
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 49
Acknowledgements
9/14/2024 Bryant and O'Hallaron, Computer Systems: A Programmer's Perspective, Third Edition 50