blob: b3467b1337120ec6612aa73c5615c42aab27ff0d [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/block.h>
12
13#define BLOCK_RQ_WR 0
14#define BLOCK_RQ_RD 1
15
16#define BLOCK_TOTAL (BLOCK_RQ_RD+1)
17
18#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
19#define EVENTWRITE REQ_RW
20#else
21#define EVENTWRITE REQ_WRITE
22#endif
23
24static ulong block_rq_wr_enabled;
25static ulong block_rq_rd_enabled;
26static ulong block_rq_wr_key;
27static ulong block_rq_rd_key;
28static atomic_t blockCnt[BLOCK_TOTAL];
29static int blockGet[BLOCK_TOTAL * 4];
30
Jon Medhurst96b56152014-10-30 18:01:15 +000031/* Tracepoint changed in 3.15 backported to older kernels. The Makefile tries to autodetect the correct value, but if it fails change the #if below */
Jon Medhurste31266f2014-08-04 15:47:44 +010032#if OLD_BLOCK_RQ_COMPLETE
Jon Medhurstaaf37a32013-06-11 12:10:56 +010033GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq))
Jon Medhurste31266f2014-08-04 15:47:44 +010034#else
35GATOR_DEFINE_PROBE(block_rq_complete, TP_PROTO(struct request_queue *q, struct request *rq, unsigned int nr_bytes))
36#endif
Jon Medhurstaaf37a32013-06-11 12:10:56 +010037{
Jon Medhurste31266f2014-08-04 15:47:44 +010038 int write;
39 unsigned int size;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010040
41 if (!rq)
42 return;
43
44 write = rq->cmd_flags & EVENTWRITE;
Jon Medhurste31266f2014-08-04 15:47:44 +010045#if OLD_BLOCK_RQ_COMPLETE
Jon Medhurstaaf37a32013-06-11 12:10:56 +010046 size = rq->resid_len;
Jon Medhurste31266f2014-08-04 15:47:44 +010047#else
48 size = nr_bytes;
49#endif
Jon Medhurstaaf37a32013-06-11 12:10:56 +010050
51 if (!size)
52 return;
53
54 if (write) {
Jon Medhurst96b56152014-10-30 18:01:15 +000055 if (block_rq_wr_enabled)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010056 atomic_add(size, &blockCnt[BLOCK_RQ_WR]);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010057 } else {
Jon Medhurst96b56152014-10-30 18:01:15 +000058 if (block_rq_rd_enabled)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010059 atomic_add(size, &blockCnt[BLOCK_RQ_RD]);
Jon Medhurstaaf37a32013-06-11 12:10:56 +010060 }
61}
62
63static int gator_events_block_create_files(struct super_block *sb, struct dentry *root)
64{
65 struct dentry *dir;
66
67 /* block_complete_wr */
68 dir = gatorfs_mkdir(sb, root, "Linux_block_rq_wr");
Jon Medhurst96b56152014-10-30 18:01:15 +000069 if (!dir)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010070 return -1;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010071 gatorfs_create_ulong(sb, dir, "enabled", &block_rq_wr_enabled);
72 gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_wr_key);
73
74 /* block_complete_rd */
75 dir = gatorfs_mkdir(sb, root, "Linux_block_rq_rd");
Jon Medhurst96b56152014-10-30 18:01:15 +000076 if (!dir)
Jon Medhurstaaf37a32013-06-11 12:10:56 +010077 return -1;
Jon Medhurstaaf37a32013-06-11 12:10:56 +010078 gatorfs_create_ulong(sb, dir, "enabled", &block_rq_rd_enabled);
79 gatorfs_create_ro_ulong(sb, dir, "key", &block_rq_rd_key);
80
81 return 0;
82}
83
84static int gator_events_block_start(void)
85{
Jon Medhurst96b56152014-10-30 18:01:15 +000086 /* register tracepoints */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010087 if (block_rq_wr_enabled || block_rq_rd_enabled)
88 if (GATOR_REGISTER_TRACE(block_rq_complete))
89 goto fail_block_rq_exit;
90 pr_debug("gator: registered block event tracepoints\n");
91
92 return 0;
93
Jon Medhurst96b56152014-10-30 18:01:15 +000094 /* unregister tracepoints on error */
Jon Medhurstaaf37a32013-06-11 12:10:56 +010095fail_block_rq_exit:
96 pr_err("gator: block event tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
97
98 return -1;
99}
100
101static void gator_events_block_stop(void)
102{
103 if (block_rq_wr_enabled || block_rq_rd_enabled)
104 GATOR_UNREGISTER_TRACE(block_rq_complete);
105 pr_debug("gator: unregistered block event tracepoints\n");
106
107 block_rq_wr_enabled = 0;
108 block_rq_rd_enabled = 0;
109}
110
Jon Medhurst96b56152014-10-30 18:01:15 +0000111static int gator_events_block_read(int **buffer, bool sched_switch)
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100112{
113 int len, value, data = 0;
114
Jon Medhurst96b56152014-10-30 18:01:15 +0000115 if (!on_primary_core())
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100116 return 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100117
118 len = 0;
119 if (block_rq_wr_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_WR])) > 0) {
120 atomic_sub(value, &blockCnt[BLOCK_RQ_WR]);
121 blockGet[len++] = block_rq_wr_key;
Jon Medhurst96b56152014-10-30 18:01:15 +0000122 /* Indicates to Streamline that value bytes were written now, not since the last message */
123 blockGet[len++] = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100124 blockGet[len++] = block_rq_wr_key;
125 blockGet[len++] = value;
126 data += value;
127 }
128 if (block_rq_rd_enabled && (value = atomic_read(&blockCnt[BLOCK_RQ_RD])) > 0) {
129 atomic_sub(value, &blockCnt[BLOCK_RQ_RD]);
130 blockGet[len++] = block_rq_rd_key;
Jon Medhurst96b56152014-10-30 18:01:15 +0000131 /* Indicates to Streamline that value bytes were read now, not since the last message */
132 blockGet[len++] = 0;
Jon Medhurstaaf37a32013-06-11 12:10:56 +0100133 blockGet[len++] = block_rq_rd_key;
134 blockGet[len++] = value;
135 data += value;
136 }
137
138 if (buffer)
139 *buffer = blockGet;
140
141 return len;
142}
143
144static struct gator_interface gator_events_block_interface = {
145 .create_files = gator_events_block_create_files,
146 .start = gator_events_block_start,
147 .stop = gator_events_block_stop,
148 .read = gator_events_block_read,
149};
150
151int gator_events_block_init(void)
152{
153 block_rq_wr_enabled = 0;
154 block_rq_rd_enabled = 0;
155
156 block_rq_wr_key = gator_events_get_key();
157 block_rq_rd_key = gator_events_get_key();
158
159 return gator_events_install(&gator_events_block_interface);
160}