Merge branch 'linux-3.10.y' of git://git.kernel.org/pub/scm/linux/kernel/git/stable...
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_events_sched.c
1 /**
2  * Copyright (C) ARM Limited 2010-2015. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  */
9
10 #include "gator.h"
11 #include <trace/events/sched.h>
12
13 #define SCHED_SWITCH    0
14 #define SCHED_TOTAL             (SCHED_SWITCH+1)
15
16 static ulong sched_switch_enabled;
17 static ulong sched_switch_key;
18 static DEFINE_PER_CPU(int[SCHED_TOTAL], schedCnt);
19 static DEFINE_PER_CPU(int[SCHED_TOTAL * 2], schedGet);
20
21 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
22 GATOR_DEFINE_PROBE(sched_switch, TP_PROTO(struct rq *rq, struct task_struct *prev, struct task_struct *next))
23 #else
24 GATOR_DEFINE_PROBE(sched_switch, TP_PROTO(struct task_struct *prev, struct task_struct *next))
25 #endif
26 {
27         unsigned long flags;
28
29         /* disable interrupts to synchronize with gator_events_sched_read()
30          * spinlocks not needed since percpu buffers are used
31          */
32         local_irq_save(flags);
33         per_cpu(schedCnt, get_physical_cpu())[SCHED_SWITCH]++;
34         local_irq_restore(flags);
35 }
36
37 static int gator_events_sched_create_files(struct super_block *sb, struct dentry *root)
38 {
39         struct dentry *dir;
40
41         /* switch */
42         dir = gatorfs_mkdir(sb, root, "Linux_sched_switch");
43         if (!dir)
44                 return -1;
45         gatorfs_create_ulong(sb, dir, "enabled", &sched_switch_enabled);
46         gatorfs_create_ro_ulong(sb, dir, "key", &sched_switch_key);
47
48         return 0;
49 }
50
51 static int gator_events_sched_start(void)
52 {
53         /* register tracepoints */
54         if (sched_switch_enabled)
55                 if (GATOR_REGISTER_TRACE(sched_switch))
56                         goto sched_switch_exit;
57         pr_debug("gator: registered scheduler event tracepoints\n");
58
59         return 0;
60
61         /* unregister tracepoints on error */
62 sched_switch_exit:
63         pr_err("gator: scheduler event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
64
65         return -1;
66 }
67
68 static void gator_events_sched_stop(void)
69 {
70         if (sched_switch_enabled)
71                 GATOR_UNREGISTER_TRACE(sched_switch);
72         pr_debug("gator: unregistered scheduler event tracepoints\n");
73
74         sched_switch_enabled = 0;
75 }
76
77 static int gator_events_sched_read(int **buffer, bool sched_switch)
78 {
79         unsigned long flags;
80         int len, value;
81         int cpu = get_physical_cpu();
82
83         len = 0;
84         if (sched_switch_enabled) {
85                 local_irq_save(flags);
86                 value = per_cpu(schedCnt, cpu)[SCHED_SWITCH];
87                 per_cpu(schedCnt, cpu)[SCHED_SWITCH] = 0;
88                 local_irq_restore(flags);
89                 per_cpu(schedGet, cpu)[len++] = sched_switch_key;
90                 per_cpu(schedGet, cpu)[len++] = value;
91         }
92
93         if (buffer)
94                 *buffer = per_cpu(schedGet, cpu);
95
96         return len;
97 }
98
99 static struct gator_interface gator_events_sched_interface = {
100         .create_files = gator_events_sched_create_files,
101         .start = gator_events_sched_start,
102         .stop = gator_events_sched_stop,
103         .read = gator_events_sched_read,
104 };
105
106 int gator_events_sched_init(void)
107 {
108         sched_switch_enabled = 0;
109
110         sched_switch_key = gator_events_get_key();
111
112         return gator_events_install(&gator_events_sched_interface);
113 }