Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
カーネルから見る OCIランタイム
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
0n1shi
March 22, 2019
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
カーネルから見る OCIランタイム
0n1shi
March 22, 2019
More Decks by 0n1shi
See All by 0n1shi
コンテナエンジンの作り方 ~ さくらの夕べ ヤンジェネバトル ~
0n1shi
4
1.9k
パーティションとファイルシステムと
0n1shi
3
890
コンテナの実現とその実装
0n1shi
0
230
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
300
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
500
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
330
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
500
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
The untapped power of vector embeddings
frankvandijk
2
1.9k
Everyday Curiosity
cassininazir
0
320
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Become a Pro
speakerdeck
PRO
31
6.3k
New Earth Scene 8
popppiees
4
2.6k
The SEO Collaboration Effect
kristinabergwall1
1
560
Transcript
カーネルから見る OCIランタイム Hosting Casual Talks #5
自己紹介 大西 和貴 (@_k_onishi_) SAKURA Internet Inc. アプリケーショングループ レンタルサーバーチーム Linux
/ Kernel / CPU / Container / Virtualization / Pwn / C / Assembly / Python Linux Kernel 勉強会
目次 • コンテナとは • Cgroupsとは • CFS Bandwidth Controlの概要と実装 •
Completely Fair Schedulerの概要と実装 • カーネルの独自パッチ • まとめ
コンテナとは Linux環境で動作するプロセスのこと。 通常のプロセスと比較した際の相違点 としては、名前空間やルート ファイルシステムによってホストから 隔離されていることやCgroupsによる リソース制限が可能であることが 挙げられる。 参考: https://access.redhat.com/ja/articles/1459113
コンテナとは カーネルが提供するコンテナを構成するサブシステム。 • 名前空間(Name Space) • Cgroups • Linux Capabilities
• Chroot • Etc...
コンテナとは カーネルが提供するコンテナを構成するサブシステム。 • 名前空間(Name Space) • Cgroups • Linux Capabilities
• Chroot • Etc...
Cgroupsとは プロセスグループのリソース(CPU、メモリ、ディスクI/Oなど)の利用を制限・隔離などを 行うLinuxカーネルの機能。 参考: https://www.slideshare.net/RohitJnagal/docker-internals
Cgroupsとは • cpu: CPUへのアクセス • cpuacct: CPUについての自動レポートを生成 • cpuset: マルチコアCPUのコア単位およびメモリノードを割り当て
• memory: メモリに対する制限設定と自動レポートの生成 • blkio: ブロックデバイスの入出力アクセス • devices: デバイスへのアクセス • net_cls: ネットワークパケットへのタグ付け • net_prio: ネットワークトラフィックの優先度を動的に設定 • freezer: タスクを一時停止または再開 参考: https://qiita.com/legitwhiz/items/72ead813f5be784534e5
Cgroupsとは • cpu: CPUへのアクセス • cpuacct: CPUについての自動レポートを生成 • cpuset: マルチコアCPUのコア単位およびメモリノードを割り当て
• memory: メモリに対する制限設定と自動レポートの生成 • blkio: ブロックデバイスの入出力アクセス • devices: デバイスへのアクセス • net_cls: ネットワークパケットへのタグ付け • net_prio: ネットワークトラフィックの優先度を動的に設定 • freezer: タスクを一時停止または再開 参考: https://qiita.com/legitwhiz/items/72ead813f5be784534e5
Cgroupsとは • cpu: CPUへのアクセス 参考: https://qiita.com/legitwhiz/items/72ead813f5be784534e5 # cgroupの作成 $ cgcreate
-g blkio,memory,cpu,pids:test # 指定のプロセスをcgroupに参加させる。 $ echo $$ 2006 $ cgclassify -g blkio,memory,cpu,pids:test 2006 # 1秒間に0.5秒CPUリソースに対するアクセスを許可する $ cgset -r cpu.cfs_period_us=1000000 test $ cgset -r cpu.cfs_quota_us=500000 test
CFS Bandwidth Control CPUに対して最大使用量の制御を可能としたもので、タスクはperiod(マイクロ秒)期間 内にquota(マイクロ)秒のみCPUの使用を許可される。 参考: https://landley.net/kdocs/ols/2010/ols2010-pages-245-254.pdf
CFS Bandwidth Controlの実装 参考: kernel/sched/core.c #ifdef CONFIG_CFS_BANDWIDTH { .name =
"cfs_quota_us" , .read_s64 = cpu_cfs_quota_read_s64, .write_s64 = cpu_cfs_quota_write_s64, }, { .name = "cfs_period_us" , .read_u64 = cpu_cfs_period_read_u64, .write_u64 = cpu_cfs_period_write_u64, }, -rw-r--r-- 1 root root 0 Mar 22 09:14 /sys/fs/cgroup/cpu,cpuacct/test/cpu.cfs_period_us -rw-r--r-- 1 root root 0 Mar 22 09:14 /sys/fs/cgroup/cpu,cpuacct/test/cpu.cfs_quota_us
CFS Bandwidth Controlの実装 • tg_set_cfs_bandwidth() ◦ __cfs_schedulable() // 対象タスクグループ配下に制限値を反映 ◦
period及びquotaを更新 ◦ __refill_cfs_bandwidth_runtime() // quota マイクロ秒のランタイムプールを補 充 ◦ start_cfs_bandwidth() // period 用のタイマーを起動 ▪ sched_cfs_period_timer() • __refill_cfs_bandwidth_runtime() // quota マイクロ秒のランタイム プールを補充 参考: kernel/sched/core.c
CFS Bandwidth Controlの実装 参考: kernel/sched/core.c void __refill_cfs_bandwidth_runtime (struct cfs_bandwidth *cfs_b)
{ u64 now; if (cfs_b->quota == RUNTIME_INF) return; now = sched_clock_cpu (smp_processor_id ()); cfs_b->runtime = cfs_b->quota; cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); cfs_b->expires_seq ++; }
Completely Fair Scheduler タスクに与えるCPU時間のバランス(公平性)を維持するために導入されたスケジュー ラ。 参考: kernel/sched/core.c
Completely Fair Scheduler 仮想実行時間(vruntime)導入し、仮想時間が短いほどプロセッサのアクセス時間が短 いとみなされ、最小の仮想実行時間を保持しているタスクを次回の実行タスクとして選 定する。 各タスクは実行中に仮想実行時間を加算し、実行可能状態での待ちタスクの仮想実行 時間は減算される。 スリープしていたタスクは起床時に最小の仮想実行時間が設定されるためユーザインタ ラクティブなプロセスは必然的に応答性が向上する。
どのパスも他のパスの2倍の長さを超えないため効率的なタスクの選択及び挿入が可 能となる{O(log n)}。 参考: kernel/sched/core.c
Completely Fair Schedulerの実装 • タイマtick割り込み(schedule_tick() or hrtick()) ◦ task_tick_fair() ▪
update_curr() • 実行した時間(delta_exec)の算出 • vruntimeの加算 • cgroup_account_cputime() // 統計情報(cpuacct)の更新 • account_cfs_rq_runtime() // CFS BW 関連の処理 ◦ __account_cfs_rq_runtime() if(bandwidth が有効) ▪ 残存するランタイム (runtime_remaining) から実行時間を減算 ▪ expire_cfs_rq_runtime() // if(! 残存するランタイムが有 効){runtime_remaining = 0;} ▪ assign_cfs_rq_runtime() if(残存するランタイムが 0) • start_cfs_bandwidth() // period 用のタイマーを起動 • ランタイムプールから残存するランタイムに補充 • 補充した分をランタイムプールから減算 ▪ 残存するランタイムが 0の場合、再度スケジューリング ▪ ... 参考: kernel/sched/core.c
Completely Fair Schedulerの実装 参考: kernel/sched/core.c 制限設定 ランタイムプールをチャージ タイマ開始 tickイベント プロセスの実行
約5us
独自パッチ URL: https://github.com/k-onishi/linux-4.20.16/compare/c017e5c...b14af01 # 1秒間に0.5秒CPUリソースに対するアクセスを許可する $ cgset -r cpu.cfs_period_us=1000000 test
$ cgset -r cpu.cfs_quota_us=500000 test # 1秒間に0.5秒CPUリソースに対するアクセスを許可する $ cgset -r cpu.cfs_bandwidth_percent=50 test
独自パッチ URL: https://github.com/k-onishi/linux-4.20.16/compare/c017e5c...b14af01 #ifdef CONFIG_CFS_BANDWIDTH { .name = "cfs_quota_us" ,
.read_s64 = cpu_cfs_quota_read_s64, .write_s64 = cpu_cfs_quota_write_s64, }, { .name = "cfs_period_us" , .read_u64 = cpu_cfs_period_read_u64, .write_u64 = cpu_cfs_period_write_u64, }, { /* New !! */ .name = "cfs_bandwidth_percent" , .read_s64 = cpu_cfs_bandwidth_read_u64, .write_u64 = cpu_cfs_bandwidth_write_u64, }
• periodやquotaを意識する必要がない場合において、より直感的なイン ターフェースとなる。 • read/writeなどの読み書きを行うシステムコールのコストを半減すること が可能。 独自パッチ URL: https://github.com/k-onishi/linux-4.20.16/compare/c017e5c...b14af01
まとめ • CgroupsにおけるCPUリソースの制限機構は、CFSスケジューラと深く関連 がある。 • period_timerがperiod期間毎にquota分のランタイムをプロセスグルー プに付与し、それを消費する形で動作している。 • 最近のトレンドとしてカーネルよりも上のレイヤでのアプローチ(様々なOCI ランタイム、k8sなどのオーケストレーション)がコンテナの界隈では一般的
だが、カーネルのレイヤからのアプローチもアリなのでは。