Real-Time Systems: Testing Your System in Linux
Jiangnan Liu CSE 520S Spring 02/13/2020
With adaptions from Haoran Li
Real-Time Systems: Testing Your System in Linux Jiangnan Liu CSE - - PowerPoint PPT Presentation
Real-Time Systems: Testing Your System in Linux Jiangnan Liu CSE 520S Spring 02/13/2020 With adaptions from Haoran Li What is meant by Time? Absolute time since some fixed past event, e.g.: q Seconds since start of the Unix Epoch (00:00:00
With adaptions from Haoran Li
q Seconds since start of the Unix Epoch
(00:00:00 UTC on 1 January 1970)
q Seconds since system boot
q Seconds between two events q Ten seconds into the future (from now) q Execution time of a program segment
q January 30th, 9:00 AM
2
q Get absolute time on each end?
q Round Trip Time (ping) q Same Clock Source
4
Clock 1 Clock 2 Start_time (clock 1) end_time (clock 2) Synchronized / Asynchronized Clocks? Start_time (clock 1) End_time (clock 1) Clock 1 Measure Time on a single Host
q time q “real”: Wall Time Elapsed (Just an estimation, Don’t rely on it) q “user”: Execution Time in User Space q “sys”: Execution Time in Kernel Space (syscall)
5
Get an overview of your program’s response / execution time.
[http://linux.die.net/man/2/sched_setscheduler]. Wall clock time
q return struct timeval, includes tv_sec and tv_usec q NOT ok for measuring overhead on standard kernel configuration q Wall clock time is subject to anomalies
6
Normal Evaluation (5 seconds) Intentionally change the wall clock, when program is running
7
When Measuring Elapse Time, don’t rely on WCT.
q CLOCK_REALTIME:
q CLOCK_MONOTONIC:
q CLOCK_MONOTONIC_RAW:
8
For short latency measurement in a single host, try CLOCK_MONOTIC_RAW
RTC (Real-Time Clock)
q Available on most computers (not on RPi 2 or 3 unless you add it) q Low precision (as low as 0.5 seconds)
Hardware Timers
q Might be used to generate interrupts, might be queryable q Run at a variety of frequencies q Programmable Interval Timer (PIT) q High-Performance Event Timer (HPET) q Programmable Interrupt Controller (PIC) q Advanced Programmable Interrupt Controller (APIC)
Processor Cycles
q Timestamp Counter (TSC) on x86, 64-bit q Cycle Counter (CCNT) on ARM, 32-bit, 64-cycle divider, not accessible in user
mode
q Potentially very high accuracy
9
Source: CSE 522S http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/03_Timers_Timing.pptx
Ø Time related system calls in the Linux kernel
q https://0xax.gitbooks.io/linux-insides/content/Timers/timers-7.html
Ø Clock_gettime()
q Man 2 clock_gettime q https://linux.die.net/man/2/clock_gettime
Ø RDTSC
q http://www.mcs.anl.gov/~kazutomo/rdtsc.html
Ø RDTSCP
q https://www.intel.com/content/www/us/en/embedded/training/ia-32-ia-64-
benchmark-code-execution-paper.html
Ø Invariant TSC
q Pitfall of TSC usage q http://oliveryang.net/2015/09/pitfalls-of-TSC-usage/
Ø (ARM) High Resolution Timing on Raspberry Pi
q https://blog.regehr.org/archives/794
10
q propagation delay: static q queuing delays q node processing delays q routing changes
12
Clock 1 Clock 2 start_time (clock 1) end_time (clock 2)
q Query NTP Server, get clock synchronized
13
Clock 1 Clock 2 start_time (clock 1) end_time (clock 2) NTP Server
14
Source: https://en.wikipedia.org/wiki/Network_Time_Protocol The U.S. Naval Observatory Alternate Master Clock Stratum 0: high-precision timekeeping devices atomic (cesium) clocks
q T
q Around one millisecond in Local Area Network[1]
q Detects the announcement at least a few seconds after the refclock
q NTP has been used in Distributed Denial of Service attacks(DDos) [2]
15
Set up an NTP server: https://ubuntuforums.org/showthread.php?t=862620 https://arstechnica.com/information-technology/2014/01/new-dos-attacks-taking-down-game-sites-deliver-crippling-100-gbps-floods/
q sub-microsecond range q making it suitable for measurement and control systems q PTP v.s. NTP
(NIC) and network switches.
16
PTP master PTP slave PTP slave
q Hierarchical NTP servers q Redundancy: Client-Server, Peer-to-Peer, Multicast q Granularity:T
q Built-in in EC2
q Grand Master-Slave(Master)-Slave q Redundancy: Fail take over q Hardware slaves (e.g. PCIe card) when possible q Granularity: Sub-microseconds (when w/ hardware support) q Not built-in in EC2, normally used for LAN
17
Ø Linux PTP
q http://linuxptp.sourceforge.net/ q SO_TIMESTAMPS
q Supports the Linux PTP Hardware Clock (PHC) subsystem by using
the clock_gettime family of calls, including the new clock_adjtimex system call. clo Ø PTP daemon man page
q sudo apt-get install ptpd q man ptpd
Ø NTP v.s PTP: How do you get accuracy
q http://www.atis.org/tam/presentations/ntp_vs_ptp_darnold.pdf q https://blog.meinbergglobal.com/2013/11/22/ntp-vs-ptp-network-timing-smackdown/ q https://www.redhat.com/en/blog/combining-ptp-ntp-get-best-both-worlds
18
19
q Traces the internal operations of the kernel q Static tracepoints within the kernel (event tracing)
q Front-End (user-level) utility for ftrace q Example:
q GUI trace-cmd reader
21
Kernel Shark: http://rostedt.homelinux.com/kernelshark/
22
q Configure, manage and query network interface parameters through
command line
q List network(socket) connections connections to the system
q Packet analyzer to trace packets transmitted over the network to
which the computer is attached
q Example:
q GUI based alternative to tcpdump 23
q SCHED_NORMAL/SCHED_OTHER: regular, interactive CFS tasks q SCHED_BATCH: low priority, non-interactive CFS tasks q SCHED_IDLE: very low priority tasks
q SCHED_RR: round-robin q SCHED_FIFO: first-in, first-out q SCHED_DEADLINE: earliest deadline first
CSE 522S – Advanced Operating Systems
25
26
Task Task Task
0ms 5ms 10ms Time
Source: CSE 522S – Advanced Operating Systems: http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/09_real_time_sched.pptx
q Scheduling preemptions (real-time scheduling) q Interrupts (can mask interrupts) q Migrations (can pin tasks to cores) q OS latency & jitter (RT_PREEMPT patch set)
Source: CSE 522S – Advanced Operating Systems: http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/09_real_time_sched.pptx
27
Ideal: Real world:
Preempt Migrate
28
Task 1 Task 2 Task 3 Time Task 1 Task 2 Task 3 Task 1 Task 2 Task 3
Source: CSE 522S – Advanced Operating Systems: http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/09_real_time_sched.pptx
29
Task 1 Task 2 Task 3 Time
Source: CSE 522S – Advanced Operating Systems: http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/09_real_time_sched.pptx
30
Task 3 Deadline: 8 Exec time: 2 Task 2 Deadline: 12 Exec time: 3 Task 1 Deadline: 5 Exec time: 4 Time 0 Task 1 Task 2 Task 3 5 8 12
Source: CSE 522S – Advanced Operating Systems: http://www.cse.wustl.edu/~cdgill/courses/cse522/slides/09_real_time_sched.pptx
Time 0 Task 1 Task 2 Task 3 5 8 12 Miss Deadline When using SCHED_FIFO
q RT class: static priority, 1 (lowest) to 99 (highest)
§ You need write a SyscallWrapper by yourself § Or: https://github.com/jlelli/schedtool-dl
§ /proc/sys/kernel/sched_rt_period_us
1000000
§ /proc/sys/kernel/sched_rt_runtime_us
950000
q Non-RT class: SCHED_OTHER with Complete Fair Scheduler
31
http://www.cyberciti.biz/faq/howto-set-real-time-scheduling-priority-process/
q sudo chrt –f –p 99 4800 # pid 4800 with priority 99 and fifo
32
#include <sched.h> int main() { … struct sched_param sched; sched.sched_priority = 98; if (sched_setscheduler(getpid(), SCHED_FIFO, &sched) < 0) { exit(EXIT_FAILURE); } … }
[http://linux.die.net/man/2/sched_setscheduler]
q sudo taskset -c 2,3 4800 # pid 4800 runs on cores 2-3
[http://linux.die.net/man/2/sched_setscheduler]
33
#include <sched.h> int main() { … unsigned long mask = 1; if (sched_setaffinity(getpid(), sizeof(mask), &mask) < 0) { exit(EXIT_FAILURE); } … }
Ø Linux schedulers
q https://www.kernel.org/doc/Documentation/scheduler/ q http://oreilly.com/catalog/linuxkernel/chapter/ch10.html
Ø Set priority
q chrt: http://www.cyberciti.biz/faq/howto-set-real-time-scheduling-priority-process/ q sched_scheduler: http://linux.die.net/man/2/sched_setscheduler
Ø Set CPU affinity on multi-core:
q taskset: http://linux.die.net/man/1/taskset q Check freq: http://www.advenage.com/topics/linux-timer-interrupt-frequency.php
Ø Linux real-time patches:
q Linux Preempt RT: https://rt.wiki.kernel.org/index.php/Main_Page q LITMUS-RT: http://www.litmus-rt.org/ q RTAI: https://www.rtai.org/ q SCHED_DEADLINE: http://gitorious.org/sched_deadline
34