1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/types.h>
5 #include <linux/percpu_counter.h>
6 #include <linux/math64.h>
7
8 #include "metric.h"
9 #include "mds_client.h"
10
ceph_mdsc_send_metrics(struct ceph_mds_client * mdsc,struct ceph_mds_session * s)11 static 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;
19 struct ceph_client_metric *m = &mdsc->metric;
20 u64 nr_caps = atomic64_read(&m->total_caps);
21 struct ceph_msg *msg;
22 struct timespec64 ts;
23 s64 sum;
24 s32 items = 0;
25 s32 len;
26
27 len = sizeof(*head) + sizeof(*cap) + sizeof(*read) + sizeof(*write)
28 + sizeof(*meta);
29
30 msg = ceph_msg_new(CEPH_MSG_CLIENT_METRICS, len, GFP_NOFS, true);
31 if (!msg) {
32 pr_err("send metrics to mds%d, failed to allocate message\n",
33 s->s_mds);
34 return false;
35 }
36
37 head = msg->front.iov_base;
38
39 /* encode the cap metric */
40 cap = (struct ceph_metric_cap *)(head + 1);
41 cap->type = cpu_to_le32(CLIENT_METRIC_TYPE_CAP_INFO);
42 cap->ver = 1;
43 cap->compat = 1;
44 cap->data_len = cpu_to_le32(sizeof(*cap) - 10);
45 cap->hit = cpu_to_le64(percpu_counter_sum(&mdsc->metric.i_caps_hit));
46 cap->mis = cpu_to_le64(percpu_counter_sum(&mdsc->metric.i_caps_mis));
47 cap->total = cpu_to_le64(nr_caps);
48 items++;
49
50 /* encode the read latency metric */
51 read = (struct ceph_metric_read_latency *)(cap + 1);
52 read->type = cpu_to_le32(CLIENT_METRIC_TYPE_READ_LATENCY);
53 read->ver = 1;
54 read->compat = 1;
55 read->data_len = cpu_to_le32(sizeof(*read) - 10);
56 sum = m->read_latency_sum;
57 jiffies_to_timespec64(sum, &ts);
58 read->sec = cpu_to_le32(ts.tv_sec);
59 read->nsec = cpu_to_le32(ts.tv_nsec);
60 items++;
61
62 /* encode the write latency metric */
63 write = (struct ceph_metric_write_latency *)(read + 1);
64 write->type = cpu_to_le32(CLIENT_METRIC_TYPE_WRITE_LATENCY);
65 write->ver = 1;
66 write->compat = 1;
67 write->data_len = cpu_to_le32(sizeof(*write) - 10);
68 sum = m->write_latency_sum;
69 jiffies_to_timespec64(sum, &ts);
70 write->sec = cpu_to_le32(ts.tv_sec);
71 write->nsec = cpu_to_le32(ts.tv_nsec);
72 items++;
73
74 /* encode the metadata latency metric */
75 meta = (struct ceph_metric_metadata_latency *)(write + 1);
76 meta->type = cpu_to_le32(CLIENT_METRIC_TYPE_METADATA_LATENCY);
77 meta->ver = 1;
78 meta->compat = 1;
79 meta->data_len = cpu_to_le32(sizeof(*meta) - 10);
80 sum = m->metadata_latency_sum;
81 jiffies_to_timespec64(sum, &ts);
82 meta->sec = cpu_to_le32(ts.tv_sec);
83 meta->nsec = cpu_to_le32(ts.tv_nsec);
84 items++;
85
86 put_unaligned_le32(items, &head->num);
87 msg->front.iov_len = len;
88 msg->hdr.version = cpu_to_le16(1);
89 msg->hdr.compat_version = cpu_to_le16(1);
90 msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
91 dout("client%llu send metrics to mds%d\n",
92 ceph_client_gid(mdsc->fsc->client), s->s_mds);
93 ceph_con_send(&s->s_con, msg);
94
95 return true;
96 }
97
98
metric_get_session(struct ceph_mds_client * mdsc)99 static void metric_get_session(struct ceph_mds_client *mdsc)
100 {
101 struct ceph_mds_session *s;
102 int i;
103
104 mutex_lock(&mdsc->mutex);
105 for (i = 0; i < mdsc->max_sessions; i++) {
106 s = __ceph_lookup_mds_session(mdsc, i);
107 if (!s)
108 continue;
109
110 /*
111 * Skip it if MDS doesn't support the metric collection,
112 * or the MDS will close the session's socket connection
113 * directly when it get this message.
114 */
115 if (check_session_state(s) &&
116 test_bit(CEPHFS_FEATURE_METRIC_COLLECT, &s->s_features)) {
117 mdsc->metric.session = s;
118 break;
119 }
120
121 ceph_put_mds_session(s);
122 }
123 mutex_unlock(&mdsc->mutex);
124 }
125
metric_delayed_work(struct work_struct * work)126 static void metric_delayed_work(struct work_struct *work)
127 {
128 struct ceph_client_metric *m =
129 container_of(work, struct ceph_client_metric, delayed_work.work);
130 struct ceph_mds_client *mdsc =
131 container_of(m, struct ceph_mds_client, metric);
132
133 if (mdsc->stopping)
134 return;
135
136 if (!m->session || !check_session_state(m->session)) {
137 if (m->session) {
138 ceph_put_mds_session(m->session);
139 m->session = NULL;
140 }
141 metric_get_session(mdsc);
142 }
143 if (m->session) {
144 ceph_mdsc_send_metrics(mdsc, m->session);
145 metric_schedule_delayed(m);
146 }
147 }
148
ceph_metric_init(struct ceph_client_metric * m)149 int ceph_metric_init(struct ceph_client_metric *m)
150 {
151 int ret;
152
153 if (!m)
154 return -EINVAL;
155
156 atomic64_set(&m->total_dentries, 0);
157 ret = percpu_counter_init(&m->d_lease_hit, 0, GFP_KERNEL);
158 if (ret)
159 return ret;
160
161 ret = percpu_counter_init(&m->d_lease_mis, 0, GFP_KERNEL);
162 if (ret)
163 goto err_d_lease_mis;
164
165 atomic64_set(&m->total_caps, 0);
166 ret = percpu_counter_init(&m->i_caps_hit, 0, GFP_KERNEL);
167 if (ret)
168 goto err_i_caps_hit;
169
170 ret = percpu_counter_init(&m->i_caps_mis, 0, GFP_KERNEL);
171 if (ret)
172 goto err_i_caps_mis;
173
174 spin_lock_init(&m->read_latency_lock);
175 m->read_latency_sq_sum = 0;
176 m->read_latency_min = KTIME_MAX;
177 m->read_latency_max = 0;
178 m->total_reads = 0;
179 m->read_latency_sum = 0;
180
181 spin_lock_init(&m->write_latency_lock);
182 m->write_latency_sq_sum = 0;
183 m->write_latency_min = KTIME_MAX;
184 m->write_latency_max = 0;
185 m->total_writes = 0;
186 m->write_latency_sum = 0;
187
188 spin_lock_init(&m->metadata_latency_lock);
189 m->metadata_latency_sq_sum = 0;
190 m->metadata_latency_min = KTIME_MAX;
191 m->metadata_latency_max = 0;
192 m->total_metadatas = 0;
193 m->metadata_latency_sum = 0;
194
195 atomic64_set(&m->opened_files, 0);
196 ret = percpu_counter_init(&m->opened_inodes, 0, GFP_KERNEL);
197 if (ret)
198 goto err_opened_inodes;
199 ret = percpu_counter_init(&m->total_inodes, 0, GFP_KERNEL);
200 if (ret)
201 goto err_total_inodes;
202
203 m->session = NULL;
204 INIT_DELAYED_WORK(&m->delayed_work, metric_delayed_work);
205
206 return 0;
207
208 err_total_inodes:
209 percpu_counter_destroy(&m->opened_inodes);
210 err_opened_inodes:
211 percpu_counter_destroy(&m->i_caps_mis);
212 err_i_caps_mis:
213 percpu_counter_destroy(&m->i_caps_hit);
214 err_i_caps_hit:
215 percpu_counter_destroy(&m->d_lease_mis);
216 err_d_lease_mis:
217 percpu_counter_destroy(&m->d_lease_hit);
218
219 return ret;
220 }
221
ceph_metric_destroy(struct ceph_client_metric * m)222 void ceph_metric_destroy(struct ceph_client_metric *m)
223 {
224 if (!m)
225 return;
226
227 cancel_delayed_work_sync(&m->delayed_work);
228
229 percpu_counter_destroy(&m->total_inodes);
230 percpu_counter_destroy(&m->opened_inodes);
231 percpu_counter_destroy(&m->i_caps_mis);
232 percpu_counter_destroy(&m->i_caps_hit);
233 percpu_counter_destroy(&m->d_lease_mis);
234 percpu_counter_destroy(&m->d_lease_hit);
235
236 ceph_put_mds_session(m->session);
237 }
238
__update_latency(ktime_t * totalp,ktime_t * lsump,ktime_t * min,ktime_t * max,ktime_t * sq_sump,ktime_t lat)239 static inline void __update_latency(ktime_t *totalp, ktime_t *lsump,
240 ktime_t *min, ktime_t *max,
241 ktime_t *sq_sump, ktime_t lat)
242 {
243 ktime_t total, avg, sq, lsum;
244
245 total = ++(*totalp);
246 lsum = (*lsump += lat);
247
248 if (unlikely(lat < *min))
249 *min = lat;
250 if (unlikely(lat > *max))
251 *max = lat;
252
253 if (unlikely(total == 1))
254 return;
255
256 /* the sq is (lat - old_avg) * (lat - new_avg) */
257 avg = DIV64_U64_ROUND_CLOSEST((lsum - lat), (total - 1));
258 sq = lat - avg;
259 avg = DIV64_U64_ROUND_CLOSEST(lsum, total);
260 sq = sq * (lat - avg);
261 *sq_sump += sq;
262 }
263
ceph_update_read_latency(struct ceph_client_metric * m,ktime_t r_start,ktime_t r_end,int rc)264 void ceph_update_read_latency(struct ceph_client_metric *m,
265 ktime_t r_start, ktime_t r_end,
266 int rc)
267 {
268 ktime_t lat = ktime_sub(r_end, r_start);
269
270 if (unlikely(rc < 0 && rc != -ENOENT && rc != -ETIMEDOUT))
271 return;
272
273 spin_lock(&m->read_latency_lock);
274 __update_latency(&m->total_reads, &m->read_latency_sum,
275 &m->read_latency_min, &m->read_latency_max,
276 &m->read_latency_sq_sum, lat);
277 spin_unlock(&m->read_latency_lock);
278 }
279
ceph_update_write_latency(struct ceph_client_metric * m,ktime_t r_start,ktime_t r_end,int rc)280 void ceph_update_write_latency(struct ceph_client_metric *m,
281 ktime_t r_start, ktime_t r_end,
282 int rc)
283 {
284 ktime_t lat = ktime_sub(r_end, r_start);
285
286 if (unlikely(rc && rc != -ETIMEDOUT))
287 return;
288
289 spin_lock(&m->write_latency_lock);
290 __update_latency(&m->total_writes, &m->write_latency_sum,
291 &m->write_latency_min, &m->write_latency_max,
292 &m->write_latency_sq_sum, lat);
293 spin_unlock(&m->write_latency_lock);
294 }
295
ceph_update_metadata_latency(struct ceph_client_metric * m,ktime_t r_start,ktime_t r_end,int rc)296 void ceph_update_metadata_latency(struct ceph_client_metric *m,
297 ktime_t r_start, ktime_t r_end,
298 int rc)
299 {
300 ktime_t lat = ktime_sub(r_end, r_start);
301
302 if (unlikely(rc && rc != -ENOENT))
303 return;
304
305 spin_lock(&m->metadata_latency_lock);
306 __update_latency(&m->total_metadatas, &m->metadata_latency_sum,
307 &m->metadata_latency_min, &m->metadata_latency_max,
308 &m->metadata_latency_sq_sum, lat);
309 spin_unlock(&m->metadata_latency_lock);
310 }
311