Skip to content

Commit 17e9fc9

Browse files
Luís Henriquesidryomov
authored andcommitted
ceph: clean-up metrics data structures to reduce code duplication
This patch modifies struct ceph_client_metric so that each metric block (read, write and metadata) becomes an element in a array. This allows to also re-write the helper functions that handle these blocks, making them simpler and, above all, reduce the amount of copy&paste every time a new metric is added. Thus, for each of these metrics there will be a new struct ceph_metric entry that'll will contain all the sizes and latencies fields (and a lock). Note however that the metadata metric doesn't really use the size_fields, and thus this metric won't be shown in the debugfs '../metrics/size' file. Signed-off-by: Luís Henriques <[email protected]> Reviewed-by: Xiubo Li <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]>
1 parent cbed4ff commit 17e9fc9

File tree

3 files changed

+115
-180
lines changed

3 files changed

+115
-180
lines changed

fs/ceph/debugfs.c

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -164,75 +164,64 @@ static int metrics_file_show(struct seq_file *s, void *p)
164164
return 0;
165165
}
166166

167+
static const char * const metric_str[] = {
168+
"read",
169+
"write",
170+
"metadata"
171+
};
167172
static int metrics_latency_show(struct seq_file *s, void *p)
168173
{
169174
struct ceph_fs_client *fsc = s->private;
170-
struct ceph_client_metric *m = &fsc->mdsc->metric;
175+
struct ceph_client_metric *cm = &fsc->mdsc->metric;
176+
struct ceph_metric *m;
171177
s64 total, sum, avg, min, max, sq;
178+
int i;
172179

173180
seq_printf(s, "item total avg_lat(us) min_lat(us) max_lat(us) stdev(us)\n");
174181
seq_printf(s, "-----------------------------------------------------------------------------------\n");
175182

176-
spin_lock(&m->read_metric_lock);
177-
total = m->total_reads;
178-
sum = m->read_latency_sum;
179-
avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
180-
min = m->read_latency_min;
181-
max = m->read_latency_max;
182-
sq = m->read_latency_sq_sum;
183-
spin_unlock(&m->read_metric_lock);
184-
CEPH_LAT_METRIC_SHOW("read", total, avg, min, max, sq);
185-
186-
spin_lock(&m->write_metric_lock);
187-
total = m->total_writes;
188-
sum = m->write_latency_sum;
189-
avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
190-
min = m->write_latency_min;
191-
max = m->write_latency_max;
192-
sq = m->write_latency_sq_sum;
193-
spin_unlock(&m->write_metric_lock);
194-
CEPH_LAT_METRIC_SHOW("write", total, avg, min, max, sq);
195-
196-
spin_lock(&m->metadata_metric_lock);
197-
total = m->total_metadatas;
198-
sum = m->metadata_latency_sum;
199-
avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
200-
min = m->metadata_latency_min;
201-
max = m->metadata_latency_max;
202-
sq = m->metadata_latency_sq_sum;
203-
spin_unlock(&m->metadata_metric_lock);
204-
CEPH_LAT_METRIC_SHOW("metadata", total, avg, min, max, sq);
183+
for (i = 0; i < METRIC_MAX; i++) {
184+
m = &cm->metric[i];
185+
spin_lock(&m->lock);
186+
total = m->total;
187+
sum = m->latency_sum;
188+
avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
189+
min = m->latency_min;
190+
max = m->latency_max;
191+
sq = m->latency_sq_sum;
192+
spin_unlock(&m->lock);
193+
CEPH_LAT_METRIC_SHOW(metric_str[i], total, avg, min, max, sq);
194+
}
205195

206196
return 0;
207197
}
208198

209199
static int metrics_size_show(struct seq_file *s, void *p)
210200
{
211201
struct ceph_fs_client *fsc = s->private;
212-
struct ceph_client_metric *m = &fsc->mdsc->metric;
202+
struct ceph_client_metric *cm = &fsc->mdsc->metric;
203+
struct ceph_metric *m;
213204
s64 total;
214-
u64 sum_sz, avg_sz, min_sz, max_sz;
205+
u64 sum, avg, min, max;
206+
int i;
215207

216208
seq_printf(s, "item total avg_sz(bytes) min_sz(bytes) max_sz(bytes) total_sz(bytes)\n");
217209
seq_printf(s, "----------------------------------------------------------------------------------------\n");
218210

219-
spin_lock(&m->read_metric_lock);
220-
total = m->total_reads;
221-
sum_sz = m->read_size_sum;
222-
avg_sz = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum_sz, total) : 0;
223-
min_sz = m->read_size_min;
224-
max_sz = m->read_size_max;
225-
spin_unlock(&m->read_metric_lock);
226-
CEPH_SZ_METRIC_SHOW("read", total, avg_sz, min_sz, max_sz, sum_sz);
227-
228-
spin_lock(&m->write_metric_lock);
229-
total = m->total_writes;
230-
sum_sz = m->write_size_sum;
231-
avg_sz = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum_sz, total) : 0;
232-
min_sz = m->write_size_min;
233-
max_sz = m->write_size_max;
234-
spin_unlock(&m->write_metric_lock);
235-
CEPH_SZ_METRIC_SHOW("write", total, avg_sz, min_sz, max_sz, sum_sz);
211+
for (i = 0; i < METRIC_MAX; i++) {
212+
/* skip 'metadata' as it doesn't use the size metric */
213+
if (i == METRIC_METADATA)
214+
continue;
215+
m = &cm->metric[i];
216+
spin_lock(&m->lock);
217+
total = m->total;
218+
sum = m->size_sum;
219+
avg = total > 0 ? DIV64_U64_ROUND_CLOSEST(sum, total) : 0;
220+
min = m->size_min;
221+
max = m->size_max;
222+
spin_unlock(&m->lock);
223+
CEPH_SZ_METRIC_SHOW(metric_str[i], total, avg, min, max, sum);
224+
}
236225

