gator: ARM DS-5.6 Streamline gator driver
[android-sdk/arm-ds5-gator.git] / gator_events_sched.c
1 /**
2  * Copyright (C) ARM Limited 2010-2011. 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  */
10 #include "gator.h"
11 #include <trace/events/sched.h>
13 #define SCHED_SWITCH    0
14 #define SCHED_TOTAL             (SCHED_SWITCH+1)
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);
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;
29         // disable interrupts to synchronize with gator_events_sched_read()
30         // spinlocks not needed since percpu buffers are used
31         local_irq_save(flags);
32         per_cpu(schedCnt, smp_processor_id())[SCHED_SWITCH]++;
33         local_irq_restore(flags);
34 }
36 static int gator_events_sched_create_files(struct super_block *sb, struct dentry *root)
37 {
38         struct dentry *dir;
40         /* switch */
41         dir = gatorfs_mkdir(sb, root, "Linux_sched_switch");
42         if (!dir) {
43                 return -1;
44         }
45         gatorfs_create_ulong(sb, dir, "enabled", &sched_switch_enabled);
46         gatorfs_create_ro_ulong(sb, dir, "key", &sched_switch_key);
48         return 0;
49 }
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");
59         return 0;
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");
65         return -1;
66 }
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");
74         sched_switch_enabled = 0;
75 }
77 static int gator_events_sched_read(int **buffer)
78 {
79         unsigned long flags;
80         int len, value;
81         int cpu = smp_processor_id();
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         }
93         if (buffer)
94                 *buffer = per_cpu(schedGet, cpu);
96         return len;
97 }
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 };
106 int gator_events_sched_init(void)
108         sched_switch_enabled = 0;
110         sched_switch_key = gator_events_get_key();
112         return gator_events_install(&gator_events_sched_interface);
114 gator_events_init(gator_events_sched_init);