Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timer #12

Open
wants to merge 9 commits into
base: Matvii.Zorin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions timer/.clang-format
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions timer/Makefile
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions timer/hrtimer_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cat /boot/config-$(uname -r) | grep CONFIG_HIGH_RES_TIMERS
sudo cat /proc/timer_list | grep .resolution

25 changes: 25 additions & 0 deletions timer/log.txt
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions timer/timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/timer.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>

#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");
14 changes: 14 additions & 0 deletions timer/timer_test.sh
Original file line number Diff line number Diff line change
@@ -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