237226
return 0;
238227
}

fs/ceph/metric.c

Lines changed: 32 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
6262
read->header.ver = 1;
6363
read->header.compat = 1;
6464
read->header.data_len = cpu_to_le32(sizeof(*read) - header_len);
65-
sum = m->read_latency_sum;
65+
sum = m->metric[METRIC_READ].latency_sum;
6666
jiffies_to_timespec64(sum, &ts);
6767
read->sec = cpu_to_le32(ts.tv_sec);
6868
read->nsec = cpu_to_le32(ts.tv_nsec);
@@ -74,7 +74,7 @@ static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
7474
write->header.ver = 1;
7575
write->header.compat = 1;
7676
write->header.data_len = cpu_to_le32(sizeof(*write) - header_len);
77-
sum = m->write_latency_sum;
77+
sum = m->metric[METRIC_WRITE].latency_sum;
7878
jiffies_to_timespec64(sum, &ts);
7979
write->sec = cpu_to_le32(ts.tv_sec);
8080
write->nsec = cpu_to_le32(ts.tv_nsec);
@@ -86,7 +86,7 @@ static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
8686
meta->header.ver = 1;
8787
meta->header.compat = 1;
8888
meta->header.data_len = cpu_to_le32(sizeof(*meta) - header_len);
89-
sum = m->metadata_latency_sum;
89+
sum = m->metric[METRIC_METADATA].latency_sum;
9090
jiffies_to_timespec64(sum, &ts);
9191
meta->sec = cpu_to_le32(ts.tv_sec);
9292
meta->nsec = cpu_to_le32(ts.tv_nsec);
@@ -141,8 +141,8 @@ static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
141141
rsize->header.ver = 1;
142142
rsize->header.compat = 1;
143143
rsize->header.data_len = cpu_to_le32(sizeof(*rsize) - header_len);
144-
rsize->total_ops = cpu_to_le64(m->total_reads);
145-
rsize->total_size = cpu_to_le64(m->read_size_sum);
144+
rsize->total_ops = cpu_to_le64(m->metric[METRIC_READ].total);
145+
rsize->total_size = cpu_to_le64(m->metric[METRIC_READ].size_sum);
146146
items++;
147147

148148
/* encode the write io size metric */
@@ -151,8 +151,8 @@ static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
151151
wsize->header.ver = 1;
152152
wsize->header.compat = 1;
153153
wsize->header.data_len = cpu_to_le32(sizeof(*wsize) - header_len);
154-
wsize->total_ops = cpu_to_le64(m->total_writes);
155-
wsize->total_size = cpu_to_le64(m->write_size_sum);
154+
wsize->total_ops = cpu_to_le64(m->metric[METRIC_WRITE].total);
155+
wsize->total_size = cpu_to_le64(m->metric[METRIC_WRITE].size_sum);
156156
items++;
157157

158158
put_unaligned_le32(items, &head->num);
@@ -220,7 +220,8 @@ static void metric_delayed_work(struct work_struct *work)
220220

