2009-10-16 11:39:17

by wu zhangjin

[permalink] [raw]
Subject: [PATCH 1/2] tracing: convert trace_clock_local() as weak function

trace_clock_local() is based on the arch-specific sched_clock(), in X86,
it is tsc(64bit) based, which can give very high precision(about 1ns
with 1GHz). but in MIPS, the sched_clock() is jiffies based, which can
give only 10ms precison with 1000 HZ. which is not enough for tracing,
especially for Real Time system.

so, we need to implement a MIPS specific sched_clock() to get higher
precision. There is a tsc like clock counter register in MIPS, whose
frequency is half of the processor, so, if the cpu frequency is 800MHz,
the time precision reaches 2.5ns, which is very good for tracing, even
for Real Time system.

but 'Cause it is only 32bit long, which will rollover quickly, so, such
a sched_clock() will bring with extra load, which is not good for the
whole system. so, we only need to implement a arch-specific
trace_clock_local() for tracing. as a preparation, we convert it as a
weak function.

The MIPS specific trace_clock_local() is coming in the next patch.

Signed-off-by: Wu Zhangjin <[email protected]>
---
kernel/trace/trace_clock.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/kernel/trace/trace_clock.c b/kernel/trace/trace_clock.c
index 20c5f92..a04dc18 100644
--- a/kernel/trace/trace_clock.c
+++ b/kernel/trace/trace_clock.c
@@ -26,7 +26,7 @@
* Useful for tracing that does not cross to other CPUs nor
* does it go through idle events.
*/
-u64 notrace trace_clock_local(void)
+u64 __attribute__((weak)) notrace trace_clock_local(void)
{
unsigned long flags;
u64 clock;
--
1.6.2.1


2009-10-16 11:39:24

by wu zhangjin

[permalink] [raw]
Subject: [PATCH 2/2] tracing: add high precision version of trace_clock_local() for MIPS

By default, trace_clock_local() call the sched_clock() to get timestamp,
in x86, there is a tsc(64bit) based sched_clock(), but in MIPS, the
sched_clock() is jiffies based, which can not give enough
precision(about 10ms with 1000 HZ). so, we need to implement a higher
precision version for MIPS.

in MIPS, there is a tsc like clock counter. based on it, we can
implement a new trace_clock_local(), but because the clock counter is
only 32bit long, we need to handle rollover for it. fortunately, there
a high-level abstraction(cyclecounter) of this action implemented in
include/linux/clocksource.h, which helps to handle the details behind,
we just use it!

Signed-off-by: Wu Zhangjin <[email protected]>
---
arch/mips/kernel/Makefile | 6 +++
arch/mips/kernel/trace_clock.c | 67 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 0 deletions(-)
create mode 100644 arch/mips/kernel/trace_clock.c

diff --git a/arch/mips/kernel/Makefile b/arch/mips/kernel/Makefile
index 8e26e9c..f228868 100644
--- a/arch/mips/kernel/Makefile
+++ b/arch/mips/kernel/Makefile
@@ -8,6 +8,10 @@ obj-y += cpu-probe.o branch.o entry.o genex.o irq.o process.o \
ptrace.o reset.o setup.o signal.o syscall.o \
time.o topology.o traps.o unaligned.o watch.o

+ifdef CONFIG_FUNCTION_TRACER
+CFLAGS_REMOVE_trace_clock.o = -pg
+endif
+
obj-$(CONFIG_CEVT_BCM1480) += cevt-bcm1480.o
obj-$(CONFIG_CEVT_R4K_LIB) += cevt-r4k.o
obj-$(CONFIG_MIPS_MT_SMTC) += cevt-smtc.o
@@ -24,6 +28,8 @@ obj-$(CONFIG_SYNC_R4K) += sync-r4k.o
obj-$(CONFIG_STACKTRACE) += stacktrace.o
obj-$(CONFIG_MODULES) += mips_ksyms.o module.o

+obj-$(CONFIG_FTRACE) += trace_clock.o
+
obj-$(CONFIG_CPU_LOONGSON2) += r4k_fpu.o r4k_switch.o
obj-$(CONFIG_CPU_MIPS32) += r4k_fpu.o r4k_switch.o
obj-$(CONFIG_CPU_MIPS64) += r4k_fpu.o r4k_switch.o
diff --git a/arch/mips/kernel/trace_clock.c b/arch/mips/kernel/trace_clock.c
new file mode 100644
index 0000000..435cd56
--- /dev/null
+++ b/arch/mips/kernel/trace_clock.c
@@ -0,0 +1,67 @@
+/*
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive for
+ * more details.
+ *
+ * Copyright (C) 2009 Lemote Inc. & DSLab, Lanzhou University, China
+ * Author: Wu Zhangjin <[email protected]>
+ */
+
+#include <linux/clocksource.h>
+#include <linux/sched.h>
+
+#include <asm/time.h>
+
+/*
+ * MIPS clock counter based timecounter.
+ */
+
+struct timecounter mips_tc;
+
+/*
+ * trace_clock_local(): the simplest and least coherent tracing clock.
+ *
+ * Useful for tracing that does not cross to other CPUs nor
+ * does it go through idle events.
+ */
+u64 trace_clock_local(void)
+{
+ unsigned long flags;
+ u64 clock;
+
+ raw_local_irq_save(flags);
+
+ preempt_disable_notrace();
+
+ clock = timecounter_read(&mips_tc);
+
+ preempt_enable_no_resched_notrace();
+
+ raw_local_irq_restore(flags);
+
+ return clock;
+}
+
+static cycle_t mips_cc_read(const struct cyclecounter *cc)
+{
+ return read_c0_count();
+}
+
+static struct cyclecounter mips_cc = {
+ .read = mips_cc_read,
+ .mask = CLOCKSOURCE_MASK(32),
+ .shift = 32,
+};
+
+static int __init mips_timecounter_init(void)
+{
+ uint64_t mips_freq = mips_hpt_frequency;
+
+ mips_cc.mult = div_sc((unsigned long)mips_freq, NSEC_PER_SEC, 32);
+
+ timecounter_init(&mips_tc, &mips_cc, sched_clock());
+
+ return 0;
+}
+
+arch_initcall(mips_timecounter_init);
--
1.6.2.1

2009-10-16 11:59:27

by Cong Wang

[permalink] [raw]
Subject: Re: [PATCH 1/2] tracing: convert trace_clock_local() as weak function

On Fri, Oct 16, 2009 at 07:38:24PM +0800, Wu Zhangjin wrote:
>trace_clock_local() is based on the arch-specific sched_clock(), in X86,
>it is tsc(64bit) based, which can give very high precision(about 1ns
>with 1GHz). but in MIPS, the sched_clock() is jiffies based, which can
>give only 10ms precison with 1000 HZ. which is not enough for tracing,
>especially for Real Time system.
>
>so, we need to implement a MIPS specific sched_clock() to get higher
>precision. There is a tsc like clock counter register in MIPS, whose
>frequency is half of the processor, so, if the cpu frequency is 800MHz,
>the time precision reaches 2.5ns, which is very good for tracing, even
>for Real Time system.
>
>but 'Cause it is only 32bit long, which will rollover quickly, so, such
>a sched_clock() will bring with extra load, which is not good for the
>whole system. so, we only need to implement a arch-specific
>trace_clock_local() for tracing. as a preparation, we convert it as a
>weak function.
>
>The MIPS specific trace_clock_local() is coming in the next patch.
>
>Signed-off-by: Wu Zhangjin <[email protected]>
>---
> kernel/trace/trace_clock.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
>diff --git a/kernel/trace/trace_clock.c b/kernel/trace/trace_clock.c
>index 20c5f92..a04dc18 100644
>--- a/kernel/trace/trace_clock.c
>+++ b/kernel/trace/trace_clock.c
>@@ -26,7 +26,7 @@
> * Useful for tracing that does not cross to other CPUs nor
> * does it go through idle events.
> */
>-u64 notrace trace_clock_local(void)
>+u64 __attribute__((weak)) notrace trace_clock_local(void)

We have __weak.


> {
> unsigned long flags;
> u64 clock;
>--
>1.6.2.1
>
>--
>To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
>the body of a message to [email protected]
>More majordomo info at http://vger.kernel.org/majordomo-info.html
>Please read the FAQ at http://www.tux.org/lkml/

--
Live like a child, think like the god.

2009-10-16 12:00:22

by wu zhangjin

[permalink] [raw]
Subject: Re: [PATCH 1/2] tracing: convert trace_clock_local() as weak function

On Fri, 2009-10-16 at 19:52 +0800, Américo Wang wrote:
> On Fri, Oct 16, 2009 at 07:38:24PM +0800, Wu Zhangjin wrote:
> >trace_clock_local() is based on the arch-specific sched_clock(), in X86,
> >it is tsc(64bit) based, which can give very high precision(about 1ns
> >with 1GHz). but in MIPS, the sched_clock() is jiffies based, which can
> >give only 10ms precison with 1000 HZ. which is not enough for tracing,
> >especially for Real Time system.
> >
> >so, we need to implement a MIPS specific sched_clock() to get higher
> >precision. There is a tsc like clock counter register in MIPS, whose
> >frequency is half of the processor, so, if the cpu frequency is 800MHz,
> >the time precision reaches 2.5ns, which is very good for tracing, even
> >for Real Time system.
> >
> >but 'Cause it is only 32bit long, which will rollover quickly, so, such
> >a sched_clock() will bring with extra load, which is not good for the
> >whole system. so, we only need to implement a arch-specific
> >trace_clock_local() for tracing. as a preparation, we convert it as a
> >weak function.
> >
> >The MIPS specific trace_clock_local() is coming in the next patch.
> >
> >Signed-off-by: Wu Zhangjin <[email protected]>
> >---
> > kernel/trace/trace_clock.c | 2 +-
> > 1 files changed, 1 insertions(+), 1 deletions(-)
> >
> >diff --git a/kernel/trace/trace_clock.c b/kernel/trace/trace_clock.c
> >index 20c5f92..a04dc18 100644
> >--- a/kernel/trace/trace_clock.c
> >+++ b/kernel/trace/trace_clock.c
> >@@ -26,7 +26,7 @@
> > * Useful for tracing that does not cross to other CPUs nor
> > * does it go through idle events.
> > */
> >-u64 notrace trace_clock_local(void)
> >+u64 __attribute__((weak)) notrace trace_clock_local(void)
>
> We have __weak.
>

Thanks, will use it in the next version.

Regards,
Wu Zhangjin

2009-10-17 08:24:10

by wu zhangjin

[permalink] [raw]
Subject: Re: [PATCH 2/2] tracing: add high precision version of trace_clock_local() for MIPS

On Fri, 2009-10-16 at 19:38 +0800, Wu Zhangjin wrote:
> By default, trace_clock_local() call the sched_clock() to get timestamp,
> in x86, there is a tsc(64bit) based sched_clock(), but in MIPS, the
> sched_clock() is jiffies based, which can not give enough
> precision(about 10ms with 1000 HZ). so, we need to implement a higher
> precision version for MIPS.
>
> in MIPS, there is a tsc like clock counter. based on it, we can
> implement a new trace_clock_local(), but because the clock counter is
> only 32bit long, we need to handle rollover for it. fortunately, there
> a high-level abstraction(cyclecounter) of this action implemented in
> include/linux/clocksource.h, which helps to handle the details behind,
> we just use it!
>
> Signed-off-by: Wu Zhangjin <[email protected]>
> ---
> arch/mips/kernel/Makefile | 6 +++
> arch/mips/kernel/trace_clock.c | 67 ++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 73 insertions(+), 0 deletions(-)
> create mode 100644 arch/mips/kernel/trace_clock.c
>
> diff --git a/arch/mips/kernel/Makefile b/arch/mips/kernel/Makefile
> index 8e26e9c..f228868 100644
> --- a/arch/mips/kernel/Makefile
> +++ b/arch/mips/kernel/Makefile
> @@ -8,6 +8,10 @@ obj-y += cpu-probe.o branch.o entry.o genex.o irq.o process.o \
> ptrace.o reset.o setup.o signal.o syscall.o \
> time.o topology.o traps.o unaligned.o watch.o
>
> +ifdef CONFIG_FUNCTION_TRACER
> +CFLAGS_REMOVE_trace_clock.o = -pg
> +endif
> +
> obj-$(CONFIG_CEVT_BCM1480) += cevt-bcm1480.o
> obj-$(CONFIG_CEVT_R4K_LIB) += cevt-r4k.o
> obj-$(CONFIG_MIPS_MT_SMTC) += cevt-smtc.o
> @@ -24,6 +28,8 @@ obj-$(CONFIG_SYNC_R4K) += sync-r4k.o
> obj-$(CONFIG_STACKTRACE) += stacktrace.o
> obj-$(CONFIG_MODULES) += mips_ksyms.o module.o
>
> +obj-$(CONFIG_FTRACE) += trace_clock.o
> +
> obj-$(CONFIG_CPU_LOONGSON2) += r4k_fpu.o r4k_switch.o
> obj-$(CONFIG_CPU_MIPS32) += r4k_fpu.o r4k_switch.o
> obj-$(CONFIG_CPU_MIPS64) += r4k_fpu.o r4k_switch.o
> diff --git a/arch/mips/kernel/trace_clock.c b/arch/mips/kernel/trace_clock.c
> new file mode 100644
> index 0000000..435cd56
> --- /dev/null
> +++ b/arch/mips/kernel/trace_clock.c
> @@ -0,0 +1,67 @@
> +/*
> + * This file is subject to the terms and conditions of the GNU General Public
> + * License. See the file "COPYING" in the main directory of this archive for
> + * more details.
> + *
> + * Copyright (C) 2009 Lemote Inc. & DSLab, Lanzhou University, China
> + * Author: Wu Zhangjin <[email protected]>
> + */
> +
> +#include <linux/clocksource.h>
> +#include <linux/sched.h>
> +
> +#include <asm/time.h>
> +
> +/*
> + * MIPS clock counter based timecounter.
> + */
> +
> +struct timecounter mips_tc;
> +
> +/*
> + * trace_clock_local(): the simplest and least coherent tracing clock.
> + *
> + * Useful for tracing that does not cross to other CPUs nor
> + * does it go through idle events.
> + */
> +u64 trace_clock_local(void)
> +{
> + unsigned long flags;
> + u64 clock;
> +
> + raw_local_irq_save(flags);
> +
> + preempt_disable_notrace();
> +
> + clock = timecounter_read(&mips_tc);
> +
> + preempt_enable_no_resched_notrace();
> +
> + raw_local_irq_restore(flags);
> +
> + return clock;
> +}
> +
> +static cycle_t mips_cc_read(const struct cyclecounter *cc)
> +{
> + return read_c0_count();
> +}
> +
> +static struct cyclecounter mips_cc = {
> + .read = mips_cc_read,
> + .mask = CLOCKSOURCE_MASK(32),
> + .shift = 32,
> +};
> +
> +static int __init mips_timecounter_init(void)
> +{
> + uint64_t mips_freq = mips_hpt_frequency;
> +

we must ensure mips_hpt_frequency is initialized before using it.

> + mips_cc.mult = div_sc((unsigned long)mips_freq, NSEC_PER_SEC, 32);
> +
> + timecounter_init(&mips_tc, &mips_cc, sched_clock());
> +
> + return 0;
> +}
> +
> +arch_initcall(mips_timecounter_init);

mips_timecounter_init() must called before ftrace_init(), so can not put
it into *_initcall sections(which will be called in rest_init()).

Regards,
Wu Zhangjin