• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Example of using getnstimeofday in Linux kernel

by admin

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

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 7 : How to check the status of a service using systemd
  2. How To Setup mutt in CentOS/RHEL
  3. UNIX / Linux : how to force user to change their password on next login after password has reset
  4. CentOS / RHEL : How to remove unused Physical Volume(PV) from Volume Group (VG) in LVM
  5. How to Identify NIS Authenticated Users
  6. checkupdates: command not found
  7. “resize2fs: Permission denied to resize filesystem” error while online resize of a filesystem
  8. Audit rules to log reboot command executions in CentOS/RHEL
  9. wc: command not found
  10. mkfs.exfat Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • protonvpn-cli Command Examples in Linux
  • protonvpn-cli connect Command Examples
  • procs Command Examples in Linux
  • prlimit: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright