I want to find the execution time of a kernel level thread in linux kernel version 5.11.0. the program is in C and I am using do_gettimeofday
function for the same.
code: a C file named task-1.c
//to create kernel level threads and measure the time of completion
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/init.h>
#include <linux/times.h>
#include <linux/delay.h>
static struct task_struct *thread_st;
int thread_fn(void *i) // Function executed by kernel thread
{
printk(KERN_INFO "In thread1");
return 0;
}
// Module Initialization
static int __init init_thread(void)
{
struct timeval t0,t1;
double temp;
//for calculating time take to create a thread
do_gettimeofday(&t0,NULL);
printk(KERN_INFO "Creating Thread\n");
thread_st = kthread_run(thread_fn, NULL, "mythread1003");
do_gettimeofday(&t1, NULL);
if (thread_st)
{
temp = ((t1.tv_sec - t0.tv_sec)*1000000 + (t1.tv_usec -
t0.tv_usec));
printk(KERN_INFO "Thread (Name: mythread1003) Created successfully and Time Take (to create thread is ) %.2f \n",temp);
}
else
printk(KERN_ERR "Thread creation failed\n");
return 0;
}
// Module Exit
static void __exit cleanup_thread(void)
{
printk(KERN_INFO "Cleaning Up\n");
if (thread_st)
{
kthread_stop(thread_st);
printk(KERN_INFO "Thread stopped");
}
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xyz");
MODULE_DESCRIPTION("A Simple Module for the k.threads");
module_init(init_thread);
module_exit(cleanup_thread);
Makefile:
obj-m += task-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
but when I am trying to execute the above using make
command it give me the following error
make -C /lib/modules/5.11.0/build M=/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1 modules
make[1]: Entering directory '/home/jayant/Downloads/linux-5.11'
CC [M] /home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.o
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c: In function ‘init_thread’:
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c:21:20: error: storage size of ‘t0’ isn’t known
struct timeval t0,t1;
^~
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c:21:23: error: storage size of ‘t1’ isn’t known
struct timeval t0,t1;
^~
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c:25:5: error: implicit declaration of function ‘do_gettimeofday’; did you mean ‘do_settimeofday64’? [-Werror=implicit-function-declaration]
do_gettimeofday(&t0,NULL);
^~~~~~~~~~~~~~~
do_settimeofday64
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c:21:23: warning: unused variable ‘t1’ [-Wunused-variable]
struct timeval t0,t1;
^~
/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.c:21:20: warning: unused variable ‘t0’ [-Wunused-variable]
struct timeval t0,t1;
^~
cc1: some warnings being treated as errors
scripts/Makefile.build:279: recipe for target '/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.o' failed
make[2]: *** [/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1/task-1.o] Error 1
Makefile:1800: recipe for target '/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1' failed
make[1]: *** [/home/jayant/Desktop/cse2005/lab-7-kernel-threads/TASK-1] Error 2
make[1]: Leaving directory '/home/jayant/Downloads/linux-5.11'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2
what I think is that, the problem is with do_gettimeofday
command. Is there any other way to calculate the execution time of kernel level thread also I tried using clock()
but still couldn't find the solution for the same.