SlideShare a Scribd company logo
1 of 29
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Timing Management
2
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
Timing Architecture
Ticking in jiffies
Delaying the process
Kernel Timers
Tasklets
Work Queues
3
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Timing Architecture
4
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Time Keeping
User space deals with the absolute time
4 'O clock for back up
12 'O clock shutdown
Kernel space deals with relative timing
Data transfer should finish in 5 msecs
Operations mostly in msecs, worst case seconds
5
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Unit of Time in Kernel
HZ – ticks per second
Varies from 50 to 1000
Typically 1000 for desktops & 100 for embedded Systems
Defined in <linux/params.h>
1 tick = 1ms (desktop), 10ms (embedded systems)
Jiffy
Internal kernel counter
Increments with each timer tick
jiffies & jiffies_64
6
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
API's
#include <linux/jiffies.h>
get_jiffies_64()
For accessing the 64 bit jiffies
Direct access not recommended
Comparison of time values
int time_before(a, b)
int time_after(a, b)
int time_before_eq(a, b)
int time_after_eq(a,b)
7
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Dealing with User space
#include <linux/time.h>
unsigned long timespec_to_jiffies(struct
timespec *)
void jiffies_to_timespec(jiffies, struct timespec *)
unsigned long timeval_to_jiffies(struct timeval *)
void jiffies_to_timespec(jiffies, struct timeval *)
8
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Resolution lesser than jiffies
Platform specific “Time Stamp Counter”
On x86
Header: <asm/msr.h>
API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks);
Getting it generically
Header: <linux/timex.h>
API: read_current_timer(unsigned long *timer_val);
9
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Absolute Time
Header: <linux/time.h>
APIs
mktime(y, m, d, h, m, s) – Seconds since Epoch
void do_gettimeofday(struct timeval *tv);
struct timespec current_kernel_time(void);
10
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Delays
11
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays
Busy wait: cpu_relax
while (time_before(jiffies, j1))
cpu_relax();
Yielding: schedule/schedule_timeout
while (time_before(jiffies, j1))
schedule();
12
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Timing/busy_wait.c
Modify example above to use schedule().
13
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Waiting for an event or timeout
#include <linux/wait.h>
wait_queue_head_t wq_head;
init_waitqueue_head(&wq_head);
wait_interruptible_timeout(wq, condition, timeout)
0 if process if timeout expires
Remaining delay, if event occured
schedule_timeout(delay_in_jiffies)
No condition to wait for
Returns 0, if timeout has expired
14
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Modify Timing/busy_wait to use wait queues &
sched_timeout
15
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Short Delays but Busy Waiting
Header: <linux/delay.h>
Arch. specific Header: <asm/delay.h>
APIs
void ndelay(unsigned long ndelays);
void udelay(unsigned long udelays);
void mdelay(unsigned long mdelays);
16
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Long Delays: Back to Yielding
Header: <linux/delay.h>
APIs
void msleep(unsigned int millisecs);
unsigned long msleep_interruptible(unsigned int millisecs);
void ssleep(unsigned int secs);
17
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Timers
18
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Kernel Timers
Allows to schedule a task, sometime in future
Executes a user defined function at user defined time
Example usage
Elimination of bouncing in a key press
Finishing some lengthy shutdowns
Execute asynchronously & in interrupt context. So,
scheduling out is not allowed
19
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Kernel Timer Data structures
Back end of the various delays
Header: <linux/timer.h>
Type: struct timer_list
APIs
void init_timer(struct timer_list *); /* Nullifies */
struct timer_list TIMER_INITIALIZER(f, t, p);
void add_timer(struct timer_list *);
void del_timer(struct timer_list *);
int mod_timer(struct timer_list *, unsigned long);
int del_timer_sync(struct timer_list *);
20
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
Timing/timers.c
Create a thread which will wake up & blink the
led every 1 sec
Modify select.c to wake up the user space
application periodically
21
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Tasklets
Tasklets mean baby tasks
Tasks to be executed later with no specific time
requirement
Normally, scheduled by interrupt handler to
finish the rest of the task such as data
processing
Executes in interrupt context
22
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Data Structures
Header: <linux/interrupt.h>
Type: struct tasklet_struct
APIs
void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long),
unsigned long data);
DECLARE_TASKLET(name, func, data);
DECLARE_TASKLET_DISABLED(name, func, data);
tasklet_enable(t); tasklet_disable(t);
tasklet_disable_nosync(t);
tasklet_[hi_]schedule(t);
tasklet_kill(t);
23
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Tasklet Features
Runs on the same CPU that schedules it
Kernel maintains a disable count for tasklet & re-enables
the tasklet only when enable is called as many times as
disables
Can be executed at higher priority
Execute latest by next timer tick
Tasklets is serialized with respect to itself
Runs in atomic context, so blocking is not allowed
24
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
BottomHalves/tasklet.c
Schedule the tasklet normal & high from the
timer handler
Reschedule the tasklet from itself
25
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Work Queues
Similar to tasklets, allow the task to be carried
out later
Work queues run in a context of special kernel
process
Its legitimate to sleep in work queues
Run on the processor from which they were
scheduled, by default
26
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Data Structures
Header: <linux/workqueue.h>
Types: struct workqueue_struct, struct work_struct
Work Queue APIs
q = create_workqueue(name);
q = create_singlethread_workqueue(name);
flush_workqueue(q);
destroy_workqueue(q);
Work APIs
DECLARE_WORK(w, void (*function)(struct work_struct *));
INIT_WORK(w, void (*function)(struct work_struct *));
Combined APIs
int queue_work(q, &w);
int queue_delayed_work(q, &w, d);
int cancel_delayed_work(&w);
27
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Hands On
BottomHalves/work_queue.c
Modify the above example to create a delayed
work
Try with cancel_delayed_work &
flush_workqueue
Use the shared work queue
28
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What have we learnt?
Timing Architecture
Ticking in jiffies
Delaying the process
Kernel Timers
Tasklets
Work Queues
29
© 2014-15 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot (20)

