Skip to content

task 4: add a userspace application and Kernel module that get current time#5

Open
SergiiPiatakov wants to merge 4 commits intoKernel-GL-HRK:sergii.piatakovfrom
SergiiPiatakov:hw_task_04
Open

task 4: add a userspace application and Kernel module that get current time#5
SergiiPiatakov wants to merge 4 commits intoKernel-GL-HRK:sergii.piatakovfrom
SergiiPiatakov:hw_task_04

Conversation

@SergiiPiatakov
Copy link

Add a Kernel module that gets a current time and a user application that also gets a current time and check a time resolution for different timers.

For more details see commit messages.

ekovalyov and others added 4 commits August 18, 2022 17:44
Add a simple userspace application that checks resolution time for all
available system timers and gets the current time.

To allow running the program on different OS (but the same
architecture) it is built with the static linkage.

All helper functions that are used for printing fractional parts are
placed in a separate header file because it is going to be reused for
the kernel module. The main idea behind helper logic is the following:
 - calculate how many zeros have to be prepended to the fractional part
   and prepare a char buffer with them;
 - then remove all trailing zeros;
 - and eventually "concatenate" all parts together.
For simplification proposed to reuse the same char buffer that is
wrapped by the accessor structure. Implementation correctness of memory
operations was checked by the Valgrind util.

Building and running are trivial:
$ make
$ ./main

The program returns the different outputs of time resolution in
different environments:
 - on the host machine:
   CLOCK_REALTIME is ........... 0.000000001
   CLOCK_REALTIME_COARSE is .... 0.004
   CLOCK_MONOTONIC is .......... 0.000000001
   CLOCK_MONOTONIC_COARSE is ... 0.004
   CLOCK_MONOTONIC_RAW is ...... 0.000000001
   CLOCK_BOOTTIME is ........... 0.000000001
   CLOCK_PROCESS_CPUTIME_ID is . 0.000000001
   CLOCK_THREAD_CPUTIME_ID is .. 0.000000001
 - on the emulator:
   CLOCK_REALTIME is ........... 0.004
   CLOCK_REALTIME_COARSE is .... 0.004
   CLOCK_MONOTONIC is .......... 0.004
   CLOCK_MONOTONIC_COARSE is ... 0.004
   CLOCK_MONOTONIC_RAW is ...... 0.004
   CLOCK_BOOTTIME is ........... 0.004
   CLOCK_PROCESS_CPUTIME_ID is . 0.000000001
   CLOCK_THREAD_CPUTIME_ID is .. 0.000000001

Signed-off-by: Sergii Piatakov <sergii.piatakov@globallogic.com>
Add a simple Kernel module that interacts with userspace via `sysfs`.
The module has two files:
 - `/sys/kernel/hello/absolute` - for reading the absolute time of a
   previous reading;
 - `/sys/kernel/hello/relative` - for reading elapsed time since the
   previous reading.

To obtain a current time the module uses the function `ktime_get_raw`
that returns the time of the monotonic clock with nanosecond precision.

To convert a `timespec` structure to the string the module uses a set
of helper functions that were implemented earlier for the userspace
application.

Note: the `vmalloc` function is used here to fill callback prototype
requirements (`vmalloc` has the same interface as `malloc`, but
`kmalloc` has an additional parameter). The callback is required by
the helper functions to make them generic and allow using them from
the Kernel and userspace.

According to the task (only six digits should be displayed) the
nanoseconds truncated up to microseconds.

The patch has been successfully tested by:
 $ checkpatch.pl --no-tree -f hello.c

To simplify testing this commit also provides a simple testing script.

Below a possible output:
 # cat /sys/kernel/hello/relative
 22.73882
 # sleep .1; cat /sys/kernel/hello/relative
 0.14556
 # sleep .2; cat /sys/kernel/hello/relative
 0.25604
 # sleep .5; cat /sys/kernel/hello/relative
 0.559808
 # sleep 1; cat /sys/kernel/hello/relative
 1.056131
 # sleep 2; cat /sys/kernel/hello/relative
 2.05554
 # sleep 5; cat /sys/kernel/hello/relative
 5.055936

 # sleep .1; cat /sys/kernel/hello/absolute
 0.000000
 # sleep .2; cat /sys/kernel/hello/absolute
 32.024149
 # sleep .5; cat /sys/kernel/hello/absolute
 32.279498
 # sleep 1; cat /sys/kernel/hello/absolute
 32.836761
 # sleep 2; cat /sys/kernel/hello/absolute
 33.892153
 # sleep 5; cat /sys/kernel/hello/absolute
 35.948182
 # cat /sys/kernel/hello/absolute
 41.003912

Signed-off-by: Sergii Piatakov <sergii.piatakov@globallogic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants