1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * RDMA Transport Layer
4  *
5  * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
6  * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
7  * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
8  */
9 #undef pr_fmt
10 #define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
11 
12 #include "rtrs-clt.h"
13 
14 void rtrs_clt_update_wc_stats(struct rtrs_clt_con *con)
15 {
16 	struct rtrs_clt_sess *sess = to_clt_sess(con->c.sess);
17 	struct rtrs_clt_stats *stats = sess->stats;
18 	struct rtrs_clt_stats_pcpu *s;
19 	int cpu;
20 
21 	cpu = raw_smp_processor_id();
22 	s = this_cpu_ptr(stats->pcpu_stats);
23 	if (con->cpu != cpu) {
24 		s->cpu_migr.to++;
25 
26 		/* Careful here, override s pointer */
27 		s = per_cpu_ptr(stats->pcpu_stats, con->cpu);
28 		atomic_inc(&s->cpu_migr.from);
29 	}
30 }
31 
32 void rtrs_clt_inc_failover_cnt(struct rtrs_clt_stats *stats)
33 {
34 	struct rtrs_clt_stats_pcpu *s;
35 
36 	s = this_cpu_ptr(stats->pcpu_stats);
37 	s->rdma.failover_cnt++;
38 }
39 
40 int rtrs_clt_stats_migration_from_cnt_to_str(struct rtrs_clt_stats *stats, char *buf)
41 {
42 	struct rtrs_clt_stats_pcpu *s;
43 
44 	size_t used;
45 	int cpu;
46 
47 	used = 0;
48 	for_each_possible_cpu(cpu) {
49 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
50 		used += sysfs_emit_at(buf, used, "%d ",
51 				  atomic_read(&s->cpu_migr.from));
52 	}
53 
54 	used += sysfs_emit_at(buf, used, "\n");
55 
56 	return used;
57 }
58 
59 int rtrs_clt_stats_migration_to_cnt_to_str(struct rtrs_clt_stats *stats, char *buf)
60 {
61 	struct rtrs_clt_stats_pcpu *s;
62 
63 	size_t used;
64 	int cpu;
65 
66 	used = 0;
67 	for_each_possible_cpu(cpu) {
68 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
69 		used += sysfs_emit_at(buf, used, "%d ", s->cpu_migr.to);
70 	}
71 
72 	used += sysfs_emit_at(buf, used, "\n");
73 
74 	return used;
75 }
76 
77 int rtrs_clt_stats_reconnects_to_str(struct rtrs_clt_stats *stats, char *buf)
78 {
79 	return sysfs_emit(buf, "%d %d\n", stats->reconnects.successful_cnt,
80 			  stats->reconnects.fail_cnt);
81 }
82 
83 ssize_t rtrs_clt_stats_rdma_to_str(struct rtrs_clt_stats *stats, char *page)
84 {
85 	struct rtrs_clt_stats_rdma sum;
86 	struct rtrs_clt_stats_rdma *r;
87 	int cpu;
88 
89 	memset(&sum, 0, sizeof(sum));
90 
91 	for_each_possible_cpu(cpu) {
92 		r = &per_cpu_ptr(stats->pcpu_stats, cpu)->rdma;
93 
94 		sum.dir[READ].cnt	  += r->dir[READ].cnt;
95 		sum.dir[READ].size_total  += r->dir[READ].size_total;
96 		sum.dir[WRITE].cnt	  += r->dir[WRITE].cnt;
97 		sum.dir[WRITE].size_total += r->dir[WRITE].size_total;
98 		sum.failover_cnt	  += r->failover_cnt;
99 	}
100 
101 	return sysfs_emit(page, "%llu %llu %llu %llu %u %llu\n",
102 			 sum.dir[READ].cnt, sum.dir[READ].size_total,
103 			 sum.dir[WRITE].cnt, sum.dir[WRITE].size_total,
104 			 atomic_read(&stats->inflight), sum.failover_cnt);
105 }
106 
107 ssize_t rtrs_clt_reset_all_help(struct rtrs_clt_stats *s, char *page)
108 {
109 	return sysfs_emit(page, "echo 1 to reset all statistics\n");
110 }
111 
112 int rtrs_clt_reset_rdma_stats(struct rtrs_clt_stats *stats, bool enable)
113 {
114 	struct rtrs_clt_stats_pcpu *s;
115 	int cpu;
116 
117 	if (!enable)
118 		return -EINVAL;
119 
120 	for_each_possible_cpu(cpu) {
121 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
122 		memset(&s->rdma, 0, sizeof(s->rdma));
123 	}
124 
125 	return 0;
126 }
127 
128 int rtrs_clt_reset_cpu_migr_stats(struct rtrs_clt_stats *stats, bool enable)
129 {
130 	struct rtrs_clt_stats_pcpu *s;
131 	int cpu;
132 
133 	if (!enable)
134 		return -EINVAL;
135 
136 	for_each_possible_cpu(cpu) {
137 		s = per_cpu_ptr(stats->pcpu_stats, cpu);
138 		memset(&s->cpu_migr, 0, sizeof(s->cpu_migr));
139 	}
140 
141 	return 0;
142 }
143 
144 int rtrs_clt_reset_reconnects_stat(struct rtrs_clt_stats *stats, bool enable)
145 {
146 	if (!enable)
147 		return -EINVAL;
148 
149 	memset(&stats->reconnects, 0, sizeof(stats->reconnects));
150 
151 	return 0;
152 }
153 
154 int rtrs_clt_reset_all_stats(struct rtrs_clt_stats *s, bool enable)
155 {
156 	if (enable) {
157 		rtrs_clt_reset_rdma_stats(s, enable);
158 		rtrs_clt_reset_cpu_migr_stats(s, enable);
159 		rtrs_clt_reset_reconnects_stat(s, enable);
160 		atomic_set(&s->inflight, 0);
161 		return 0;
162 	}
163 
164 	return -EINVAL;
165 }
166 
167 static inline void rtrs_clt_update_rdma_stats(struct rtrs_clt_stats *stats,
168 					       size_t size, int d)
169 {
170 	struct rtrs_clt_stats_pcpu *s;
171 
172 	s = this_cpu_ptr(stats->pcpu_stats);
173 	s->rdma.dir[d].cnt++;
174 	s->rdma.dir[d].size_total += size;
175 }
176 
177 void rtrs_clt_update_all_stats(struct rtrs_clt_io_req *req, int dir)
178 {
179 	struct rtrs_clt_con *con = req->con;
180 	struct rtrs_clt_sess *sess = to_clt_sess(con->c.sess);
181 	struct rtrs_clt_stats *stats = sess->stats;
182 	unsigned int len;
183 
184 	len = req->usr_len + req->data_len;
185 	rtrs_clt_update_rdma_stats(stats, len, dir);
186 	if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
187 		atomic_inc(&stats->inflight);
188 }
189 
190 int rtrs_clt_init_stats(struct rtrs_clt_stats *stats)
191 {
192 	stats->pcpu_stats = alloc_percpu(typeof(*stats->pcpu_stats));
193 	if (!stats->pcpu_stats)
194 		return -ENOMEM;
195 
196 	/*
197 	 * successful_cnt will be set to 0 after session
198 	 * is established for the first time
199 	 */
200 	stats->reconnects.successful_cnt = -1;
201 
202 	return 0;
203 }
204