root/kernel/trace/trace_clock.c
// SPDX-License-Identifier: GPL-2.0
/*
 * tracing clocks
 *
 *  Copyright (C) 2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 *
 * Implements 3 trace clock variants, with differing scalability/precision
 * tradeoffs:
 *
 *  -   local: CPU-local trace clock
 *  -  medium: scalable global clock with some jitter
 *  -  global: globally monotonic, serialized clock
 *
 * Tracer plugins will chose a default from these clocks.
 */
#include <linux/spinlock.h>
#include <linux/irqflags.h>
#include <linux/hardirq.h>
#include <linux/module.h>
#include <linux/percpu.h>
#include <linux/sched.h>
#include <linux/sched/clock.h>
#include <linux/ktime.h>
#include <linux/trace_clock.h>

/*
 * 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 notrace trace_clock_local(void)
{
        u64 clock;

        /*
         * sched_clock() is an architecture implemented, fast, scalable,
         * lockless clock. It is not guaranteed to be coherent across
         * CPUs, nor across CPU idle events.
         */
        preempt_disable_notrace();
        clock = sched_clock();
        preempt_enable_notrace();

        return clock;
}
EXPORT_SYMBOL_GPL(trace_clock_local);

/*
 * trace_clock(): 'between' trace clock. Not completely serialized,
 * but not completely incorrect when crossing CPUs either.
 *
 * This is based on cpu_clock(), which will allow at most ~1 jiffy of
 * jitter between CPUs. So it's a pretty scalable clock, but there
 * can be offsets in the trace data.
 */
u64 notrace trace_clock(void)
{
        return local_clock();
}
EXPORT_SYMBOL_GPL(trace_clock);

/*
 * trace_jiffy_clock(): Simply use jiffies as a clock counter.
 * Note that this use of jiffies_64 is not completely safe on
 * 32-bit systems. But the window is tiny, and the effect if
 * we are affected is that we will have an obviously bogus
 * timestamp on a trace event - i.e. not life threatening.
 */
u64 notrace trace_clock_jiffies(void)
{
        return jiffies_64_to_clock_t(jiffies_64 - INITIAL_JIFFIES);
}
EXPORT_SYMBOL_GPL(trace_clock_jiffies);

/*
 * trace_clock_global(): special globally coherent trace clock
 *
 * It has higher overhead than the other trace clocks but is still
 * an order of magnitude faster than GTOD derived hardware clocks.
 *
 * Used by plugins that need globally coherent timestamps.
 */

/* keep prev_time and lock in the same cacheline. */
static struct {
        u64 prev_time;
        arch_spinlock_t lock;
} trace_clock_struct ____cacheline_aligned_in_smp =
        {
                .lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED,
        };

u64 notrace trace_clock_global(void)
{
        unsigned long flags;
        int this_cpu;
        u64 now, prev_time;

        raw_local_irq_save(flags);

        this_cpu = raw_smp_processor_id();

        /*
         * The global clock "guarantees" that the events are ordered
         * between CPUs. But if two events on two different CPUS call
         * trace_clock_global at roughly the same time, it really does
         * not matter which one gets the earlier time. Just make sure
         * that the same CPU will always show a monotonic clock.
         *
         * Use a read memory barrier to get the latest written
         * time that was recorded.
         */
        smp_rmb();
        prev_time = READ_ONCE(trace_clock_struct.prev_time);
        now = sched_clock_cpu(this_cpu);

        /* Make sure that now is always greater than or equal to prev_time */
        if ((s64)(now - prev_time) < 0)
                now = prev_time;

        /*
         * If in an NMI context then dont risk lockups and simply return
         * the current time.
         */
        if (unlikely(in_nmi()))
                goto out;

        /* Tracing can cause strange recursion, always use a try lock */
        if (arch_spin_trylock(&trace_clock_struct.lock)) {
                /* Reread prev_time in case it was already updated */
                prev_time = READ_ONCE(trace_clock_struct.prev_time);
                if ((s64)(now - prev_time) < 0)
                        now = prev_time;

                trace_clock_struct.prev_time = now;

                /* The unlock acts as the wmb for the above rmb */
                arch_spin_unlock(&trace_clock_struct.lock);
        }
 out:
        raw_local_irq_restore(flags);

        return now;
}
EXPORT_SYMBOL_GPL(trace_clock_global);

static atomic64_t trace_counter;

/*
 * trace_clock_counter(): simply an atomic counter.
 * Use the trace_counter "counter" for cases where you do not care
 * about timings, but are interested in strict ordering.
 */
u64 notrace trace_clock_counter(void)
{
        return atomic64_inc_return(&trace_counter);
}