Example of using getnstimeofday in Linux kernel

getnstimeofday is a front-end for __get_realtime_clock_ts but also works if no high-resolution clocks are available in the system. In this case, getnstimeofday as defined in kernel/time.c (instead of kernel/time/timekeeping.c) is used to provide a timespec that fulfills only low-resolution requirements.

The linux kernel provides a number of interfaces to manage time. getnstimeofday is one of them, which gives the time in seconds and nanoseconds. The function is implemented in “timekeeping32.h” and returns a structure of the type timespec which has two members.

struct timespec64 {
 time64_t tv_sec;   /* seconds */ long  tv_nsec;  /* nanoseconds */};

To print the time, we only need to print the values of tv_sec and tv_nsec which gets filled by the call to the function getnstimeofday. In the following example code, we have created a proc entry called gettime, which prints out the values of seconds and nanoseconds when read.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>

int len;
char *msg;
ssize_t size;
struct timespec ts;

ssize_t read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) 
{
struct timespec ts;
char *temp;
temp=kmalloc(50*sizeof(char),GFP_KERNEL);
getnstimeofday(&ts);
sprintf(temp,"%ld seconds \n%ld nanoseconds\n",ts.tv_sec, ts.tv_nsec);
len=strlen(temp);
size=sizeof(char)*len;

return simple_read_from_buffer(buf,count,offp,temp,size);
}

struct file_operations proc_fops = {
read:   read_proc
};
void create_new_proc_entry(void) 
{
proc_create("gettime",0,NULL,&proc_fops);
}

int proc_init (void) {
    create_new_proc_entry();
    return 0;
}

void proc_cleanup(void) {
        remove_proc_entry("gettime",NULL);
}

MODULE_LICENSE("GPL");  
module_init(proc_init);
module_exit(proc_cleanup);

Save the above code as proc_read_gettimeofday.c and compile the code using the following makefile.

ifneq ($(KERNELRELEASE),)    
   obj-m := proc_read_gettimeofday.o 
else 

KERNELDIR ?= /lib/modules/$(shell uname -r)/build 
PWD := $(shell pwd)

default:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules  
clean:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) clean
endif

Compile and insert the module using:

$ make
$ sudo insmod proc_read_getnstimeofday.ko

To see the output, just read the proc entry gettime, using the cat command.

# cat /proc/gettime 
1584690328 seconds 
290430470 nanoseconds
Related Post