gator: ARM DS-5.3 Streamline gator driver
[android-sdk/arm-ds5-gator.git] / gator_events_block.c
1 /**
2  * Copyright (C) ARM Limited 2010. 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 <linux/version.h>
11 #include <linux/blkdev.h>
12 #include <trace/events/block.h>
13 #include <linux/slab.h>
14 #include <linux/fs.h>
16 #include "gator.h"
17 #include "gator_trace.h"
19 #define BLOCK_RQ_WR             0
20 #define BLOCK_RQ_RD             1
22 #define BLOCK_TOTAL             (BLOCK_RQ_RD+1)
24 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
25 #define EVENTWRITE REQ_RW
26 #else
27 #define EVENTWRITE REQ_WRITE
28 #endif
30 static ulong block_rq_wr_enabled;
31 static ulong block_rq_rd_enabled;
32 static ulong block_rq_wr_key;
33 static ulong block_rq_rd_key;
34 static DEFINE_PER_CPU(int[BLOCK_TOTAL], blockCnt);
35 static DEFINE_PER_CPU(int[BLOCK_TOTAL * 2], blockGet);
36 static DEFINE_PER_CPU(bool, new_data_avail);
38 GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq))
39 {
40         unsigned long flags;
41         int write, size;
42         int cpu = raw_smp_processor_id();
44         if (!rq)
45                 return;
47         write = rq->cmd_flags & EVENTWRITE;
48         size = rq->resid_len;
50         if (!size)
51                 return;
53         // disable interrupts to synchronize with gator_events_block_read()
54         // spinlocks not needed since percpu buffers are used
55         local_irq_save(flags);
56         if (write)
57                 per_cpu(blockCnt, cpu)[BLOCK_RQ_WR] += size;
58         else
59                 per_cpu(blockCnt, cpu)[BLOCK_RQ_RD] += size;
60         local_irq_restore(flags);
62         per_cpu(new_data_avail, cpu) = true;
63 }
65 static int gator_events_block_create_files(struct super_block *sb, struct dentry *root)
66 {
67         struct dentry *dir;
69         /* block_complete_wr */
70         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_wr");
71         if (!dir) {
72                 return -1;
73         }
74         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_wr_enabled);
75         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_wr_key);
77         /* block_complete_rd */
78         dir = gatorfs_mkdir(sb, root, "Linux_block_rq_rd");
79         if (!dir) {
80                 return -1;
81         }
82         gatorfs_create_ulong(sb, dir, "enabled", &block_rq_rd_enabled);
83         gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_rd_key);
85         return 0;
86 }
88 static int gator_events_block_init(int *key)
89 {
90         block_rq_wr_enabled = 0;
91         block_rq_rd_enabled = 0;
93         block_rq_wr_key = *key;
94         *key = *key + 1;
95         block_rq_rd_key = *key;
96         *key = *key + 1;
98         return 0;
99 }
101 static int gator_events_block_start(void)
103         int cpu;
105         for_each_possible_cpu(cpu) 
106                 per_cpu(new_data_avail, cpu) = true;
108         // register tracepoints
109         if (block_rq_wr_enabled || block_rq_rd_enabled)
110                 if (GATOR_REGISTER_TRACE(block_rq_complete))
111                         goto fail_block_rq_exit;
112         pr_debug("gator: registered block event tracepoints\n");
114         return 0;
116         // unregister tracepoints on error
117 fail_block_rq_exit:
118         pr_err("gator: block event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
120         return -1;
123 static void gator_events_block_stop(void)
125         if (block_rq_wr_enabled || block_rq_rd_enabled)
126                 GATOR_UNREGISTER_TRACE(block_rq_complete);
127         pr_debug("gator: unregistered block event tracepoints\n");
129         block_rq_wr_enabled = 0;
130         block_rq_rd_enabled = 0;
133 static int gator_events_block_read(int **buffer)
135         unsigned long flags;
136         int len, value, cpu, data = 0;
137         cpu = raw_smp_processor_id();
139         if (per_cpu(new_data_avail, cpu) == false)
140                 return 0;
142         per_cpu(new_data_avail, cpu) = false;
144         len = 0;
145         if (block_rq_wr_enabled) {
146                 local_irq_save(flags);
147                 value = per_cpu(blockCnt, cpu)[BLOCK_RQ_WR];
148                 per_cpu(blockCnt, cpu)[BLOCK_RQ_WR] = 0;
149                 local_irq_restore(flags);
150                 per_cpu(blockGet, cpu)[len++] = block_rq_wr_key;
151                 per_cpu(blockGet, cpu)[len++] = value;
152                 data += value;
153         }
154         if (block_rq_rd_enabled) {
155                 local_irq_save(flags);
156                 value = per_cpu(blockCnt, cpu)[BLOCK_RQ_RD];
157                 per_cpu(blockCnt, cpu)[BLOCK_RQ_RD] = 0;
158                 local_irq_restore(flags);
159                 per_cpu(blockGet, cpu)[len++] = block_rq_rd_key;
160                 per_cpu(blockGet, cpu)[len++] = value;
161                 data += value;
162         }
164         if (data != 0)
165                 per_cpu(new_data_avail, cpu) = true;
167         if (buffer)
168                 *buffer = per_cpu(blockGet, cpu);
170         return len;
173 int gator_events_block_install(gator_interface *gi) {
174         gi->create_files = gator_events_block_create_files;
175         gi->init = gator_events_block_init;
176         gi->start = gator_events_block_start;
177         gi->stop = gator_events_block_stop;
178         gi->read = gator_events_block_read;
179         return 0;