Embedded C
Embedded CEmbedded C
Embedded C
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
POSIX Threads
POSIX ThreadsPOSIX Threads
POSIX Threads
 
Platform Drivers
Platform DriversPlatform Drivers
Platform Drivers
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Network Drivers
Network DriversNetwork Drivers
Network Drivers
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting ProcessBeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Processes
ProcessesProcesses
Processes
 
Processes
ProcessesProcesses
Processes
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
I2C Drivers
I2C DriversI2C Drivers
I2C Drivers
 

Similar to Kernel Timing Management

101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and rebootAcácio Oliveira
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2Acácio Oliveira
 
리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2Sangho Park
 
Process scheduling
Process schedulingProcess scheduling
Process schedulingHao-Ran Liu
 
1.3 runlevels, shutdown, and reboot v3
1.3 runlevels, shutdown, and reboot v31.3 runlevels, shutdown, and reboot v3
1.3 runlevels, shutdown, and reboot v3Acácio Oliveira
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLGeorgi Kodinov
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedAnne Nicolas
 
SCHEDULING.ppt
SCHEDULING.pptSCHEDULING.ppt
SCHEDULING.pptAkashJ55
 
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSVTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSvtunotesbysree
 
Adaptive Replication for Elastic Data Stream Processing
Adaptive Replication for Elastic Data Stream ProcessingAdaptive Replication for Elastic Data Stream Processing
Adaptive Replication for Elastic Data Stream ProcessingZbigniew Jerzak
 
ch5_EN_CPUSched_2022.pdf
ch5_EN_CPUSched_2022.pdfch5_EN_CPUSched_2022.pdf
ch5_EN_CPUSched_2022.pdfCuracaoJTR
 
Tips Tricks and Little known features in SAP ASE
Tips Tricks and Little known features in SAP ASETips Tricks and Little known features in SAP ASE
Tips Tricks and Little known features in SAP ASESAP Technology
 
Module 3-cpu-scheduling
Module 3-cpu-schedulingModule 3-cpu-scheduling
Module 3-cpu-schedulingHesham Elmasry
 
Real-Time Query for Data Guard
Real-Time Query for Data Guard Real-Time Query for Data Guard
Real-Time Query for Data Guard Uwe Hesse
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).pptssuserf67e3a
 

Similar to Kernel Timing Management (20)

Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot
 
101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2101 1.3 runlevels, shutdown, and reboot v2
101 1.3 runlevels, shutdown, and reboot v2
 
