Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Static Linux SDK を使った Linux アプリ開発を始めよう!

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Yuya Oka Yuya Oka
December 06, 2024

Static Linux SDK を使った Linux アプリ開発を始めよう!

Chiba.swift #2 にて発表

WWDC24 にて Apple に発表された Static Linux SDK を説明及びパフォーマンスの検証等を行った資料

- Swift 6.0.1
- Docker
- aarch64

Source code: https://github.com/nnsnodnb/chiba-swift-static-linux-sdk-presentation

Avatar for Yuya Oka

Yuya Oka

December 06, 2024
Tweet

More Decks by Yuya Oka

Other Decks in Programming

Transcript

  1. Static Linux SDK Λ࢖ͬͨ Linux ΞϓϦ։ൃΛ࢝ΊΑ͏ʂ Let’s start Linux app

    development using the "Static Linux SDK"! Chiba.swift #2
  2. ͓લ୭Α Who am I খઘͻ΍͔͠ My name is Yuya Oka.

    ͜ΜͳΞΠίϯͰ΍ͬͯΔΑ Twitterʢݱ 𝕏ʣ GitHub ଞɿ@nnsnodnb iOSΞϓϦΤϯδχΞΛҾୀͨ͠ϓϩμΫτΤϯδχΞ I'm no longer an iOS apps engineer. ઍ༿ݝദࢢएࣲʹॅΜͰ͍·ͨ͠ I lived in Wakashiba Kashiwa-city, Chiba. ϑϥʔגࣜձࣾ͞ΜͰಇ͍͍ͯ·ͨ͠ I worked at Fuller. Inc. 2
  3. લճͷSwift for Linuxʂ Linux support until now. Linux޲͚ʹϏϧυ Build for

    Linux $ mkdir hello && cd hello $ swift package init --type executable Creating executable package: hello Creating Package.swift Creating .gitignore Creating Sources/ Creating Sources/main.swift $ swift build Building for debugging... [8/8] Linking hello Build complete! (1.18s) 4
  4. લճͷSwift for Linuxʂ Linux support until now. Linux޲͚ʹϏϧυ Build for

    Linux $ mkdir hello && cd hello $ swift package init --type executable Creating executable package: hello Creating Package.swift… $ swift build Building for debugging... [8/8] Linking hello… $ file .build/debug/hello .build/debug/hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, with debug_info, not stripped 4
  5. લճͷSwift for Linuxʂ Linux support until now. Swift ϥϯλΠϜͳ͠ͷUbuntuͰ࣮ߦ Runs

    on Ubuntu without Swift runtime. $ docker cp f95bff9ca073:/hello/.build/debug/hello ./hello Successfully copied 78.8kB to /Users/nnsnodnb/hello $ docker run --rm -t -v $(pwd)/hello:/hello ubuntu:24.04 /hello /hello: error while loading shared libraries: libswiftSwiftOnoneSupport.so: cannot open shared object file: No such file or directory 4
  6. Static Linux SDKʹ͍ͭͯ About "Static Linux SDK" WWDC24ʹͯൃද͞ΕͨLinux޲͚ϓϩάϥϜͷ׬શ੩తϦϯΫΛՄೳͱ͢Δ The announcement

    made at WWDC24 allows for fully statically linked for Linux programs. $ swift sdk install <URL> [--checksum <checksum>] $ swift build --swift-sdk x86_64-swift-linux-musl $ swift build --swift-sdk aarch64-swift-linux-musl 5
  7. ಈతϦϯΫɾ੩తϦϯΫ Dynamic Linking / Static Linking ಈతϦϯΫʢDynamic Linkingʣ ࣮ߦ࣌ʹඞཁͳϥΠϒϥϦ΍ϞδϡʔϧΛϦϯΫͯ͠ىಈ Linking

    and launching the necessary libraries and modules at runtime. CocoaPods͸جຊͪ͜Β͕࢖༻͞ΕΔʢuse_frameworks!ʣ CocoaPods is generally used here. ࣮ߦόΠφϦ͕খ͍͞ The executable binary is small. ಈతϥΠϒϥϦΛΠϯετʔϧ͢Δඞཁ͕͋Δ It’s necessary to install dynamic libraries. 6
  8. ಈతϦϯΫɾ੩తϦϯΫ Dynamic Linking / Static Linking ੩తϦϯΫʢStatic Linkingʣ Ϗϧυ࣌ʹඞཁͳϥΠϒϥϦ΍ϞδϡʔϧΛϦϯΫͯ͠੒Ռ෺Λੜ੒ Linking

    the required libraries and modules during the build process to generate product. Swift Package Manager͸جຊͪ͜Β͕࢖༻͞ΕΔ Swift Package Manager is generally used here. ࣮ߦόΠφϦ͕େ͖͍ The executable binary is large. ಈతϥΠϒϥϦΛΠϯετʔϧ͢Δඞཁ͕ͳ͍ It’s not necessary to install dynamic libraries. 6
  9. ಈతϦϯΫɾ੩తϦϯΫ Dynamic Linking / Static Linking ಈతϦϯΫͷόΠφϦαΠζ Binary size of

    Dynamic Linking $ ls -sh .build/debug/hello 76K .build/debug/hello 6
  10. ಈతϦϯΫɾ੩తϦϯΫ Dynamic Linking / Static Linking ੩తϦϯΫͷόΠφϦαΠζ Binary size of

    Static Linking $ ls -sh .build/debug/hello 41M .build/debug/hello 6
  11. Static Linux SDK͕ೖͬͨDockerΠϝʔδ΄͍͠ͳʔ I want Docker images with a "Static

    Linux SDK" included. Static Linux SDK ؚ͕·Εͨ Docker Πϝʔδ͕΄͍͠ I want Docker image that includes the Static Linux SDK. Θ͔ΔŊ ࣗ࡞ͨ͠ I know. I made one myself. @arasan01_me ఱ࠽ Genius. ΞϩέʔλΛmuslඪ४͔Βmimallocʹͨ͠΍ͭ΋ศར The one that switches the allocator from the musl default to mimalloc is also convenient. @arasan01_me https://x.com/nnsnodnb/status/1843598365839491082 ຊਓ͔Β࢖༻ڐ୚ࡁΈʢThanks @arasan01_meʣ 7
  12. ύϑΥʔϚϯεݕূ Performance testing 1. ಈతϦϯΫ Dynamic Linking $ time .build/debug/hello

    Hello, world! real 0m0.005s user 0m0.002s sys 0m0.003s 2. ੩తϦϯΫ Static Linking $ time .build/debug/hello Hello, world! real 0m0.002s user 0m0.000s sys 0m0.002s 8
  13. ύϑΥʔϚϯεݕূ ver.2 Performance testing ver.2 VaporͰݕূ͠Α͏ Let’s test it with

    Vapor. # Vapor create $ vapor new hello -n # Build $ swift build -c release [--swift-sdk aarch64-swift-linux-musl] # Benchmark (4 threads 128 connections 5 secs) $ wrk -t 4 -c 128 -d 5 http://127.0.0.1:8080 9
  14. ύϑΥʔϚϯεݕূ ver.2 Performance testing ver.2 1. ಈతϦϯΫ Dynamic Linking Running

    10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 11.05ms 34.23ms 613.21ms 98.50% Req/Sec 4.29k 501.65 5.15k 96.46% 156876 requests in 10.06s, 22.29MB read Requests/sec: 15595.64 Transfer/sec: 2.22MB 2. ੩తϦϯΫ Static Linking Running 10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 18.51ms 36.18ms 582.17ms 98.25% Req/Sec 2.19k 272.25 3.35k 96.20% 80459 requests in 10.08s, 11.43MB read Requests/sec: 7980.65 Transfer/sec: 1.13MB 9
  15. ύϑΥʔϚϯεݕূ ver.2 Performance testing ver.2 1. ಈతϦϯΫ Dynamic Linking Running

    10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 11.05ms 34.23ms 613.21ms 98.50% Req/Sec 4.29k 501.65 5.15k 96.46% 156876 requests in 10.06s, 22.29MB read Requests/sec: 15595.64 Transfer/sec: 2.22MB 2. ੩తϦϯΫ Static Linking Running 10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 18.51ms 36.18ms 582.17ms 98.25% Req/Sec 2.19k 272.25 3.35k 96.20% 80459 requests in 10.08s, 11.43MB read Requests/sec: 7980.65 Transfer/sec: 1.13MB 9
  16. ύϑΥʔϚϯεݕূ ver.2 Performance testing ver.2 mimalloc ͱ͍͏ Microsoft ͕։ൃΛ͍ͯ͠ΔΞϩέʔλʔΛ࢖༻ͯ͠Έͨ৔߹ When

    I tried using mimalloc, an allocator developed by Microsoft 1. ಈతϦϯΫ Dynamic Linking Running 10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 11.05ms 34.23ms 613.21ms 98.50% Req/Sec 4.29k 501.65 5.15k 96.46% 156876 requests in 10.06s, 22.29MB read Requests/sec: 15595.64 Transfer/sec: 2.22MB 2. ੩తϦϯΫ Static Linking (mimalloc) Running 10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 10.83ms 33.36ms 603.73ms 98.78% Req/Sec 4.23k 485.55 5.06k 97.28% 154832 requests in 10.04s, 22.00MB read Requests/sec: 15417.36 Transfer/sec: 2.19MB 10
  17. ·ͱΊ Wrap up Static Linux SDKͷಋೖ͸؆୯ Introducing the Static Linux

    SDK is easy. Alpine LinuxͷΑ͏ͳܰྔͳ؀ڥͰ΋࣮ߦͰ͖Δ It can run even in lightweight environments like Alpine Linux. ඞͣ͠΋ಈతϦϯΫΑΓύϑΥʔϚϯε͕ྑ͍Θ͚Ͱ͸ͳ͍ It doesn’t necessarily offer better performance than dynamic linking. Swift ϥϯλΠϜͳ͠Ͱ࣮ߦόΠφϦΛ഑෍Ͱ͖ΔΑ͏ʹͳΔ We can distribute the executable binary without Swift runtime. 11
  18. ͓·͚ Extra GolangͰͷ৔߹ʢ੩తϦϯΫʣ In the case of Golang (Static Link)

    $ cat main.go package main import "fmt" func main() { fmt.Println("Hello, world!") } $ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $ ls -sh hello 2.1M hello 12
  19. ͓·͚ Extra GolangͰͷ৔߹ʢ੩తϦϯΫʣ In the case of Golang (Static Link)

    Running 10s test @ http://127.0.0.1:8080 4 threads and 128 connections Thread Stats Avg Stdev Max +/- Stdev Latency 6.51ms 34.05ms 573.51ms 98.67% Req/Sec 10.50k 1.17k 11.86k 96.74% 385676 requests in 10.07s, 46.71MB read Requests/sec: 38300.46 Transfer/sec: 4.64MB ଎͗ͨ͢ɻൺֱͨ͠ͷΛޙչͨ͠ɻ Too fast. I regret comparing them. 12
  20. ࢀߟ References 1. Swiftͷ৽ػೳ https://developer.apple.com/jp/videos/play/wwdc2024/10136/ 2. Getting Started with the

    Static Linux SDK https://www.swift.org/documentation/articles/static-linux-getting-started.html 13
  21. ँࣙ Acknowledgements Fuller. Inc. https://www.fuller-inc.com @arasan01_me https://x.com/arasan01_me mtj0928/SlideGen https://github.com/mtj0928/SlideGen mtj0928/SlideKit

    https://github.com/mtj0928/SlideKit arasan01/swift-linux-sdk-cutomallocator https://github.com/arasan01/swift-linux-sdk-cutomallocator 14
  22. Static Linux SDK Λ࢖ͬͨ Linux ΞϓϦ։ൃΛ࢝ΊΑ͏ʂ Let’s start Linux app

    development using the "Static Linux SDK"! Chiba.swift #2 Thank you Slide Source: https://github.com/nnsnodnb/chiba-swift-static-linux-sdk-presentation 15