Skip to contentSkip to conversation

Clocksource: Add sched_clock to Atmel TCB clocksource

PATCH Superseded Carsten Emde 2 participants 5 messages Jun 2, 2015 Clocksource, clockevent drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Clocksource: Add sched_clock to Atmel TCB clocksource

The default sched_clock provides a maximum resolution of 1 ms which normally is not sufficient to investigate scheduling related irregularities - more so, if a low-latency or even a real-time kernel is used. Therefore, register the Atmel TCB clocksource that provides a resolution of 63 ns as sched_clock.

---
 drivers/clocksource/tcb_clksrc.c |   15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/clocksource/tcb_clksrc.c +15 -0
Index: linux-3.18.13-rt10-r7s4/drivers/clocksource/tcb_clksrc.c
===================================================================
@@ -10,6 +10,7 @@
 #include <linux/io.h>
 #include <linux/platform_device.h>
 #include <linux/atmel_tc.h>
+#include <linux/sched_clock.h>
 
 
 /*
@@ -40,6 +41,7 @@
  */
 
 static void __iomem *tcaddr;
+static cycle_t (*do_sched_clock_get_cycles)(struct clocksource *cs);
 
 static cycle_t tc_get_cycles(struct clocksource *cs)
 {
@@ -61,6 +63,13 @@ static cycle_t tc_get_cycles32(struct cl
 	return __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
 }
 
+u64 sched_clock_get_cycles(void)
+{
+	struct clocksource *cs = NULL;
+
+	return (u64) do_sched_clock_get_cycles(cs);
+}
+
 static struct clocksource clksrc = {
 	.name           = "tcb_clksrc",
 	.rating         = 200,
@@ -273,6 +282,7 @@ static int __init tcb_clksrc_init(void)
 	int clk32k_divisor_idx = -1;
 	int i;
 	int ret;
+	unsigned long flags;
 
 	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
 	if (!tc) {
@@ -339,6 +349,11 @@ static int __init tcb_clksrc_init(void)
 	if (ret)
 		goto err_disable_t1;
 
+	do_sched_clock_get_cycles = clksrc.read;
+	local_irq_save(flags);
+	sched_clock_register(sched_clock_get_cycles, 32, divided_rate);
+	local_irq_restore(flags);
+
 	/* channel 2:  periodic and oneshot timer support */
 	ret = setup_clkevents(tc, clk32k_divisor_idx);
 	if (ret)
#1 lore raw
Quoted 2 lines · Carsten Emde

static void __iomem *tcaddr;
+static cycle_t (*do_sched_clock_get_cycles)(struct clocksource *cs);

what's that for? Indirection for indirection sake?

drivers/clocksource/tcb_clksrc.c static cycle_t tc_get_cycles32(struct cl · line 66

Quoted 7 lines

static cycle_t tc_get_cycles(struct clocksource *cs) {

drivers/clocksource/tcb_clksrc.c +1 -0
@@ -61,6 +63,13 @@ static cycle_t tc_get_cycles32(struct cl
 	return __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
 }
 
+u64 sched_clock_get_cycles(void)

Static?

drivers/clocksource/tcb_clksrc.c static cycle_t tc_get_cycles32(struct cl · line 70

Quoted 4 lines
drivers/clocksource/tcb_clksrc.c +4 -0
+{
+	struct clocksource *cs = NULL;
+
+	return (u64) do_sched_clock_get_cycles(cs);

What's wrong with implementing this like:

static u64 sched_clock_get_cycles(void)

{
	return tc_get_cycles(NULL); 
}

Hmm?

drivers/clocksource/tcb_clksrc.c static int __init tcb_clksrc_init(void) · line 352

Quoted 13 lines
drivers/clocksource/tcb_clksrc.c +2 -0
@@ -273,6 +282,7 @@ static int __init tcb_clksrc_init(void)
 	int clk32k_divisor_idx = -1;
 	int i;
 	int ret;
+	unsigned long flags;
 
 	tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
 	if (!tc) {
@@ -339,6 +349,11 @@ static int __init tcb_clksrc_init(void)
 	if (ret)
 		goto err_disable_t1;
 
+	do_sched_clock_get_cycles = clksrc.read;

Sigh.

drivers/clocksource/tcb_clksrc.c static int __init tcb_clksrc_init(void) · line 353

Quoted 1 line
drivers/clocksource/tcb_clksrc.c +1 -0
+	local_irq_save(flags);

What's the purpose of this?

Thanks,

tglx

#2 lore raw

Hi Thomas,

Quoted 4 lines
Quoted 2 lines

static void __iomem *tcaddr;
+static cycle_t (*do_sched_clock_get_cycles)(struct clocksource *cs);

what's that for? Indirection for indirection sake?

The code apparently provides two different functions to read the current clock - depending on whether the clock is setup in single-channel mode or not.

The default function is predefined in the struct clocksource clksrc:
.read = tc_get_cycles
This structure element may later on be overwritten, if the counter is configured in single-channel mode:
clksrc.read = tc_get_cycles32;

This is why my code sets do_sched_clock_get_cycles to clksrc.read before sched_clock is registered to make sure the appropriate read function will be used.

Quoted 2 lines
Quoted 1 line

+u64 sched_clock_get_cycles(void)

Static?

Yes, sure, should be static. Will fix it.

Quoted 7 lines

What's wrong with implementing this like:

static u64 sched_clock_get_cycles(void)

{
	return tc_get_cycles(NULL);
}

Hmm?

See above.

Quoted 2 lines

drivers/clocksource/tcb_clksrc.c static int __init tcb_clksrc_init(void) · line 353

Quoted 1 line
drivers/clocksource/tcb_clksrc.c +1 -0
+	local_irq_save(flags);

What's the purpose of this?

The sched_clock_register() function contains
WARN_ON(!irqs_disabled());
which I did not want to trigger.

Thanks,
-Carsten.

#3 lore raw
Quoted 13 lines · Carsten Emde
Quoted 4 lines
Quoted 2 lines

static void __iomem *tcaddr;
+static cycle_t (*do_sched_clock_get_cycles)(struct clocksource *cs);

what's that for? Indirection for indirection sake?

The code apparently provides two different functions to read the current clock
- depending on whether the clock is setup in single-channel mode or not.

The default function is predefined in the struct clocksource clksrc:
.read = tc_get_cycles
This structure element may later on be overwritten, if the counter is configured in single-channel mode:
clksrc.read = tc_get_cycles32;

Quoted 3 lines

This is why my code sets do_sched_clock_get_cycles to clksrc.read before sched_clock is registered to make sure the appropriate read function will be used.

  return clksrc.read(NULL);

then?

Quoted 5 lines
Quoted 2 lines

drivers/clocksource/tcb_clksrc.c static int __init tcb_clksrc_init(void) · line 353

Quoted 1 line
drivers/clocksource/tcb_clksrc.c +1 -0
+	local_irq_save(flags);

What's the purpose of this?

The sched_clock_register() function contains
WARN_ON(!irqs_disabled());
which I did not want to trigger.

That code is run in the early setup with interrupts disabled unless I'm missing something ...

Thanks,

tglx

#4 lore raw

Hi Thomas,

Quoted 13 lines
Quoted 13 lines
Quoted 4 lines
Quoted 2 lines
   static void __iomem *tcaddr;

+static cycle_t (*do_sched_clock_get_cycles)(struct clocksource *cs);

what's that for? Indirection for indirection sake?

The code apparently provides two different functions to read the current clock
- depending on whether the clock is setup in single-channel mode or not.

The default function is predefined in the struct clocksource clksrc:
.read = tc_get_cycles
This structure element may later on be overwritten, if the counter is configured in single-channel mode:

   clksrc.read = tc_get_cycles32;
Quoted 5 lines
Quoted 3 lines

This is why my code sets do_sched_clock_get_cycles to clksrc.read before sched_clock is registered to make sure the appropriate read function will be used.

return clksrc.read(NULL);
then?

Much better, of course, V2 will have it.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

drivers/clocksource/tcb_clksrc.c static int __init tcb_clksrc_init(void) · line 353

Quoted 1 line
drivers/clocksource/tcb_clksrc.c +1 -0
+	local_irq_save(flags);

What's the purpose of this?

The sched_clock_register() function contains
WARN_ON(!irqs_disabled());
which I did not want to trigger.

That code is run in the early setup with interrupts disabled unless I'm missing something ...

Maybe, something else is wrong, and interrupts are enabled too early on this system. I added the lines around sched_clock_register(), because WARN_ON triggered. Will investigate further.

Thanks,
-Carsten.