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
Open
task 4: add a userspace application and Kernel module that get current time#5SergiiPiatakov wants to merge 4 commits intoKernel-GL-HRK:sergii.piatakovfrom
SergiiPiatakov wants to merge 4 commits intoKernel-GL-HRK:sergii.piatakovfrom
Conversation
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>
c874ac1 to
465410a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.