blob: c47347d2e84e3227d1f2f5383c14a2e0126bc648 [file] [log] [blame]
Xiubo Lif9009ef2020-03-19 23:44:59 -04001/* SPDX-License-Identifier: GPL-2.0 */
Xiubo Li18f473b2020-07-16 10:05:57 -04002#include <linux/ceph/ceph_debug.h>
Xiubo Lif9009ef2020-03-19 23:44:59 -04003
4#include <linux/types.h>
5#include <linux/percpu_counter.h>
Xiubo Li97e27aa2020-03-19 23:45:01 -04006#include <linux/math64.h>
Xiubo Lif9009ef2020-03-19 23:44:59 -04007
8#include "metric.h"
Xiubo Li18f473b2020-07-16 10:05:57 -04009#include "mds_client.h"
10
Venky Shankar8d728c72022-03-08 07:42:16 -050011static void ktime_to_ceph_timespec(struct ceph_timespec *ts, ktime_t val)
12{
13 struct timespec64 t = ktime_to_timespec64(val);
14 ceph_encode_timespec64(ts, &t);
15}
16
Xiubo Li18f473b2020-07-16 10:05:57 -040017static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
18 struct ceph_mds_session *s)
19{
20 struct ceph_metric_head *head;
21 struct ceph_metric_cap *cap;
22 struct ceph_metric_read_latency *read;
23 struct ceph_metric_write_latency *write;
24 struct ceph_metric_metadata_latency *meta;
Xiubo Li8ba3b8c2020-11-05 23:30:21 -050025 struct ceph_metric_dlease *dlease;
Xiubo Li3d8b6982021-03-24 13:08:25 +080026 struct ceph_opened_files *files;
27 struct ceph_pinned_icaps *icaps;
28 struct ceph_opened_inodes *inodes;
Xiubo Li903f4fec2021-05-13 09:40:53 +080029 struct ceph_read_io_size *rsize;
30 struct ceph_write_io_size *wsize;
Xiubo Li18f473b2020-07-16 10:05:57 -040031 struct ceph_client_metric *m = &mdsc->metric;
32 u64 nr_caps = atomic64_read(&m->total_caps);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080033 u32 header_len = sizeof(struct ceph_metric_header);
Xiubo Li18f473b2020-07-16 10:05:57 -040034 struct ceph_msg *msg;
Xiubo Li18f473b2020-07-16 10:05:57 -040035 s64 sum;
36 s32 items = 0;
37 s32 len;
38
39 len = sizeof(*head) + sizeof(*cap) + sizeof(*read) + sizeof(*write)
Xiubo Li3d8b6982021-03-24 13:08:25 +080040 + sizeof(*meta) + sizeof(*dlease) + sizeof(*files)
Xiubo Li903f4fec2021-05-13 09:40:53 +080041 + sizeof(*icaps) + sizeof(*inodes) + sizeof(*rsize)
42 + sizeof(*wsize);
Xiubo Li18f473b2020-07-16 10:05:57 -040043
44 msg = ceph_msg_new(CEPH_MSG_CLIENT_METRICS, len, GFP_NOFS, true);
45 if (!msg) {
46 pr_err("send metrics to mds%d, failed to allocate message\n",
47 s->s_mds);
48 return false;
49 }
50
51 head = msg->front.iov_base;
52
53 /* encode the cap metric */
54 cap = (struct ceph_metric_cap *)(head + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080055 cap->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_CAP_INFO);
56 cap->header.ver = 1;
57 cap->header.compat = 1;
58 cap->header.data_len = cpu_to_le32(sizeof(*cap) - header_len);
Xiubo Li8ba3b8c2020-11-05 23:30:21 -050059 cap->hit = cpu_to_le64(percpu_counter_sum(&m->i_caps_hit));
60 cap->mis = cpu_to_le64(percpu_counter_sum(&m->i_caps_mis));
Xiubo Li18f473b2020-07-16 10:05:57 -040061 cap->total = cpu_to_le64(nr_caps);
62 items++;
63
64 /* encode the read latency metric */
65 read = (struct ceph_metric_read_latency *)(cap + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080066 read->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_READ_LATENCY);
Venky Shankar54d7b822022-03-08 07:42:18 -050067 read->header.ver = 2;
Xiubo Li8ecd34c2021-05-13 09:40:52 +080068 read->header.compat = 1;
69 read->header.data_len = cpu_to_le32(sizeof(*read) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010070 sum = m->metric[METRIC_READ].latency_sum;
Venky Shankar8d728c72022-03-08 07:42:16 -050071 ktime_to_ceph_timespec(&read->lat, sum);
Venky Shankar54d7b822022-03-08 07:42:18 -050072 ktime_to_ceph_timespec(&read->avg, m->metric[METRIC_READ].latency_avg);
73 read->sq_sum = cpu_to_le64(m->metric[METRIC_READ].latency_sq_sum);
74 read->count = cpu_to_le64(m->metric[METRIC_READ].total);
Xiubo Li18f473b2020-07-16 10:05:57 -040075 items++;
76
77 /* encode the write latency metric */
78 write = (struct ceph_metric_write_latency *)(read + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080079 write->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_WRITE_LATENCY);
Venky Shankar54d7b822022-03-08 07:42:18 -050080 write->header.ver = 2;
Xiubo Li8ecd34c2021-05-13 09:40:52 +080081 write->header.compat = 1;
82 write->header.data_len = cpu_to_le32(sizeof(*write) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010083 sum = m->metric[METRIC_WRITE].latency_sum;
Venky Shankar8d728c72022-03-08 07:42:16 -050084 ktime_to_ceph_timespec(&write->lat, sum);
Venky Shankar54d7b822022-03-08 07:42:18 -050085 ktime_to_ceph_timespec(&write->avg, m->metric[METRIC_WRITE].latency_avg);
86 write->sq_sum = cpu_to_le64(m->metric[METRIC_WRITE].latency_sq_sum);
87 write->count = cpu_to_le64(m->metric[METRIC_WRITE].total);
Xiubo Li18f473b2020-07-16 10:05:57 -040088 items++;
89
90 /* encode the metadata latency metric */
91 meta = (struct ceph_metric_metadata_latency *)(write + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080092 meta->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_METADATA_LATENCY);
Venky Shankar54d7b822022-03-08 07:42:18 -050093 meta->header.ver = 2;
Xiubo Li8ecd34c2021-05-13 09:40:52 +080094 meta->header.compat = 1;
95 meta->header.data_len = cpu_to_le32(sizeof(*meta) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010096 sum = m->metric[METRIC_METADATA].latency_sum;
Venky Shankar8d728c72022-03-08 07:42:16 -050097 ktime_to_ceph_timespec(&meta->lat, sum);
Venky Shankar54d7b822022-03-08 07:42:18 -050098 ktime_to_ceph_timespec(&meta->avg, m->metric[METRIC_METADATA].latency_avg);
99 meta->sq_sum = cpu_to_le64(m->metric[METRIC_METADATA].latency_sq_sum);
100 meta->count = cpu_to_le64(m->metric[METRIC_METADATA].total);
Xiubo Li18f473b2020-07-16 10:05:57 -0400101 items++;
102
Xiubo Li8ba3b8c2020-11-05 23:30:21 -0500103 /* encode the dentry lease metric */
104 dlease = (struct ceph_metric_dlease *)(meta + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800105 dlease->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_DENTRY_LEASE);
106 dlease->header.ver = 1;
107 dlease->header.compat = 1;
108 dlease->header.data_len = cpu_to_le32(sizeof(*dlease) - header_len);
Xiubo Li8ba3b8c2020-11-05 23:30:21 -0500109 dlease->hit = cpu_to_le64(percpu_counter_sum(&m->d_lease_hit));
110 dlease->mis = cpu_to_le64(percpu_counter_sum(&m->d_lease_mis));
111 dlease->total = cpu_to_le64(atomic64_read(&m->total_dentries));
112 items++;
113
Xiubo Li3d8b6982021-03-24 13:08:25 +0800114 sum = percpu_counter_sum(&m->total_inodes);
115
116 /* encode the opened files metric */
117 files = (struct ceph_opened_files *)(dlease + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800118 files->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_OPENED_FILES);
119 files->header.ver = 1;
120 files->header.compat = 1;
121 files->header.data_len = cpu_to_le32(sizeof(*files) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800122 files->opened_files = cpu_to_le64(atomic64_read(&m->opened_files));
123 files->total = cpu_to_le64(sum);
124 items++;
125
126 /* encode the pinned icaps metric */
127 icaps = (struct ceph_pinned_icaps *)(files + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800128 icaps->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_PINNED_ICAPS);
129 icaps->header.ver = 1;
130 icaps->header.compat = 1;
131 icaps->header.data_len = cpu_to_le32(sizeof(*icaps) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800132 icaps->pinned_icaps = cpu_to_le64(nr_caps);
133 icaps->total = cpu_to_le64(sum);
134 items++;
135
136 /* encode the opened inodes metric */
137 inodes = (struct ceph_opened_inodes *)(icaps + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800138 inodes->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_OPENED_INODES);
139 inodes->header.ver = 1;
140 inodes->header.compat = 1;
141 inodes->header.data_len = cpu_to_le32(sizeof(*inodes) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800142 inodes->opened_inodes = cpu_to_le64(percpu_counter_sum(&m->opened_inodes));
143 inodes->total = cpu_to_le64(sum);
144 items++;
145
Xiubo Li903f4fec2021-05-13 09:40:53 +0800146 /* encode the read io size metric */
147 rsize = (struct ceph_read_io_size *)(inodes + 1);
148 rsize->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_READ_IO_SIZES);
149 rsize->header.ver = 1;
150 rsize->header.compat = 1;
151 rsize->header.data_len = cpu_to_le32(sizeof(*rsize) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +0100152 rsize->total_ops = cpu_to_le64(m->metric[METRIC_READ].total);
153 rsize->total_size = cpu_to_le64(m->metric[METRIC_READ].size_sum);
Xiubo Li903f4fec2021-05-13 09:40:53 +0800154 items++;
155
156 /* encode the write io size metric */
157 wsize = (struct ceph_write_io_size *)(rsize + 1);
158 wsize->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_WRITE_IO_SIZES);
159 wsize->header.ver = 1;
160 wsize->header.compat = 1;
161 wsize->header.data_len = cpu_to_le32(sizeof(*wsize) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +0100162 wsize->total_ops = cpu_to_le64(m->metric[METRIC_WRITE].total);
163 wsize->total_size = cpu_to_le64(m->metric[METRIC_WRITE].size_sum);
Xiubo Li903f4fec2021-05-13 09:40:53 +0800164 items++;
165
Xiubo Li18f473b2020-07-16 10:05:57 -0400166 put_unaligned_le32(items, &head->num);
167 msg->front.iov_len = len;
168 msg->hdr.version = cpu_to_le16(1);
169 msg->hdr.compat_version = cpu_to_le16(1);
170 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Xiubo Li18f473b2020-07-16 10:05:57 -0400171 ceph_con_send(&s->s_con, msg);
172
173 return true;
174}
175
176
177static void metric_get_session(struct ceph_mds_client *mdsc)
178{
179 struct ceph_mds_session *s;
180 int i;
181
182 mutex_lock(&mdsc->mutex);
183 for (i = 0; i < mdsc->max_sessions; i++) {
184 s = __ceph_lookup_mds_session(mdsc, i);
185 if (!s)
186 continue;
187
188 /*
189 * Skip it if MDS doesn't support the metric collection,
190 * or the MDS will close the session's socket connection
191 * directly when it get this message.
192 */
193 if (check_session_state(s) &&
194 test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &s->s_features)) {
195 mdsc->metric.session = s;
196 break;
197 }
198
199 ceph_put_mds_session(s);
200 }
201 mutex_unlock(&mdsc->mutex);
202}
203
204static void metric_delayed_work(struct work_struct *work)
205{
206 struct ceph_client_metric *m =
207 container_of(work, struct ceph_client_metric, delayed_work.work);
208 struct ceph_mds_client *mdsc =
209 container_of(m, struct ceph_mds_client, metric);
210
211 if (mdsc->stopping)
212 return;
213
214 if (!m->session || !check_session_state(m->session)) {
215 if (m->session) {
216 ceph_put_mds_session(m->session);
217 m->session = NULL;
218 }
219 metric_get_session(mdsc);
220 }
221 if (m->session) {
222 ceph_mdsc_send_metrics(mdsc, m->session);
223 metric_schedule_delayed(m);
224 }
225}
Xiubo Lif9009ef2020-03-19 23:44:59 -0400226
227int ceph_metric_init(struct ceph_client_metric *m)
228{
Luís Henriques17e9fc92021-10-29 15:09:28 +0100229 struct ceph_metric *metric;
230 int ret, i;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400231
232 if (!m)
233 return -EINVAL;
234
235 atomic64_set(&m->total_dentries, 0);
236 ret = percpu_counter_init(&m->d_lease_hit, 0, GFP_KERNEL);
237 if (ret)
238 return ret;
Xiubo Li1af16d52020-03-19 23:45:00 -0400239
Xiubo Lif9009ef2020-03-19 23:44:59 -0400240 ret = percpu_counter_init(&m->d_lease_mis, 0, GFP_KERNEL);
Xiubo Li1af16d52020-03-19 23:45:00 -0400241 if (ret)
242 goto err_d_lease_mis;
243
Xiubo Li4f1d7562020-06-30 03:52:16 -0400244 atomic64_set(&m->total_caps, 0);
Xiubo Li1af16d52020-03-19 23:45:00 -0400245 ret = percpu_counter_init(&m->i_caps_hit, 0, GFP_KERNEL);
246 if (ret)
247 goto err_i_caps_hit;
248
249 ret = percpu_counter_init(&m->i_caps_mis, 0, GFP_KERNEL);
250 if (ret)
251 goto err_i_caps_mis;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400252
Luís Henriques17e9fc92021-10-29 15:09:28 +0100253 for (i = 0; i < METRIC_MAX; i++) {
254 metric = &m->metric[i];
255 spin_lock_init(&metric->lock);
256 metric->size_sum = 0;
257 metric->size_min = U64_MAX;
258 metric->size_max = 0;
259 metric->total = 0;
260 metric->latency_sum = 0;
Venky Shankar367290e2022-03-08 07:42:17 -0500261 metric->latency_avg = 0;
Luís Henriques17e9fc92021-10-29 15:09:28 +0100262 metric->latency_sq_sum = 0;
263 metric->latency_min = KTIME_MAX;
264 metric->latency_max = 0;
265 }
Xiubo Li70c94822020-03-19 23:45:02 -0400266
Xiubo Li1dd8d472020-09-03 09:01:40 -0400267 atomic64_set(&m->opened_files, 0);
268 ret = percpu_counter_init(&m->opened_inodes, 0, GFP_KERNEL);
269 if (ret)
270 goto err_opened_inodes;
271 ret = percpu_counter_init(&m->total_inodes, 0, GFP_KERNEL);
272 if (ret)
273 goto err_total_inodes;
274
Xiubo Li18f473b2020-07-16 10:05:57 -0400275 m->session = NULL;
276 INIT_DELAYED_WORK(&m->delayed_work, metric_delayed_work);
277
Xiubo Lif9009ef2020-03-19 23:44:59 -0400278 return 0;
Xiubo Li1af16d52020-03-19 23:45:00 -0400279
Xiubo Li1dd8d472020-09-03 09:01:40 -0400280err_total_inodes:
281 percpu_counter_destroy(&m->opened_inodes);
282err_opened_inodes:
283 percpu_counter_destroy(&m->i_caps_mis);
Xiubo Li1af16d52020-03-19 23:45:00 -0400284err_i_caps_mis:
285 percpu_counter_destroy(&m->i_caps_hit);
286err_i_caps_hit:
287 percpu_counter_destroy(&m->d_lease_mis);
288err_d_lease_mis:
289 percpu_counter_destroy(&m->d_lease_hit);
290
291 return ret;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400292}
293
294void ceph_metric_destroy(struct ceph_client_metric *m)
295{
296 if (!m)
297 return;
298
Jeff Laytonb4002172021-07-27 15:47:12 -0400299 cancel_delayed_work_sync(&m->delayed_work);
300
Xiubo Li1dd8d472020-09-03 09:01:40 -0400301 percpu_counter_destroy(&m->total_inodes);
302 percpu_counter_destroy(&m->opened_inodes);
Xiubo Li1af16d52020-03-19 23:45:00 -0400303 percpu_counter_destroy(&m->i_caps_mis);
304 percpu_counter_destroy(&m->i_caps_hit);
Xiubo Lif9009ef2020-03-19 23:44:59 -0400305 percpu_counter_destroy(&m->d_lease_mis);
306 percpu_counter_destroy(&m->d_lease_hit);
Xiubo Li18f473b2020-07-16 10:05:57 -0400307
Jeff Layton7e656242021-06-09 14:09:52 -0400308 ceph_put_mds_session(m->session);
Xiubo Lif9009ef2020-03-19 23:44:59 -0400309}
Xiubo Li97e27aa2020-03-19 23:45:01 -0400310
Xiubo Lifc123d52021-04-28 14:08:39 +0800311#define METRIC_UPDATE_MIN_MAX(min, max, new) \
312{ \
313 if (unlikely(new < min)) \
314 min = new; \
315 if (unlikely(new > max)) \
316 max = new; \
317}
318
Venky Shankar367290e2022-03-08 07:42:17 -0500319static inline void __update_mean_and_stdev(ktime_t total, ktime_t *lavg,
320 ktime_t *sq_sump, ktime_t lat)
Xiubo Li97e27aa2020-03-19 23:45:01 -0400321{
Venky Shankar367290e2022-03-08 07:42:17 -0500322 ktime_t avg;
Xiubo Li97e27aa2020-03-19 23:45:01 -0400323
Venky Shankar367290e2022-03-08 07:42:17 -0500324 if (unlikely(total == 1)) {
325 *lavg = lat;
326 } else {
327 /* the sq is (lat - old_avg) * (lat - new_avg) */
328 avg = *lavg + div64_s64(lat - *lavg, total);
329 *sq_sump += (lat - *lavg)*(lat - avg);
330 *lavg = avg;
331 }
Xiubo Li97e27aa2020-03-19 23:45:01 -0400332}
333
Luís Henriques17e9fc92021-10-29 15:09:28 +0100334void ceph_update_metrics(struct ceph_metric *m,
335 ktime_t r_start, ktime_t r_end,
336 unsigned int size, int rc)
Xiubo Li97e27aa2020-03-19 23:45:01 -0400337{
338 ktime_t lat = ktime_sub(r_end, r_start);
Xiubo Lifc123d52021-04-28 14:08:39 +0800339 ktime_t total;
Xiubo Li97e27aa2020-03-19 23:45:01 -0400340
341 if (unlikely(rc < 0 && rc != -ENOENT && rc != -ETIMEDOUT))
342 return;
343
Luís Henriques17e9fc92021-10-29 15:09:28 +0100344 spin_lock(&m->lock);
345 total = ++m->total;
346 m->size_sum += size;
347 METRIC_UPDATE_MIN_MAX(m->size_min, m->size_max, size);
348 m->latency_sum += lat;
349 METRIC_UPDATE_MIN_MAX(m->latency_min, m->latency_max, lat);
Venky Shankar367290e2022-03-08 07:42:17 -0500350 __update_mean_and_stdev(total, &m->latency_avg, &m->latency_sq_sum,
351 lat);
Luís Henriques17e9fc92021-10-29 15:09:28 +0100352 spin_unlock(&m->lock);
Xiubo Li70c94822020-03-19 23:45:02 -0400353}