blob: 463d834960736642faca325023bf4669e034b7ba [file] [log] [blame]
Jon Medhurstaaf37a32013-06-11 12:10:56 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurstaaf37a32013-06-11 12:10:56 +01003 *
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
16static ulong sched_switch_enabled;
17static ulong sched_switch_key;
18static DEFINE_PER_CPU(int[SCHED_TOTAL], schedCnt);
19static DEFINE_PER_CPU(int[SCHED_TOTAL * 2], schedGet);
20
21#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
22GATOR_DEFINE_PROBE(sched_switch, TP_PROTO(struct rq *rq, struct task_struct *prev, struct task_struct *next))
23#else
24GATOR_DEFINE_PROBE(sched_switch, TP_PROTO(struct task_struct *prev, struct task_struct *next))
25#endif
26{
27 unsigned long flags;
28
Jon Medhurst96b56152014-10-30 18:01:15 +000029 /* disable interrupts to synchronize with gator_events_sched_read()
30 * spinlocks not needed since percpu buffers are used
31 */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010032 local_irq_save(flags);
33 per_cpu(schedCnt, get_physical_cpu())[SCHED_SWITCH]++;
34 local_irq_restore(flags);
35}
36
37static 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");
Jon Medhurst96b56152014-10-30 18:01:15 +000043 if (!dir)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010044 return -1;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010045 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
51static int gator_events_sched_start(void)
52{
Jon Medhurst96b56152014-10-30 18:01:15 +000053 /* register tracepoints */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010054 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
Jon Medhurst96b56152014-10-30 18:01:15 +000061 /* unregister tracepoints on error */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010062sched_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
68static 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
Jon Medhurst96b56152014-10-30 18:01:15 +000077static int gator_events_sched_read(int **buffer, bool sched_switch)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010078{
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
99static 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
106int 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}