-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency_stats.c
193 lines (175 loc) · 4.79 KB
/
latency_stats.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* latency_stats.c
*
* informations for IO latency and size
*
* Copyright (C) 2013, Coly Li <[email protected]>
* Robin Dong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License, version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
*/
#include <asm-generic/div64.h>
#include <linux/slab.h>
#include <linux/clocksource.h>
#include <linux/percpu.h>
#include "latency_stats.h"
static struct kmem_cache *latency_stats_cache;
static unsigned long long us2msecs(unsigned long long usec)
{
usec += 500;
do_div(usec, 1000);
return usec;
}
static unsigned long long us2secs(unsigned long long usec)
{
usec += 500;
do_div(usec, 1000);
usec += 500;
do_div(usec, 1000);
return usec;
}
/*
static unsigned long long ms2secs(unsigned long long msec)
{
msec += 500;
do_div(msec, 1000);
return msec;
}*/
int init_latency_stats(void)
{
latency_stats_cache = kmem_cache_create("io-latency-stats",
sizeof(struct latency_stats), 0, 0, NULL);
if (!latency_stats_cache)
return -ENOMEM;
return 0;
}
void exit_latency_stats(void)
{
if (latency_stats_cache) {
kmem_cache_destroy(latency_stats_cache);
latency_stats_cache = NULL;
}
}
void reset_latency_stats(struct latency_stats __percpu *lstats)
{
int r, cpu;
struct latency_stats *pstats;
for_each_possible_cpu(cpu) {
pstats = per_cpu_ptr(lstats, cpu);
/* reset latency stats buckets */
for (r = 0; r < IO_LATENCY_STATS_S_NR; r++) {
pstats->latency_stats_s[r] = 0;
pstats->latency_read_stats_s[r] = 0;
pstats->latency_write_stats_s[r] = 0;
pstats->soft_latency_stats_s[r] = 0;
pstats->soft_latency_read_stats_s[r] = 0;
pstats->soft_latency_write_stats_s[r] = 0;
}
for (r = 0; r < IO_LATENCY_STATS_MS_NR; r++) {
pstats->latency_stats_ms[r] = 0;
pstats->latency_read_stats_ms[r] = 0;
pstats->latency_write_stats_ms[r] = 0;
pstats->soft_latency_stats_ms[r] = 0;
pstats->soft_latency_read_stats_ms[r] = 0;
pstats->soft_latency_write_stats_ms[r] = 0;
}
for (r = 0; r < IO_LATENCY_STATS_US_NR; r++) {
pstats->latency_stats_us[r] = 0;
pstats->latency_read_stats_us[r] = 0;
pstats->latency_write_stats_us[r] = 0;
pstats->soft_latency_stats_us[r] = 0;
pstats->soft_latency_read_stats_us[r] = 0;
pstats->soft_latency_write_stats_us[r] = 0;
}
for (r = 0; r < IO_SIZE_STATS_NR; r++) {
pstats->io_size_stats[r] = 0;
pstats->io_read_size_stats[r] = 0;
pstats->io_write_size_stats[r] = 0;
}
}
}
struct latency_stats __percpu *create_latency_stats(void)
{
return alloc_percpu(struct latency_stats);
}
void destroy_latency_stats(struct latency_stats __percpu *lstats)
{
if (lstats)
free_percpu(lstats);
}
#define INC_LATENCY(lstats, idx, soft, rw, grain) \
do { \
\
if (soft) { \
lstats->soft_latency_stats_##grain[idx]++; \
if (rw) \
lstats->soft_latency_write_stats_##grain[idx]++; \
else \
lstats->soft_latency_read_stats_##grain[idx]++; \
} else { \
lstats->latency_stats_##grain[idx]++; \
if (rw) \
lstats->latency_write_stats_##grain[idx]++; \
else \
lstats->latency_read_stats_##grain[idx]++; \
} \
\
} while (0)
void update_latency_stats(struct latency_stats *lstats, unsigned long stime,
unsigned long now, int soft, int rw)
{
unsigned long latency;
int idx;
/*
* if now <= io->start_time_usec, it means counter
* in ktime_get() over flows, just ignore this I/O
*/
if (unlikely(now <= stime))
return;
latency = now - stime;
#ifndef USE_US
latency *= 1000;
#endif
if (latency < 1000) {
/* microseconds */
idx = latency/IO_LATENCY_STATS_US_GRAINSIZE;
if (idx > (IO_LATENCY_STATS_US_NR - 1))
idx = IO_LATENCY_STATS_US_NR - 1;
INC_LATENCY(lstats, idx, soft, rw, us);
} else if (latency < 1000000) {
/* milliseconds */
idx = us2msecs(latency)/IO_LATENCY_STATS_MS_GRAINSIZE;
if (idx > (IO_LATENCY_STATS_MS_NR - 1))
idx = IO_LATENCY_STATS_MS_NR - 1;
INC_LATENCY(lstats, idx, soft, rw, ms);
} else {
/* seconds */
idx = us2secs(latency)/IO_LATENCY_STATS_S_GRAINSIZE;
if (idx > (IO_LATENCY_STATS_S_NR - 1))
idx = IO_LATENCY_STATS_S_NR - 1;
INC_LATENCY(lstats, idx, soft, rw, s);
}
}
void update_io_size_stats(struct latency_stats *lstats, unsigned long size,
int rw)
{
int idx;
if (size < IO_SIZE_MAX) {
idx = size/IO_SIZE_STATS_GRAINSIZE;
if (idx > (IO_SIZE_STATS_NR - 1))
idx = IO_SIZE_STATS_NR - 1;
lstats->io_size_stats[idx]++;
if (rw)
lstats->io_write_size_stats[idx]++;
else
lstats->io_read_size_stats[idx]++;
}
}