From dd8bdd1d9d187626f7d5f57bd95cd34b576235e0 Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Thu, 6 Feb 2020 10:17:53 +0200 Subject: [PATCH 1/9] timer: Make init module Add sysfs class, read attribute Add Makefile and format file Signed-off-by: Zorin Matvii --- timer/.clang-format | 104 ++++++++++++++++++++++++++++++++++++++++++++ timer/Makefile | 21 +++++++++ timer/timer.c | 56 ++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 timer/.clang-format create mode 100644 timer/Makefile create mode 100644 timer/timer.c diff --git a/timer/.clang-format b/timer/.clang-format new file mode 100644 index 0000000..f508db0 --- /dev/null +++ b/timer/.clang-format @@ -0,0 +1,104 @@ +AccessModifierOffset: -4 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: false +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakBeforeTernaryOperators: false +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeComma +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: false +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false + +IncludeCategories: + - Regex: '.*' + Priority: 1 +IncludeIsMainRegex: '(Test)?$' +IndentCaseLabels: false +IndentPPDirectives: None +IndentWidth: 8 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: Inner +#ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 8 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true + +# Taken from git's rules +PenaltyBreakAssignment: 10 +PenaltyBreakBeforeFirstCallParameter: 30 +PenaltyBreakComment: 10 +PenaltyBreakFirstLessLess: 0 +PenaltyBreakString: 10 +PenaltyExcessCharacter: 100 +PenaltyReturnTypeOnItsOwnLine: 60 + +PointerAlignment: Right +ReflowComments: false +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp03 +TabWidth: 8 +UseTab: Always diff --git a/timer/Makefile b/timer/Makefile new file mode 100644 index 0000000..a8a9f21 --- /dev/null +++ b/timer/Makefile @@ -0,0 +1,21 @@ +ifneq ($(KERNELRELEASE),) + +obj-m += timer.o + +else + +.PHONY: all clean install uninstall format + +all: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) modules +clean: + $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) clean +install: + sudo insmod timer.ko + lsmod | head -n 3 +uninstall: + sudo rmmod timer.ko + lsmod | head -n 3 +format: + clang-format-9 -i timer.c +endif diff --git a/timer/timer.c b/timer/timer.c new file mode 100644 index 0000000..3cc6e30 --- /dev/null +++ b/timer/timer.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +static ssize_t inter_show(struct class *class, struct class_attribute *attr, + char *buffer); + +static struct class *class_timer = NULL; +CLASS_ATTR_RO(inter); + +static ssize_t inter_show(struct class *class, struct class_attribute *attr, + char *buffer) +{ + sprintf(buffer, "time = \n"); + return strlen(buffer); +} + +static int timer_init(void) +{ + int ret; + + class_timer = class_create(THIS_MODULE, "timer"); + if (IS_ERR(class_timer)) { + ret = PTR_ERR(class_timer); + printk(KERN_ERR "timer: failed to create sysfs class: %d\n", + ret); + return ret; + } + + ret = class_create_file(class_timer, &class_attr_inter); + if (ret) { + printk(KERN_ERR + "timer: failed to create sysfs class attribute inter: %d\n", + ret); + return ret; + } + + printk(KERN_INFO "timer: module loaded\n"); + return 0; +} + +static void timer_exit(void) +{ + if (class_timer) { + class_remove_file(class_timer, &class_attr_inter); + } + class_destroy(class_timer); +} + +module_init(timer_init); +module_exit(timer_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("timer sysfs"); +MODULE_VERSION("0.1"); From 7ad55a805c5ae2d49cb348132d34c7f898b9aded Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Thu, 6 Feb 2020 13:31:08 +0200 Subject: [PATCH 2/9] timer: Add seconds counter Add test script and log file Signed-off-by: Zorin Matvii --- timer/log.txt | 38 ++++++++++++++++++++++++++++++++++++++ timer/timer.c | 32 +++++++++++++++++++++++++++++++- timer/timer_test.sh | 13 +++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 timer/log.txt create mode 100755 timer/timer_test.sh diff --git a/timer/log.txt b/timer/log.txt new file mode 100644 index 0000000..f722a4b --- /dev/null +++ b/timer/log.txt @@ -0,0 +1,38 @@ +clang-format-9 -i timer.c +make -C /lib/modules/5.3.0-26-generic/build M=/home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer modules +make[1]: Entering directory '/usr/src/linux-headers-5.3.0-26-generic' + Building modules, stage 2. + MODPOST 1 modules +make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-26-generic' +sudo insmod timer.ko +lsmod | head -n 3 +Module Size Used by +timer 16384 0 +rfcomm 81920 4 +time = 0 s +delay = 0 time = 0 s +delay = 5 time = 4 s +delay = 10 time = 9 s +delay = 15 time = 14 s +delay = 20 time = 19 s +delay = 25 time = 24 s +delay = 30 time = 29 s +delay = 35 time = 34 s +delay = 40 time = 39 s +delay = 45 time = 43 s +delay = 50 time = 48 s +delay = 55 time = 53 s +delay = 60 time = 58 s +delay = 65 time = 63 s +delay = 70 time = 68 s +delay = 75 time = 73 s +delay = 80 time = 78 s +delay = 85 time = 83 s +delay = 90 time = 87 s +delay = 95 time = 92 s +delay = 100 time = 97 s +sudo rmmod timer.ko +lsmod | head -n 3 +Module Size Used by +rfcomm 81920 4 +ccm 20480 3 diff --git a/timer/timer.c b/timer/timer.c index 3cc6e30..709242a 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -2,20 +2,40 @@ #include #include #include +#include +#include + +#define TIMEOUT 1000U + +//static struct timer_list inter_timer; +static unsigned long sec = 0; static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer); +void inter_callback(struct timer_list *timer); + static struct class *class_timer = NULL; CLASS_ATTR_RO(inter); +DEFINE_TIMER(inter_timer, inter_callback); + static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer) { - sprintf(buffer, "time = \n"); + mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); + sprintf(buffer, "time = %lu s\n", sec); + sec = 0; return strlen(buffer); } +void inter_callback(struct timer_list *timer) +{ + mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); + ++sec; + //printk(KERN_INFO "timer: callback\n"); +} + static int timer_init(void) { int ret; @@ -36,6 +56,12 @@ static int timer_init(void) return ret; } + //setup_timer(&inter_timer, inter_callback, 0); + + add_timer(&inter_timer); + sec = 0; + mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); + printk(KERN_INFO "timer: module loaded\n"); return 0; } @@ -46,6 +72,10 @@ static void timer_exit(void) class_remove_file(class_timer, &class_attr_inter); } class_destroy(class_timer); + + del_timer(&inter_timer); + + printk(KERN_INFO "timer: module unloaded\n"); } module_init(timer_init); diff --git a/timer/timer_test.sh b/timer/timer_test.sh new file mode 100755 index 0000000..981858c --- /dev/null +++ b/timer/timer_test.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +make format +make +make install +cat /sys/class/timer/inter +for (( del = 0; del < 101; del += 5)); do + sleep $del + echo -e "delay = $del\t$(cat /sys/class/timer/inter)" +done +make uninstall + +exit $RETURN_SUCCESS From d2d6fab42f782def0089dccbe0aa3326ad96fda7 Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Thu, 6 Feb 2020 15:37:09 +0200 Subject: [PATCH 3/9] timer: Add high resolution timer Add simple script for checking the high resolution timer state Add sysfs read interface Signed-off-by: Zorin Matvii --- timer/hrtimer_check.sh | 6 +++++ timer/log.txt | 31 +++++++--------------- timer/timer.c | 58 +++++++++++++++++++++++++++++++++--------- timer/timer_test.sh | 6 +++-- 4 files changed, 65 insertions(+), 36 deletions(-) create mode 100755 timer/hrtimer_check.sh diff --git a/timer/hrtimer_check.sh b/timer/hrtimer_check.sh new file mode 100755 index 0000000..e3028ae --- /dev/null +++ b/timer/hrtimer_check.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +cat /boot/config-$(uname -r) | grep CONFIG_HIGH_RES_TIMERS +sudo cat /proc/timer_list | grep .resolution + +exit $RETURN_SUCCESS diff --git a/timer/log.txt b/timer/log.txt index f722a4b..d4873b7 100644 --- a/timer/log.txt +++ b/timer/log.txt @@ -1,36 +1,23 @@ clang-format-9 -i timer.c make -C /lib/modules/5.3.0-26-generic/build M=/home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer modules make[1]: Entering directory '/usr/src/linux-headers-5.3.0-26-generic' + CC [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.o Building modules, stage 2. MODPOST 1 modules + LD [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.ko make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-26-generic' sudo insmod timer.ko lsmod | head -n 3 Module Size Used by timer 16384 0 rfcomm 81920 4 -time = 0 s -delay = 0 time = 0 s -delay = 5 time = 4 s -delay = 10 time = 9 s -delay = 15 time = 14 s -delay = 20 time = 19 s -delay = 25 time = 24 s -delay = 30 time = 29 s -delay = 35 time = 34 s -delay = 40 time = 39 s -delay = 45 time = 43 s -delay = 50 time = 48 s -delay = 55 time = 53 s -delay = 60 time = 58 s -delay = 65 time = 63 s -delay = 70 time = 68 s -delay = 75 time = 73 s -delay = 80 time = 78 s -delay = 85 time = 83 s -delay = 90 time = 87 s -delay = 95 time = 92 s -delay = 100 time = 97 s +inter = 0 s +delay = 0 inter = 0 s inter = 9225 s 645643198 ns +delay = 5 inter = 4 s inter = 9230 s 651359080 ns +delay = 10 inter = 9 s inter = 9240 s 660130462 ns +delay = 15 inter = 14 s inter = 9255 s 668047092 ns +delay = 20 inter = 19 s inter = 9275 s 675716388 ns +delay = 25 inter = 24 s inter = 9300 s 679152476 ns sudo rmmod timer.ko lsmod | head -n 3 Module Size Used by diff --git a/timer/timer.c b/timer/timer.c index 709242a..6c74161 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -3,38 +3,65 @@ #include #include #include -#include +#include +#include #define TIMEOUT 1000U -//static struct timer_list inter_timer; -static unsigned long sec = 0; +static u32 inter_sec = 0; static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer); +static ssize_t time_show(struct class *class, struct class_attribute *attr, + char *buffer); void inter_callback(struct timer_list *timer); +//enum hrtimer_restart hr_callback(struct hrtimer *timer); static struct class *class_timer = NULL; CLASS_ATTR_RO(inter); +CLASS_ATTR_RO(time); DEFINE_TIMER(inter_timer, inter_callback); +static struct hrtimer hr_timer; static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer) { mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); - sprintf(buffer, "time = %lu s\n", sec); - sec = 0; + sprintf(buffer, "inter = %u s\n", inter_sec); + inter_sec = 0; + return strlen(buffer); +} + +static ssize_t time_show(struct class *class, struct class_attribute *attr, + char *buffer) +{ + static ktime_t time = 0; + static s32 hr_sec = 0; + static s64 hr_nsec = 0; + time = hrtimer_cb_get_time(&hr_timer); + hr_sec = time / NSEC_PER_SEC; + hr_nsec = time % NSEC_PER_SEC; + sprintf(buffer, "inter = %u s %lld ns\n", hr_sec, hr_nsec); return strlen(buffer); } void inter_callback(struct timer_list *timer) { mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); - ++sec; - //printk(KERN_INFO "timer: callback\n"); + ++inter_sec; +} + +//For restarting the timer after ~585 years +/* +enum hrtimer_restart hr_callback(struct hrtimer *timer) +{ + //--restart actions-- + hrtimer_forward_now(timer,KTIME_MAX)); + return HRTIMER_RESTART; } +*/ static int timer_init(void) { @@ -50,17 +77,22 @@ static int timer_init(void) ret = class_create_file(class_timer, &class_attr_inter); if (ret) { - printk(KERN_ERR - "timer: failed to create sysfs class attribute inter: %d\n", - ret); + printk(KERN_ERR "timer: bad create attribute inter: %d\n", ret); return ret; } - //setup_timer(&inter_timer, inter_callback, 0); + ret = class_create_file(class_timer, &class_attr_time); + if (ret) { + printk(KERN_ERR "timer: bad create attribute time: %d\n", ret); + return ret; + } add_timer(&inter_timer); - sec = 0; + hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + //hr_timer.function = &hr_callback; + mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); + hrtimer_start(&hr_timer, KTIME_MAX, HRTIMER_MODE_REL); printk(KERN_INFO "timer: module loaded\n"); return 0; @@ -70,10 +102,12 @@ static void timer_exit(void) { if (class_timer) { class_remove_file(class_timer, &class_attr_inter); + class_remove_file(class_timer, &class_attr_time); } class_destroy(class_timer); del_timer(&inter_timer); + hrtimer_cancel(&hr_timer); printk(KERN_INFO "timer: module unloaded\n"); } diff --git a/timer/timer_test.sh b/timer/timer_test.sh index 981858c..1db4f4e 100755 --- a/timer/timer_test.sh +++ b/timer/timer_test.sh @@ -4,9 +4,11 @@ make format make make install cat /sys/class/timer/inter -for (( del = 0; del < 101; del += 5)); do +for (( del = 0; del < 30; del += 5)); do sleep $del - echo -e "delay = $del\t$(cat /sys/class/timer/inter)" + iter=$(cat /sys/class/timer/inter) + time=$(cat /sys/class/timer/time) + echo -e "delay = $del\t${iter}\t${time}" done make uninstall From 1ef5122019125ecb4d296bc82ffb91faee7cc2ae Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Thu, 6 Feb 2020 16:20:56 +0200 Subject: [PATCH 4/9] timer: Change high resolution timer show mode to float point Remove useless callback function and its adding to hrtimer structure Signed-off-by: Zorin Matvii --- timer/log.txt | 15 ++++++--------- timer/timer.c | 36 ++++++++++++++++-------------------- timer/timer_test.sh | 1 - 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/timer/log.txt b/timer/log.txt index d4873b7..ae5cc4b 100644 --- a/timer/log.txt +++ b/timer/log.txt @@ -1,23 +1,20 @@ clang-format-9 -i timer.c make -C /lib/modules/5.3.0-26-generic/build M=/home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer modules make[1]: Entering directory '/usr/src/linux-headers-5.3.0-26-generic' - CC [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.o Building modules, stage 2. MODPOST 1 modules - LD [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.ko make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-26-generic' sudo insmod timer.ko lsmod | head -n 3 Module Size Used by timer 16384 0 rfcomm 81920 4 -inter = 0 s -delay = 0 inter = 0 s inter = 9225 s 645643198 ns -delay = 5 inter = 4 s inter = 9230 s 651359080 ns -delay = 10 inter = 9 s inter = 9240 s 660130462 ns -delay = 15 inter = 14 s inter = 9255 s 668047092 ns -delay = 20 inter = 19 s inter = 9275 s 675716388 ns -delay = 25 inter = 24 s inter = 9300 s 679152476 ns +delay = 0 inter = 0 s time = 12023.773065613 s +delay = 5 inter = 4 s time = 12028.779487398 s +delay = 10 inter = 9 s time = 12038.887123080 s +delay = 15 inter = 14 s time = 12053.994505999 s +delay = 20 inter = 19 s time = 12073.001877621 s +delay = 25 inter = 24 s time = 12098.007975765 s sudo rmmod timer.ko lsmod | head -n 3 Module Size Used by diff --git a/timer/timer.c b/timer/timer.c index 6c74161..1eecb6d 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -7,6 +7,8 @@ #include #define TIMEOUT 1000U +#define MAX_HR_BUFF 64 +#define DOT_POS 9 static u32 inter_sec = 0; @@ -16,7 +18,6 @@ static ssize_t time_show(struct class *class, struct class_attribute *attr, char *buffer); void inter_callback(struct timer_list *timer); -//enum hrtimer_restart hr_callback(struct hrtimer *timer); static struct class *class_timer = NULL; CLASS_ATTR_RO(inter); @@ -37,13 +38,19 @@ static ssize_t inter_show(struct class *class, struct class_attribute *attr, static ssize_t time_show(struct class *class, struct class_attribute *attr, char *buffer) { - static ktime_t time = 0; - static s32 hr_sec = 0; - static s64 hr_nsec = 0; + ktime_t time = 0; + char hr_sec[MAX_HR_BUFF] = ""; + size_t len = 0; time = hrtimer_cb_get_time(&hr_timer); - hr_sec = time / NSEC_PER_SEC; - hr_nsec = time % NSEC_PER_SEC; - sprintf(buffer, "inter = %u s %lld ns\n", hr_sec, hr_nsec); + len = snprintf(hr_sec, MAX_HR_BUFF, "%lld", time); + if (len > DOT_POS && len < MAX_HR_BUFF - 1) { + size_t i; + for (i = len - 1; i > len - DOT_POS; --i) { + hr_sec[i + 1] = hr_sec[i]; + } + hr_sec[len - DOT_POS] = '.'; + } + sprintf(buffer, "time = %s s\n", hr_sec); return strlen(buffer); } @@ -53,16 +60,6 @@ void inter_callback(struct timer_list *timer) ++inter_sec; } -//For restarting the timer after ~585 years -/* -enum hrtimer_restart hr_callback(struct hrtimer *timer) -{ - //--restart actions-- - hrtimer_forward_now(timer,KTIME_MAX)); - return HRTIMER_RESTART; -} -*/ - static int timer_init(void) { int ret; @@ -77,19 +74,18 @@ static int timer_init(void) ret = class_create_file(class_timer, &class_attr_inter); if (ret) { - printk(KERN_ERR "timer: bad create attribute inter: %d\n", ret); + printk(KERN_ERR "timer: bad attribute inter create: %d\n", ret); return ret; } ret = class_create_file(class_timer, &class_attr_time); if (ret) { - printk(KERN_ERR "timer: bad create attribute time: %d\n", ret); + printk(KERN_ERR "timer: bad attribute time create: %d\n", ret); return ret; } add_timer(&inter_timer); hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); - //hr_timer.function = &hr_callback; mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); hrtimer_start(&hr_timer, KTIME_MAX, HRTIMER_MODE_REL); diff --git a/timer/timer_test.sh b/timer/timer_test.sh index 1db4f4e..4dad63b 100755 --- a/timer/timer_test.sh +++ b/timer/timer_test.sh @@ -3,7 +3,6 @@ make format make make install -cat /sys/class/timer/inter for (( del = 0; del < 30; del += 5)); do sleep $del iter=$(cat /sys/class/timer/inter) From 4bfe98cefdc0c876d1fbc4e63784c8411826ed3d Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Sat, 8 Feb 2020 15:55:54 +0200 Subject: [PATCH 5/9] timer: Change sysfs show mode Replace the time value conversion from kernel module to bash script Remove plain text from showing in sysfs interfaces Signed-off-by: Zorin Matvii --- timer/log.txt | 12 ++++++------ timer/timer.c | 17 +++-------------- timer/timer_test.sh | 5 +++-- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/timer/log.txt b/timer/log.txt index ae5cc4b..b8c87e1 100644 --- a/timer/log.txt +++ b/timer/log.txt @@ -9,12 +9,12 @@ lsmod | head -n 3 Module Size Used by timer 16384 0 rfcomm 81920 4 -delay = 0 inter = 0 s time = 12023.773065613 s -delay = 5 inter = 4 s time = 12028.779487398 s -delay = 10 inter = 9 s time = 12038.887123080 s -delay = 15 inter = 14 s time = 12053.994505999 s -delay = 20 inter = 19 s time = 12073.001877621 s -delay = 25 inter = 24 s time = 12098.007975765 s +delay = 0 s 0 s 43101.479117324 s +delay = 5 s 4 s 43106.487744126 s +delay = 10 s 9 s 43116.499542923 s +delay = 15 s 14 s 43131.512692214 s +delay = 20 s 19 s 43151.521922340 s +delay = 25 s 24 s 43176.534086133 s sudo rmmod timer.ko lsmod | head -n 3 Module Size Used by diff --git a/timer/timer.c b/timer/timer.c index 1eecb6d..45c7b88 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -30,7 +30,7 @@ static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer) { mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); - sprintf(buffer, "inter = %u s\n", inter_sec); + sprintf(buffer, "%u\n", inter_sec); inter_sec = 0; return strlen(buffer); } @@ -38,19 +38,8 @@ static ssize_t inter_show(struct class *class, struct class_attribute *attr, static ssize_t time_show(struct class *class, struct class_attribute *attr, char *buffer) { - ktime_t time = 0; - char hr_sec[MAX_HR_BUFF] = ""; - size_t len = 0; - time = hrtimer_cb_get_time(&hr_timer); - len = snprintf(hr_sec, MAX_HR_BUFF, "%lld", time); - if (len > DOT_POS && len < MAX_HR_BUFF - 1) { - size_t i; - for (i = len - 1; i > len - DOT_POS; --i) { - hr_sec[i + 1] = hr_sec[i]; - } - hr_sec[len - DOT_POS] = '.'; - } - sprintf(buffer, "time = %s s\n", hr_sec); + ktime_t time = hrtimer_cb_get_time(&hr_timer); + sprintf(buffer, "%lu\n", time); return strlen(buffer); } diff --git a/timer/timer_test.sh b/timer/timer_test.sh index 4dad63b..ac0aa71 100755 --- a/timer/timer_test.sh +++ b/timer/timer_test.sh @@ -5,9 +5,10 @@ make make install for (( del = 0; del < 30; del += 5)); do sleep $del - iter=$(cat /sys/class/timer/inter) + inter=$(cat /sys/class/timer/inter) time=$(cat /sys/class/timer/time) - echo -e "delay = $del\t${iter}\t${time}" + time=$(echo "scale=9;$time/1000000000" | bc) + echo -e "delay = $del s\t${inter} s\t${time} s" done make uninstall From be0fb1ee77fdb0fca4b4f4d5bb984f796fb46799 Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Sat, 8 Feb 2020 16:10:09 +0200 Subject: [PATCH 6/9] timer: Change absolute time interface Show absolute time of previous reading (before was current) Signed-off-by: Zorin Matvii --- timer/log.txt | 15 +++++++++------ timer/timer.c | 6 ++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/timer/log.txt b/timer/log.txt index b8c87e1..e36bcc2 100644 --- a/timer/log.txt +++ b/timer/log.txt @@ -1,20 +1,23 @@ clang-format-9 -i timer.c make -C /lib/modules/5.3.0-26-generic/build M=/home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer modules make[1]: Entering directory '/usr/src/linux-headers-5.3.0-26-generic' + CC [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.o Building modules, stage 2. MODPOST 1 modules + CC /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.mod.o + LD [M] /home/matthewzorin/Linux_Kernel/gl-kernel-basecamp-2020/timer/timer.ko make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-26-generic' sudo insmod timer.ko lsmod | head -n 3 Module Size Used by timer 16384 0 rfcomm 81920 4 -delay = 0 s 0 s 43101.479117324 s -delay = 5 s 4 s 43106.487744126 s -delay = 10 s 9 s 43116.499542923 s -delay = 15 s 14 s 43131.512692214 s -delay = 20 s 19 s 43151.521922340 s -delay = 25 s 24 s 43176.534086133 s +delay = 0 s 0 s 43938.387953473 s +delay = 5 s 4 s 43938.401249981 s +delay = 10 s 9 s 43943.409896369 s +delay = 15 s 14 s 43953.422080384 s +delay = 20 s 19 s 43968.434459577 s +delay = 25 s 24 s 43988.446966911 s sudo rmmod timer.ko lsmod | head -n 3 Module Size Used by diff --git a/timer/timer.c b/timer/timer.c index 45c7b88..6807db3 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -11,6 +11,7 @@ #define DOT_POS 9 static u32 inter_sec = 0; +static ktime_t time = 0; static ssize_t inter_show(struct class *class, struct class_attribute *attr, char *buffer); @@ -38,8 +39,8 @@ static ssize_t inter_show(struct class *class, struct class_attribute *attr, static ssize_t time_show(struct class *class, struct class_attribute *attr, char *buffer) { - ktime_t time = hrtimer_cb_get_time(&hr_timer); - sprintf(buffer, "%lu\n", time); + sprintf(buffer, "%llu\n", time); + time = hrtimer_cb_get_time(&hr_timer); return strlen(buffer); } @@ -78,6 +79,7 @@ static int timer_init(void) mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); hrtimer_start(&hr_timer, KTIME_MAX, HRTIMER_MODE_REL); + time = hrtimer_cb_get_time(&hr_timer); printk(KERN_INFO "timer: module loaded\n"); return 0; From 531406d3294406053b9cdf4fa34d3c9719ad79de Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Mon, 10 Feb 2020 12:24:18 +0200 Subject: [PATCH 7/9] timer: Remove useless define Signed-off-by: Zorin Matvii --- timer/timer.c | 1 - 1 file changed, 1 deletion(-) diff --git a/timer/timer.c b/timer/timer.c index 6807db3..6bd40d4 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -8,7 +8,6 @@ #define TIMEOUT 1000U #define MAX_HR_BUFF 64 -#define DOT_POS 9 static u32 inter_sec = 0; static ktime_t time = 0; From ebfabad9f4c06b1d7b10b94c68eb0a7621517c1f Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Tue, 11 Feb 2020 12:28:22 +0200 Subject: [PATCH 8/9] timer: Refactor code Signed-off-by: Zorin Matvii --- timer/timer.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/timer/timer.c b/timer/timer.c index 6bd40d4..7bf65f2 100644 --- a/timer/timer.c +++ b/timer/timer.c @@ -7,21 +7,11 @@ #include #define TIMEOUT 1000U -#define MAX_HR_BUFF 64 -static u32 inter_sec = 0; -static ktime_t time = 0; +static u32 inter_sec; +static ktime_t time; -static ssize_t inter_show(struct class *class, struct class_attribute *attr, - char *buffer); -static ssize_t time_show(struct class *class, struct class_attribute *attr, - char *buffer); - -void inter_callback(struct timer_list *timer); - -static struct class *class_timer = NULL; -CLASS_ATTR_RO(inter); -CLASS_ATTR_RO(time); +static void inter_callback(struct timer_list *timer); DEFINE_TIMER(inter_timer, inter_callback); static struct hrtimer hr_timer; @@ -43,33 +33,36 @@ static ssize_t time_show(struct class *class, struct class_attribute *attr, return strlen(buffer); } -void inter_callback(struct timer_list *timer) +static struct class *class_timer; +CLASS_ATTR_RO(inter); +CLASS_ATTR_RO(time); + +static void inter_callback(struct timer_list *timer) { mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT)); ++inter_sec; } -static int timer_init(void) +static int __init timer_init(void) { int ret; class_timer = class_create(THIS_MODULE, "timer"); if (IS_ERR(class_timer)) { ret = PTR_ERR(class_timer); - printk(KERN_ERR "timer: failed to create sysfs class: %d\n", - ret); + pr_err("timer: failed to create sysfs class: %d\n", ret); return ret; } ret = class_create_file(class_timer, &class_attr_inter); if (ret) { - printk(KERN_ERR "timer: bad attribute inter create: %d\n", ret); + pr_err("timer: bad attribute inter create: %d\n", ret); return ret; } ret = class_create_file(class_timer, &class_attr_time); if (ret) { - printk(KERN_ERR "timer: bad attribute time create: %d\n", ret); + pr_err("timer: bad attribute time create: %d\n", ret); return ret; } @@ -80,27 +73,28 @@ static int timer_init(void) hrtimer_start(&hr_timer, KTIME_MAX, HRTIMER_MODE_REL); time = hrtimer_cb_get_time(&hr_timer); - printk(KERN_INFO "timer: module loaded\n"); + pr_info("timer: module loaded\n"); return 0; } -static void timer_exit(void) +static void __exit timer_exit(void) { + del_timer(&inter_timer); + hrtimer_cancel(&hr_timer); + if (class_timer) { class_remove_file(class_timer, &class_attr_inter); class_remove_file(class_timer, &class_attr_time); } class_destroy(class_timer); - del_timer(&inter_timer); - hrtimer_cancel(&hr_timer); - - printk(KERN_INFO "timer: module unloaded\n"); + pr_info("timer: module unloaded\n"); } module_init(timer_init); module_exit(timer_exit); MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Matvii Zorin"); MODULE_DESCRIPTION("timer sysfs"); MODULE_VERSION("0.1"); From bc84d65be0573c47af64a9a335a2de31329de65a Mon Sep 17 00:00:00 2001 From: Zorin Matvii Date: Mon, 17 Feb 2020 11:48:19 +0200 Subject: [PATCH 9/9] timer: Remove exit command from each test script Signed-off-by: Zorin Matvii --- timer/hrtimer_check.sh | 1 - timer/timer_test.sh | 1 - 2 files changed, 2 deletions(-) diff --git a/timer/hrtimer_check.sh b/timer/hrtimer_check.sh index e3028ae..90c743d 100755 --- a/timer/hrtimer_check.sh +++ b/timer/hrtimer_check.sh @@ -3,4 +3,3 @@ cat /boot/config-$(uname -r) | grep CONFIG_HIGH_RES_TIMERS sudo cat /proc/timer_list | grep .resolution -exit $RETURN_SUCCESS diff --git a/timer/timer_test.sh b/timer/timer_test.sh index ac0aa71..83ea57c 100755 --- a/timer/timer_test.sh +++ b/timer/timer_test.sh @@ -12,4 +12,3 @@ for (( del = 0; del < 30; del += 5)); do done make uninstall -exit $RETURN_SUCCESS