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/hrtimer_check.sh b/timer/hrtimer_check.sh new file mode 100755 index 0000000..90c743d --- /dev/null +++ b/timer/hrtimer_check.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cat /boot/config-$(uname -r) | grep CONFIG_HIGH_RES_TIMERS +sudo cat /proc/timer_list | grep .resolution + diff --git a/timer/log.txt b/timer/log.txt new file mode 100644 index 0000000..e36bcc2 --- /dev/null +++ b/timer/log.txt @@ -0,0 +1,25 @@ +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 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 +rfcomm 81920 4 +ccm 20480 3 diff --git a/timer/timer.c b/timer/timer.c new file mode 100644 index 0000000..7bf65f2 --- /dev/null +++ b/timer/timer.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include + +#define TIMEOUT 1000U + +static u32 inter_sec; +static ktime_t time; + +static void inter_callback(struct timer_list *timer); + +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, "%u\n", inter_sec); + inter_sec = 0; + return strlen(buffer); +} + +static ssize_t time_show(struct class *class, struct class_attribute *attr, + char *buffer) +{ + sprintf(buffer, "%llu\n", time); + time = hrtimer_cb_get_time(&hr_timer); + return strlen(buffer); +} + +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 __init timer_init(void) +{ + int ret; + + class_timer = class_create(THIS_MODULE, "timer"); + if (IS_ERR(class_timer)) { + ret = PTR_ERR(class_timer); + pr_err("timer: failed to create sysfs class: %d\n", ret); + return ret; + } + + ret = class_create_file(class_timer, &class_attr_inter); + if (ret) { + pr_err("timer: bad attribute inter create: %d\n", ret); + return ret; + } + + ret = class_create_file(class_timer, &class_attr_time); + if (ret) { + pr_err("timer: bad attribute time create: %d\n", ret); + return ret; + } + + add_timer(&inter_timer); + hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + + 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); + + pr_info("timer: module loaded\n"); + return 0; +} + +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); + + 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"); diff --git a/timer/timer_test.sh b/timer/timer_test.sh new file mode 100755 index 0000000..83ea57c --- /dev/null +++ b/timer/timer_test.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +make format +make +make install +for (( del = 0; del < 30; del += 5)); do + sleep $del + inter=$(cat /sys/class/timer/inter) + time=$(cat /sys/class/timer/time) + time=$(echo "scale=9;$time/1000000000" | bc) + echo -e "delay = $del s\t${inter} s\t${time} s" +done +make uninstall +