221221
int ceph_metric_init(struct ceph_client_metric *m)
222222
{
223-
int ret;
223+
struct ceph_metric *metric;
224+
int ret, i;
224225

225226
if (!m)
226227
return -EINVAL;
@@ -243,32 +244,18 @@ int ceph_metric_init(struct ceph_client_metric *m)
243244
if (ret)
244245
goto err_i_caps_mis;
245246

246-
spin_lock_init(&m->read_metric_lock);
247-
m->read_latency_sq_sum = 0;
248-
m->read_latency_min = KTIME_MAX;
249-
m->read_latency_max = 0;
250-
m->total_reads = 0;
251-
m->read_latency_sum = 0;
252-
m->read_size_min = U64_MAX;
253-
m->read_size_max = 0;
254-
m->read_size_sum = 0;
255-
256-
spin_lock_init(&m->write_metric_lock);
257-
m->write_latency_sq_sum = 0;
258-
m->write_latency_min = KTIME_MAX;
259-
m->write_latency_max = 0;
260-
m->total_writes = 0;
261-
m->write_latency_sum = 0;
262-
m->write_size_min = U64_MAX;
263-
m->write_size_max = 0;
264-
m->write_size_sum = 0;
265-
266-
spin_lock_init(&m->metadata_metric_lock);
267-
m->metadata_latency_sq_sum = 0;
268-
m->metadata_latency_min = KTIME_MAX;
269-
m->metadata_latency_max = 0;
270-
m->total_metadatas = 0;
271-
m->metadata_latency_sum = 0;
247+
for (i = 0; i < METRIC_MAX; i++) {
248+
metric = &m->metric[i];
249+
spin_lock_init(&metric->lock);
250+
metric->size_sum = 0;
251+
metric->size_min = U64_MAX;
252+
metric->size_max = 0;
253+
metric->total = 0;
254+
metric->latency_sum = 0;
255+
metric->latency_sq_sum = 0;
256+
metric->latency_min = KTIME_MAX;
257+
metric->latency_max = 0;
258+
}
272259

273260
atomic64_set(&m->opened_files, 0);
274261
ret = percpu_counter_init(&m->opened_inodes, 0, GFP_KERNEL);
@@ -338,73 +325,22 @@ static inline void __update_stdev(ktime_t total, ktime_t lsum,
338325
*sq_sump += sq;
339326
}
340327

341-
void ceph_update_read_metrics(struct ceph_client_metric *m,
342-
ktime_t r_start, ktime_t r_end,
343-
unsigned int size, int rc)
328+
void ceph_update_metrics(struct ceph_metric *m,
329+
ktime_t r_start, ktime_t r_end,
330+
unsigned int size, int rc)
344331
{
345332
ktime_t lat = ktime_sub(r_end, r_start);
346333
ktime_t total;
347334

348335
if (unlikely(rc < 0 && rc != -ENOENT && rc != -ETIMEDOUT))
349336
return;
350337

351-
spin_lock(&m->read_metric_lock);
352-
total = ++m->total_reads;
353-
m->read_size_sum += size;
354-
m->read_latency_sum += lat;
355-
METRIC_UPDATE_MIN_MAX(m->read_size_min,
356-
m->read_size_max,
357-
size);
358-
METRIC_UPDATE_MIN_MAX(m->read_latency_min,
359-
m->read_latency_max,
360-
lat);
361-
__update_stdev(total, m->read_latency_sum,
362-
&m->read_latency_sq_sum, lat);
363-
spin_unlock(&m->read_metric_lock);
364-
}
365-
366-
void ceph_update_write_metrics(struct ceph_client_metric *m,
367-
ktime_t r_start, ktime_t r_end,
368-
unsigned int size, int rc)
369-
{
370-
ktime_t lat = ktime_sub(r_end, r_start);
371-
ktime_t total;
372-
373-
if (unlikely(rc && rc != -ETIMEDOUT))
374-
return;
375-
376-
spin_lock(&m->write_metric_lock);
377-
total = ++m->total_writes;
378-
m->write_size_sum += size;
379-
m->write_latency_sum += lat;
380-
METRIC_UPDATE_MIN_MAX(m->write_size_min,
381-
m->write_size_max,
382-
size);
383-
METRIC_UPDATE_MIN_MAX(m->write_latency_min,
384-
m->write_latency_max,
385-
lat);
386-
__update_stdev(total, m->write_latency_sum,
387-
&m->write_latency_sq_sum, lat);
388-
spin_unlock(&m->write_metric_lock);
389-
}
390-
391-
void ceph_update_metadata_metrics(struct ceph_client_metric *m,
392-
ktime_t r_start, ktime_t r_end,
393-
int rc)
394-
{
395-
ktime_t lat = ktime_sub(r_end, r_start);
396-
ktime_t total;
397-
398-
if (unlikely(rc && rc != -ENOENT))
399-
return;
400-
401-
spin_lock(&m->metadata_metric_lock);
402-
total = ++m->total_metadatas;
403-
m->metadata_latency_sum += lat;
404-
METRIC_UPDATE_MIN_MAX(m->metadata_latency_min,
405-
m->metadata_latency_max,
406-
lat);
407-
__update_stdev(total, m->metadata_latency_sum,
408-
&m->metadata_latency_sq_sum, lat);
409-
spin_unlock(&m->metadata_metric_lock);
338+
spin_lock(&m->lock);
339+
total = ++m->total;
340+
m->size_sum += size;
341+
METRIC_UPDATE_MIN_MAX(m->size_min, m->size_max, size);
342+
m->latency_sum += lat;
343+
METRIC_UPDATE_MIN_MAX(m->latency_min, m->latency_max, lat);
344+
__update_stdev(total, m->latency_sum, &m->latency_sq_sum, lat);
345+
spin_unlock(&m->lock);
410346
}

0 commit comments

Comments
 (0)