版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
计算机系统基础Computer
System
Fundamentals想听什么?你来定义!sh@欢迎入群,一起
,共同定义课程!Class
1计算机系统概述@sh
注:本节内容大量参考《Computer
Systems:A
Programmer’s
Perspec<ve》一书及课件为什么要上这门课?课程总体介绍信息的表示AgendaSource:/571/*
Kernel
memory
region
holding
user-accessible
data
*/#define
KSIZE
1024char
kbuf[KSIZE];size_t
memcpy(void*,
void*,
size_t);/*
Copy
at
most
maxlen
bytes
from
kernel
region
to
user
buffer
*/int
copy_from_kernel(void
*user_dest,
int
maxlen){/*
Byte
count
len
is
minimum
of
buffer
size
and
maxlen
*/int
len
=
KSIZE
<
maxlen
?
KSIZE
:
maxlen;memcpy(user_dest,
kbuf,
len);return
len;}这段代码有什么问题?/*
Kernel
memory
region
holding
user-accessible
data
*/#define
KSIZE
1024char
kbuf[KSIZE];size_t
memcpy(void*,
void*,
size_t);/*
Copy
at
most
maxlen
bytes
from
kernel
region
to
user
buffer
*/int
copy_from_kernel(void
*user_dest,
int
maxlen)
{/*
Byte
count
len
is
minimum
of
buffer
size
and
maxlen
*/int
len
=
KSIZE
<
maxlen
?
KSIZE
:
maxlen;memcpy(user_dest,
kbuf,
len);return
len;}如果这么用呢?#define
MSIZE
528void
getstuff()
{char
mybuf[MSIZE];copy_from_kernel(mybuf,
-MSIZE);.
.
.}了解你的系统
–1Q1:
Is
x2
≥
0?–
Float’s:
Yes!–
Int’s:40000
*
40000
➙
160000000050000
*
50000
➙
??Q2:
Is
(x
+
y)
+
z
= x
+
(y
+z)?Unsigned
&
Signed
Int’s:
Yes!Float’s:(1e20
+
-‐1e20)
+
3.14
➙
3.141e20
+
(-‐1e20
+3.14)
➙
??int不是整数,float不是实数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];}假设In
x86架构fun(0)fun(1)fun(2)fun(3)fun(4)➙➙➙➙➙?????什么返回值?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];}假设In
x86架构fun(0)fun(1)fun(2)fun(3)fun(4)➙
3.14➙
3.14➙
3.1399998664856➙
2.00000061035156➙
3.14,
then
segmentation
fault返回值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)fun(1)fun(2)fun(3)fun(4)➙
3.14➙
3.14➙
3.1399998664856➙
2.00000061035156➙
3.14,
then
segmentation
faultLoca<on
accessed
byfun(i)Explana<on:Saved
Stated7
...
d4d3
...
d0a[1]a[0]43210原因了解你的系统
–2内存需要被管理和分配堆vs.栈在C/C++中内存是不受保护的数组越界无效指针内存错误可能导致非常诡异的问题症状跟系统和编译器的实现相关可能延迟很久才出现借助内存
检查工具
(e.g.
valgrind)哪个比较快?void
copyji(int
src[2048][2048],intdst[2048][2048]){int
i,j;for
(j
=
0;
j
<
2048;
j++)for
(i
=
0;
i
<
2048;
i++)dst[i][j]
=
src[i][j];}void
copyij(int
src[2048][2048],intdst[2048][2048]){int
i,j;for
(i
=
0;
i
<
2048;
i++)for
(j
=
0;
j
<
2048;
j++)dst[i][j]
=
src[i][j];}Pen<um
4上性能相差21倍!数组:从概念到现实A[2047][2047]A[2047][2046]…A[2047][1]A[2047][0]…A[1][2047]A[1][2046]…A[1][1]A[1][0]A[0][2047]A[0][2046]…A[0][1]A[0][0]x
+
4xx
+
8192x
+
8188x
+
8184数组的内存布局MainmemoryI/ObridgeBusinterfaceALURegisterfileCPU
chipSystem
busMemory
busCachememories典型的系统结构L1
CacheL2
CacheMemory110100SRAMSRAMDRAM位置延迟(cycles)介质Registers(SRAM)Main
memory(DRAM)Larger,slower,cheaperper
byteRemote
secondarystorage(tapes,
distributed
file
systems,
Web
servers)Local
disks
hold
filesretrieved
from
disks
on
remote
network
serversMain
memory
holds
diskblocksretrieved
from
local
disksL2
cache(SRAM)L1
cache
holds
cache
lines
retrieved
from
L2
cacheCPU
registers
hold
words
retrievedfrom
L1
cacheL2
cache
holds
cache
linesretrieved
from
main
memoryL0:L1:
L1
cacheL2:L3:L4:Local
secondary
storage(local
disks)L5:Smaller,faster,costlierper
byte典型的Memory
Hierarchy8M1M128K16K2K01000200030006000500040007000s1s3s5s7s9s11s13s15s3264MSize
(bytes)Readthroughput
(MB/s)Stride
(x8
bytes)L1L2MemL3copyijcopyjiIn Core
i72.67
GHz32
KB
L1
d-cache256
KB
L2
cache8
MB
L3
cacheThe
Memory
Mountain了解你的系统
–3内存的性能不是均匀的cache和虚拟内存特性对性能有极大的影响使程序适应于内存的特性即使算法复杂度相同,程序性能可能仍有几十倍的差异Multiple
threads:
4xVector
instructions:
4xMemory
hierarchy
and
other
optimizations:
20xLinux时钟中断周期:
10ms,#define
HZ
100采用x86硬件寄存器Time
Stamp
Counter记录CPU执行的cycle数指令:rdtsc如何精确计时
–1uint64
t1,
t2;t1
=get_counter();func();t2
=get_counter();printf(“func
required
%u64
clock
cycles,
%f
seconds\n",t2-t1,
(double)(t2-t1)/FREQUENCY);如何精确计时
–2/*
Set
*hi
and
*lo
to
the
high
and
low
order
bits* of
the
cycle
counter.*/void
access_counter(uint32
*hi,
uint32
*lo){asm("rdtsc;
movl
%%edx,%0;
movl
%%eax,%1":
"=r"
(*hi),
"=r"
(*lo)::
"%edx",
"%eax");}uint64
get_counter(){uint64
t;access_counter(((uint32*)&t)+1,
(uint32*)&t);return
t;}如何高效拷贝文件
–1/*
Note:
to
simplify
the
implementation,*
no
failure
handling
is
provided.*/void
copyfile(FILE
*dst_fp,
FILE
*src_fp,int
size){char
buf[4096];while
(size
>
4096)
{fread(buf,
4096,
1,
src_fp);fwrite(buf,
4096,
1,
dst_fp);size
-=
4096;}if
(size
>
0)
{fread(buf,
size,
1,
src_fp);fwrite(buf,
size,
1,
dst_fp);}}diskmemkernelspaceuserspacesrcdstbufFSBufferFILE
stream
Buffer如何高效拷贝文件
–2void
mmapcopy(int
dst_fd,
int
src_fd,
int
size){char
*bufp;bufp
=
mmap(NULL,
size,
PROT_READ,
MAP_PRIVATE,
src_fd,
0);write(dst_fd,
bufp,
size);}diskmemkernelspaceuserspacesrcdstbufpFSBuffervoid
*mmap(void
*start,
int
len,int
prot,
int
flags,
int
fd,
int
offset);len
bytesstart(or
addresschosen
bykernel)Process
virtual
memoryDisk
file
specified
byfile
descriptor
fdlenbytesoffset(bytes)00mmap如何高效传输文件–1/*
Note:
to
simplify
the
implementation,*
no
failure
handling
is
provided.*/void
tranfile(int
sock,
int
fd,int
size){char
buf[4096];while
(size
>
4096)
{read(fd,
buf,
4096);write(sock,
buf,
4096);size
-=
4096;}if
(size
>
0)
{read(fd,
buf,
size);write(sock,
buf,
size);}}Network
StackdiskmemkernelspaceuserspacefdbufFSBuffersock如何高效传输
文件
–
2ssize_dfile(int
sock,
int
fd,off_t
*offset,
size_t
count);Network
StackdiskmemkernelspaceuserspacefdFSBuffersockkernel
2.1Network
StackdiskmemkernelspaceuserspacefdFSBuffersockkernel2.4你需要知道的还有很多CPU的工作机理计算机的功能部件与特点OS的工作机理编译器的实现软硬件如何配合、各
层次如何配合通讯与网络编程模型OS体系结构为什么要上这门课?课程总体介绍信息的表示Agenda课程内容涉及5大基础领域Computer
ArchitectureOpera<ng
SystemsProgramming
ModelNetworkingDistributed
Systems目标:理解你看到的系统从场景和实例出发介绍概念兼顾应用
原理
(程序员视角
+
系统工程师视角)体系化,强调系统全景内容组织方式Applica<onDesign
&Implementa<onPrinciplesWhat:
50%How:
30%Why:
20%理解你的系统:1.
实例+概念2.应用+原理3.
体系化为什么要上这门课?课程总体介绍信息的表示Agenda3.3V2.8V0.5V0.0V010二进制的物理表示简单•性强Byte
=
8
bits–2进制:000000002
~
111111112–10进制:010
~25510–16进制:0016
~
FF16‘0’
~
‘9’,
‘A’
~
‘F’FA1D37B16
在C语言中的表示0xFA1D37B0xfa1d37b000000110001220010330011440100550101660110770111881000991001A101010B111011C121100D131101E141110F151111用bit组成byte逻辑上是一个大“字节数组”虚拟内存地址空间进程私有w连续:0~2-‐1,但有空洞虚实动态分配:malloc多种形式:sta<c,stack,heap实际–
Memory
Hierarchy•
•
•内存以byte组成RegistersL1
cache(SRAM)Main
memory(DRAM)Local
secondary
storage(localdisks)Remote
secondarystorage(tapes,
distributed
file
systems,
Web
servers)L2
cache(SRAM)L0:L1:L2:L3:L4:L5:字长(word
size)操作数宽度,地址宽度一般机器采用32位字长(4bytes)地址空间4G高端机器采用64位字长(8bytes)地址空间1.8x1019x86-‐64使用48位地址:256TB多种操作数宽度1,
2,
4,
8
bytes计算机的字
(word)CData
TypeTypical
32-bitIn
IA32x86-64char111short222int444long448long
long888float444double888long
double810/1210/16pointer448数据类型的宽度printf(”%d\n”,
sizeof(long));一个word在内存中如何以byte存放?在字节流中如何排列?交换数据时必须一致两个传统大尾端(Big
Endian):Sun,PPC
Mac,
InternetLSB
(Least
Significant
Byte)在高地址小尾端(Livle
Endian):x86LSB在低地址字节序
(byte
ordering)假设int
x=0x01234567,&x=0x100大尾端–LSB在高地址(阅读序)0x100
0x1010x102
0x10301234567小尾端–LSB在低地址(逆阅读序)0x100
0x101
0x102
0x10367452301字节序的例子MSB
LSB起始地址,渐增4Bytes汇编二进制机器码的文本表示,语义上等价可由程序自动翻译,objdump例子值:填满32bits(padding):按byte分解:倒序:0x12ab0x000012ab00
00
12
abab
12
00
00地址8048365:8048366:机器码5b81
c3ab120000汇编表示
popadd%ebx$0x12ab,%ebx804836c:83
bb2800000000cmpl$0x0,0x28(%ebx)•如何读数看一段汇编代码从byte到数据类型整数
int浮点数float字符串string指针pointer可把任意数据类型转化为字节数组来观测–用unsigned
char*指向其地址typedef
unsigned
char
*pointer;void
show_bytes(pointer
start,
int
len){int
i;for
(i
=
0;
i
<
len;
i++)printf(”%p\t0x%.2x\n",
start+i,
start[i]);printf("\n");}show_bytes例子int
a
=
15213;printf("int
a
=
15213;\n");show_bytes((pointer)&a,
sizeof(int));Result
(Linux):int
a
=
15213;0x11ffffcb8
0x6d0x11ffffcb9
0x3b0x11ffffcba
0x000x11ffffcbb
0x00Decimal:Binary:Hex:152130011
1011
0110
11013
B
6
D6D3B0000IA32,
x86-6400003B6DSunint
a
=
15213;93C4FFFFIA32,
x86-64FF
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高等数学(第五版)课件 7.2 多元函数微分学
- 公职人员酒驾检讨书格式(标准版)
- 教师节学生代表国旗下的演讲稿范文(3篇)
- 教师晋升职称述职报告【大全12篇】
- 教师最美家庭先进事迹材料(3篇)
- 2024年小班幼儿教师随笔范文大全(17篇)
- 教师度述职报告
- 小学音乐教师的教学计划范文
- 高校教师教学科研总结范文(3篇)
- 感恩教育教师国旗下讲话稿(3篇)
- 新疆地方教材五年级可爱的中国计划、教案
- 船舶贸易智慧树知到答案章节测试2023年上海海事大学
- 《幼儿园中班第一学期家长会》 PPT课件
- 降低剖宫产率的PDCA循环应用.ppt
- 英语介绍自己成立一家餐饮公司的ppt课件
- 中班数学10以内的相邻数课件
- 中博饼游戏规则
- 一些国外先进安全生产管理经验
- 最全的汽车配件产品海关商品编码
- 【EHS流程图】项目安全环保部部门工作流程
- 人体正常菌群与常见致病菌
评论
0/150
提交评论