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 1 commit
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
15 changes: 6 additions & 9 deletions timer/log.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 16 additions & 20 deletions timer/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#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;

Expand All @@ -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);
Expand All @@ -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);
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);
}

Expand All @@ -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;
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion timer/timer_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down