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

プロファイラ開発者と見る「推測するな、計測せよ」 - YAPC::Hakodate 2024

osyoyu
October 05, 2024

プロファイラ開発者と見る「推測するな、計測せよ」 - YAPC::Hakodate 2024

osyoyu

October 05, 2024
Tweet

More Decks by osyoyu

Other Decks in Programming

Transcript

  1. X

  2. ϓϩϑΝΠϥͱ͸ w ϓϩάϥϜͷ஗͍෦෼Λಛఆ͢ΔͨΊͷπʔϧ % perl -d:Profile program.pl time elapsed (wall):

    86.8212 time running program: 65.7657 (75.75%) time profiling (est.): 21.0556 (24.25%) number of calls: 647248 %Time Sec. #calls sec/call F name 31.74 20.8770 2306 0.009053 Configable::init_from_config 20.09 13.2116 144638 0.000091 Configable::init_field_from_config 17.49 11.5043 297997 0.000039 Configable::has_attr 8.22 5.4028 312 0.017317 MonEl::recycle 7.54 4.9570 64239 0.000077 Configable::inherit 5.02 3.3042 101289 0.000033 MonEl::unique [...] $PO fi HBCMFJOJU@GSPN@DPO fi HͰ ࣮ߦ࣌ؒͷΛ࢖ͬͨ ʢ͕͜͜ϘτϧωοΫ͔΋͠Εͳ͍ʣ
  3. ϓϩϑΝΠϥ͍Ζ͍Ζ w %FWFM1SP fi MF %FWFM/:51SPG 1FSM  w QZTQZ

    1ZUIPO  w QQSPG (P  w BTZODQSP fi MFS +BWB  w $ISPNF%FW5PPMT +BWB4DSJQU  w *OTUSVNFOUT 9DPEF  w QFSG -JOVY
  4. Ͱ͸ʮܭଌʯ͢Ε͹͍͍ͷ͔ʁ w ౴͑͸൱ w ͳΜͱͳ͘ϓϩϑΝΠϥΛೖΕͯɺͳΜͱͳ͘ fl BNFHSBQIΛݟͯ ͳΜͱͳ͘ॲཧ͕࣌ؒ௕ͦ͏ͳͱ͜ΖΛ࡟ͬͯΈΔͷ͸ ʮ౰ͯͣͬΆ͏ͳվળʯʹଞͳΒͳ͍ w

    ֏Λ͍ͯ͠Δਓ͕དྷͨͱ͖ʹɺN૸ͷλΠϜΛଌͬͯ΋͋·Γҙຯ͕ͳ͍ ౰ͯͣͬΆ͏ͷվળ͸ແҙຯ͕ͩɺ ౰ͯͣͬΆ͏ͷܭଌʹ΋ҙຯ͕ͳ͍
  5. ໰୊ൃੜ ղܾ ܭଌ ରԠ ʘ͜Ε͕ݪҼͩʂʗ ຊ౰ʁ ʮਪଌ͠ͳ͍Ͱɺܭଌ͢Δʯ w ʮܭଌʯΛ͢Δ͚ͩͰɺ໰୊ͷਅҼ͕ಘΒΕΔͱ͸·ͬͨ͘ݶΒͳ͍ w

    ਅҼʹͨͲΓ͍͍ͭͯͳ͍ঢ়ଶͷʮରԠʯ͸Ձ஋͕௿͍ w ढज़ࣜύϑΥʔϚϯενϡʔχϯάͱݴ͑Α͏
  6. ԿΛܭଌ͢Δ͔ ࣌ؒ ࣮࣌ؒ XBMMUJNF  $16࣌ؒ *0଴ͪͷ࣌ؒ $16ͷαΠΫϧ਺ $166UJMJ[BUJPO Ωϟογϡώοτ཰

    γεςϜίʔϧ਺ ϝϞϦ࢖༻ྔ 744 NBMMPD  344 ෺ཧϝϞϦ  144 ϝϞϦϑϥάϝϯςʔ γϣϯ ϓϩηε਺ εϨουͷ਺ -PBE"WFSBHF ϦΫΤετճ਺ ϨεϙϯελΠϜ CBDLMPH௕ )551ϔομʔͷ ύʔε࣌ؒ %#.4 42-  ݁Ռͷߦ਺ εΩϟϯߦ਺ +0*/ͷίετ ϩοΫ֫ಘ଴ͪ࣌ؒ ؔ਺ͷݺͼग़͠ճ ਺ 7.໋ྩ਺ ($ͷ࣌ؒ +*5ͷώοτ཰ ϝιουσΟεύο ν
  7. ԿΛܭଌ͢Δ͔ ࣌ؒ ࣮࣌ؒ XBMMUJNF  $16࣌ؒ *0଴ͪͷ࣌ؒ $16ͷαΠΫϧ਺ $166UJMJ[BUJPO Ωϟογϡώοτ཰

    γεςϜίʔϧ਺ ϝϞϦ࢖༻ྔ 744 NBMMPD  344 ෺ཧϝϞϦ  144 ϝϞϦϑϥάϝϯςʔ γϣϯ ϓϩηε਺ εϨουͷ਺ -PBE"WFSBHF ϦΫΤετճ਺ ϨεϙϯελΠϜ CBDLMPH௕ )551ϔομʔͷ ύʔε࣌ؒ %#.4 42-  ݁Ռͷߦ਺ εΩϟϯߦ਺ +0*/ͷίετ ϩοΫ֫ಘ଴ͪ࣌ؒ ؔ਺ͷݺͼग़͠ճ ਺ 7.໋ྩ਺ ($ͷ࣌ؒ +*5ͷώοτ཰ ϝιουσΟεύο ν Ͳ͔͜Βணख͢Ε͹ʁ
  8. Ռ αʔϏε͕஗͍ %#αʔόʔͷ $16͕ Ҽ ʁʁʁ ΫΤϦ͕ͨ͘͞ΜඈͿ / ͕͋Δ εΩϟϯྔ͕૿͑Δ

    ϨεϙϯελΠϜ͕ ৳ͼΔ CBDLMPH͕ ৳ͼΔ ϝτϦΫε͕ ͓͔͘͠ͳΔ
  9. Ռ αʔϏε͕஗͍ %#αʔόʔͷ $16͕ &91-"*/ ໰୊͸ͳ͔ͬͨʜʜ EFBEUVQMF͕ ര૿͢Δ ϝϞϦͷ ࢖༻ྔ΋૿͑Δ

    5-#͕͋;Ε ϖʔδϑΥϧτ Χʔωϧ͕$16Λ ࢖ͬͯϖʔδΛ୳͢ ʜʜܭଌՄೳͳ͜ͱ $16TZTUFN QFSG QH@TUBU DQV Ҽ ʁʁʁ
  10. Ռ αʔϏε͕஗͍ Ҽ VQEBUF͕ ଟ͗͢Δઃܭ %#αʔόʔͷ $16͕ &91-"*/ ໰୊͸ͳ͔ͬͨʜʜ EFBEUVQMF͕

    ര૿͢Δ ϝϞϦͷ ࢖༻ྔ΋૿͑Δ 5-#͕͋;Ε ϖʔδϑΥϧτ Χʔωϧ͕$16Λ ࢖ͬͯϖʔδΛ୳͢ ʜʜܭଌՄೳͳ͜ͱ $16TZTUFN QFSG QH@TUBU DQV
  11. Ռ αʔϏε͕஗͍ Ҽ VQEBUF͕ ଟ͗͢Δઃܭ %#αʔόʔͷ $16͕ &91-"*/ ໰୊͸ͳ͔ͬͨʜʜ ϩάྔ͕ര૿ͯ͠

    ετϨʔδѹഭ EFBEUVQMF͕ ര૿͢Δ ϝϞϦͷ ࢖༻ྔ΋૿͑Δ 5-#͕͋;Ε ϖʔδϑΥϧτ Χʔωϧ͕$16Λ ࢖ͬͯϖʔδΛ୳͢ ʜʜ ʜʜܭଌՄೳͳ͜ͱ $16TZTUFN QFSG QH@TUBU DQV ʜʜ ʜʜ
  12. ଘࡏ͢Β஌Βͳ͍͜ͱ͸ௐ΂ΒΕͳ͍ w ্ͷίʔυ͸Լͷίʔυͷഒ஗͍ w x[i][j]Λx[j][i]ʹ͢Δͱɺ ಺ଆͷ഑ྻ͕$16Ωϟογϡʹ ৐Βͳ͘ͳΔ w ͜Ε͸ʮܭଌʯͰൃݟՄೳͰ͸͋Δ ͕ɺ໨ʹೖͬͯ΋$16Ωϟογϡͷ

    ͜ͱΛ஌ΒͶ͹վળ͸Ͱ͖ͳ͍ int x[10000][10000]; for (i = 0; i < 10000; i++) { for (j = 0; j < 10000; j++) { puts(x[i][j]); } } for (i = 0; i < 10000; i++) { for (j = 0; j < 10000; j++) { puts(x[j][i]); } }
  13. Ռ αʔϏε͕஗͍ Ҽ VQEBUF͕ ଟ͗͢Δઃܭ %#αʔόʔͷ $16͕ &91-"*/ ໰୊͸ͳ͔ͬͨʜʜ ϩάྔ͕ര૿ͯ͠

    ετϨʔδѹഭ EFBEUVQMF͕ ര૿͢Δ ϝϞϦͷ ࢖༻ྔ΋૿͑Δ 5-#͕͋;Ε ϖʔδϑΥϧτ Χʔωϧ͕$16Λ ࢖ͬͯϖʔδΛ୳͢ ʜʜ $16TZTUFN QFSG QH@TUBU DQV ʜʜ ʜʜ
  14. Ռ αʔϏε͕஗͍ Ҽ VQEBUF͕ ଟ͗͢Δઃܭ %#αʔόʔͷ $16͕ &91-"*/ ໰୊͸ͳ͔ͬͨʜʜ ϩάྔ͕ര૿ͯ͠

    ετϨʔδѹഭ EFBEUVQMF͕ ര૿͢Δ ϝϞϦͷ ࢖༻ྔ΋૿͑Δ 5-#͕͋;Ε ϖʔδϑΥϧτ Χʔωϧ͕$16Λ ࢖ͬͯϖʔδΛ୳͢ ʜʜ $16TZTUFN QFSG QH@TUBU DQV ʜʜ ʜʜ ޿ൣͳ஌͕ࣝͳ͍ͱ ݪҼʹͨͲΓண͚ͳ͍ྫ
  15. ʮܭଌʯ͍Ζ͍Ζ ؆ศ μϧ͍ ͔ͬ͠Γ ͬ͘͟Γ ϓϩϑΝΠϥ ΞΫηεϩά "1. /FX3FMJD %BUBEPH

    GSFF TBS IUPQ εϩʔΫΤϦϩά ֤छ$MPVE.FUSJDT &91-"*/ QSJOUG σόοά TUSBDF
  16. ࠶ܝԿΛܭଌ͢Δ͔ ࣌ؒ ࣮࣌ؒ XBMMUJNF  $16࣌ؒ *0଴ͪͷ࣌ؒ $16ͷαΠΫϧ਺ $166UJMJ[BUJPO Ωϟογϡώοτ཰

    γεςϜίʔϧ਺ ϝϞϦ࢖༻ྔ 744 NBMMPD  344 ෺ཧϝϞϦ  144 ϝϞϦϑϥάϝϯςʔ γϣϯ ϓϩηε਺ εϨουͷ਺ -PBE"WFSBHF ϦΫΤετճ਺ ϨεϙϯελΠϜ CBDLMPH௕ )551ϔομʔͷ ύʔε࣌ؒ %#.4 42-  ݁Ռͷߦ਺ εΩϟϯߦ਺ +0*/ͷίετ ϩοΫ֫ಘ଴ͪ࣌ؒ ؔ਺ͷݺͼग़͠ճ ਺ 7.໋ྩ਺ ($ͷ࣌ؒ +*5ͷώοτ཰ ϝιουσΟεύο ν
  17. ࣌ؒͷܭଌ def foo start = Time.now query("select *") end =

    Time.now elapsed = end - start puts "query consumed #{elapsed}s" end
  18. ࣌ؒͷܭଌ def foo start = Time.now query("select *") end =

    Time.now elapsed = end - start puts "query consumed #{elapsed}s" end w ճͷ࣮ߦͰ͍͍ͷʁ w ͨ·ͨ·֎෦ͷӨڹ͕େ͖͍͔΋ ͠Εͳ͍ w 5JNFOPX͸ϞϊτχοΫ͡Όͳ͍ w RVFSZதʹݱࡏ࣌ࠁ͕มߋ͞Ε ͨΒόάΔ  w ($΍(7-֫ಘ଴ͪ΋RVFSZʹܭ ্͞Εͯ͠·͏ w ԿΑΓɺ͋ΒΏΔ৔ॴʹ͜ΕΛ ࢓ࠐΉͷ͸ݱ࣮త͡Όͳ͍
  19. ͦ΋ͦ΋ʮ࣌ؒʯʹ΋͍Ζ͍Ζ͋Δ w ࣮࣌ؒ $-0$,@3&"-5*.&  w զʑ͕ೝ͍ࣝͯ͠Δʮ࣌ؒʯ w ୯ௐ૿Ճͳ࣮࣌ؒ $-0$,@.0/050/*$

     w γεςϜ͕ىಈ͔ͯ͠Βͷ࣌ؒʜʜͳͲ w ר͖໭Βͳ͍ w $16࣌ؒ $-0$,@130$&44@$165*.&@*%  w $16͕ܭࢉΛߦ͍ͬͯͨ࣌ؒ w *0 σΟεΫ΍ωοτϫʔΫ ͷ࣌ؒ͸আ͘ def foo <start> query("select *") <end> end
  20. ࣌ؒΛଌΔͷ͸೉͍͠ w ϢʔβʔϥϯυͰ͸ଌΕͳ͍छྨͷ࣌ؒ΋͋ Δ w ʮ$16࣌ؒʯ͸ΧʔωϧͷΈ͕஌ΓಘΔ w ϢʔβʔϥϯυͰ͸VTFSͱJPXBJUΛ۠ผ Ͱ͖ͳ͍ w

    εϨου QUISFBE ͝ͱʹͳΔͱঘߋ w "஍఺͔Β#஍఺ͷ࣌ؒ͸ܭΕͯ΋ɺ ʮNTඵ͝ͱʹλΠϚʔΛಈ͔͢ʯ͸࣮ݱ Ͱ͖ͳ͍ w ׂΓࠐΈͰ͖ͳ͍ͱ͍͚ͳ͍ struct sigevent sigevent = { .sigev_notify = SIGEV_THREAD_ID; .sigev_signo = SIGPROF; }; struct itimerspec timer = { .it_interval = { .tv_sec = 0; .tv_nsec = 1000000; // 10 ms }; ... }; timer_create(CLOCK_MONOTONIC, &sigevent, &timer)
  21. ਤղɿγάφϧϋϯυϥΛ࢖ͬͨεέδϡʔϦϯά 0ms 10ms 20ms 30ms Thread 1 Thread 2 ΧʔωϧλΠϚʔʹΑΔׂΓࠐΈ

    Using CPU Using CPU Using CPU γάφϧϋϯυϥ ࣮ߦதͷελοΫΛݴޠ VM or Χʔωϧ ʹΫΤϦ ετϨʔδʹऔಘɾه࿥ γάφϧϋϯυϥ ΧʔωϧׂΓࠐΈ
  22. $16ͷύϑΥʔϚϯεΧ΢ϯλ 1.$ % perf list branch-instructions OR branches [Hardware event]

    branch-misses [Hardware event] bus-cycles [Hardware event] cache-misses [Hardware event] cache-references [Hardware event] cpu-cycles OR cycles [Hardware event] instructions [Hardware event] ref-cycles [Hardware event] alignment-faults [Software event] bpf-output [Software event] cgroup-switches [Software event] context-switches OR cs [Software event] cpu-clock [Software event] cpu-migrations OR migrations [Software event] dummy [Software event] emulation-faults [Software event] major-faults [Software event]
  23. Χʔωϧ͔Β΋औΕΔ৘ใ͍Ζ͍Ζ % ls /proc/sys/kernel acct hung_task_warnings ostype random softlockup_panic acpi_video_flags

    io_delay_type overflowgid randomize_va_space soft_watchdog arch io_uring_disabled overflowuid real-root-dev split_lock_mitigate auto_msgmni io_uring_group panic sched_autogroup_enabled stack_tracer_enabled bootloader_type kexec_load_disabled panic_on_io_nmi sched_cfs_bandwidth_slice_us sysctl_writes_strict bootloader_version kexec_load_limit_panic panic_on_oops sched_deadline_period_max_us sysrq bpf_stats_enabled kexec_load_limit_reboot panic_on_rcu_stall sched_deadline_period_min_us tainted cad_pid keys panic_on_unrecovered_nmi sched_energy_aware task_delayacct cap_last_cap kptr_restrict panic_on_warn sched_itmt_enabled threads-max core_pattern max_lock_depth panic_print sched_rr_timeslice_ms timer_migration core_pipe_limit max_rcu_stall_to_panic perf_cpu_time_max_percent sched_rt_period_us traceoff_on_warning core_uses_pid modprobe perf_event_max_contexts_per_stack sched_rt_runtime_us tracepoint_printk ctrl-alt-del modules_disabled perf_event_max_sample_rate sched_schedstats unknown_nmi_panic dmesg_restrict msgmax perf_event_max_stack sched_util_clamp_max unprivileged_bpf_disabled domainname msgmnb perf_event_mlock_kb sched_util_clamp_min unprivileged_userns_clone ftrace_dump_on_oops msgmni perf_event_paranoid sched_util_clamp_min_rt_default user_events_max ftrace_enabled msg_next_id pid_max seccomp usermodehelper hardlockup_all_cpu_backtrace ngroups_max poweroff_cmd sem version hardlockup_panic nmi_watchdog print-fatal-signals sem_next_id warn_limit hostname ns_last_pid printk shmall watchdog hung_task_all_cpu_backtrace numa_balancing printk_delay shmmax watchdog_cpumask hung_task_check_count numa_balancing_promote_rate_limit_MBps printk_devkmsg shmmni watchdog_thresh hung_task_check_interval_secs oops_all_cpu_backtrace printk_ratelimit shm_next_id yama hung_task_panic oops_limit printk_ratelimit_burst shm_rmid_forced hung_task_timeout_secs osrelease pty softlockup_all_cpu_backtrace
  24. ϨΠϠʔΛԼΓͳ͍ͱ؍࡯͸Ͱ͖ͳ͍ ΞϓϦέʔγϣϯ ϥΠϒϥϦ ϑϨʔϜϫʔΫ 04Χʔωϧ ϋʔυ΢ΣΞ ϛυϧ΢ΣΞ ݴޠॲཧܥ 7. ࠓͳʹΛͯ͠Δ͔͸

    7.ʹ͔͠෼͔Βͳ͍ αΠΫϧ਺͸ ϋʔυ΢ΣΞࢧԉ͕ ͳ͍ͱऔΕͳ͍ 42-ͷ࣮ߦܭը͸ %#.4ʹ͔͠෼͔Βͳ͍ ਖ਼֬ͳ࣌ؒ͸ Χʔωϧ͔͠ଌΕͳ͍
  25. ৗʹʮܭଌʯ͍ͯ͠Δ͜ͱͷՁ஋ w /FX3FMJD΍%BUBEPHͳͲͷ"1.͸ɺ ʮৗʹʯܭଌ͍ͯ͠Δ͜ͱ͕࠷େͷՁ஋ w ͜ͷจ຺Ͱ$POUJOVPVT1SP fi MJOHͳͲ΋ۙ೥ ൃలΛݟ͍ͤͯΔ w

    1ZSPTDPQFFUD w ࠓ·Ͱ͸ʮॏްʯͳιϦϡʔγϣϯͩͬͨϓϩϑΝΠϦϯάΛ ϞχλϦϯάͷΑ͏ʹ࢖͑Δͱ͍͍͔΋͠Ε·ͤΜͶ
  26. ଞʹ΋͜Μͳ೰Έ͕ w ଟ͘ͷύϑΥʔϚϯεπʔϧ͸୯Ұͷ໨తͷͨΊʹ࡞ΒΕ͍ͯΔ w TUSBDF JPTUBU GSFF TBS UPQ w

    UPQͱ͔͸ͦ͏Ͱ΋ͳ͍͔΋ w ϓϩϑΝΠϥ͸ɺԿΛٻΊΒΕ͍ͯΔ͔෼͔Βͳ͍ʂʂʂ
  27. QFSGͰऔΕΔελοΫ $ϨϕϧͷελοΫ rb_hash_new() rb_vm_exec() vm_exec_core() rb_main() main() __libc_start_main() 1GͰऔΕΔελοΫ 3VCZϨϕϧͷελοΫ

    Hash#new() foo() <main> w ౰ͨΓલ͕ͩɺશવରԠ͠ͳ͍ w ؤுͬͯ໨ࢹ͢ΔͳΓɺ؆୯ͳεΫϦϓτΛ ॻ͍ͨΓ w ΢ιΛ͔ͭͳ͍ϓϩϑΝΠϥ΁ͷಓ͸ԕ͍