Timer : Periodic ================== .. tab-set:: .. tab-item:: Timer : Periodic * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to create the kernel timer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to modify the timer's timeout ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Timer API's ? * `timer_setup `_ * `mod_timer `_ * `del_timer `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Key learnings in this program ` * :ref:`Explanation of program part by part ` * :ref:`List of headers ` * :ref:`Modules macro ` * :ref:`Initialize variables and functions ` * :ref:`Module init functions ` * :ref:`Module exit functions ` * :ref:`Timer APIs ` * :ref:`Mention init and exit functions ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load ` * :ref:`Summary of Timer APIs ` .. _p2_timerPeriodic_0: .. tab-set:: .. tab-item:: Key learnings in this program * A timer is a specialized type of clock used for measuring specific time intervals. * ``timer_setup`` used to initialize and set up a timer object. .. code-block:: c timer_setup(&my_timer, timer_callback, 0); * ``mod_timer`` used to modify a timer’s timeout. This is a more efficient way to update the expires field of an active timer (if the timer is inactive it will be activated). For example, .. code-block:: c mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); * ``del_timer`` used to stop a running timer and prevent it's callback function from being executed. For example .. code-block:: c del_timer(&my_timer); * To modify the timers timeout periodically, again we need to call the ``mod_timer`` inside the ``timer_callback``. .. code-block:: c static void timer_callback(struct timer_list * timer) { pr_info("Timer Callback function Called [%d]\n", count++); mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); } .. _p2_timerPeriodic_4: .. tab-set:: .. tab-item:: Explanation of program part by part * Here is the part by part explanation of the source code. .. _p2_timerPeriodic_5: .. tab-set:: .. tab-item:: List of headers * Add the list of headers to the refer the APIs used in this source code. .. code-block:: c #include #include #include #include #include #include #include .. _p2_timerPeriodic_6: .. tab-set:: .. tab-item:: Modules macro * Add the modules macro which gives information about the author, license and description. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux_usr"); MODULE_DESCRIPTION("Periodic Timer"); .. _p2_timerPeriodic_7: .. tab-set:: .. tab-item:: Initialize variables and functions * Initialize the functions and variables to be using in this program. .. code-block:: c static struct timer_list my_timer; static void timer_callback(struct timer_list *timer) .. _p2_timerPeriodic_8: .. tab-set:: .. tab-item:: Module init function * Create the module init function to execute when the module is loaded to kernel. .. code-block:: static int __init timer_init(void) { pr_info("Hello world\n"); timer_setup(&my_timer, timer_callback, 0); mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); return 0; } .. _p2_timerPeriodic_9: .. tab-set:: .. tab-item:: Module exit function * Create the module exit function which executes once when the module is unloaded from the kernel. .. code-block:: c static void __exit timer_exit(void) { del_timer(&my_timer); pr_info("Good bye, world\n"); } .. _p2_timerPeriodic_10: .. tab-set:: .. tab-item:: Timer APIs * Create the timer APIs which will be executed once the timer is called. .. code-block:: c static void timer_callback(struct timer_list *timer) { pr_info("Timer Callback function Called [%d]\n", count++); mod_timer(&my_timer, jiffies + msecs_to_jiffies(TIMEOUT)); } .. _p2_timerPeriodic_11: .. tab-set:: .. tab-item:: Mention init and exit functions * Add the module init and exit functions to execute it once the module is loaded and unloaded. .. code-block:: c module_init(timer_init); module_exit(timer_exit); .. _p2_timerPeriodic_12: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: timer.c .. literalinclude:: p2_timerPeriodic/timer.c :language: c :linenos: .. _p2_timerPeriodic_1: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p2_timerPeriodic/Makefile :language: c :linenos: .. _p2_timerPeriodic_2: .. tab-set:: .. tab-item:: Compile and Load * Run make to compile the kernel module. .. code-block:: shell $ make make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_timer modules make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic' CC [M] $HOME/kernel_timer/timer.o Building modules, stage 2. MODPOST 1 modules CC [M] $HOME/kernel_timer/timer.mod.o LD [M] $HOME/kernel_timer/timer.ko make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic' * Check if timer.ko is generated or not using ls command. .. code-block:: shell $ ls -l total 36 -rw-rw-r-- 1 test test 154 Feb 26 13:18 Makefile -rw-rw-r-- 1 test test 47 Feb 26 13:18 modules.order -rw-rw-r-- 1 test test 0 Feb 26 13:18 Module.symvers -rw-rw-r-- 1 test test 820 Feb 26 13:18 timer.c -rw-rw-r-- 1 test test 5880 Feb 26 13:18 timer.ko -rw-rw-r-- 1 test test 47 Feb 26 13:18 timer.mod -rw-rw-r-- 1 test test 919 Feb 26 13:18 timer.mod.c -rw-rw-r-- 1 test test 3448 Feb 26 13:18 timer.mod.o -rw-rw-r-- 1 test test 3320 Feb 26 13:18 timer.o * Run insmod command to load the module in kernel. .. code-block:: shell $ sudo insmod ./timer.ko * Check kernel messages to see if the module is loaded or not. .. code-block:: shell $ dmesg [ 404.787513] Hello world [ 409.821548] Timer Callback function Called [0] [ 414.941540] Timer Callback function Called [1] [ 420.061548] Timer Callback function Called [2] [ 425.181498] Timer Callback function Called [3] [ 430.301546] Timer Callback function Called [4] [ 435.421542] Timer Callback function Called [5] [ 440.541522] Timer Callback function Called [6] [ 445.661495] Timer Callback function Called [7] [ 450.781472] Timer Callback function Called [8] [ 455.901446] Timer Callback function Called [9] [ 461.021382] Timer Callback function Called [10] * Run rmmod to unload the kernel module. .. code-block:: shell $ sudo rmmod timer * Check the kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [ 404.787513] Hello world [ 409.821548] Timer Callback function Called [0] [ 414.941540] Timer Callback function Called [1] [ 420.061548] Timer Callback function Called [2] [ 425.181498] Timer Callback function Called [3] [ 430.301546] Timer Callback function Called [4] [ 435.421542] Timer Callback function Called [5] [ 440.541522] Timer Callback function Called [6] [ 445.661495] Timer Callback function Called [7] [ 450.781472] Timer Callback function Called [8] [ 455.901446] Timer Callback function Called [9] [ 461.021382] Timer Callback function Called [10] [ 461.553199] Good bye, world .. _p2_timerPeriodic_3: .. tab-set:: .. tab-item:: Summary of timer APIs =============================== =========================== API Learning =============================== =========================== timer_setup To initialize the timer mod_timer To modify a timer's timeout del_timer To stop a running timer msecs_to_jiffies To convert milliseconds =============================== ===========================