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

Performance Profiling Swift on Linux

JP Simard
September 23, 2017

Performance Profiling Swift on Linux

So you're a bigshot and you've ported your Swift code to Linux, congrats 🎉. But now it's slow & you don't know why 🤦. In this talk, we'll discover various approaches to profiling Swift performance on Linux and get back that speeeed 🏎. Oh, and there are flamegraphs 🔥🔥🔥.

JP Simard

September 23, 2017
Tweet

More Decks by JP Simard

Other Decks in Programming

Transcript

  1. Reasons why Swi! code might be slower on Linux —

    Platform differences — Low level library differences (mach ports, libpthread, allocator) — Different implementations — Darwin Foundation vs swift-corelibs-foundation — Lack of Objective-C runtime & optimizations
  2. CPU Profilers Tool OS Notes Instruments.app macOS Super powerful, accurate,

    great GUI DTrace macOS, Solaris, FreeBSD Powers part of Instruments.app, very scriptable, not on Linux Callgrind macOS, Linux, Solaris Fickle, slow, single- threaded, can run in VMs & containers Perf Events Linux on bare metal Fast, accurate, CLI only, requires support for hardware counters
  3. VMs vs Containers vs Bare Metal VMs/Containers Bare Metal Cheap

    Expensive Local on macOS Dedicated Machine or Dual Boot Convenient Not So Much Limited Perf Tools No Limitations
  4. Swi!Lint A tool to enforce Swi! style and conventions —

    Integrates into Xcode — Plugins for AppCode, Vim, Sublime Text, Atom, Emacs — 109 rules and counting, covering lint, idiomatic, style, metrics & performance
  5. $ docker run -it --rm swift bash $ > apt-get

    update && apt-get install -y valgrind $ > git clone https://github.com/realm/SwiftLint.git $ > cd SwiftLint $ > swift build $ > valgrind --tool=callgrind .build/debug/swiftlint $ brew install qcachegrind gprof2dot $ gprof2dot -f callgrind callgrind.out.* | dot -Tsvg -o dotgraph.svg $ open -a Safari dotgraph.svg $ open -a qcachegrind
  6. $ docker run -it --rm swift bash $ > apt-get

    update && apt-get install -y valgrind $ > git clone https://github.com/realm/SwiftLint.git $ > cd SwiftLint $ > swift build $ > valgrind --tool=callgrind .build/debug/swiftlint $ brew install qcachegrind gprof2dot $ gprof2dot -f callgrind callgrind.out.* | dot -Tsvg -o dotgraph.svg $ open -a Safari dotgraph.svg $ open -a qcachegrind
  7. $ docker run -it --rm swift bash $ > apt-get

    update && apt-get install -y valgrind $ > git clone https://github.com/realm/SwiftLint.git $ > cd SwiftLint $ > swift build $ > valgrind --tool=callgrind .build/debug/swiftlint $ brew install qcachegrind gprof2dot $ gprof2dot -f callgrind callgrind.out.* | dot -Tsvg -o dotgraph.svg $ open -a Safari dotgraph.svg $ open -a qcachegrind
  8. $ docker run -it --rm swift bash $ > apt-get

    update && apt-get install -y valgrind $ > git clone https://github.com/realm/SwiftLint.git $ > cd SwiftLint $ > swift build $ > valgrind --tool=callgrind .build/debug/swiftlint $ brew install qcachegrind gprof2dot $ gprof2dot -f callgrind callgrind.out.* | dot -Tsvg -o dotgraph.svg $ open -a Safari dotgraph.svg $ open -a qcachegrind
  9. $ doctl compute droplet create perf --size 16gb \ --image

    ubuntu-16-10-x64 --region sfo1 $ > apt-get update && apt-get dist-upgrade $ > reboot $ > apt-get install -y clang libblocksruntime0 libcurl4-openssl-dev \ linux-tools-common linux-tools-generic linux-tools-`uname -r` $ > SWIFT_VERSION=swift-4.0-RELEASE $ > BASE_URL=https://swift.org/builds/swift-4.0-release/ubuntu1610 $ > URL=$BASE_URL/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu16.10.tar.gz $ > curl $URL | tar xz --directory $HOME --strip-components=1 $ > export PATH=$HOME/usr/bin:$PATH $ > export LINUX_SOURCEKIT_LIB_PATH=$HOME/usr/lib $ > git clone https://github.com/realm/SwiftLint.git $ > git clone https://github.com/brendangregg/FlameGraph.git $ > cd SwiftLint $ > swift build $ > perf record -g .build/debug/swiftlint $ > perf script > out.perf $ > ../FlameGraph/stackcollapse-perf.pl out.perf > out.folded $ > ../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
  10. $ doctl compute droplet create perf --size 16gb \ --image

    ubuntu-16-10-x64 --region sfo1 $ > apt-get update && apt-get dist-upgrade $ > reboot $ > apt-get install -y clang libblocksruntime0 libcurl4-openssl-dev \ linux-tools-common linux-tools-generic linux-tools-`uname -r` $ > SWIFT_VERSION=swift-4.0-RELEASE $ > BASE_URL=https://swift.org/builds/swift-4.0-release/ubuntu1610 $ > URL=$BASE_URL/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu16.10.tar.gz $ > curl $URL | tar xz --directory $HOME --strip-components=1 $ > export PATH=$HOME/usr/bin:$PATH $ > export LINUX_SOURCEKIT_LIB_PATH=$HOME/usr/lib $ > git clone https://github.com/realm/SwiftLint.git $ > git clone https://github.com/brendangregg/FlameGraph.git $ > cd SwiftLint $ > swift build $ > perf record -g .build/debug/swiftlint $ > perf script > out.perf $ > ../FlameGraph/stackcollapse-perf.pl out.perf > out.folded $ > ../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
  11. $ doctl compute droplet create perf --size 16gb \ --image

    ubuntu-16-10-x64 --region sfo1 $ > apt-get update && apt-get dist-upgrade $ > reboot $ > apt-get install -y clang libblocksruntime0 libcurl4-openssl-dev \ linux-tools-common linux-tools-generic linux-tools-`uname -r` $ > SWIFT_VERSION=swift-4.0-RELEASE $ > BASE_URL=https://swift.org/builds/swift-4.0-release/ubuntu1610 $ > URL=$BASE_URL/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu16.10.tar.gz $ > curl $URL | tar xz --directory $HOME --strip-components=1 $ > export PATH=$HOME/usr/bin:$PATH $ > export LINUX_SOURCEKIT_LIB_PATH=$HOME/usr/lib $ > git clone https://github.com/realm/SwiftLint.git $ > git clone https://github.com/brendangregg/FlameGraph.git $ > cd SwiftLint $ > swift build $ > perf record -g .build/debug/swiftlint $ > perf script > out.perf $ > ../FlameGraph/stackcollapse-perf.pl out.perf > out.folded $ > ../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
  12. $ doctl compute droplet create perf --size 16gb \ --image

    ubuntu-16-10-x64 --region sfo1 $ > apt-get update && apt-get dist-upgrade $ > reboot $ > apt-get install -y clang libblocksruntime0 libcurl4-openssl-dev \ linux-tools-common linux-tools-generic linux-tools-`uname -r` $ > SWIFT_VERSION=swift-4.0-RELEASE $ > BASE_URL=https://swift.org/builds/swift-4.0-release/ubuntu1610 $ > URL=$BASE_URL/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu16.10.tar.gz $ > curl $URL | tar xz --directory $HOME --strip-components=1 $ > export PATH=$HOME/usr/bin:$PATH $ > export LINUX_SOURCEKIT_LIB_PATH=$HOME/usr/lib $ > git clone https://github.com/realm/SwiftLint.git $ > git clone https://github.com/brendangregg/FlameGraph.git $ > cd SwiftLint $ > swift build $ > perf record -g .build/debug/swiftlint $ > perf script > out.perf $ > ../FlameGraph/stackcollapse-perf.pl out.perf > out.folded $ > ../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
  13. $ doctl compute droplet create perf --size 16gb \ --image

    ubuntu-16-10-x64 --region sfo1 $ > apt-get update && apt-get dist-upgrade $ > reboot $ > apt-get install -y clang libblocksruntime0 libcurl4-openssl-dev \ linux-tools-common linux-tools-generic linux-tools-`uname -r` $ > SWIFT_VERSION=swift-4.0-RELEASE $ > BASE_URL=https://swift.org/builds/swift-4.0-release/ubuntu1610 $ > URL=$BASE_URL/$SWIFT_VERSION/$SWIFT_VERSION-ubuntu16.10.tar.gz $ > curl $URL | tar xz --directory $HOME --strip-components=1 $ > export PATH=$HOME/usr/bin:$PATH $ > export LINUX_SOURCEKIT_LIB_PATH=$HOME/usr/lib $ > git clone https://github.com/realm/SwiftLint.git $ > git clone https://github.com/brendangregg/FlameGraph.git $ > cd SwiftLint $ > swift build $ > perf record -g .build/debug/swiftlint $ > perf script > out.perf $ > ../FlameGraph/stackcollapse-perf.pl out.perf > out.folded $ > ../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
  14. $ xcrun swift-demangle _T018SwiftLintFramework20LetVarWhitespaceRuleV20att ributeLineNumbers33_013BAF1EF367799B68F2E028EAD9 BE9DLLs3SetVySiG012SourceKittenC04FileC4file_tF // Module SwiftLintFramework. //

    Type (struct) LetVarWhitespaceRule. // Function (attributeLineNumbers in \_013BAF1EF367799B68F2E028EAD9BE9D) // Parameter (file : SourceKittenFramework.File) // Return type -> Swift.Set<Swift.Int>
  15. public struct LetVarWhitespaceRule: ConfigurationProviderRule, OptInRule { /* ... */ //

    Collects all the line numbers containing attributes but not declarations // other than let/var private func attributeLineNumbers(file: File) -> Set<Int> { let matches = file.match(pattern: "[@_a-z]+", with: [.attributeBuiltin]) let matchLines = matches.map { file.line(offset: $0.location) } return Set<Int>(matchLines) } /* ... */ }
  16. $ git diff // Collects all the line numbers containing

    attributes but not declarations // other than let/var private func attributeLineNumbers(file: File) -> Set<Int> { - let matches = file.match(pattern: "[@_a-z]+", with: [.attributeBuiltin]) - let matchLines = matches.map { file.line(offset: $0.location) } - - return Set<Int>(matchLines) + return Set(file.syntaxMap.tokens.flatMap({ token in + if token.type == SyntaxKind.attributeBuiltin.rawValue { + return file.line(byteOffset: token.offset) + } + return nil + })) }
  17. Tools — Perf — Valgrind — Callgrind — GNU gprof

    — KCachegrind — FlameGraph — gprof2dot
  18. Closing Thoughts — Consider writing an app-specific benchmark mode —

    Prefer measuring with Instruments.app if possible — Prefer measuring on: bare metal > container > VM — Prefer measuring your code: release > debug — Callgrind is extremely slow, try to avoid it — There's a tooling opportunity to convert perf/callgrind data to something Instruments compatible