Investigating the Effects of Workqueue on Sleep Duration and Jiffy Count
This investigation looks into the effects of using a workqueue on sleep duration and jiffy count.
Kernel Module
The following kernel module is used:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
static DECLARE_WAIT_QUEUE_HEAD(my_wq);
static int condition = 0;
/* declare a work queue*/
static struct work_struct wrk;
static void work_handler(struct work_struct *work)
{
pr_info("Waitqueue module handler %s, ms cnt is %u\n", __FUNCTION__, jiffies_to_msecs(jiffies));
msleep(3000);
pr_info("Wake up the sleeping module\n");
condition = 1;
wake_up_interruptible(&my_wq);
}
static int __init my_init(void)
{
pr_info("Wait queue example, ms count is %u\n", jiffies_to_msecs(jiffies));
INIT_WORK(&wrk, work_handler);
schedule_work(&wrk);
pr_info("Going to sleep %s\n", __FUNCTION__);
wait_event_interruptible(my_wq, condition != 0);
pr_info("woken up by the work job at %ums\n", jiffies_to_msecs(jiffies));
return 0;
}
void my_exit(void)
{
pr_info("waitqueue example cleanup\n");
}
module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("Aijaz Baig <aijazbaig1@gmail.com>");
MODULE_LICENSE("GPL");
Results
Output from running the module:
-
First run:
Jan 3 06:53:27 buildroot user.info kernel: Wait queue example, ms count is 6144890 Jan 3 06:53:27 buildroot user.info kernel: Going to sleep my_init Jan 3 06:53:27 buildroot user.info kernel: Waitqueue module handler work_handler, ms cnt is 6144890 Jan 3 06:53:30 buildroot user.info kernel: Wake up the sleeping module Jan 3 06:53:30 buildroot user.info kernel: woken up by the work job at 6147900ms
-
Second run:
Jan 3 06:59:28 buildroot user.info kernel: Wait queue example, ms count is 6506580 Jan 3 06:59:28 buildroot user.info kernel: Going to sleep my_init Jan 3 06:59:29 buildroot user.info kernel: Waitqueue module handler work_handler, ms cnt is 6506640 Jan 3 06:59:31 buildroot user.info kernel: Wake up the sleeping module Jan 3 06:59:31 buildroot user.info kernel: woken up by the work job at 6509660ms
Analysis
Two noticeable things can be observed:
- The actual sleep duration is different from the one intended (a difference of ~15ms -20ms).
- The jiffy count under the
__init
function and thework_handler()
function is different for the second run.
This suggests that using a workqueue can have an effect on sleep duration and jiffy count.