ronnie
2022-10-23 e4b2278a4015864069fc7b2eb574f5aeb7736bc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * Copyright (C) ARM Limited 2011-2016. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
 
static void (*callback)(void);
static DEFINE_PER_CPU(struct hrtimer, percpu_hrtimer);
static DEFINE_PER_CPU(ktime_t, hrtimer_expire);
static DEFINE_PER_CPU(int, hrtimer_is_active);
static ktime_t profiling_interval;
static void gator_hrtimer_online(void);
static void gator_hrtimer_offline(void);
 
static enum hrtimer_restart gator_hrtimer_notify(struct hrtimer *hrtimer)
{
    int cpu = get_logical_cpu();
 
    hrtimer_forward(hrtimer, per_cpu(hrtimer_expire, cpu), profiling_interval);
    per_cpu(hrtimer_expire, cpu) = ktime_add(per_cpu(hrtimer_expire, cpu), profiling_interval);
    (*callback)();
    return HRTIMER_RESTART;
}
 
static void gator_hrtimer_online(void)
{
    int cpu = get_logical_cpu();
    struct hrtimer *hrtimer = &per_cpu(percpu_hrtimer, cpu);
 
    if (per_cpu(hrtimer_is_active, cpu) || (ktime_to_ns(profiling_interval) == 0))
        return;
 
    per_cpu(hrtimer_is_active, cpu) = 1;
    hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
    hrtimer->function = gator_hrtimer_notify;
#ifdef CONFIG_PREEMPT_RT_BASE
    hrtimer->irqsafe = 1;
#endif
    per_cpu(hrtimer_expire, cpu) = ktime_add(hrtimer->base->get_time(), profiling_interval);
    hrtimer_start(hrtimer, per_cpu(hrtimer_expire, cpu), HRTIMER_MODE_ABS_PINNED);
}
 
static void gator_hrtimer_offline(void)
{
    int cpu = get_logical_cpu();
    struct hrtimer *hrtimer = &per_cpu(percpu_hrtimer, cpu);
 
    if (!per_cpu(hrtimer_is_active, cpu))
        return;
 
    per_cpu(hrtimer_is_active, cpu) = 0;
    hrtimer_cancel(hrtimer);
}
 
static int gator_hrtimer_init(int interval, void (*func)(void))
{
    int cpu;
 
    (callback) = (func);
 
    for_each_present_cpu(cpu) {
        per_cpu(hrtimer_is_active, cpu) = 0;
    }
 
    /* calculate profiling interval */
    if (interval > 0)
        profiling_interval = ns_to_ktime(1000000000UL / interval);
    else
        profiling_interval = ns_to_ktime(0);
 
    return 0;
}
 
static void gator_hrtimer_shutdown(void)
{
    /* empty */
}