Open takeshi-1000 opened 2 years ago
LLVMがバックエンドと呼ばれるってことはそういうことなのか?
機械語が目標とするのは
https://wa3.i-3-i.info/word17530.html
実メモリ、仮想メモリの話
プログラムから見たメモリは実メモリの大きさよりも大きく見える。 仮想メモリは、実メモリ以上の容量をハードディスクで担保できるだからとのこと。
この辺の考え方必要になってくるかな?
↑ 上記の技をスワップ という
システムコールはアプリケーションに対して、ハードウェアを操作するための抽象化されたシンプルなインターフェイスを提供しています。これによってアプリケーションのコードは、背後にあるハードウェアの詳細に関して意識する必要がなくなります。
リンクとは、コンパイラが出力した機械語をOSに依存する実行形式に変換する
コンパイルは二種類の文脈で使われていることがありそう
正確には後者の意味であるが、前者のニュアンスが広義の意味として広まっている感もある
この辺のコメントは https://shop.cqpub.co.jp/hanbai/books/38/38071/38071.pdf のあたりから借用
==
リンク後の実行形式は、CPUが実行できるベタな機械語にしたものではなく、 ヘッダ情報を持ちある特定のフォーマットに従っている
CPUはメインメモリ上の実行コードしか実行できない。 それゆえにリンク作業を通じて実行形式が出来上がるが、それはファイルシステム上に格納されるので、 ローダを用いてロードという作業を通じて、メインメモリへのロードが必要になる
オブジェクトファイルには、シンボル情報が格納されている
オブジェクトファイル -> [リンカ] -> 実行形式
全てのオブジェクトファイルをリンクする最終リンクの段階で、 メモリ上の固定アドレスに割り当てられる? このアドレスの割り当て作業をrelocationという
再配置可能の二つの条件
objectファイル発見
$gcc hello.c -o hello -v
$gcc hello.c
それぞれのサイズ
OSが上記の実行形式をメモリ上に展開していく
実行形式を実行
$objdump -h hello
セクションが8種類
上の画像からもわかるようにmacでは mach-o
という実行形式のファイルフォーマットが採用されている
nm コマンドはファイル内のシンボル情報を返す
$ nm values_sub.o
0000000000000010 D _c
0000000000000028 S _ci1
000000000000002c S _ci2
0000000000000004 C _cio
0000000000000000 T _func
0000000000000004 C _i0
0000000000000014 D _i2
0000000000000098 S _il
0000000000000018 D _p1
0000000000000008 C _po
0000000000000020 D _str
プロセス -> システムコール [ユーザーモード] -> [カーネルモード]
$ sysctl machdep.cpu.brand_string
machdep.cpu.brand_string: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
sysctl -a machdep.cpu
OSはハードウェアとアプリケーション/ミドルウェアとの仲介役
アプリケーション ⇅ OS ⇅ ハードウェア
ハードウェアへの実行の詳細をもち、 アプリケーションとのインターフェースを持つ
カーネルモードで動作する、OSの処理をまとめたプログラムをカーネルと呼ぶ
カーネルはCPUやメモリなどのリソース管理をしている
システムコールはカーネルに処理を依頼する前提っぽい
システム
が即ちハードウェアのことでそれを操作するためのプログラムがカーネルにあるので
ユーザーモードのプロセスから、システムコール依頼を受け、 CPUに割り込みイベントが発生し、カーネルの処理が発生する? 終わったら再びユーザーモードに戻り、プロセスの処理が継続される?
top -o cpu
-> リアルタイムのcpuとプロセスの状況が確認できる
straceコマンドは、linux向けなのでmac向けっぽいdtrussを使用
$ sudo dtruss -a ./hello
dtrace: system integrity protection is on, some features will not be available
PID/THRD RELATIVE ELAPSD CPU SYSCALL(args) = return
hello world!
9935/0xf4919: 1181: 0: 0 fork() = 0 0
9935/0xf4919: 1185 66 3 access("/AppleInternal/XBS/.isChrooted\0", 0x0, 0x0) = -1 2
9935/0xf4919: 1370 28 24 bsdthread_register(0x7FF81550C020, 0x7FF81550C00C, 0x2000) = 1073742303 0
9935/0xf4919: 1431 8 5 shm_open(0x7FF8153DAF5D, 0x0, 0x153D97BA) = 3 0
9935/0xf4919: 1432 3 0 fstat64(0x3, 0x7FF7BD96D2A0, 0x0) = 0 0
9935/0xf4919: 1438 8 4 mmap(0x0, 0x2000, 0x1, 0x40001, 0x3, 0x0) = 0x1026A3000 0
9935/0xf4919: 1441 3 1 close(0x3) = 0 0
9935/0xf4919: 1724 4 1 ioctl(0x2, 0x4004667A, 0x7FF7BD96D354) = 0 0
9935/0xf4919: 1737 5 2 mprotect(0x1026AA000, 0x1000, 0x0) = 0 0
9935/0xf4919: 1739 2 0 mprotect(0x1026B6000, 0x1000, 0x0) = 0 0
9935/0xf4919: 1756 2 0 mprotect(0x1026B7000, 0x1000, 0x0) = 0 0
9935/0xf4919: 1757 2 0 mprotect(0x1026C3000, 0x1000, 0x0) = 0 0
9935/0xf4919: 1777 3 1 mprotect(0x1026A5000, 0x90, 0x1) = 0 0
9935/0xf4919: 1779 3 1 mprotect(0x1026A5000, 0x90, 0x3) = 0 0
9935/0xf4919: 1785 3 1 mprotect(0x1026A5000, 0x90, 0x1) = 0 0
9935/0xf4919: 1792 3 0 mprotect(0x1026C4000, 0x1000, 0x1) = 0 0
9935/0xf4919: 1797 3 1 mprotect(0x1026C5000, 0x90, 0x1) = 0 0
9935/0xf4919: 1807 3 1 mprotect(0x1026C5000, 0x90, 0x3) = 0 0
9935/0xf4919: 1810 3 1 mprotect(0x1026C5000, 0x90, 0x1) = 0 0
9935/0xf4919: 1812 3 1 mprotect(0x1026A5000, 0x90, 0x3) = 0 0
9935/0xf4919: 1815 3 1 mprotect(0x1026A5000, 0x90, 0x1) = 0 0
9935/0xf4919: 1817 2 0 mprotect(0x1026C4000, 0x1000, 0x3) = 0 0
9935/0xf4919: 1819 2 0 mprotect(0x1026C4000, 0x1000, 0x1) = 0 0
9935/0xf4919: 1943 3 0 issetugid(0x0, 0x0, 0x0) = 0 0
9935/0xf4919: 2360 8 4 getentropy(0x7FF7BD96D170, 0x20, 0x0) = 0 0
9935/0xf4919: 2362 3 0 getentropy(0x7FF7BD96D1D0, 0x40, 0x0) = 0 0
9935/0xf4919: 2431 3 0 getpid(0x0, 0x0, 0x0) = 9935 0
9935/0xf4919: 2456 5 2 stat64("/AppleInternal\0", 0x7FF7BD96D870, 0x0) = -1 2
9935/0xf4919: 2484 5 1 csops_audittoken(0x26CF, 0x7, 0x7FF7BD96D3A0) = -1 22
9935/0xf4919: 2487 5 2 proc_info(0x2, 0x26CF, 0xD) = 64 0
9935/0xf4919: 2502 3 0 csops_audittoken(0x26CF, 0x7, 0x7FF7BD96D490) = -1 22
9935/0xf4919: 2513 10 5 sysctlbyname(kern.osvariant_status, 0x15, 0x7FF7BD96D8C0, 0x7FF7BD96D8B8, 0x0) = 0 0
9935/0xf4919: 2517 2 0 csops(0x26CF, 0x0, 0x7FF7BD96D8F4) = 0 0
9935/0xf4919: 2579 65 3 mprotect(0x1025A1000, 0x100000, 0x1) = 0 0
9935/0xf4919: 2586 3 0 getrlimit(0x1008, 0x7FF7BD96E750, 0x0) = 0 0
9935/0xf4919: 2599 5 2 fstat64(0x1, 0x7FF7BD96E738, 0x0) = 0 0
9935/0xf4919: 2601 2 0 ioctl(0x1, 0x4004667A, 0x7FF7BD96E784) = 0 0
dtrace: error on enabled probe ID 1688 (ID 962: syscall::write_nocancel:return): invalid kernel access in action #13 at DIF offset 68
CALL COUNT
access 1
bsdthread_register 1
close 1
csops 1
exit 1
getpid 1
getrlimit 1
issetugid 1
mmap 1
proc_info 1
shm_open 1
stat64 1
sysctlbyname 1
write_nocancel 1
csops_audittoken 2
fstat64 2
getentropy 2
ioctl 2
mprotect 16
c言語でのプログラムにprint文を2つにし、
#include <stdio.h>
int main(void){
printf("hello world!\n");
printf("hello world2!\n");
return 0;
}
gcc hello.c -o hello
でコンパイルし、
再度 sudo dtruss -a ./hello
をすると、 write_nocancel
が2つに増えた
$ sudo dtruss -a ./hello
Password:
dtrace: system integrity protection is on, some features will not be available
PID/THRD RELATIVE ELAPSD CPU SYSCALL(args) = return
hello world!
hello world2!
10011/0xf5ec6: 845: 0: 0 fork() = 0 0
10011/0xf5ec6: 848 65 2 access("/AppleInternal/XBS/.isChrooted\0", 0x0, 0x0) = -1 2
10011/0xf5ec6: 891 27 23 bsdthread_register(0x7FF81550C020, 0x7FF81550C00C, 0x2000) = 1073742303 0
10011/0xf5ec6: 922 7 4 shm_open(0x7FF8153DAF5D, 0x0, 0x153D97BA) = 3 0
10011/0xf5ec6: 924 2 0 fstat64(0x3, 0x7FF7B3E652A0, 0x0) = 0 0
10011/0xf5ec6: 930 9 5 mmap(0x0, 0x2000, 0x1, 0x40001, 0x3, 0x0) = 0x10C1AB000 0
10011/0xf5ec6: 934 3 1 close(0x3) = 0 0
10011/0xf5ec6: 945 3 1 ioctl(0x2, 0x4004667A, 0x7FF7B3E65354) = 0 0
10011/0xf5ec6: 957 5 3 mprotect(0x10C1B2000, 0x1000, 0x0) = 0 0
10011/0xf5ec6: 959 3 1 mprotect(0x10C1BE000, 0x1000, 0x0) = 0 0
10011/0xf5ec6: 977 2 0 mprotect(0x10C1BF000, 0x1000, 0x0) = 0 0
10011/0xf5ec6: 978 2 0 mprotect(0x10C1CB000, 0x1000, 0x0) = 0 0
10011/0xf5ec6: 998 3 1 mprotect(0x10C1AD000, 0x90, 0x1) = 0 0
10011/0xf5ec6: 1000 4 1 mprotect(0x10C1AD000, 0x90, 0x3) = 0 0
10011/0xf5ec6: 1007 3 1 mprotect(0x10C1AD000, 0x90, 0x1) = 0 0
10011/0xf5ec6: 1013 3 0 mprotect(0x10C1CC000, 0x1000, 0x1) = 0 0
10011/0xf5ec6: 1019 3 1 mprotect(0x10C1CD000, 0x90, 0x1) = 0 0
10011/0xf5ec6: 1029 3 1 mprotect(0x10C1CD000, 0x90, 0x3) = 0 0
10011/0xf5ec6: 1032 3 1 mprotect(0x10C1CD000, 0x90, 0x1) = 0 0
10011/0xf5ec6: 1034 3 1 mprotect(0x10C1AD000, 0x90, 0x3) = 0 0
10011/0xf5ec6: 1038 3 1 mprotect(0x10C1AD000, 0x90, 0x1) = 0 0
10011/0xf5ec6: 1039 2 0 mprotect(0x10C1CC000, 0x1000, 0x3) = 0 0
10011/0xf5ec6: 1042 2 0 mprotect(0x10C1CC000, 0x1000, 0x1) = 0 0
10011/0xf5ec6: 1058 3 0 issetugid(0x0, 0x0, 0x0) = 0 0
10011/0xf5ec6: 1130 5 1 getentropy(0x7FF7B3E65170, 0x20, 0x0) = 0 0
10011/0xf5ec6: 1131 2 0 getentropy(0x7FF7B3E651D0, 0x40, 0x0) = 0 0
10011/0xf5ec6: 1203 3 0 getpid(0x0, 0x0, 0x0) = 10011 0
10011/0xf5ec6: 1211 4 2 stat64("/AppleInternal\0", 0x7FF7B3E65870, 0x0) = -1 2
10011/0xf5ec6: 1239 5 1 csops_audittoken(0x271B, 0x7, 0x7FF7B3E653A0) = -1 22
10011/0xf5ec6: 1242 4 2 proc_info(0x2, 0x271B, 0xD) = 64 0
10011/0xf5ec6: 1257 3 0 csops_audittoken(0x271B, 0x7, 0x7FF7B3E65490) = -1 22
10011/0xf5ec6: 1268 10 6 sysctlbyname(kern.osvariant_status, 0x15, 0x7FF7B3E658C0, 0x7FF7B3E658B8, 0x0) = 0 0
10011/0xf5ec6: 1272 2 0 csops(0x271B, 0x0, 0x7FF7B3E658F4) = 0 0
10011/0xf5ec6: 1327 66 3 mprotect(0x10C0A9000, 0x100000, 0x1) = 0 0
10011/0xf5ec6: 1334 3 0 getrlimit(0x1008, 0x7FF7B3E66750, 0x0) = 0 0
10011/0xf5ec6: 1342 5 3 fstat64(0x1, 0x7FF7B3E66738, 0x0) = 0 0
10011/0xf5ec6: 1344 2 0 ioctl(0x1, 0x4004667A, 0x7FF7B3E66784) = 0 0
dtrace: error on enabled probe ID 1688 (ID 962: syscall::write_nocancel:return): invalid kernel access in action #13 at DIF offset 68
dtrace: error on enabled probe ID 1688 (ID 962: syscall::write_nocancel:return): invalid kernel access in action #13 at DIF offset 68
CALL COUNT
access 1
bsdthread_register 1
close 1
csops 1
exit 1
getpid 1
getrlimit 1
issetugid 1
mmap 1
proc_info 1
shm_open 1
stat64 1
sysctlbyname 1
csops_audittoken 2
fstat64 2
getentropy 2
ioctl 2
write_nocancel 2
mprotect 16
CPU利用率として下記3パターンある
コマンド: top -o cpu
個人で使用しているAmazon Linuxにファイルを転送したりして遊ぶ コマンド
scp -i ~/.ssh/xxxxx.pem fork.c ec2-user@XX.XX.XXX.XXX:/home/study
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
static void child()
{
printf("I'm child! my pid is %d.\n", getpid());
exit(EXIT_SUCCESS);
}
static void parent(pid_t pid_c)
{
printf("I'm parent! my pid is %d and the pid of my child is %d.\n",
getpid(), pid_c);
exit(EXIT_SUCCESS);
}
int main(void)
{
pid_t ret;
ret = fork();
if (ret == -1)
err(EXIT_FAILURE, "fork() failed");
if (ret == 0) {
// child process came here because fork() returns 0 for child process
child();
} else {
// parent process came here because fork() returns the pid of newly created child process (> 1)
parent(ret);
}
// shouldn't reach here
err(EXIT_FAILURE, "shouldn't reach here");
}
fork関数の戻り値を利用して、同じ処理のプログラムを複数のプロセスに分けて処理する
を実現
loop.py の実行時、%idle が 0に
loop.py
while True:
pass
何もしてない時
プロセス実行時に、そのプロセスに対する論理CPUのアイドルの割合が低いほど、スループットが大きくなる…?
論理CPUのリソースを全て使い切る=即ちアイドルした状態が状態がなければないほど、スループットが大きくなる、という理解であってそう
また全然関係ないところの話になるが、下記のメモリとCPU間のデータの流れは本を読む前と後ではイメージの湧き方が違う。 本を読んだ後だと、レジスタ含め全てcpuであるといった方がよく、ALUがいわゆる=CPU的な話がちょこちょこ見られていた気がするが、処理の複雑度的にALUを構成する回路の数が多いんだろうな
[ec2-user@ip-10-0-1-73 ~]$ free
total used free shared buffers cached
Mem: 596480 484784 111696 164 57584 124852
-/+ buffers/cache: 302348 294132
Swap: 0 0 0
OOM Killer
メモリ量が増大し、容量オーバーになった場合に、任意のプロセスを終了するプログラム?
仮想メモリ、実メモリイメージ
仮想アドレス
[ec2-user@ip-10-0-1-73 ~]$ readelf -h /bin/sleep
ELF ヘッダ:
マジック: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
クラス: ELF64
データ: 2 の補数、リトルエンディアン
バージョン: 1 (current)
OS/ABI: UNIX - System V
ABI バージョン: 0
型: EXEC (実行可能ファイル)
マシン: Advanced Micro Devices X86-64
バージョン: 0x1
エントリポイントアドレス: 0x401770
プログラムの開始ヘッダ: 64 (バイト)
セクションヘッダ始点: 26176 (バイト)
フラグ: 0x0
このヘッダのサイズ: 64 (バイト)
プログラムヘッダサイズ: 56 (バイト)
プログラムヘッダ数: 8
セクションヘッダ: 64 (バイト)
セクションヘッダサイズ: 29
セクションヘッダ文字列表索引: 28
[ec2-user@ip-10-0-1-73 ~]$ cat /proc/21404/maps
00400000-004dc000 r-xp 00000000 ca:01 2747 /bin/bash
006db000-006e5000 rw-p 000db000 ca:01 2747 /bin/bash
006e5000-006ea000 rw-p 00000000 00:00 0
0070a000-0076d000 rw-p 00000000 00:00 0 [heap]
7fd06a112000-7fd06a11e000 r-xp 00000000 ca:01 2700 /lib64/libnss_files-2.17.so
7fd06a11e000-7fd06a31d000 ---p 0000c000 ca:01 2700 /lib64/libnss_files-2.17.so
7fd06a31d000-7fd06a31e000 r--p 0000b000 ca:01 2700 /lib64/libnss_files-2.17.so
7fd06a31e000-7fd06a31f000 rw-p 0000c000 ca:01 2700 /lib64/libnss_files-2.17.so
7fd06a31f000-7fd06a325000 rw-p 00000000 00:00 0
7fd06a325000-7fd07084f000 r--p 00000000 ca:01 20457 /usr/lib/locale/locale-archive
7fd07084f000-7fd070a12000 r-xp 00000000 ca:01 2682 /lib64/libc-2.17.so
7fd070a12000-7fd070c12000 ---p 001c3000 ca:01 2682 /lib64/libc-2.17.so
7fd070c12000-7fd070c16000 r--p 001c3000 ca:01 2682 /lib64/libc-2.17.so
7fd070c16000-7fd070c18000 rw-p 001c7000 ca:01 2682 /lib64/libc-2.17.so
7fd070c18000-7fd070c1d000 rw-p 00000000 00:00 0
7fd070c1d000-7fd070c1f000 r-xp 00000000 ca:01 2688 /lib64/libdl-2.17.so
7fd070c1f000-7fd070e1f000 ---p 00002000 ca:01 2688 /lib64/libdl-2.17.so
7fd070e1f000-7fd070e20000 r--p 00002000 ca:01 2688 /lib64/libdl-2.17.so
7fd070e20000-7fd070e21000 rw-p 00003000 ca:01 2688 /lib64/libdl-2.17.so
7fd070e21000-7fd070e3e000 r-xp 00000000 ca:01 2732 /lib64/libtinfo.so.5.7
7fd070e3e000-7fd07103d000 ---p 0001d000 ca:01 2732 /lib64/libtinfo.so.5.7
7fd07103d000-7fd071042000 rw-p 0001c000 ca:01 2732 /lib64/libtinfo.so.5.7
7fd071042000-7fd071064000 r-xp 00000000 ca:01 2675 /lib64/ld-2.17.so
7fd07122e000-7fd071252000 r--p 00000000 ca:01 2767 /usr/share/locale/ja/LC_MESSAGES/bash.mo
7fd071252000-7fd071259000 r--s 00000000 ca:01 132641 /usr/lib64/gconv/gconv-modules.cache
7fd071259000-7fd07125c000 rw-p 00000000 00:00 0
7fd071260000-7fd071263000 rw-p 00000000 00:00 0
7fd071263000-7fd071264000 r--p 00021000 ca:01 2675 /lib64/ld-2.17.so
7fd071264000-7fd071265000 rw-p 00022000 ca:01 2675 /lib64/ld-2.17.so
7fd071265000-7fd071266000 rw-p 00000000 00:00 0
7ffcd2b81000-7ffcd2ba2000 rw-p 00000000 00:00 0 [stack]
7ffcd2bc7000-7ffcd2bca000 r--p 00000000 00:00 0 [vvar]
7ffcd2bca000-7ffcd2bcc000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
メモリとストレージ(ハードディスク)は、そもそも保存する原理が異なる
なんかメモ書きなどあれば、ここに貯める