리눅스 드라이버 #2
리눅스 드라이버 #2리눅스 드라이버 #2
리눅스 드라이버 #2
 
Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
1.3 runlevels, shutdown, and reboot v3
1.3 runlevels, shutdown, and reboot v31.3 runlevels, shutdown, and reboot v3
1.3 runlevels, shutdown, and reboot v3
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQL
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
Vx works RTOS
Vx works RTOSVx works RTOS
Vx works RTOS
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
 
SCHEDULING.ppt
SCHEDULING.pptSCHEDULING.ppt
SCHEDULING.ppt
 
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERSVTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
VTU 5TH SEM CSE OPERATING SYSTEMS SOLVED PAPERS
 
Adaptive Replication for Elastic Data Stream Processing
Adaptive Replication for Elastic Data Stream ProcessingAdaptive Replication for Elastic Data Stream Processing
Adaptive Replication for Elastic Data Stream Processing
 
13-scheduling.ppt
13-scheduling.ppt13-scheduling.ppt
13-scheduling.ppt
 
ch5_EN_CPUSched_2022.pdf
ch5_EN_CPUSched_2022.pdfch5_EN_CPUSched_2022.pdf
ch5_EN_CPUSched_2022.pdf
 
Tips Tricks and Little known features in SAP ASE
Tips Tricks and Little known features in SAP ASETips Tricks and Little known features in SAP ASE
Tips Tricks and Little known features in SAP ASE
 
Module 3-cpu-scheduling
Module 3-cpu-schedulingModule 3-cpu-scheduling
Module 3-cpu-scheduling
 
Real-Time Query for Data Guard
Real-Time Query for Data Guard Real-Time Query for Data Guard
Real-Time Query for Data Guard
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
 

More from SysPlay eLearning Academy for You (11)

Linux Internals Part - 3
Linux Internals Part - 3Linux Internals Part - 3
Linux Internals Part - 3
 
Linux Internals Part - 2
Linux Internals Part - 2Linux Internals Part - 2
Linux Internals Part - 2
 
Linux Internals Part - 1
Linux Internals Part - 1Linux Internals Part - 1
Linux Internals Part - 1
 
Understanding the BBB
Understanding the BBBUnderstanding the BBB
Understanding the BBB
 
Cache Management
Cache ManagementCache Management
Cache Management
 
Introduction to BeagleBone Black
Introduction to BeagleBone BlackIntroduction to BeagleBone Black
Introduction to BeagleBone Black
 
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xMIntroduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
BeagleBoard-xM Bootloaders
BeagleBoard-xM BootloadersBeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
 
Linux System
Linux SystemLinux System
Linux System
 

