Whamcloud - gitweb
LU-6770 osc: use global osc_rq_pool to reduce memory usage
[fs/lustre-release.git] / lustre / osc / lproc_osc.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/version.h>
39 #include <asm/statfs.h>
40 #include <obd_cksum.h>
41 #include <obd_class.h>
42 #include <lprocfs_status.h>
43 #include <linux/seq_file.h>
44 #include "osc_internal.h"
45
46 #ifdef CONFIG_PROC_FS
47 static int osc_active_seq_show(struct seq_file *m, void *v)
48 {
49         struct obd_device *dev = m->private;
50         int rc;
51
52         LPROCFS_CLIMP_CHECK(dev);
53         rc = seq_printf(m, "%d\n", !dev->u.cli.cl_import->imp_deactive);
54         LPROCFS_CLIMP_EXIT(dev);
55         return rc;
56 }
57
58 static ssize_t osc_active_seq_write(struct file *file,
59                                     const char __user *buffer,
60                                     size_t count, loff_t *off)
61 {
62         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
63         int val, rc;
64
65         rc = lprocfs_write_helper(buffer, count, &val);
66         if (rc)
67                 return rc;
68         if (val < 0 || val > 1)
69                 return -ERANGE;
70
71         /* opposite senses */
72         if (dev->u.cli.cl_import->imp_deactive == val)
73                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
74         else
75                 CDEBUG(D_CONFIG, "activate %d: ignoring repeat request\n", val);
76
77         return count;
78 }
79 LPROC_SEQ_FOPS(osc_active);
80
81 static int osc_max_rpcs_in_flight_seq_show(struct seq_file *m, void *v)
82 {
83         struct obd_device *dev = m->private;
84         struct client_obd *cli = &dev->u.cli;
85         int rc;
86
87         spin_lock(&cli->cl_loi_list_lock);
88         rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
89         spin_unlock(&cli->cl_loi_list_lock);
90         return rc;
91 }
92
93 static ssize_t osc_max_rpcs_in_flight_seq_write(struct file *file,
94                                                 const char __user *buffer,
95                                                 size_t count, loff_t *off)
96 {
97         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
98         struct client_obd *cli = &dev->u.cli;
99         int val, rc;
100         int adding, added, req_count;
101
102         rc = lprocfs_write_helper(buffer, count, &val);
103         if (rc)
104                 return rc;
105
106         if (val < 1 || val > OSC_MAX_RIF_MAX)
107                 return -ERANGE;
108
109         LPROCFS_CLIMP_CHECK(dev);
110
111         adding = val - cli->cl_max_rpcs_in_flight;
112         req_count = atomic_read(&osc_pool_req_count);
113         if (adding > 0 && req_count < osc_reqpool_maxreqcount) {
114                 /*
115                  * There might be some race which will cause over-limit
116                  * allocation, but it is fine.
117                  */
118                 if (req_count + adding > osc_reqpool_maxreqcount)
119                         adding = osc_reqpool_maxreqcount - req_count;
120
121                 added = osc_rq_pool->prp_populate(osc_rq_pool, adding);
122                 atomic_add(added, &osc_pool_req_count);
123         }
124
125         spin_lock(&cli->cl_loi_list_lock);
126         cli->cl_max_rpcs_in_flight = val;
127         client_adjust_max_dirty(cli);
128         spin_unlock(&cli->cl_loi_list_lock);
129
130         LPROCFS_CLIMP_EXIT(dev);
131         return count;
132 }
133 LPROC_SEQ_FOPS(osc_max_rpcs_in_flight);
134
135 static int osc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
136 {
137         struct obd_device *dev = m->private;
138         struct client_obd *cli = &dev->u.cli;
139         long val;
140         int mult;
141
142         spin_lock(&cli->cl_loi_list_lock);
143         val = cli->cl_dirty_max_pages;
144         spin_unlock(&cli->cl_loi_list_lock);
145
146         mult = 1 << (20 - PAGE_CACHE_SHIFT);
147         return lprocfs_seq_read_frac_helper(m, val, mult);
148 }
149
150 static ssize_t osc_max_dirty_mb_seq_write(struct file *file,
151                                           const char __user *buffer,
152                                           size_t count, loff_t *off)
153 {
154         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
155         struct client_obd *cli = &dev->u.cli;
156         int pages_number, mult, rc;
157
158         mult = 1 << (20 - PAGE_CACHE_SHIFT);
159         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
160         if (rc)
161                 return rc;
162
163         if (pages_number <= 0 ||
164             pages_number > OSC_MAX_DIRTY_MB_MAX << (20 - PAGE_CACHE_SHIFT) ||
165             pages_number > totalram_pages / 4) /* 1/4 of RAM */
166                 return -ERANGE;
167
168         spin_lock(&cli->cl_loi_list_lock);
169         cli->cl_dirty_max_pages = pages_number;
170         osc_wake_cache_waiters(cli);
171         spin_unlock(&cli->cl_loi_list_lock);
172
173         return count;
174 }
175 LPROC_SEQ_FOPS(osc_max_dirty_mb);
176
177 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
178 {
179         struct obd_device *dev = m->private;
180         struct client_obd *cli = &dev->u.cli;
181         int shift = 20 - PAGE_CACHE_SHIFT;
182         int rc;
183
184         rc = seq_printf(m,
185                       "used_mb: %ld\n"
186                       "busy_cnt: %ld\n",
187                       (atomic_long_read(&cli->cl_lru_in_list) +
188                         atomic_long_read(&cli->cl_lru_busy)) >> shift,
189                       atomic_long_read(&cli->cl_lru_busy));
190
191         return rc;
192 }
193
194 /* shrink the number of caching pages to a specific number */
195 static ssize_t
196 osc_cached_mb_seq_write(struct file *file, const char __user *buffer,
197                         size_t count, loff_t *off)
198 {
199         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
200         struct client_obd *cli = &dev->u.cli;
201         __u64 val;
202         long pages_number;
203         long rc;
204         int mult;
205         char kernbuf[128];
206
207         if (count >= sizeof(kernbuf))
208                 return -EINVAL;
209
210         if (copy_from_user(kernbuf, buffer, count))
211                 return -EFAULT;
212         kernbuf[count] = 0;
213
214         mult = 1 << (20 - PAGE_CACHE_SHIFT);
215         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
216                   kernbuf;
217         rc = lprocfs_write_frac_u64_helper(buffer, count, &val, mult);
218
219         if (rc)
220                 return rc;
221
222         if (val > LONG_MAX)
223                 return -ERANGE;
224         pages_number = (long)val;
225
226         if (pages_number < 0)
227                 return -ERANGE;
228
229         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
230         if (rc > 0) {
231                 struct lu_env *env;
232                 int refcheck;
233
234                 env = cl_env_get(&refcheck);
235                 if (!IS_ERR(env)) {
236                         (void)osc_lru_shrink(env, cli, rc, true);
237                         cl_env_put(env, &refcheck);
238                 }
239         }
240
241         return count;
242 }
243 LPROC_SEQ_FOPS(osc_cached_mb);
244
245 static int osc_cur_dirty_bytes_seq_show(struct seq_file *m, void *v)
246 {
247         struct obd_device *dev = m->private;
248         struct client_obd *cli = &dev->u.cli;
249         int rc;
250
251         spin_lock(&cli->cl_loi_list_lock);
252         rc = seq_printf(m, "%lu\n", cli->cl_dirty_pages << PAGE_CACHE_SHIFT);
253         spin_unlock(&cli->cl_loi_list_lock);
254         return rc;
255 }
256 LPROC_SEQ_FOPS_RO(osc_cur_dirty_bytes);
257
258 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
259 {
260         struct obd_device *dev = m->private;
261         struct client_obd *cli = &dev->u.cli;
262         int rc;
263
264         spin_lock(&cli->cl_loi_list_lock);
265         rc = seq_printf(m, "%lu\n", cli->cl_avail_grant);
266         spin_unlock(&cli->cl_loi_list_lock);
267         return rc;
268 }
269
270 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
271                                              const char __user *buffer,
272                                              size_t count, loff_t *off)
273 {
274         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
275         struct client_obd *cli = &obd->u.cli;
276         int                rc;
277         __u64              val;
278
279         if (obd == NULL)
280                 return 0;
281
282         rc = lprocfs_write_u64_helper(buffer, count, &val);
283         if (rc)
284                 return rc;
285
286         /* this is only for shrinking grant */
287         spin_lock(&cli->cl_loi_list_lock);
288         if (val >= cli->cl_avail_grant) {
289                 spin_unlock(&cli->cl_loi_list_lock);
290                 return 0;
291         }
292
293         spin_unlock(&cli->cl_loi_list_lock);
294
295         LPROCFS_CLIMP_CHECK(obd);
296         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
297                 rc = osc_shrink_grant_to_target(cli, val);
298         LPROCFS_CLIMP_EXIT(obd);
299         if (rc)
300                 return rc;
301         return count;
302 }
303 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
304
305 static int osc_cur_lost_grant_bytes_seq_show(struct seq_file *m, void *v)
306 {
307         struct obd_device *dev = m->private;
308         struct client_obd *cli = &dev->u.cli;
309         int rc;
310
311         spin_lock(&cli->cl_loi_list_lock);
312         rc = seq_printf(m, "%lu\n", cli->cl_lost_grant);
313         spin_unlock(&cli->cl_loi_list_lock);
314         return rc;
315 }
316 LPROC_SEQ_FOPS_RO(osc_cur_lost_grant_bytes);
317
318 static int osc_grant_shrink_interval_seq_show(struct seq_file *m, void *v)
319 {
320         struct obd_device *obd = m->private;
321
322         if (obd == NULL)
323                 return 0;
324         return seq_printf(m, "%d\n",
325                           obd->u.cli.cl_grant_shrink_interval);
326 }
327
328 static ssize_t osc_grant_shrink_interval_seq_write(struct file *file,
329                                                    const char __user *buffer,
330                                                    size_t count, loff_t *off)
331 {
332         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
333         int val, rc;
334
335         if (obd == NULL)
336                 return 0;
337
338         rc = lprocfs_write_helper(buffer, count, &val);
339         if (rc)
340                 return rc;
341
342         if (val <= 0)
343                 return -ERANGE;
344
345         obd->u.cli.cl_grant_shrink_interval = val;
346
347         return count;
348 }
349 LPROC_SEQ_FOPS(osc_grant_shrink_interval);
350
351 static int osc_checksum_seq_show(struct seq_file *m, void *v)
352 {
353         struct obd_device *obd = m->private;
354
355         if (obd == NULL)
356                 return 0;
357
358         return seq_printf(m, "%d\n",
359                           obd->u.cli.cl_checksum ? 1 : 0);
360 }
361
362 static ssize_t osc_checksum_seq_write(struct file *file,
363                                       const char __user *buffer,
364                                       size_t count, loff_t *off)
365 {
366         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
367         int val, rc;
368
369         if (obd == NULL)
370                 return 0;
371
372         rc = lprocfs_write_helper(buffer, count, &val);
373         if (rc)
374                 return rc;
375
376         obd->u.cli.cl_checksum = (val ? 1 : 0);
377
378         return count;
379 }
380 LPROC_SEQ_FOPS(osc_checksum);
381
382 static int osc_checksum_type_seq_show(struct seq_file *m, void *v)
383 {
384         struct obd_device *obd = m->private;
385         int i;
386         DECLARE_CKSUM_NAME;
387
388         if (obd == NULL)
389                 return 0;
390
391         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
392                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
393                         continue;
394                 if (obd->u.cli.cl_cksum_type == (1 << i))
395                         seq_printf(m, "[%s] ", cksum_name[i]);
396                 else
397                         seq_printf(m, "%s ", cksum_name[i]);
398         }
399         seq_printf(m, "\n");
400         return 0;
401 }
402
403 static ssize_t osc_checksum_type_seq_write(struct file *file,
404                                            const char __user *buffer,
405                                            size_t count, loff_t *off)
406 {
407         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
408         int i;
409         DECLARE_CKSUM_NAME;
410         char kernbuf[10];
411
412         if (obd == NULL)
413                 return 0;
414
415         if (count > sizeof(kernbuf) - 1)
416                 return -EINVAL;
417         if (copy_from_user(kernbuf, buffer, count))
418                 return -EFAULT;
419         if (count > 0 && kernbuf[count - 1] == '\n')
420                 kernbuf[count - 1] = '\0';
421         else
422                 kernbuf[count] = '\0';
423
424         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
425                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
426                         continue;
427                 if (!strcmp(kernbuf, cksum_name[i])) {
428                        obd->u.cli.cl_cksum_type = 1 << i;
429                        return count;
430                 }
431         }
432         return -EINVAL;
433 }
434 LPROC_SEQ_FOPS(osc_checksum_type);
435
436 static int osc_resend_count_seq_show(struct seq_file *m, void *v)
437 {
438         struct obd_device *obd = m->private;
439
440         return seq_printf(m, "%u\n", atomic_read(&obd->u.cli.cl_resends));
441 }
442
443 static ssize_t osc_resend_count_seq_write(struct file *file,
444                                           const char __user *buffer,
445                                           size_t count, loff_t *off)
446 {
447         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
448         int val, rc;
449
450         rc = lprocfs_write_helper(buffer, count, &val);
451         if (rc)
452                 return rc;
453
454         if (val < 0)
455                 return -EINVAL;
456
457         atomic_set(&obd->u.cli.cl_resends, val);
458
459         return count;
460 }
461 LPROC_SEQ_FOPS(osc_resend_count);
462
463 static int osc_contention_seconds_seq_show(struct seq_file *m, void *v)
464 {
465         struct obd_device *obd = m->private;
466         struct osc_device *od  = obd2osc_dev(obd);
467
468         return seq_printf(m, "%u\n", od->od_contention_time);
469 }
470
471 static ssize_t osc_contention_seconds_seq_write(struct file *file,
472                                                 const char __user *buffer,
473                                                 size_t count, loff_t *off)
474 {
475         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
476         struct osc_device *od  = obd2osc_dev(obd);
477
478         return lprocfs_write_helper(buffer, count, &od->od_contention_time) ?: count;
479 }
480 LPROC_SEQ_FOPS(osc_contention_seconds);
481
482 static int osc_lockless_truncate_seq_show(struct seq_file *m, void *v)
483 {
484         struct obd_device *obd = m->private;
485         struct osc_device *od  = obd2osc_dev(obd);
486
487         return seq_printf(m, "%u\n", od->od_lockless_truncate);
488 }
489
490 static ssize_t osc_lockless_truncate_seq_write(struct file *file,
491                                                const char __user *buffer,
492                                     size_t count, loff_t *off)
493 {
494         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
495         struct osc_device *od  = obd2osc_dev(obd);
496
497         return lprocfs_write_helper(buffer, count, &od->od_lockless_truncate) ?:
498                 count;
499 }
500 LPROC_SEQ_FOPS(osc_lockless_truncate);
501
502 static int osc_destroys_in_flight_seq_show(struct seq_file *m, void *v)
503 {
504         struct obd_device *obd = m->private;
505         return seq_printf(m, "%u\n",
506                           atomic_read(&obd->u.cli.cl_destroy_in_flight));
507 }
508 LPROC_SEQ_FOPS_RO(osc_destroys_in_flight);
509
510 static int osc_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *v)
511 {
512         return lprocfs_obd_max_pages_per_rpc_seq_show(m, m->private);
513 }
514
515 static ssize_t osc_obd_max_pages_per_rpc_seq_write(struct file *file,
516                                                    const char __user *buffer,
517                                                    size_t count, loff_t *off)
518 {
519         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
520         struct client_obd *cli = &dev->u.cli;
521         struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
522         int chunk_mask, rc;
523         __u64 val;
524
525         rc = lprocfs_write_u64_helper(buffer, count, &val);
526         if (rc)
527                 return rc;
528
529         /* if the max_pages is specified in bytes, convert to pages */
530         if (val >= ONE_MB_BRW_SIZE)
531                 val >>= PAGE_CACHE_SHIFT;
532
533         LPROCFS_CLIMP_CHECK(dev);
534
535         chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_CACHE_SHIFT)) - 1);
536         /* max_pages_per_rpc must be chunk aligned */
537         val = (val + ~chunk_mask) & chunk_mask;
538         if (val == 0 || (ocd->ocd_brw_size != 0 &&
539                          val > ocd->ocd_brw_size >> PAGE_CACHE_SHIFT)) {
540                 LPROCFS_CLIMP_EXIT(dev);
541                 return -ERANGE;
542         }
543         spin_lock(&cli->cl_loi_list_lock);
544         cli->cl_max_pages_per_rpc = val;
545         client_adjust_max_dirty(cli);
546         spin_unlock(&cli->cl_loi_list_lock);
547
548         LPROCFS_CLIMP_EXIT(dev);
549         return count;
550 }
551 LPROC_SEQ_FOPS(osc_obd_max_pages_per_rpc);
552
553 static int osc_unstable_stats_seq_show(struct seq_file *m, void *v)
554 {
555         struct obd_device *dev = m->private;
556         struct client_obd *cli = &dev->u.cli;
557         long pages;
558         int mb;
559
560         pages = atomic_long_read(&cli->cl_unstable_count);
561         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
562
563         return seq_printf(m, "unstable_pages: %20ld\n"
564                           "unstable_mb:              %10d\n",
565                         pages, mb);
566 }
567 LPROC_SEQ_FOPS_RO(osc_unstable_stats);
568
569 LPROC_SEQ_FOPS_RO_TYPE(osc, uuid);
570 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
571 LPROC_SEQ_FOPS_RO_TYPE(osc, blksize);
572 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytestotal);
573 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesfree);
574 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesavail);
575 LPROC_SEQ_FOPS_RO_TYPE(osc, filestotal);
576 LPROC_SEQ_FOPS_RO_TYPE(osc, filesfree);
577 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
578 LPROC_SEQ_FOPS_RO_TYPE(osc, conn_uuid);
579 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
580 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
581
582 LPROC_SEQ_FOPS_WO_TYPE(osc, ping);
583
584 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
585 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
586
587 struct lprocfs_vars lprocfs_osc_obd_vars[] = {
588         { .name =       "uuid",
589           .fops =       &osc_uuid_fops                  },
590         { .name =       "ping",
591           .fops =       &osc_ping_fops,
592           .proc_mode =  0222                            },
593         { .name =       "connect_flags",
594           .fops =       &osc_connect_flags_fops         },
595         { .name =       "blocksize",
596           .fops =       &osc_blksize_fops               },
597         { .name =       "kbytestotal",
598           .fops =       &osc_kbytestotal_fops           },
599         { .name =       "kbytesfree",
600           .fops =       &osc_kbytesfree_fops            },
601         { .name =       "kbytesavail",
602           .fops =       &osc_kbytesavail_fops           },
603         { .name =       "filestotal",
604           .fops =       &osc_filestotal_fops            },
605         { .name =       "filesfree",
606           .fops =       &osc_filesfree_fops             },
607         { .name =       "ost_server_uuid",
608           .fops =       &osc_server_uuid_fops           },
609         { .name =       "ost_conn_uuid",
610           .fops =       &osc_conn_uuid_fops             },
611         { .name =       "active",
612           .fops =       &osc_active_fops                },
613         { .name =       "max_pages_per_rpc",
614           .fops =       &osc_obd_max_pages_per_rpc_fops },
615         { .name =       "max_rpcs_in_flight",
616           .fops =       &osc_max_rpcs_in_flight_fops    },
617         { .name =       "destroys_in_flight",
618           .fops =       &osc_destroys_in_flight_fops    },
619         { .name =       "max_dirty_mb",
620           .fops =       &osc_max_dirty_mb_fops          },
621         { .name =       "osc_cached_mb",
622           .fops =       &osc_cached_mb_fops             },
623         { .name =       "cur_dirty_bytes",
624           .fops =       &osc_cur_dirty_bytes_fops       },
625         { .name =       "cur_grant_bytes",
626           .fops =       &osc_cur_grant_bytes_fops       },
627         { .name =       "cur_lost_grant_bytes",
628           .fops =       &osc_cur_lost_grant_bytes_fops  },
629         { .name =       "grant_shrink_interval",
630           .fops =       &osc_grant_shrink_interval_fops },
631         { .name =       "checksums",
632           .fops =       &osc_checksum_fops              },
633         { .name =       "checksum_type",
634           .fops =       &osc_checksum_type_fops         },
635         { .name =       "resend_count",
636           .fops =       &osc_resend_count_fops          },
637         { .name =       "timeouts",
638           .fops =       &osc_timeouts_fops              },
639         { .name =       "contention_seconds",
640           .fops =       &osc_contention_seconds_fops    },
641         { .name =       "lockless_truncate",
642           .fops =       &osc_lockless_truncate_fops     },
643         { .name =       "import",
644           .fops =       &osc_import_fops                },
645         { .name =       "state",
646           .fops =       &osc_state_fops                 },
647         { .name =       "pinger_recov",
648           .fops =       &osc_pinger_recov_fops          },
649         { .name =       "unstable_stats",
650           .fops =       &osc_unstable_stats_fops        },
651         { NULL }
652 };
653
654 #define pct(a,b) (b ? a * 100 / b : 0)
655
656 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
657 {
658         struct timeval now;
659         struct obd_device *dev = seq->private;
660         struct client_obd *cli = &dev->u.cli;
661         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
662         int i;
663
664         do_gettimeofday(&now);
665
666         spin_lock(&cli->cl_loi_list_lock);
667
668         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
669                    now.tv_sec, now.tv_usec);
670         seq_printf(seq, "read RPCs in flight:  %d\n",
671                    cli->cl_r_in_flight);
672         seq_printf(seq, "write RPCs in flight: %d\n",
673                    cli->cl_w_in_flight);
674         seq_printf(seq, "pending write pages:  %d\n",
675                    atomic_read(&cli->cl_pending_w_pages));
676         seq_printf(seq, "pending read pages:   %d\n",
677                    atomic_read(&cli->cl_pending_r_pages));
678
679         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
680         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
681         seq_printf(seq, "       rpcs   %% cum %%\n");
682
683         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
684         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
685
686         read_cum = 0;
687         write_cum = 0;
688         for (i = 0; i < OBD_HIST_MAX; i++) {
689                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
690                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
691
692                 read_cum += r;
693                 write_cum += w;
694                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
695                            1 << i, r, pct(r, read_tot),
696                            pct(read_cum, read_tot), w,
697                            pct(w, write_tot),
698                            pct(write_cum, write_tot));
699                 if (read_cum == read_tot && write_cum == write_tot)
700                         break;
701         }
702
703         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
704         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
705         seq_printf(seq, "       rpcs   %% cum %%\n");
706
707         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
708         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
709
710         read_cum = 0;
711         write_cum = 0;
712         for (i = 0; i < OBD_HIST_MAX; i++) {
713                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
714                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
715                 read_cum += r;
716                 write_cum += w;
717                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
718                                  i, r, pct(r, read_tot),
719                                  pct(read_cum, read_tot), w,
720                                  pct(w, write_tot),
721                                  pct(write_cum, write_tot));
722                 if (read_cum == read_tot && write_cum == write_tot)
723                         break;
724         }
725
726         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
727         seq_printf(seq, "offset                rpcs   %% cum %% |");
728         seq_printf(seq, "       rpcs   %% cum %%\n");
729
730         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
731         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
732
733         read_cum = 0;
734         write_cum = 0;
735         for (i = 0; i < OBD_HIST_MAX; i++) {
736                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
737                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
738                 read_cum += r;
739                 write_cum += w;
740                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
741                            (i == 0) ? 0 : 1 << (i - 1),
742                            r, pct(r, read_tot), pct(read_cum, read_tot),
743                            w, pct(w, write_tot), pct(write_cum, write_tot));
744                 if (read_cum == read_tot && write_cum == write_tot)
745                         break;
746         }
747
748         spin_unlock(&cli->cl_loi_list_lock);
749
750         return 0;
751 }
752 #undef pct
753
754 static ssize_t osc_rpc_stats_seq_write(struct file *file,
755                                        const char __user *buf,
756                                        size_t len, loff_t *off)
757 {
758         struct seq_file *seq = file->private_data;
759         struct obd_device *dev = seq->private;
760         struct client_obd *cli = &dev->u.cli;
761
762         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
763         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
764         lprocfs_oh_clear(&cli->cl_read_page_hist);
765         lprocfs_oh_clear(&cli->cl_write_page_hist);
766         lprocfs_oh_clear(&cli->cl_read_offset_hist);
767         lprocfs_oh_clear(&cli->cl_write_offset_hist);
768
769         return len;
770 }
771 LPROC_SEQ_FOPS(osc_rpc_stats);
772
773 static int osc_stats_seq_show(struct seq_file *seq, void *v)
774 {
775         struct timeval now;
776         struct obd_device *dev = seq->private;
777         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
778
779         do_gettimeofday(&now);
780
781         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
782                    now.tv_sec, now.tv_usec);
783         seq_printf(seq, "lockless_write_bytes\t\t"LPU64"\n",
784                    stats->os_lockless_writes);
785         seq_printf(seq, "lockless_read_bytes\t\t"LPU64"\n",
786                    stats->os_lockless_reads);
787         seq_printf(seq, "lockless_truncate\t\t"LPU64"\n",
788                    stats->os_lockless_truncates);
789         return 0;
790 }
791
792 static ssize_t osc_stats_seq_write(struct file *file,
793                                    const char __user *buf,
794                                    size_t len, loff_t *off)
795 {
796         struct seq_file *seq = file->private_data;
797         struct obd_device *dev = seq->private;
798         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
799
800         memset(stats, 0, sizeof(*stats));
801         return len;
802 }
803
804 LPROC_SEQ_FOPS(osc_stats);
805
806 int lproc_osc_attach_seqstat(struct obd_device *dev)
807 {
808         int rc;
809
810         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
811                                 &osc_stats_fops, dev);
812         if (rc == 0)
813                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
814                                             &osc_rpc_stats_fops, dev);
815
816         return rc;
817 }
818 #endif /* CONFIG_PROC_FS */