Clock / Timer

Clock / Timer

社内LT大会 0x64物語 #03 OS Kernel 資料

3f1b3c7b051241022d973fff2e713c1b?s=128

HORINOUCHI Masato

December 15, 2015
Tweet

Transcript

  1. Clock / Timer 2015/12/15 0x64 Tales #03 OS Kernel Livesense

    Inc. HORINOUCHI Masato
  2. Linux Kernel Clock େผ͢ΔͱҎԼͷ 2ͭʹ෼͚ΒΕΔɻ • Hardware Clock • ͦͷ໊ͷ௨Γϋʔυ΢ΣΞ্ʹ͋Δ

    Clock σόΠεɻ • System Clock • Linux Kernel ಺෦ʹ͋Δ Clock Ͱشൃੑɻ
  3. Linux࣌ࠁ؅ཧͷ࢓૊Έͱઃఆ ͔ΒҾ༻

  4. HW Clock • Real&me Clock (RTC), BIOS Clock, CMOS Clock

    ͱݺ͹ΕΔɻ • Ұൠతʹ͸όοςϦʔόοΫΞοϓ͕ߦͳΘΕΔɻ • ిݯ͕੾Ε͍ͯͯ΋όοςϦʔ΍ίϯσϯαʔʹΑΓۦಈɻ • &mezone Λҙࣝ͠ͳ͍ɻUTC ͔ local&me ͔Θ͔Βͳ͍ɻ • Windows ؀ڥ͸ local&me ͱͯ͠ɺLinux ؀ڥ͸ UTC ͱͯ͠ ಈ࡞͢ΔͨΊɺJST-9 ؀ڥͩͱ 9࣌ؒͷζϨ͕ൃੜ͢Δɻ
  5. System Clock • Linux Kernel ಺෦ͷϝϞϦʔͰอ͍࣋ͯ͠ΔɻͳͷͰشൃੑɻ • 2ͭͷσʔλΛ͍࣋ͬͯΔɻ • x-me:

    UNIX Epoch (1970/01/01 00:00:00 UTC) ͔Βͷඵ਺ • jiffies: ݱࡏඵ͔Βͷܦաφϊඵ਺ • UTC Ͱಈ࡞͢Δɻ
  6. Interval Timer • ϋʔυ΢ΣΞʹΑΓׂΓࠐΈΛൃੜͤ͞ΔσόΠεɻ • ׂΓࠐΈൃੜ͢Δ౓ʹ jiffies Λ૿΍͍ͯ͘͠ɻ • Ұൠతͳ

    Linux Kernel Ͱ͸ 1,000Hz (1ms) ຖʹׂΓࠐΈൃ ੜɻ • jiffies ͷ૿Ճ෼Λ x7me ʹ൓ө͍ͯ͘͠ɻ
  7. HW Clock ʹΞΫηε • hwclock ͱ͍͏πʔϧͰΞΫηεͰ͖Δɻ • hwclock ͸ /etc/adj/me

    ΛΈͯ UTC ͔ local/me ͔Λ൑ผɻ $ sudo hwclock --show --localtime 2015೥12݄15೔ 08࣌04෼42ඵ .785058 seconds $ cat /etc/adjtime 0.000000 1450089054 0.000000 1450089054 UTC
  8. HW Clock ͱ System Clock ͷؔ܎ • SysVinit (Upstart) ͷ৔߹

    • Ubuntu Ͱ͸ /etc/init.d/hwclock.sh Ͱ OS ىಈ/ऴྃ ࣌ʹಉظɻ • CentOS 6 Ͱ͸ /etc/rc.d/init.d/halt Ͱ OS ऴྃ࣌ʹ ಉظɻ • ىಈ࣌͸Ͳ͏͍ͯ͠Δͷ͔ͪΐͬͱௐ΂͚ͨͲෆ໌…ɻ
  9. Systemd Ͱ RTC ʹΞΫηε • "medatectl ͱ͍͏πʔϧͰΞΫηεͰ͖Δɻ $ timedatectl Local

    time: Ր 2015-12-15 17:06:22 JST Universal time: Ր 2015-12-15 08:06:22 UTC RTC time: Ր 2015-12-15 08:06:20 Time zone: Asia/Tokyo (JST, +0900) Network time on: yes NTP synchronized: yes RTC in local TZ: no
  10. Raspberry Pi • ࣮͸ RTC Λ౥ࡌ͍ͯ͠ͳ͍ɻ • ͚Ͳࣗ࡞͢Δ໠ऀɻ • RaspberryPiʹRTCϞδϡʔϧΛ౥ࡌͯ͠ɺϋʔυ΢ΣΞىಈ

    ࣌ʹ࣌ࠁΛ߹ΘͤΔ
  11. Tickless Kernel • ϋʔυ΢ΣΞʹΑΓ CPU ʹׂΓࠐΈൃੜͤ͞Δͱిྗফඅʹݒ ೦ɻ • ࣌ؒͷߋ৽ΛఆظతͳׂΓࠐΈͰ͸ͳ͘ Clock

    Source Λࢀর͢ Δ͜ͱʹɻ • ͜ΕʹΑΓ CPU ͕লిྗϞʔυͰ͍ΒΕΔ࣌ؒΛ௕͘͢Δ ͜ͱ͕Մೳʹɻ
  12. Clock Source ओʹҎԼͷ΋ͷ͕͋Δɻ্ʹ͋Δํ͕ΞΫηείετ͕௿͍ɻ • kvm-clock • Time Stamp Counter (TSC)

    • High Precision Event Timer (HPET) • ACPI Power Management Timer (ACPI_PM) • Programmable Interval Timer (PIT) • Real Time Clock (RTC)
  13. RHEL6ͷ&ckless kernel ͔ΒҾ༻

  14. Clock Source Λௐ΂Δ • VirtualBox 5.0.10 (४Ծ૝ԽΠϯλʔϑΣʔε KVM) • Ubuntu

    15.10 64bit + Guest Add@ons $ uname -a Linux ubuntu 4.2.0-19-generic #23-Ubuntu SMP Wed Nov 11 11:39:30 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux $ cat /sys/devices/system/clocksource/clocksource0/available_clocksource kvm-clock tsc acpi_pm $ cat /sys/devices/system/clocksource/clocksource0/current_clocksource kvm-clock
  15. Clock Source ͷ଎౓Λܭଌ ݩίʔυ1 Λվมͯ͠ 10,000,000 ճ࣮ߦɻ #include <sys/time.h> #include

    <stdio.h> int main() { struct timeval tv; for (int i=0; i<10000000; i++) { int r = gettimeofday(&tv, 0); if (r < 0) { perror("gettimeofday"); return 1; } } return 0; } 1 VirtualBox 5 Ͱར༻Մೳʹͳͬͨ Paravirtualiza1on ػೳ kvmclock Λ࢖͏ ͔ΒҾ༻ɻ
  16. ܭଌͯ͠ΈΔ ͜Μͳ shell script ʹͯ͠Έͨɻ #! /bin/zsh # kvm-clock sudo

    sh -c "echo kvm-clock > /sys/devices/system/clocksource/clocksource0/current_clocksource" cat /sys/devices/system/clocksource/clocksource0/current_clocksource time ./t # tsc sudo sh -c "echo tsc > /sys/devices/system/clocksource/clocksource0/current_clocksource" cat /sys/devices/system/clocksource/clocksource0/current_clocksource time ./t # acpi_pm sudo sh -c "echo acpi_pm > /sys/devices/system/clocksource/clocksource0/current_clocksource" cat /sys/devices/system/clocksource/clocksource0/current_clocksource time ./t
  17. Clock Source ߟ࡯ • kvm-clock ΑΓ TSC ͷํ͕ 2ഒ΄Ͳ଎͍ɻ •

    ͕ɺVM ͩͱ TSC ͷ஋͕ո͍͠έʔε΋ଟͦ͏ɻ • ͳͷͰૉ௚ʹ kvm-clock Λ࢖͏ͷ͕ྑͦ͞͏ɻ • acpi_pm ͸਺ेഒ(30ʙ70ഒ)஗͍ɻ • ͔͠΋γεςϜίʔϧ͠·͘Γ system ࣌ؒ࢖͍·͘Γɻ
  18. ·ͱΊ • Linux Kernel ͷ Clock / Timer पΓ͸ྺ࢙తܦҢʹΑΓෳࡶɻ •

    Clock / Timer ͷղ૾౓΍ਫ਼౓͸্͕͍ͬͯΔɻ • Ҏલ͸ VM ͩͱ Clock ζϨ·ͬͨ͘΋ͷ͕ͩɺ͜ͷล΋ղܾ͠ ͖͍ͯͯΔɻ • ͱ͸͍͑݁ہ͸ Guest OS Ͱ ntp ࢖͏ͷ͕ྑ͍໛༷ → KVM ʹ͓͍ͯ ϗετͱήετͷ࣌ؒ؅ཧ͸NTPΛ༻͍Δ΂͖͔ʁ
  19. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