blob: 0fcba68f9a9903164a6a501d6a768c2183870429 [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
11static bool ceph_mdsc_send_metrics(struct ceph_mds_client *mdsc,
12 struct ceph_mds_session *s)
13{
14 struct ceph_metric_head *head;
15 struct ceph_metric_cap *cap;
16 struct ceph_metric_read_latency *read;
17 struct ceph_metric_write_latency *write;
18 struct ceph_metric_metadata_latency *meta;
Xiubo Li8ba3b8c2020-11-05 23:30:21 -050019 struct ceph_metric_dlease *dlease;
Xiubo Li3d8b6982021-03-24 13:08:25 +080020 struct ceph_opened_files *files;
21 struct ceph_pinned_icaps *icaps;
22 struct ceph_opened_inodes *inodes;
Xiubo Li903f4fec2021-05-13 09:40:53 +080023 struct ceph_read_io_size *rsize;
24 struct ceph_write_io_size *wsize;
Xiubo Li18f473b2020-07-16 10:05:57 -040025 struct ceph_client_metric *m = &mdsc->metric;
26 u64 nr_caps = atomic64_read(&m->total_caps);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080027 u32 header_len = sizeof(struct ceph_metric_header);
Xiubo Li18f473b2020-07-16 10:05:57 -040028 struct ceph_msg *msg;
29 struct timespec64 ts;
30 s64 sum;
31 s32 items = 0;
32 s32 len;
33
34 len = sizeof(*head) + sizeof(*cap) + sizeof(*read) + sizeof(*write)
Xiubo Li3d8b6982021-03-24 13:08:25 +080035 + sizeof(*meta) + sizeof(*dlease) + sizeof(*files)
Xiubo Li903f4fec2021-05-13 09:40:53 +080036 + sizeof(*icaps) + sizeof(*inodes) + sizeof(*rsize)
37 + sizeof(*wsize);
Xiubo Li18f473b2020-07-16 10:05:57 -040038
39 msg = ceph_msg_new(CEPH_MSG_CLIENT_METRICS, len, GFP_NOFS, true);
40 if (!msg) {
41 pr_err("send metrics to mds%d, failed to allocate message\n",
42 s->s_mds);
43 return false;
44 }
45
46 head = msg->front.iov_base;
47
48 /* encode the cap metric */
49 cap = (struct ceph_metric_cap *)(head + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080050 cap->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_CAP_INFO);
51 cap->header.ver = 1;
52 cap->header.compat = 1;
53 cap->header.data_len = cpu_to_le32(sizeof(*cap) - header_len);
Xiubo Li8ba3b8c2020-11-05 23:30:21 -050054 cap->hit = cpu_to_le64(percpu_counter_sum(&m->i_caps_hit));
55 cap->mis = cpu_to_le64(percpu_counter_sum(&m->i_caps_mis));
Xiubo Li18f473b2020-07-16 10:05:57 -040056 cap->total = cpu_to_le64(nr_caps);
57 items++;
58
59 /* encode the read latency metric */
60 read = (struct ceph_metric_read_latency *)(cap + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080061 read->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_READ_LATENCY);
62 read->header.ver = 1;
63 read->header.compat = 1;
64 read->header.data_len = cpu_to_le32(sizeof(*read) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010065 sum = m->metric[METRIC_READ].latency_sum;
Xiubo Li18f473b2020-07-16 10:05:57 -040066 jiffies_to_timespec64(sum, &ts);
67 read->sec = cpu_to_le32(ts.tv_sec);
68 read->nsec = cpu_to_le32(ts.tv_nsec);
69 items++;
70
71 /* encode the write latency metric */
72 write = (struct ceph_metric_write_latency *)(read + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080073 write->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_WRITE_LATENCY);
74 write->header.ver = 1;
75 write->header.compat = 1;
76 write->header.data_len = cpu_to_le32(sizeof(*write) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010077 sum = m->metric[METRIC_WRITE].latency_sum;
Xiubo Li18f473b2020-07-16 10:05:57 -040078 jiffies_to_timespec64(sum, &ts);
79 write->sec = cpu_to_le32(ts.tv_sec);
80 write->nsec = cpu_to_le32(ts.tv_nsec);
81 items++;
82
83 /* encode the metadata latency metric */
84 meta = (struct ceph_metric_metadata_latency *)(write + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080085 meta->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_METADATA_LATENCY);
86 meta->header.ver = 1;
87 meta->header.compat = 1;
88 meta->header.data_len = cpu_to_le32(sizeof(*meta) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +010089 sum = m->metric[METRIC_METADATA].latency_sum;
Xiubo Li18f473b2020-07-16 10:05:57 -040090 jiffies_to_timespec64(sum, &ts);
91 meta->sec = cpu_to_le32(ts.tv_sec);
92 meta->nsec = cpu_to_le32(ts.tv_nsec);
93 items++;
94
Xiubo Li8ba3b8c2020-11-05 23:30:21 -050095 /* encode the dentry lease metric */
96 dlease = (struct ceph_metric_dlease *)(meta + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +080097 dlease->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_DENTRY_LEASE);
98 dlease->header.ver = 1;
99 dlease->header.compat = 1;
100 dlease->header.data_len = cpu_to_le32(sizeof(*dlease) - header_len);
Xiubo Li8ba3b8c2020-11-05 23:30:21 -0500101 dlease->hit = cpu_to_le64(percpu_counter_sum(&m->d_lease_hit));
102 dlease->mis = cpu_to_le64(percpu_counter_sum(&m->d_lease_mis));
103 dlease->total = cpu_to_le64(atomic64_read(&m->total_dentries));
104 items++;
105
Xiubo Li3d8b6982021-03-24 13:08:25 +0800106 sum = percpu_counter_sum(&m->total_inodes);
107
108 /* encode the opened files metric */
109 files = (struct ceph_opened_files *)(dlease + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800110 files->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_OPENED_FILES);
111 files->header.ver = 1;
112 files->header.compat = 1;
113 files->header.data_len = cpu_to_le32(sizeof(*files) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800114 files->opened_files = cpu_to_le64(atomic64_read(&m->opened_files));
115 files->total = cpu_to_le64(sum);
116 items++;
117
118 /* encode the pinned icaps metric */
119 icaps = (struct ceph_pinned_icaps *)(files + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800120 icaps->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_PINNED_ICAPS);
121 icaps->header.ver = 1;
122 icaps->header.compat = 1;
123 icaps->header.data_len = cpu_to_le32(sizeof(*icaps) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800124 icaps->pinned_icaps = cpu_to_le64(nr_caps);
125 icaps->total = cpu_to_le64(sum);
126 items++;
127
128 /* encode the opened inodes metric */
129 inodes = (struct ceph_opened_inodes *)(icaps + 1);
Xiubo Li8ecd34c2021-05-13 09:40:52 +0800130 inodes->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_OPENED_INODES);
131 inodes->header.ver = 1;
132 inodes->header.compat = 1;
133 inodes->header.data_len = cpu_to_le32(sizeof(*inodes) - header_len);
Xiubo Li3d8b6982021-03-24 13:08:25 +0800134 inodes->opened_inodes = cpu_to_le64(percpu_counter_sum(&m->opened_inodes));
135 inodes->total = cpu_to_le64(sum);
136 items++;
137
Xiubo Li903f4fec2021-05-13 09:40:53 +0800138 /* encode the read io size metric */
139 rsize = (struct ceph_read_io_size *)(inodes + 1);
140 rsize->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_READ_IO_SIZES);
141 rsize->header.ver = 1;
142 rsize->header.compat = 1;
143 rsize->header.data_len = cpu_to_le32(sizeof(*rsize) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +0100144 rsize->total_ops = cpu_to_le64(m->metric[METRIC_READ].total);
145 rsize->total_size = cpu_to_le64(m->metric[METRIC_READ].size_sum);
Xiubo Li903f4fec2021-05-13 09:40:53 +0800146 items++;
147
148 /* encode the write io size metric */
149 wsize = (struct ceph_write_io_size *)(rsize + 1);
150 wsize->header.type = cpu_to_le32(CLIENT_METRIC_TYPE_WRITE_IO_SIZES);
151 wsize->header.ver = 1;
152 wsize->header.compat = 1;
153 wsize->header.data_len = cpu_to_le32(sizeof(*wsize) - header_len);
Luís Henriques17e9fc92021-10-29 15:09:28 +0100154 wsize->total_ops = cpu_to_le64(m->metric[METRIC_WRITE].total);
155 wsize->total_size = cpu_to_le64(m->metric[METRIC_WRITE].size_sum);
Xiubo Li903f4fec2021-05-13 09:40:53 +0800156 items++;
157
Xiubo Li18f473b2020-07-16 10:05:57 -0400158 put_unaligned_le32(items, &head->num);
159 msg->front.iov_len = len;
160 msg->hdr.version = cpu_to_le16(1);
161 msg->hdr.compat_version = cpu_to_le16(1);
162 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
Xiubo Li18f473b2020-07-16 10:05:57 -0400163 ceph_con_send(&s->s_con, msg);
164
165 return true;
166}
167
168
169static void metric_get_session(struct ceph_mds_client *mdsc)
170{
171 struct ceph_mds_session *s;
172 int i;
173
174 mutex_lock(&mdsc->mutex);
175 for (i = 0; i < mdsc->max_sessions; i++) {
176 s = __ceph_lookup_mds_session(mdsc, i);
177 if (!s)
178 continue;
179
180 /*
181 * Skip it if MDS doesn't support the metric collection,
182 * or the MDS will close the session's socket connection
183 * directly when it get this message.
184 */
185 if (check_session_state(s) &&
186 test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &s->s_features)) {
187 mdsc->metric.session = s;
188 break;
189 }
190
191 ceph_put_mds_session(s);
192 }
193 mutex_unlock(&mdsc->mutex);
194}
195
196static void metric_delayed_work(struct work_struct *work)
197{
198 struct ceph_client_metric *m =
199 container_of(work, struct ceph_client_metric, delayed_work.work);
200 struct ceph_mds_client *mdsc =
201 container_of(m, struct ceph_mds_client, metric);
202
203 if (mdsc->stopping)
204 return;
205
206 if (!m->session || !check_session_state(m->session)) {
207 if (m->session) {
208 ceph_put_mds_session(m->session);
209 m->session = NULL;
210 }
211 metric_get_session(mdsc);
212 }
213 if (m->session) {
214 ceph_mdsc_send_metrics(mdsc, m->session);
215 metric_schedule_delayed(m);
216 }
217}
Xiubo Lif9009ef2020-03-19 23:44:59 -0400218
219int ceph_metric_init(struct ceph_client_metric *m)
220{
Luís Henriques17e9fc92021-10-29 15:09:28 +0100221 struct ceph_metric *metric;
222 int ret, i;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400223
224 if (!m)
225 return -EINVAL;
226
227 atomic64_set(&m->total_dentries, 0);
228 ret = percpu_counter_init(&m->d_lease_hit, 0, GFP_KERNEL);
229 if (ret)
230 return ret;
Xiubo Li1af16d52020-03-19 23:45:00 -0400231
Xiubo Lif9009ef2020-03-19 23:44:59 -0400232 ret = percpu_counter_init(&m->d_lease_mis, 0, GFP_KERNEL);
Xiubo Li1af16d52020-03-19 23:45:00 -0400233 if (ret)
234 goto err_d_lease_mis;
235
Xiubo Li4f1d7562020-06-30 03:52:16 -0400236 atomic64_set(&m->total_caps, 0);
Xiubo Li1af16d52020-03-19 23:45:00 -0400237 ret = percpu_counter_init(&m->i_caps_hit, 0, GFP_KERNEL);
238 if (ret)
239 goto err_i_caps_hit;
240
241 ret = percpu_counter_init(&m->i_caps_mis, 0, GFP_KERNEL);
242 if (ret)
243 goto err_i_caps_mis;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400244
Luís Henriques17e9fc92021-10-29 15:09:28 +0100245 for (i = 0; i < METRIC_MAX; i++) {
246 metric = &m->metric[i];
247 spin_lock_init(&metric->lock);
248 metric->size_sum = 0;
249 metric->size_min = U64_MAX;
250 metric->size_max = 0;
251 metric->total = 0;
252 metric->latency_sum = 0;
253 metric->latency_sq_sum = 0;
254 metric->latency_min = KTIME_MAX;
255 metric->latency_max = 0;
256 }
Xiubo Li70c94822020-03-19 23:45:02 -0400257
Xiubo Li1dd8d472020-09-03 09:01:40 -0400258 atomic64_set(&m->opened_files, 0);
259 ret = percpu_counter_init(&m->opened_inodes, 0, GFP_KERNEL);
260 if (ret)
261 goto err_opened_inodes;
262 ret = percpu_counter_init(&m->total_inodes, 0, GFP_KERNEL);
263 if (ret)
264 goto err_total_inodes;
265
Xiubo Li18f473b2020-07-16 10:05:57 -0400266 m->session = NULL;
267 INIT_DELAYED_WORK(&m->delayed_work, metric_delayed_work);
268
Xiubo Lif9009ef2020-03-19 23:44:59 -0400269 return 0;
Xiubo Li1af16d52020-03-19 23:45:00 -0400270
Xiubo Li1dd8d472020-09-03 09:01:40 -0400271err_total_inodes:
272 percpu_counter_destroy(&m->opened_inodes);
273err_opened_inodes:
274 percpu_counter_destroy(&m->i_caps_mis);
Xiubo Li1af16d52020-03-19 23:45:00 -0400275err_i_caps_mis:
276 percpu_counter_destroy(&m->i_caps_hit);
277err_i_caps_hit:
278 percpu_counter_destroy(&m->d_lease_mis);
279err_d_lease_mis:
280 percpu_counter_destroy(&m->d_lease_hit);
281
282 return ret;
Xiubo Lif9009ef2020-03-19 23:44:59 -0400283}
284
285void ceph_metric_destroy(struct ceph_client_metric *m)
286{
287 if (!m)
288 return;
289
Jeff Laytonb4002172021-07-27 15:47:12 -0400290 cancel_delayed_work_sync(&m->delayed_work);
291
Xiubo Li1dd8d472020-09-03 09:01:40 -0400292 percpu_counter_destroy(&m->total_inodes);
293 percpu_counter_destroy(&m->opened_inodes);
Xiubo Li1af16d52020-03-19 23:45:00 -0400294 percpu_counter_destroy(&m->i_caps_mis);
295 percpu_counter_destroy(&m->i_caps_hit);
Xiubo Lif9009ef2020-03-19 23:44:59 -0400296 percpu_counter_destroy(&m->d_lease_mis);
297 percpu_counter_destroy(&m->d_lease_hit);
Xiubo Li18f473b2020-07-16 10:05:57 -0400298
Jeff Layton7e656242021-06-09 14:09:52 -0400299 ceph_put_mds_session(m->session);
Xiubo Lif9009ef2020-03-19 23:44:59 -0400300}
Xiubo Li97e27aa2020-03-19 23:45:01 -0400301
Xiubo Lifc123d52021-04-28 14:08:39 +0800302#define METRIC_UPDATE_MIN_MAX(min, max, new) \
303{ \
304 if (unlikely(new < min)) \
305 min = new; \
306 if (unlikely(new > max)) \
307 max = new; \
308}
309
310static inline void __update_stdev(ktime_t total, ktime_t lsum,
311 ktime_t *sq_sump, ktime_t lat)
Xiubo Li97e27aa2020-03-19 23:45:01 -0400312{
Xiubo Lifc123d52021-04-28 14:08:39 +0800313 ktime_t avg, sq;
Xiubo Li97e27aa2020-03-19 23:45:01 -0400314
315 if (unlikely(total == 1))
316 return;
317
318 /* the sq is (lat - old_avg) * (lat - new_avg) */
319 avg = DIV64_U64_ROUND_CLOSEST((lsum - lat), (total - 1));
320 sq = lat - avg;
321 avg = DIV64_U64_ROUND_CLOSEST(lsum, total);
322 sq = sq * (lat - avg);
323 *sq_sump += sq;
324}
325
Luís Henriques17e9fc92021-10-29 15:09:28 +0100326void ceph_update_metrics(struct ceph_metric *m,
327 ktime_t r_start, ktime_t r_end,
328 unsigned int size, int rc)
Xiubo Li97e27aa2020-03-19 23:45:01 -0400329{
330 ktime_t lat = ktime_sub(r_end, r_start);
Xiubo Lifc123d52021-04-28 14:08:39 +0800331 ktime_t total;
Xiubo Li97e27aa2020-03-19 23:45:01 -0400332
333 if (unlikely(rc < 0 && rc != -ENOENT && rc != -ETIMEDOUT))
334 return;
335
Luís Henriques17e9fc92021-10-29 15:09:28 +0100336 spin_lock(&m->lock);
337 total = ++m->total;
338 m->size_sum += size;
339 METRIC_UPDATE_MIN_MAX(m->size_min, m->size_max, size);
340 m->latency_sum += lat;
341 METRIC_UPDATE_MIN_MAX(m->latency_min, m->latency_max, lat);
342 __update_stdev(total, m->latency_sum, &m->latency_sq_sum, lat);
343 spin_unlock(&m->lock);
Xiubo Li70c94822020-03-19 23:45:02 -0400344}