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 4 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
6 changes: 6 additions & 0 deletions timer/hrtimer_check.sh
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions timer/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
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
rfcomm 81920 4
ccm 20480 3
116 changes: 116 additions & 0 deletions timer/timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#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
#define MAX_HR_BUFF 64
#define DOT_POS 9
yekovalyov marked this conversation as resolved.
Show resolved Hide resolved

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);

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, "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)
{
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for timer printing redesign?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ktime_t store value in nanoseconds. So, for getting time string in seconds, I have used this string conversion. Before I was getting the same via division and modulo operations

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simplify this code? I think previous implementation was better.

return strlen(buffer);
}

void inter_callback(struct timer_list *timer)
{
mod_timer(&inter_timer, jiffies + msecs_to_jiffies(TIMEOUT));
++inter_sec;
}

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: 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);
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);

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_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");
}

module_init(timer_init);
module_exit(timer_exit);

MODULE_LICENSE("GPL");
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
iter=$(cat /sys/class/timer/inter)
time=$(cat /sys/class/timer/time)
echo -e "delay = $del\t${iter}\t${time}"
done
make uninstall

exit $RETURN_SUCCESS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the value of RETURN_SUCCESS?
Is this exit required?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's just code style in this example. exit $RETURN_SUCCESS directly indicates the end of script

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that using uninitialized variables is not good practice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this unnecessary exit call.