版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
計算機系統(tǒng)基礎(chǔ)Computer
System
Fundamentals想聽什么?你來定義!sh@歡迎入群,一起
,共同定義課程!Class
1計算機系統(tǒng)概述@sh
注:本節(jié)內(nèi)容大量參考《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);.
.
.}了解你的系統(tǒng)
–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不是整數(shù),?oat不是實數(shù)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];}假設(shè)In
x86架構(gòu)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];}假設(shè)In
x86架構(gòu)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原因了解你的系統(tǒng)
–2內(nèi)存需要被管理和分配堆vs.棧在C/C++中內(nèi)存是不受保護(hù)的數(shù)組越界無效指針內(nèi)存錯誤可能導(dǎo)致非常詭異的問題癥狀跟系統(tǒng)和編譯器的實現(xiàn)相關(guān)可能延遲很久才出現(xiàn)借助內(nèi)存
檢查工具
(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倍!數(shù)組:從概念到現(xiàn)實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數(shù)組的內(nèi)存布局MainmemoryI/ObridgeBusinterfaceALURegister?leCPU
chipSystem
busMemory
busCachememories典型的系統(tǒng)結(jié)構(gòu)L1
CacheL2
CacheMemory110100SRAMSRAMDRAM位置延遲(cycles)介質(zhì)Registers(SRAM)Main
memory(DRAM)Larger,slower,cheaperper
byteRemote
secondarystorage(tapes,
distributed
?le
systems,
Web
servers)Local
disks
hold
?lesretrieved
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了解你的系統(tǒng)
–3內(nèi)存的性能不是均勻的cache和虛擬內(nèi)存特性對性能有極大的影響使程序適應(yīng)于內(nèi)存的特性即使算法復(fù)雜度相同,程序性能可能仍有幾十倍的差異Multiple
threads:
4xVector
instructions:
4xMemory
hierarchy
and
other
optimizations:
20xLinux時鐘中斷周期:
10ms,#de?ne
HZ
100采用x86硬件寄存器Time
Stamp
Counter記錄CPU執(zhí)行的cycle數(shù)指令: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);}}diskmemkernelspaceuserspacesrcdstbufFSBu?erFILE
stream
Bu?er如何高效拷貝文件
–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);}diskmemkernelspaceuserspacesrcdstbufpFSBu?ervoid
*mmap(void
*start,
int
len,int
prot,
int
flags,
int
fd,
int
offset);len
bytesstart(or
addresschosen
bykernel)Process
virtual
memoryDisk
?le
speci?ed
by?le
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
StackdiskmemkernelspaceuserspacefdbufFSBu?ersock如何高效傳輸
文件
–
2ssize_dfile(int
sock,
int
fd,off_t
*offset,
size_t
count);Network
StackdiskmemkernelspaceuserspacefdFSBu?ersockkernel
2.1Network
StackdiskmemkernelspaceuserspacefdFSBu?ersockkernel2.4你需要知道的還有很多CPU的工作機理計算機的功能部件與特點OS的工作機理編譯器的實現(xiàn)軟硬件如何配合、各
層次如何配合通訊與網(wǎng)絡(luò)編程模型OS體系結(jié)構(gòu)為什么要上這門課?課程總體介紹信息的表示Agenda課程內(nèi)容涉及5大基礎(chǔ)領(lǐng)域Computer
ArchitectureOpera<ng
SystemsProgramming
ModelNetworkingDistributed
Systems目標(biāo):理解你看到的系統(tǒng)從場景和實例出發(fā)介紹概念兼顧應(yīng)用
原理
(程序員視角
+
系統(tǒng)工程師視角)體系化,強調(diào)系統(tǒng)全景內(nèi)容組織方式Applica<onDesign
&Implementa<onPrinciplesWhat:
50%How:
30%Why:
20%理解你的系統(tǒng):1.
實例+概念2.應(yīng)用+原理3.
體系化為什么要上這門課?課程總體介紹信息的表示Agenda3.3V2.8V0.5V0.0V010二進(jìn)制的物理表示簡單?性強Byte
=
8
bits–2進(jìn)制:000000002
~
111111112–10進(jìn)制:010
~25510–16進(jìn)制:0016
~
FF16‘0’
~
‘9’,
‘A’
~
‘F’FA1D37B16
在C語言中的表示0xFA1D37B0xfa1d37b000000110001220010330011440100550101660110770111881000991001A101010B111011C121100D131101E141110F151111用bit組成byte邏輯上是一個大“字節(jié)數(shù)組”虛擬內(nèi)存地址空間進(jìn)程私有w連續(xù):0~2-‐1,但有空洞虛實動態(tài)分配:malloc多種形式:sta<c,stack,heap實際–
Memory
Hierarchy?
?
?內(nèi)存以byte組成RegistersL1
cache(SRAM)Main
memory(DRAM)Local
secondary
storage(localdisks)Remote
secondarystorage(tapes,
distributed
?le
systems,
Web
servers)L2
cache(SRAM)L0:L1:L2:L3:L4:L5:字長(word
size)操作數(shù)寬度,地址寬度一般機器采用32位字長(4bytes)地址空間4G高端機器采用64位字長(8bytes)地址空間1.8x1019x86-‐64使用48位地址:256TB多種操作數(shù)寬度1,
2,
4,
8
bytes計算機的字
(word)CData
TypeTypical
32-bitIn
IA32x86-64char111short222int444long448long
long888float444double888long
double810/1210/16pointer448數(shù)據(jù)類型的寬度printf(”%d\n”,
sizeof(long));一個word在內(nèi)存中如何以byte存放?在字節(jié)流中如何排列?交換數(shù)據(jù)時必須一致兩個傳統(tǒng)大尾端(Big
Endian):Sun,PPC
Mac,
InternetLSB
(Least
Signi?cant
Byte)在高地址小尾端(Livle
Endian):x86LSB在低地址字節(jié)序
(byte
ordering)假設(shè)int
x=0x01234567,&x=0x100大尾端–LSB在高地址(閱讀序)0x100
0x1010x102
0x10301234567小尾端–LSB在低地址(逆閱讀序)0x100
0x101
0x102
0x10367452301字節(jié)序的例子MSB
LSB起始地址,漸增4Bytes匯編二進(jìn)制機器碼的文本表示,語義上等價可由程序自動翻譯,objdump例子值:填滿32bits(padding):按byte分解:倒序:0x12ab0x000012ab00
00
12
abab
12
00
00地址8048365:8048366:機器碼5b81
c3ab120000匯編表示
popadd%ebx$0x12ab,%ebx804836c:83
bb2800000000cmpl$0x0,0x28(%ebx)?如何讀數(shù)看一段匯編代碼從byte到數(shù)據(jù)類型整數(shù)
int浮點數(shù)?oat字符串string指針pointer可把任意數(shù)據(jù)類型轉(zhuǎn)化為字節(jié)數(shù)組來觀測–用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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 機械設(shè)計基礎(chǔ)4-習(xí)題及解答 朱龍英 - 第8-14章
- 北京三中2021-2022學(xué)年物理高一第二學(xué)期期末統(tǒng)考模擬試題含解析
- 安徽省六安市金安區(qū)六安市第一中學(xué)2021-2022學(xué)年物理高一第二學(xué)期期末考試試題含解析
- 國慶節(jié)的課件教學(xué)課件
- 2022年浙江省寧波諾丁漢大學(xué)附中物理高一第二學(xué)期期末檢測試題含解析
- 2022年云南省昆明八中物理高一第二學(xué)期期末達(dá)標(biāo)檢測試題含解析
- 2022年物理高一下期末復(fù)習(xí)檢測試題含解析
- 六下語文五單元課件
- 2024年醇酸磁漆項目規(guī)劃申請報告
- 2024年X射線高頻高壓發(fā)生裝置項目立項申請報告模稿
- 第一次月考 (1-2單元)(月考)- 2024-2025學(xué)年六年級上冊數(shù)學(xué)北師大版
- 2024-2030年中國智慧物流行業(yè)發(fā)展分析及發(fā)展前景與趨勢預(yù)測研究報告
- 2024中國鐵路集團(tuán)全國招聘高頻考題難、易錯點模擬試題(共500題)附帶答案詳解
- 4S店內(nèi)部承包經(jīng)營管理合同
- 部編版語文2024-2025學(xué)年七年級上冊第一單元測試卷
- 智能機器人設(shè)計與實踐智慧樹知到答案2024年北京航空航天大學(xué)
- StarterUnit3Welcome!SectionA1a-2d教學(xué)設(shè)計2024-2025學(xué)年新人教版七年級英語上冊
- 2024中華人民共和國農(nóng)村集體經(jīng)濟(jì)組織法詳細(xì)解讀課件
- 拖車損壞賠償協(xié)議書
- 2024礦山尾礦購銷合同范本
- 2024年機械制圖員(中級)技能鑒定理論考試題庫(含答案)
評論
0/150
提交評論