Recently uploaded

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Kernel Timing Management

  • 1. © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Timing Management
  • 2. 2 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? Timing Architecture Ticking in jiffies Delaying the process Kernel Timers Tasklets Work Queues
  • 3. 3 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Timing Architecture
  • 4. 4 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of Time Keeping User space deals with the absolute time 4 'O clock for back up 12 'O clock shutdown Kernel space deals with relative timing Data transfer should finish in 5 msecs Operations mostly in msecs, worst case seconds
  • 5. 5 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Unit of Time in Kernel HZ – ticks per second Varies from 50 to 1000 Typically 1000 for desktops & 100 for embedded Systems Defined in <linux/params.h> 1 tick = 1ms (desktop), 10ms (embedded systems) Jiffy Internal kernel counter Increments with each timer tick jiffies & jiffies_64
  • 6. 6 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. API's #include <linux/jiffies.h> get_jiffies_64() For accessing the 64 bit jiffies Direct access not recommended Comparison of time values int time_before(a, b) int time_after(a, b) int time_before_eq(a, b) int time_after_eq(a,b)
  • 7. 7 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Dealing with User space #include <linux/time.h> unsigned long timespec_to_jiffies(struct timespec *) void jiffies_to_timespec(jiffies, struct timespec *) unsigned long timeval_to_jiffies(struct timeval *) void jiffies_to_timespec(jiffies, struct timeval *)
  • 8. 8 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Resolution lesser than jiffies Platform specific “Time Stamp Counter” On x86 Header: <asm/msr.h> API: rdtsc(ul low_tsc_ticks, ul high_tsc_ticks); Getting it generically Header: <linux/timex.h> API: read_current_timer(unsigned long *timer_val);
  • 9. 9 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Absolute Time Header: <linux/time.h> APIs mktime(y, m, d, h, m, s) – Seconds since Epoch void do_gettimeofday(struct timeval *tv); struct timespec current_kernel_time(void);
  • 10. 10 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Delays
  • 11. 11 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays Busy wait: cpu_relax while (time_before(jiffies, j1)) cpu_relax(); Yielding: schedule/schedule_timeout while (time_before(jiffies, j1)) schedule();
  • 12. 12 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Timing/busy_wait.c Modify example above to use schedule().
  • 13. 13 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Waiting for an event or timeout #include <linux/wait.h> wait_queue_head_t wq_head; init_waitqueue_head(&wq_head); wait_interruptible_timeout(wq, condition, timeout) 0 if process if timeout expires Remaining delay, if event occured schedule_timeout(delay_in_jiffies) No condition to wait for Returns 0, if timeout has expired
  • 14. 14 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Modify Timing/busy_wait to use wait queues & sched_timeout
  • 15. 15 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Short Delays but Busy Waiting Header: <linux/delay.h> Arch. specific Header: <asm/delay.h> APIs void ndelay(unsigned long ndelays); void udelay(unsigned long udelays); void mdelay(unsigned long mdelays);
  • 16. 16 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Long Delays: Back to Yielding Header: <linux/delay.h> APIs void msleep(unsigned int millisecs); unsigned long msleep_interruptible(unsigned int millisecs); void ssleep(unsigned int secs);
  • 17. 17 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Timers
  • 18. 18 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. W's of Kernel Timers Allows to schedule a task, sometime in future Executes a user defined function at user defined time Example usage Elimination of bouncing in a key press Finishing some lengthy shutdowns Execute asynchronously & in interrupt context. So, scheduling out is not allowed
  • 19. 19 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Kernel Timer Data structures Back end of the various delays Header: <linux/timer.h> Type: struct timer_list APIs void init_timer(struct timer_list *); /* Nullifies */ struct timer_list TIMER_INITIALIZER(f, t, p); void add_timer(struct timer_list *); void del_timer(struct timer_list *); int mod_timer(struct timer_list *, unsigned long); int del_timer_sync(struct timer_list *);
  • 20. 20 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On Timing/timers.c Create a thread which will wake up & blink the led every 1 sec Modify select.c to wake up the user space application periodically
  • 21. 21 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Tasklets Tasklets mean baby tasks Tasks to be executed later with no specific time requirement Normally, scheduled by interrupt handler to finish the rest of the task such as data processing Executes in interrupt context
  • 22. 22 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Data Structures Header: <linux/interrupt.h> Type: struct tasklet_struct APIs void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data); DECLARE_TASKLET(name, func, data); DECLARE_TASKLET_DISABLED(name, func, data); tasklet_enable(t); tasklet_disable(t); tasklet_disable_nosync(t); tasklet_[hi_]schedule(t); tasklet_kill(t);
  • 23. 23 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Tasklet Features Runs on the same CPU that schedules it Kernel maintains a disable count for tasklet & re-enables the tasklet only when enable is called as many times as disables Can be executed at higher priority Execute latest by next timer tick Tasklets is serialized with respect to itself Runs in atomic context, so blocking is not allowed
  • 24. 24 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On BottomHalves/tasklet.c Schedule the tasklet normal & high from the timer handler Reschedule the tasklet from itself
  • 25. 25 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Work Queues Similar to tasklets, allow the task to be carried out later Work queues run in a context of special kernel process Its legitimate to sleep in work queues Run on the processor from which they were scheduled, by default
  • 26. 26 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Data Structures Header: <linux/workqueue.h> Types: struct workqueue_struct, struct work_struct Work Queue APIs q = create_workqueue(name); q = create_singlethread_workqueue(name); flush_workqueue(q); destroy_workqueue(q); Work APIs DECLARE_WORK(w, void (*function)(struct work_struct *)); INIT_WORK(w, void (*function)(struct work_struct *)); Combined APIs int queue_work(q, &w); int queue_delayed_work(q, &w, d); int cancel_delayed_work(&w);
  • 27. 27 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Hands On BottomHalves/work_queue.c Modify the above example to create a delayed work Try with cancel_delayed_work & flush_workqueue Use the shared work queue
  • 28. 28 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. What have we learnt? Timing Architecture Ticking in jiffies Delaying the process Kernel Timers Tasklets Work Queues
  • 29. 29 © 2014-15 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?