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