Whamcloud - gitweb
0242cd581a40c33dcd171d2330dc7f745005c326
[fs/lustre-release.git] / lustre / mdc / lproc_mdc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <linux/vfs.h>
35 #include <obd_class.h>
36 #include <lprocfs_status.h>
37 #include <lustre_osc.h>
38 #include <cl_object.h>
39 #include "mdc_internal.h"
40
41 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
42                            char *buf)
43 {
44         struct obd_device *dev = container_of(kobj, struct obd_device,
45                                               obd_kset.kobj);
46         ssize_t len;
47
48         LPROCFS_CLIMP_CHECK(dev);
49         len = sprintf(buf, "%d\n", !dev->u.cli.cl_import->imp_deactive);
50         LPROCFS_CLIMP_EXIT(dev);
51         return len;
52 }
53
54 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
55                             const char *buffer, size_t count)
56 {
57         struct obd_device *dev = container_of(kobj, struct obd_device,
58                                               obd_kset.kobj);
59         bool val;
60         int rc;
61
62         rc = kstrtobool(buffer, &val);
63         if (rc)
64                 return rc;
65
66         /* opposite senses */
67         if (dev->u.cli.cl_import->imp_deactive == val)
68                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
69         else
70                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
71                        val);
72
73         return count;
74 }
75 LUSTRE_RW_ATTR(active);
76
77 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
78                                        struct attribute *attr,
79                                        char *buf)
80 {
81         struct obd_device *dev = container_of(kobj, struct obd_device,
82                                               obd_kset.kobj);
83         ssize_t len;
84         u32 max;
85
86         max = obd_get_max_rpcs_in_flight(&dev->u.cli);
87         len = sprintf(buf, "%u\n", max);
88
89         return len;
90 }
91
92 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
93                                         struct attribute *attr,
94                                         const char *buffer,
95                                         size_t count)
96 {
97         struct obd_device *dev = container_of(kobj, struct obd_device,
98                                               obd_kset.kobj);
99         unsigned int val;
100         int rc;
101
102         rc = kstrtouint(buffer, 10, &val);
103         if (rc)
104                 return rc;
105
106         rc = obd_set_max_rpcs_in_flight(&dev->u.cli, val);
107         if (rc)
108                 count = rc;
109
110         return count;
111 }
112 LUSTRE_RW_ATTR(max_rpcs_in_flight);
113
114 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
115                                            struct attribute *attr,
116                                            char *buf)
117 {
118         struct obd_device *dev = container_of(kobj, struct obd_device,
119                                               obd_kset.kobj);
120         u16 max;
121
122         max = obd_get_max_mod_rpcs_in_flight(&dev->u.cli);
123         return sprintf(buf, "%hu\n", max);
124 }
125
126 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
127                                             struct attribute *attr,
128                                             const char *buffer,
129                                             size_t count)
130 {
131         struct obd_device *dev = container_of(kobj, struct obd_device,
132                                               obd_kset.kobj);
133         u16 val;
134         int rc;
135
136         rc = kstrtou16(buffer, 10, &val);
137         if (rc)
138                 return rc;
139
140         rc = obd_set_max_mod_rpcs_in_flight(&dev->u.cli, val);
141         if (rc)
142                 count = rc;
143
144         return count;
145 }
146 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
147
148 static int mdc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
149 {
150         struct obd_device *dev = m->private;
151         struct client_obd *cli = &dev->u.cli;
152         long val;
153         int mult;
154
155         spin_lock(&cli->cl_loi_list_lock);
156         val = cli->cl_dirty_max_pages;
157         spin_unlock(&cli->cl_loi_list_lock);
158
159         mult = 1 << (20 - PAGE_SHIFT);
160         return lprocfs_seq_read_frac_helper(m, val, mult);
161 }
162
163 static ssize_t mdc_max_dirty_mb_seq_write(struct file *file,
164                                           const char __user *buffer,
165                                           size_t count, loff_t *off)
166 {
167         struct seq_file *sfl = file->private_data;
168         struct obd_device *dev = sfl->private;
169         struct client_obd *cli = &dev->u.cli;
170         s64 pages_number;
171         int rc;
172
173         rc = lprocfs_str_with_units_to_s64(buffer, count, &pages_number, 'M');
174         if (rc)
175                 return rc;
176
177         pages_number >>= PAGE_SHIFT;
178
179         if (pages_number <= 0 ||
180             pages_number >= OSC_MAX_DIRTY_MB_MAX << (20 - PAGE_SHIFT) ||
181             pages_number > totalram_pages / 4) /* 1/4 of RAM */
182                 return -ERANGE;
183
184         spin_lock(&cli->cl_loi_list_lock);
185         cli->cl_dirty_max_pages = pages_number;
186         osc_wake_cache_waiters(cli);
187         spin_unlock(&cli->cl_loi_list_lock);
188
189         return count;
190 }
191 LPROC_SEQ_FOPS(mdc_max_dirty_mb);
192
193 static ssize_t contention_seconds_show(struct kobject *kobj,
194                                        struct attribute *attr,
195                                        char *buf)
196 {
197         struct obd_device *obd = container_of(kobj, struct obd_device,
198                                               obd_kset.kobj);
199         struct osc_device *od = obd2osc_dev(obd);
200
201         return sprintf(buf, "%lld\n", od->od_contention_time);
202 }
203
204 static ssize_t contention_seconds_store(struct kobject *kobj,
205                                         struct attribute *attr,
206                                         const char *buffer,
207                                         size_t count)
208 {
209         struct obd_device *obd = container_of(kobj, struct obd_device,
210                                               obd_kset.kobj);
211         struct osc_device *od = obd2osc_dev(obd);
212         time64_t val;
213         int rc;
214
215         rc = kstrtoll(buffer, 0, &val);
216         if (rc)
217                 return rc;
218
219         od->od_contention_time = val;
220
221         return count;
222 }
223 LUSTRE_RW_ATTR(contention_seconds);
224
225 LUSTRE_RO_ATTR(conn_uuid);
226
227 LUSTRE_WO_ATTR(ping);
228
229 static int mdc_cached_mb_seq_show(struct seq_file *m, void *v)
230 {
231         struct obd_device *dev = m->private;
232         struct client_obd *cli = &dev->u.cli;
233         int shift = 20 - PAGE_SHIFT;
234
235         seq_printf(m, "used_mb: %ld\n"
236                    "busy_cnt: %ld\n"
237                    "reclaim: %llu\n",
238                    (atomic_long_read(&cli->cl_lru_in_list) +
239                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
240                     atomic_long_read(&cli->cl_lru_busy),
241                    cli->cl_lru_reclaim);
242
243         return 0;
244 }
245
246 /* shrink the number of caching pages to a specific number */
247 static ssize_t
248 mdc_cached_mb_seq_write(struct file *file, const char __user *buffer,
249                         size_t count, loff_t *off)
250 {
251         struct seq_file *sfl = file->private_data;
252         struct obd_device *dev = sfl->private;
253         struct client_obd *cli = &dev->u.cli;
254         __s64 pages_number;
255         long rc;
256         char kernbuf[128];
257
258         if (count >= sizeof(kernbuf))
259                 return -EINVAL;
260
261         if (copy_from_user(kernbuf, buffer, count))
262                 return -EFAULT;
263         kernbuf[count] = 0;
264
265         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
266                   kernbuf;
267         rc = lprocfs_str_with_units_to_s64(buffer, count, &pages_number, 'M');
268         if (rc)
269                 return rc;
270
271         pages_number >>= PAGE_SHIFT;
272
273         if (pages_number < 0)
274                 return -ERANGE;
275
276         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
277         if (rc > 0) {
278                 struct lu_env *env;
279                 __u16 refcheck;
280
281                 env = cl_env_get(&refcheck);
282                 if (!IS_ERR(env)) {
283                         (void)osc_lru_shrink(env, cli, rc, true);
284                         cl_env_put(env, &refcheck);
285                 }
286         }
287
288         return count;
289 }
290 LPROC_SEQ_FOPS(mdc_cached_mb);
291
292 static int mdc_unstable_stats_seq_show(struct seq_file *m, void *v)
293 {
294         struct obd_device *dev = m->private;
295         struct client_obd *cli = &dev->u.cli;
296         long pages;
297         int mb;
298
299         pages = atomic_long_read(&cli->cl_unstable_count);
300         mb    = (pages * PAGE_SIZE) >> 20;
301
302         seq_printf(m, "unstable_pages: %20ld\n"
303                    "unstable_mb:              %10d\n", pages, mb);
304         return 0;
305 }
306 LPROC_SEQ_FOPS_RO(mdc_unstable_stats);
307
308 static ssize_t mdc_rpc_stats_seq_write(struct file *file,
309                                        const char __user *buf,
310                                        size_t len, loff_t *off)
311 {
312         struct seq_file *seq = file->private_data;
313         struct obd_device *dev = seq->private;
314         struct client_obd *cli = &dev->u.cli;
315
316         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
317
318         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
319         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
320         lprocfs_oh_clear(&cli->cl_read_page_hist);
321         lprocfs_oh_clear(&cli->cl_write_page_hist);
322         lprocfs_oh_clear(&cli->cl_read_offset_hist);
323         lprocfs_oh_clear(&cli->cl_write_offset_hist);
324
325         return len;
326 }
327
328 #define pct(a, b) (b ? a * 100 / b : 0)
329 static int mdc_rpc_stats_seq_show(struct seq_file *seq, void *v)
330 {
331         struct obd_device *dev = seq->private;
332         struct client_obd *cli = &dev->u.cli;
333         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
334         int i;
335
336         obd_mod_rpc_stats_seq_show(&dev->u.cli, seq);
337
338         spin_lock(&cli->cl_loi_list_lock);
339
340         seq_printf(seq, "\nread RPCs in flight:  %d\n",
341                    cli->cl_r_in_flight);
342         seq_printf(seq, "write RPCs in flight: %d\n",
343                    cli->cl_w_in_flight);
344         seq_printf(seq, "pending write pages:  %d\n",
345                    atomic_read(&cli->cl_pending_w_pages));
346         seq_printf(seq, "pending read pages:   %d\n",
347                    atomic_read(&cli->cl_pending_r_pages));
348
349         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
350         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
351         seq_printf(seq, "       rpcs   %% cum %%\n");
352
353         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
354         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
355
356         read_cum = 0;
357         write_cum = 0;
358         for (i = 0; i < OBD_HIST_MAX; i++) {
359                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
360                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
361
362                 read_cum += r;
363                 write_cum += w;
364                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
365                            1 << i, r, pct(r, read_tot),
366                            pct(read_cum, read_tot), w,
367                            pct(w, write_tot),
368                            pct(write_cum, write_tot));
369                 if (read_cum == read_tot && write_cum == write_tot)
370                         break;
371         }
372
373         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
374         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
375         seq_printf(seq, "       rpcs   %% cum %%\n");
376
377         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
378         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
379
380         read_cum = 0;
381         write_cum = 0;
382         for (i = 0; i < OBD_HIST_MAX; i++) {
383                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
384                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
385
386                 read_cum += r;
387                 write_cum += w;
388                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
389                            i, r, pct(r, read_tot), pct(read_cum, read_tot), w,
390                            pct(w, write_tot), pct(write_cum, write_tot));
391                 if (read_cum == read_tot && write_cum == write_tot)
392                         break;
393         }
394
395         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
396         seq_printf(seq, "offset                rpcs   %% cum %% |");
397         seq_printf(seq, "       rpcs   %% cum %%\n");
398
399         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
400         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
401
402         read_cum = 0;
403         write_cum = 0;
404         for (i = 0; i < OBD_HIST_MAX; i++) {
405                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
406                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
407
408                 read_cum += r;
409                 write_cum += w;
410                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
411                            (i == 0) ? 0 : 1 << (i - 1),
412                            r, pct(r, read_tot), pct(read_cum, read_tot),
413                            w, pct(w, write_tot), pct(write_cum, write_tot));
414                 if (read_cum == read_tot && write_cum == write_tot)
415                         break;
416         }
417         spin_unlock(&cli->cl_loi_list_lock);
418
419         return 0;
420 }
421 #undef pct
422 LPROC_SEQ_FOPS(mdc_rpc_stats);
423
424 static int mdc_stats_seq_show(struct seq_file *seq, void *v)
425 {
426         struct timespec64 now;
427         struct obd_device *dev = seq->private;
428         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
429
430         ktime_get_real_ts64(&now);
431
432         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
433                    (s64)now.tv_sec, now.tv_nsec);
434         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
435                    stats->os_lockless_writes);
436         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
437                    stats->os_lockless_reads);
438         seq_printf(seq, "lockless_truncate\t\t%llu\n",
439                    stats->os_lockless_truncates);
440         return 0;
441 }
442
443 static ssize_t mdc_stats_seq_write(struct file *file,
444                                    const char __user *buf,
445                                    size_t len, loff_t *off)
446 {
447         struct seq_file *seq = file->private_data;
448         struct obd_device *dev = seq->private;
449         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
450
451         memset(stats, 0, sizeof(*stats));
452         return len;
453 }
454 LPROC_SEQ_FOPS(mdc_stats);
455
456 LPROC_SEQ_FOPS_RO_TYPE(mdc, connect_flags);
457 LPROC_SEQ_FOPS_RO_TYPE(mdc, server_uuid);
458 LPROC_SEQ_FOPS_RO_TYPE(mdc, timeouts);
459 LPROC_SEQ_FOPS_RO_TYPE(mdc, state);
460 LPROC_SEQ_FOPS_RW_TYPE(mdc, obd_max_pages_per_rpc);
461 LPROC_SEQ_FOPS_RW_TYPE(mdc, import);
462 LPROC_SEQ_FOPS_RW_TYPE(mdc, pinger_recov);
463
464 struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
465         { .name =       "connect_flags",
466           .fops =       &mdc_connect_flags_fops },
467         { .name =       "mds_server_uuid",
468           .fops =       &mdc_server_uuid_fops   },
469         { .name =       "max_pages_per_rpc",
470           .fops =       &mdc_obd_max_pages_per_rpc_fops },
471         { .name =       "max_dirty_mb",
472           .fops =       &mdc_max_dirty_mb_fops          },
473         { .name =       "mdc_cached_mb",
474           .fops =       &mdc_cached_mb_fops             },
475         { .name =       "timeouts",
476           .fops =       &mdc_timeouts_fops              },
477         { .name =       "import",
478           .fops =       &mdc_import_fops                },
479         { .name =       "state",
480           .fops =       &mdc_state_fops                 },
481         { .name =       "pinger_recov",
482           .fops =       &mdc_pinger_recov_fops          },
483         { .name =       "rpc_stats",
484           .fops =       &mdc_rpc_stats_fops             },
485         { .name =       "unstable_stats",
486           .fops =       &mdc_unstable_stats_fops        },
487         { .name =       "mdc_stats",
488           .fops =       &mdc_stats_fops                 },
489         { NULL }
490 };
491
492 static struct attribute *mdc_attrs[] = {
493         &lustre_attr_active.attr,
494         &lustre_attr_max_rpcs_in_flight.attr,
495         &lustre_attr_max_mod_rpcs_in_flight.attr,
496         &lustre_attr_contention_seconds.attr,
497         &lustre_attr_conn_uuid.attr,
498         &lustre_attr_ping.attr,
499         NULL,
500 };
501
502 int mdc_tunables_init(struct obd_device *obd)
503 {
504         int rc;
505
506         obd->obd_ktype.default_attrs = mdc_attrs;
507         obd->obd_vars = lprocfs_mdc_obd_vars;
508
509         rc = lprocfs_obd_setup(obd, false);
510         if (rc)
511                 goto out_failed;
512 #ifdef CONFIG_PROC_FS
513         rc = lprocfs_alloc_md_stats(obd, 0);
514         if (rc) {
515                 lprocfs_obd_cleanup(obd);
516                 goto out_failed;
517         }
518 #endif
519         rc = sptlrpc_lprocfs_cliobd_attach(obd);
520         if (rc) {
521 #ifdef CONFIG_PROC_FS
522                 lprocfs_free_md_stats(obd);
523 #endif
524                 lprocfs_obd_cleanup(obd);
525                 goto out_failed;
526         }
527         ptlrpc_lprocfs_register_obd(obd);
528
529 out_failed:
530         return rc;
531 }