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

2017 COSCUP:用 GCC 讓你的 PostgreSQL 在運行上噴噴噴

Skymizer
August 05, 2017

2017 COSCUP:用 GCC 讓你的 PostgreSQL 在運行上噴噴噴

在這篇簡報當中,我們分析了 PostgreSQL 在運行時間上的瓶頸,並提出各種可能的解決方案。我們先利用機器學習的方式,尋找可能的優化編譯器參數。再用消去的方式,留下特定的參數,以確保編譯器優化能夠同時兼顧穩定性與效能提高。

成功的將效能提升了 12%,讓 PostgreSQL 效能開始噴噴噴

Skymizer

August 05, 2017
Tweet

More Decks by Skymizer

Other Decks in Programming

Transcript

  1. PostgreSQL • 罉꠸〭䧮⨞涸㖈 鄄❜➰涸痧♧갪⟤⹡ • Open source (剤 source code)

    • PostgreSQL License (BSD-like License)鯱㻫논 • 剓䑞岌⢪欽涸Ꟛ佞⾲㨥焺须俲䏨⛓♧
  2. Compiler • ⫹僽♧⦐ Google 缺陼Google 缺陼〳⟃䪾薊俒缺 陼䧭⚥俒罜  ⵱僽㼟➃⦛㻨涸玑䒭铃鎊缺陼䧭堥㐼〳 ⟃㛂遤涸堥㐼铃鎊

    • 穪㼩♶腋㣁缺陼ꐫ铐 • 缺陼涸ㅷ颶䖎ꅾ銴 • 剤⿡ꤑⱔ鑂飢㶶涸腋⸂ source code . . . Compiler .c .c .c .o .o .o Compiler Compiler Linker object file executable
  3. inline 鿪♧㹁鿪腋㣁䌟⢵㥪贖㌨ • inline 剚鸤䧭㛂遤墂隶㣐 Alice 騈 Mary 鿪䪾㨽鎷窍 Paul

    ⨞涸✲䞕䭭 㔐⢵荈䊹⨞ぐ荈鿪銴剤♧⟨湱ず涸乽⡲䭸⽂麕㢵ⶰ劥)搂岁佞㖈ず ♧⦐ cache ⚥㺂僒鸤䧭 cache miss⿾罜⩔ꧽ♶衽躮䪾碜 • ⨞⮛⻋ v.s. ♶⨞⮛⻋ NO!!!
  4. 〥㢫♧⦐ꨈ겗  麕㢵涸⮛⻋穉ざ • GCC 籏Ⱏ剤♧涰㢵珏⮛⻋倰岁䨾剤⮛⻋倰岁涸穉ざ侸ꆀ僽♧ ⦐㣔俒侸㶶 涸♧涰㢵妄倰 • 管陼♧妄

    PostgreSQL 㽠銴 ⴕꗻ䊩〸蕰僽〫剤 妄 管陼儘꟦㽠꨾銴 㣔鼩♶⺫䭍㛂遤儘꟦ • ⢪欽 ⿡㼦䪪♴♧⦐〳腋鯱⢕涸⮛⻋穉ざ䢩䢩鹧鵜 剓⢕涸⮛⻋穉ざ
  5. 鼩剤♧⦐ꨈ겗 ˊ 玑䒭⮛⻋涸〳腋䚍㣐㌨ • CPU bound v.s. IO bound •

    鑨铞㔐⢵䨾⟃ PostgreSQL ⮛⻋涸〳腋䚍㣐㌨
  6. PostgreSQL 莅 Compiler 涸佦✲ • 管陼㐼⮛⻋㼩 PostgreSQL 㛂遤佪腋涸䕧갠 • -O0

    㸤♶⨞⮛⻋湬䱺缺陼 • -O3 ⨞✫㣐鿈ⴕ涸⮛⻋倰岁 • -O3 涸㛂遤佪腋秉捀 -O0 涸 1.5⦔ • PostgreSQL 㛂遤佪腋僽剚「ⵌ管陼㐼⮛⻋䕧갠涸 • 佖⮛⻋⿮侸〳⟃⢵⮛⻋ PostgreSQL㟞⸈㛂遤♳涸佪腋 0 0.5 1 1.5 2 CentOS Ubuntu Compiler Sensitive O0 O3
  7. Function cycles 嫲⢿ⴕ區 Self Command Shared Object Symbol 4.40% postgres

    postgres AlloSetAlloc 3.66% postgres postgres SearchCatCache 3.58% postgres postgres base_yyparse 2.09% postgres postgres hash_search_with_hash_value 1.17% postgres libc-2.17.so __strcmp_sse42 1.15% postgres postgres palloc 1.13% postgres postgres MemoryContextAllocZeroAligned 0.98% postgres postgres expression_tree_walker 0.94% postgres postgres core_yylex 0.88% postgres postgres LWLockAttemptLock 0.87% postgres libc-2.17.so vfprintf 0.85% postgres libc-2.17.so _int_malloc
  8. Function total self PostgresMain 83.73% 0.80% pg_plan_queries 27.92% 0.07% pg_plan_query

    27.80% 0.10% standard_planner 27.65% 0.24% subquery_planner 24.18% 0.22% grouping_planner 22.00% 0.80% query_planner 20.41% 0.12% Call graph ⴕ區
  9. Benchmark • pgbench, select-only mode • PⰗ぀莅CⰗ぀㣐鿈ⴕ涸须俲䏨⢪欽䞕屣鿪僽隡《⡥穪㣐㢵侸 • 须俲䏨ⴲ㨥㣐㼭 13MB

    10蠝瘘 • 幾㼱 IO 䕧갠 • 4000 瘘Ⰽ⦐狲䬺 ♧〵堥㐼 • 㛂遤儘꟦5 mins CPU Intel® Core™ i7-6700 Memory 16GB OS CentOS 7.3 Compiler GCC 5.4.0 PostgreSQL 9.6.2
  10. 4⼪瘘 㻜뀿穡卓 (12% improvement) 0.9 0.95 1 1.05 1.1 1.15

    baseline O2 O3 O2 flto O2 finline-functions O2 flto finline- functions CentOS CentOS 1.12 1.03 1.04
  11. 㻜뀿穡卓 • -finline-functions • 㼟䨾剤 functions 鿪罌䣂䱰欽 inline⽰⢪㸐⦛劢鄄㹒デ捀  •

    ㄎ〭 function 涸겙㢫頾二㼟鄄嶋ꤑ • -flto (linking time optimization) • 㖈鸮穡儘劍㼟䨾剤銴鄄鸮穡涸 object file ざ⢘㖈♧饱⨞⮛⻋ • 管陼㐼涸⮛⻋莅ⴕ區〳騗麕 object file • 鸏Ⰽ⦐⮛⻋ず儘䩧Ꟛ⤑〳㼟㖈〥㢫♧⦐ object file 涸 function 窍 inline 鹎⢵
  12. MySQL 0 10 20 30 40 50 60 70 80

    90 100 110 120 130 innodb (mixed mode) myisam (write only) MySQL baseline (-O3) optimized 114.23 % 117.59 %
  13. Evaluate • build system • makefile • cmake • automake

    • bazel • high watermark (-O0) / low watermark (-O3) • Compiler sensitive – The performance distance between watermarks • https://blog.skymizer.com/evaluate_tutorial/