Whamcloud - gitweb
allow kill process which stick in waiting statahead result.
[fs/lustre-release.git] / lustre / llite / lproc_llite.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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_LLITE
37
38 #include <linux/version.h>
39 #include <lustre_lite.h>
40 #include <lprocfs_status.h>
41 #include <linux/seq_file.h>
42 #include <obd_support.h>
43
44 #include "llite_internal.h"
45
46 struct proc_dir_entry *proc_lustre_fs_root;
47
48 #ifdef LPROCFS
49 /* /proc/lustre/llite mount point registration */
50 struct file_operations llite_dump_pgcache_fops;
51 struct file_operations ll_rw_extents_stats_fops;
52 struct file_operations ll_rw_extents_stats_pp_fops;
53 struct file_operations ll_rw_offset_stats_fops;
54
55 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
56                          int *eof, void *data)
57 {
58         struct super_block *sb = (struct super_block *)data;
59         struct obd_statfs osfs;
60         int rc;
61
62         LASSERT(sb != NULL);
63         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
64                                 OBD_STATFS_NODELAY);
65         if (!rc) {
66               *eof = 1;
67               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
68         }
69
70         return rc;
71 }
72
73 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
74                              int *eof, void *data)
75 {
76         struct super_block *sb = (struct super_block *)data;
77         struct obd_statfs osfs;
78         int rc;
79
80         LASSERT(sb != NULL);
81         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
82                                 OBD_STATFS_NODELAY);
83         if (!rc) {
84                 __u32 blk_size = osfs.os_bsize >> 10;
85                 __u64 result = osfs.os_blocks;
86
87                 while (blk_size >>= 1)
88                         result <<= 1;
89
90                 *eof = 1;
91                 rc = snprintf(page, count, LPU64"\n", result);
92         }
93         return rc;
94
95 }
96
97 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
98                             int *eof, void *data)
99 {
100         struct super_block *sb = (struct super_block *)data;
101         struct obd_statfs osfs;
102         int rc;
103
104         LASSERT(sb != NULL);
105         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
106                                 OBD_STATFS_NODELAY);
107         if (!rc) {
108                 __u32 blk_size = osfs.os_bsize >> 10;
109                 __u64 result = osfs.os_bfree;
110
111                 while (blk_size >>= 1)
112                         result <<= 1;
113
114                 *eof = 1;
115                 rc = snprintf(page, count, LPU64"\n", result);
116         }
117         return rc;
118 }
119
120 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
121                              int *eof, void *data)
122 {
123         struct super_block *sb = (struct super_block *)data;
124         struct obd_statfs osfs;
125         int rc;
126
127         LASSERT(sb != NULL);
128         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
129                                 OBD_STATFS_NODELAY);
130         if (!rc) {
131                 __u32 blk_size = osfs.os_bsize >> 10;
132                 __u64 result = osfs.os_bavail;
133
134                 while (blk_size >>= 1)
135                         result <<= 1;
136
137                 *eof = 1;
138                 rc = snprintf(page, count, LPU64"\n", result);
139         }
140         return rc;
141 }
142
143 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
144                             int *eof, void *data)
145 {
146         struct super_block *sb = (struct super_block *)data;
147         struct obd_statfs osfs;
148         int rc;
149
150         LASSERT(sb != NULL);
151         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
152                                 OBD_STATFS_NODELAY);
153         if (!rc) {
154                  *eof = 1;
155                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
156         }
157         return rc;
158 }
159
160 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
161                            int *eof, void *data)
162 {
163         struct super_block *sb = (struct super_block *)data;
164         struct obd_statfs osfs;
165         int rc;
166
167         LASSERT(sb != NULL);
168         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
169                                 OBD_STATFS_NODELAY);
170         if (!rc) {
171                  *eof = 1;
172                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
173         }
174         return rc;
175
176 }
177
178 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
179                         int *eof, void *data)
180 {
181         struct super_block *sb = (struct super_block*)data;
182
183         LASSERT(sb != NULL);
184         *eof = 1;
185         return snprintf(page, count, "%s\n", sb->s_type->name);
186 }
187
188 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
189                          int *eof, void *data)
190 {
191         struct super_block *sb = (struct super_block *)data;
192
193         LASSERT(sb != NULL);
194         *eof = 1;
195         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
196 }
197
198 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
199                                    int count, int *eof, void *data)
200 {
201         struct super_block *sb = data;
202         struct ll_sb_info *sbi = ll_s2sbi(sb);
203         long pages_number;
204         int mult;
205
206         spin_lock(&sbi->ll_lock);
207         pages_number = sbi->ll_ra_info.ra_max_pages;
208         spin_unlock(&sbi->ll_lock);
209
210         mult = 1 << (20 - CFS_PAGE_SHIFT);
211         return lprocfs_read_frac_helper(page, count, pages_number, mult);
212 }
213
214 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
215                                    unsigned long count, void *data)
216 {
217         struct super_block *sb = data;
218         struct ll_sb_info *sbi = ll_s2sbi(sb);
219         int mult, rc, pages_number;
220
221         mult = 1 << (20 - CFS_PAGE_SHIFT);
222         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
223         if (rc)
224                 return rc;
225
226         if (pages_number < 0 || pages_number > num_physpages / 2) {
227                 CERROR("can't set file readahead more than %lu MB\n",
228                         num_physpages >> (20 - CFS_PAGE_SHIFT + 1)); /*1/2 of RAM*/
229                 return -ERANGE;
230         }
231
232         spin_lock(&sbi->ll_lock);
233         sbi->ll_ra_info.ra_max_pages = pages_number;
234         spin_unlock(&sbi->ll_lock);
235
236         return count;
237 }
238
239 static int ll_rd_max_readahead_per_file_mb(char *page, char **start, off_t off,
240                                           int count, int *eof, void *data)
241 {
242         struct super_block *sb = data;
243         struct ll_sb_info *sbi = ll_s2sbi(sb);
244         long pages_number;
245         int mult;
246
247         spin_lock(&sbi->ll_lock);
248         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
249         spin_unlock(&sbi->ll_lock);
250
251         mult = 1 << (20 - CFS_PAGE_SHIFT);
252         return lprocfs_read_frac_helper(page, count, pages_number, mult);
253 }
254
255 static int ll_wr_max_readahead_per_file_mb(struct file *file, const char *buffer,
256                                           unsigned long count, void *data)
257 {
258         struct super_block *sb = data;
259         struct ll_sb_info *sbi = ll_s2sbi(sb);
260         int mult, rc, pages_number;
261
262         mult = 1 << (20 - CFS_PAGE_SHIFT);
263         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
264         if (rc)
265                 return rc;
266
267         if (pages_number < 0 ||
268                 pages_number > sbi->ll_ra_info.ra_max_pages) {
269                 CERROR("can't set file readahead more than"
270                        "max_read_ahead_mb %lu MB\n", sbi->ll_ra_info.ra_max_pages);
271                 return -ERANGE;
272         }
273
274         spin_lock(&sbi->ll_lock);
275         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
276         spin_unlock(&sbi->ll_lock);
277
278         return count;
279 }
280
281 static int ll_rd_max_read_ahead_whole_mb(char *page, char **start, off_t off,
282                                        int count, int *eof, void *data)
283 {
284         struct super_block *sb = data;
285         struct ll_sb_info *sbi = ll_s2sbi(sb);
286         long pages_number;
287         int mult;
288
289         spin_lock(&sbi->ll_lock);
290         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
291         spin_unlock(&sbi->ll_lock);
292
293         mult = 1 << (20 - CFS_PAGE_SHIFT);
294         return lprocfs_read_frac_helper(page, count, pages_number, mult);
295 }
296
297 static int ll_wr_max_read_ahead_whole_mb(struct file *file, const char *buffer,
298                                        unsigned long count, void *data)
299 {
300         struct super_block *sb = data;
301         struct ll_sb_info *sbi = ll_s2sbi(sb);
302         int mult, rc, pages_number;
303
304         mult = 1 << (20 - CFS_PAGE_SHIFT);
305         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
306         if (rc)
307                 return rc;
308
309         /* Cap this at the current max readahead window size, the readahead
310          * algorithm does this anyway so it's pointless to set it larger. */
311         if (pages_number < 0 ||
312             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
313                 CERROR("can't set max_read_ahead_whole_mb more than "
314                        "max_read_ahead_per_file_mb: %lu\n",
315                         sbi->ll_ra_info.ra_max_pages_per_file >> (20 - CFS_PAGE_SHIFT));
316                 return -ERANGE;
317         }
318
319         spin_lock(&sbi->ll_lock);
320         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
321         spin_unlock(&sbi->ll_lock);
322
323         return count;
324 }
325
326 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
327                                int count, int *eof, void *data)
328 {
329         struct super_block *sb = data;
330         struct ll_sb_info *sbi = ll_s2sbi(sb);
331         long pages_number;
332         int mult;
333
334         spin_lock(&sbi->ll_lock);
335         pages_number = sbi->ll_async_page_max;
336         spin_unlock(&sbi->ll_lock);
337
338         mult = 1 << (20 - CFS_PAGE_SHIFT);
339         return lprocfs_read_frac_helper(page, count, pages_number, mult);;
340 }
341
342 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
343                                   unsigned long count, void *data)
344 {
345         struct super_block *sb = data;
346         struct ll_sb_info *sbi = ll_s2sbi(sb);
347         unsigned long budget;
348         int mult, rc, pages_number, cpu;
349
350         mult = 1 << (20 - CFS_PAGE_SHIFT);
351         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
352         if (rc)
353                 return rc;
354
355         if (pages_number < 0 || pages_number > num_physpages) {
356                 CERROR("can't set max cache more than %lu MB\n",
357                         num_physpages >> (20 - CFS_PAGE_SHIFT));
358                 return -ERANGE;
359         }
360
361         spin_lock(&sbi->ll_lock);
362         sbi->ll_async_page_max = pages_number ;
363         spin_unlock(&sbi->ll_lock);
364
365         if (!sbi->ll_osc_exp)
366                 /* Not set up yet, don't call llap_shrink_cache */
367                 return count;
368
369         spin_lock(&sbi->ll_async_page_reblnc_lock);
370         budget = sbi->ll_async_page_max / num_online_cpus();
371         for_each_online_cpu(cpu)
372                 LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_budget = budget;
373         spin_unlock(&sbi->ll_async_page_reblnc_lock);
374
375         if (lcounter_read(&sbi->ll_async_page_count) >= sbi->ll_async_page_max)
376                 llap_shrink_cache(sbi, -1);
377
378         return count;
379 }
380
381 static int ll_rd_pgcache_balance(char *page, char **start, off_t off,
382                                  int count, int *eof, void *data)
383 {
384         struct super_block *sb = data;
385         struct ll_sb_info *sbi = ll_s2sbi(sb);
386         struct ll_pglist_data *pd;
387         unsigned long total_budget = 0;
388         int n = 0, cpu;
389
390         n += snprintf(page +n, count - n, "cpu\tpage count\tbudget"
391                       "\t\treblnc count\tgen\thit\tmiss\tcross\n");
392         for_each_online_cpu(cpu) {
393                 pd = LL_PGLIST_DATA_CPU(sbi, cpu);
394                 n += snprintf(page + n, count - n,
395                               "%d\t%-8lu\t%-8lu\t%-8lu\t%lu\t%lu\t%lu\t%lu\n",
396                               cpu, pd->llpd_count, pd->llpd_budget,
397                               pd->llpd_reblnc_count, pd->llpd_gen,
398                               pd->llpd_hit, pd->llpd_miss, pd->llpd_cross);
399                 total_budget += pd->llpd_budget;
400         }
401         n += snprintf(page + n, count - n,
402                       "Total budget: %lu, page max: %lu, rebalance cnt: %lu\n",
403                       total_budget, sbi->ll_async_page_max,
404                       sbi->ll_async_page_reblnc_count);
405         *eof = 1;
406         return n;
407 }
408
409 static int ll_rd_checksum(char *page, char **start, off_t off,
410                           int count, int *eof, void *data)
411 {
412         struct super_block *sb = data;
413         struct ll_sb_info *sbi = ll_s2sbi(sb);
414
415         return snprintf(page, count, "%u\n",
416                         (sbi->ll_flags & LL_SBI_LLITE_CHECKSUM) ? 1 : 0);
417 }
418
419 static int ll_wr_checksum(struct file *file, const char *buffer,
420                           unsigned long count, void *data)
421 {
422         struct super_block *sb = data;
423         struct ll_sb_info *sbi = ll_s2sbi(sb);
424         int val, rc;
425
426         if (!sbi->ll_osc_exp)
427                 /* Not set up yet */
428                 return -EAGAIN;
429
430         rc = lprocfs_write_helper(buffer, count, &val);
431         if (rc)
432                 return rc;
433         if (val)
434                 sbi->ll_flags |=  (LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
435         else
436                 sbi->ll_flags &= ~(LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
437
438         rc = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM), KEY_CHECKSUM,
439                                 sizeof(val), &val, NULL);
440         if (rc)
441                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
442
443         return count;
444 }
445
446 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
447                           int count, int *eof, void *data)
448 {
449         struct super_block *sb = data;
450
451         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
452 }
453
454 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
455                           unsigned long count, void *data)
456 {
457         struct super_block *sb = data;
458         int rc, val;
459
460         rc = lprocfs_write_helper(buffer, count, &val);
461         if (rc)
462                 return rc;
463         ll_s2sbi(sb)->ll_max_rw_chunk = val;
464         return count;
465 }
466
467 static int ll_rd_track_id(char *page, int count, void *data,
468                           enum stats_track_type type)
469 {
470         struct super_block *sb = data;
471
472         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
473                 return snprintf(page, count, "%d\n",
474                                 ll_s2sbi(sb)->ll_stats_track_id);
475
476         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
477                 return snprintf(page, count, "0 (all)\n");
478         } else {
479                 return snprintf(page, count, "untracked\n");
480         }
481 }
482
483 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
484                           enum stats_track_type type)
485 {
486         struct super_block *sb = data;
487         int rc, pid;
488
489         rc = lprocfs_write_helper(buffer, count, &pid);
490         if (rc)
491                 return rc;
492         ll_s2sbi(sb)->ll_stats_track_id = pid;
493         if (pid == 0)
494                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
495         else
496                 ll_s2sbi(sb)->ll_stats_track_type = type;
497         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
498         return count;
499 }
500
501 static int ll_rd_track_pid(char *page, char **start, off_t off,
502                           int count, int *eof, void *data)
503 {
504         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
505 }
506
507 static int ll_wr_track_pid(struct file *file, const char *buffer,
508                           unsigned long count, void *data)
509 {
510         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
511 }
512
513 static int ll_rd_track_ppid(char *page, char **start, off_t off,
514                           int count, int *eof, void *data)
515 {
516         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
517 }
518
519 static int ll_wr_track_ppid(struct file *file, const char *buffer,
520                           unsigned long count, void *data)
521 {
522         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
523 }
524
525 static int ll_rd_track_gid(char *page, char **start, off_t off,
526                           int count, int *eof, void *data)
527 {
528         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
529 }
530
531 static int ll_wr_track_gid(struct file *file, const char *buffer,
532                           unsigned long count, void *data)
533 {
534         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
535 }
536
537 static int ll_rd_contention_time(char *page, char **start, off_t off,
538                                  int count, int *eof, void *data)
539 {
540         struct super_block *sb = data;
541
542         *eof = 1;
543         return snprintf(page, count, "%u\n", ll_s2sbi(sb)->ll_contention_time);
544
545 }
546
547 static int ll_wr_contention_time(struct file *file, const char *buffer,
548                                  unsigned long count, void *data)
549 {
550         struct super_block *sb = data;
551         struct ll_sb_info *sbi = ll_s2sbi(sb);
552
553         return lprocfs_write_helper(buffer, count,&sbi->ll_contention_time) ?:
554                 count;
555 }
556
557 static int ll_rd_lockless_truncate(char *page, char **start, off_t off,
558                                    int count, int *eof, void *data)
559 {
560         struct super_block *sb = data;
561
562         *eof = 1;
563         return snprintf(page, count, "%u\n",
564                         ll_s2sbi(sb)->ll_lockless_truncate_enable);
565 }
566
567 static int ll_wr_lockless_truncate(struct file *file, const char *buffer,
568                                    unsigned long count, void *data)
569 {
570         struct super_block *sb = data;
571         struct ll_sb_info *sbi = ll_s2sbi(sb);
572
573         return lprocfs_write_helper(buffer, count,
574                                     &sbi->ll_lockless_truncate_enable)
575                 ?: count;
576 }
577
578 static int ll_rd_statahead_max(char *page, char **start, off_t off,
579                                int count, int *eof, void *data)
580 {
581         struct super_block *sb = data;
582         struct ll_sb_info *sbi = ll_s2sbi(sb);
583
584         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
585 }
586
587 static int ll_wr_statahead_max(struct file *file, const char *buffer,
588                                unsigned long count, void *data)
589 {
590         struct super_block *sb = data;
591         struct ll_sb_info *sbi = ll_s2sbi(sb);
592         int val, rc;
593
594         rc = lprocfs_write_helper(buffer, count, &val);
595         if (rc)
596                 return rc;
597
598         if (val >= 0 && val <= LL_SA_RPC_MAX)
599                 sbi->ll_sa_max = val;
600         else
601                 CERROR("Bad statahead_max value %d. Valid values are in the "
602                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
603
604         return count;
605 }
606
607 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
608                                  int count, int *eof, void *data)
609 {
610         struct super_block *sb = data;
611         struct ll_sb_info *sbi = ll_s2sbi(sb);
612
613         return snprintf(page, count,
614                         "statahead wrong: %u\n"
615                         "statahead total: %u\n"
616                         "ls blocked:      %llu\n"
617                         "ls cached:       %llu\n"
618                         "hit count:       %llu\n"
619                         "miss count:      %llu\n",
620                         sbi->ll_sa_wrong,
621                         sbi->ll_sa_total,
622                         sbi->ll_sa_blocked,
623                         sbi->ll_sa_cached,
624                         sbi->ll_sa_hit,
625                         sbi->ll_sa_miss);
626 }
627
628 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
629         { "uuid",         ll_rd_sb_uuid,          0, 0 },
630         //{ "mntpt_path",   ll_rd_path,             0, 0 },
631         { "fstype",       ll_rd_fstype,           0, 0 },
632         { "blocksize",    ll_rd_blksize,          0, 0 },
633         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
634         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
635         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
636         { "filestotal",   ll_rd_filestotal,       0, 0 },
637         { "filesfree",    ll_rd_filesfree,        0, 0 },
638         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
639         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
640                                ll_wr_max_readahead_mb, 0 },
641         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
642                                         ll_wr_max_readahead_per_file_mb, 0 },
643         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
644                                      ll_wr_max_read_ahead_whole_mb, 0 },
645         { "max_cached_mb",  ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
646         { "pgcache_balance",ll_rd_pgcache_balance, 0, 0 },
647         { "checksum_pages", ll_rd_checksum, ll_wr_checksum, 0 },
648         { "max_rw_chunk",   ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
649         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
650         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
651         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
652         { "contention_seconds", ll_rd_contention_time,
653                                 ll_wr_contention_time, 0},
654         { "lockless_truncate", ll_rd_lockless_truncate,
655                                ll_wr_lockless_truncate, 0},
656         { "statahead_max",      ll_rd_statahead_max, ll_wr_statahead_max, 0 },
657         { "statahead_stats",    ll_rd_statahead_stats, 0, 0 },
658         { 0 }
659 };
660
661 #define MAX_STRING_SIZE 128
662
663 struct llite_file_opcode {
664         __u32       opcode;
665         __u32       type;
666         const char *opname;
667 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
668         /* file operation */
669         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
670         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
671         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
672                                    "writeback_from_writepage" },
673         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
674                                    "writeback_from_pressure" },
675         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
676                                    "writeback_ok_pages" },
677         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
678                                    "writeback_failed_pages" },
679         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
680                                    "read_bytes" },
681         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
682                                    "write_bytes" },
683         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
684                                    "brw_read" },
685         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
686                                    "brw_write" },
687
688         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
689         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
690         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
691         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
692         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
693         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
694         /* inode operation */
695         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
696         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
697         { LPROC_LL_LOCKLESS_TRUNC, LPROCFS_TYPE_REGS, "lockless_truncate" },
698         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
699         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
700         /* special inode operation */
701         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
702         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
703         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
704         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
705         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
706         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
707         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
708         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
709                                    "direct_read" },
710         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
711                                    "direct_write" },
712         { LPROC_LL_LOCKLESS_READ,  LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
713                                    "lockless_read_bytes" },
714         { LPROC_LL_LOCKLESS_WRITE, LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
715                                    "lockless_write_bytes" },
716
717 };
718
719 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
720 {
721         if (!sbi->ll_stats)
722                 return;
723         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
724                 lprocfs_counter_add(sbi->ll_stats, op, count);
725         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
726                  sbi->ll_stats_track_id == current->pid)
727                 lprocfs_counter_add(sbi->ll_stats, op, count);
728         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
729                  sbi->ll_stats_track_id == current->p_pptr->pid)
730                 lprocfs_counter_add(sbi->ll_stats, op, count);
731         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
732                  sbi->ll_stats_track_id == current->gid)
733                 lprocfs_counter_add(sbi->ll_stats, op, count);
734 }
735 EXPORT_SYMBOL(ll_stats_ops_tally);
736
737 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
738                                 struct super_block *sb, char *osc, char *mdc)
739 {
740         struct lprocfs_vars lvars[2];
741         struct lustre_sb_info *lsi = s2lsi(sb);
742         struct ll_sb_info *sbi = ll_s2sbi(sb);
743         struct obd_device *obd;
744         char name[MAX_STRING_SIZE + 1], *ptr;
745         int err, id, len;
746         struct proc_dir_entry *entry;
747         static const char *ra_stats_string[] = LL_RA_STAT_STRINGS;
748         ENTRY;
749
750         memset(lvars, 0, sizeof(lvars));
751
752         name[MAX_STRING_SIZE] = '\0';
753         lvars[0].name = name;
754
755         LASSERT(sbi != NULL);
756         LASSERT(mdc != NULL);
757         LASSERT(osc != NULL);
758
759         /* Get fsname */
760         len = strlen(lsi->lsi_lmd->lmd_profile);
761         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
762         if (ptr && (strcmp(ptr, "-client") == 0))
763                 len -= 7;
764
765         /* Mount info */
766         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
767                  lsi->lsi_lmd->lmd_profile, sb);
768
769         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
770         if (IS_ERR(sbi->ll_proc_root)) {
771                 err = PTR_ERR(sbi->ll_proc_root);
772                 sbi->ll_proc_root = NULL;
773                 RETURN(err);
774         }
775
776         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
777         if (entry == NULL)
778                 GOTO(out, err = -ENOMEM);
779         entry->proc_fops = &llite_dump_pgcache_fops;
780         entry->data = sbi;
781
782         sbi->ll_ra_stats = lprocfs_alloc_stats(LL_RA_STAT,
783                                                LPROCFS_STATS_FLAG_NONE);
784         for (id = 0; id < LL_RA_STAT; id++)
785                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
786                         ra_stats_string[id], "pages");
787         lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
788                 sbi->ll_ra_stats);
789
790         entry = create_proc_entry("extents_stats", 0644, sbi->ll_proc_root);
791         if (entry == NULL)
792                  GOTO(out, err = -ENOMEM);
793         entry->proc_fops = &ll_rw_extents_stats_fops;
794         entry->data = sbi;
795
796         entry = create_proc_entry("extents_stats_per_process", 0644,
797                                   sbi->ll_proc_root);
798         if (entry == NULL)
799                  GOTO(out, err = -ENOMEM);
800         entry->proc_fops = &ll_rw_extents_stats_pp_fops;
801         entry->data = sbi;
802
803         entry = create_proc_entry("offset_stats", 0644, sbi->ll_proc_root);
804         if (entry == NULL)
805                 GOTO(out, err = -ENOMEM);
806         entry->proc_fops = &ll_rw_offset_stats_fops;
807         entry->data = sbi;
808
809         /* File operations stats */
810         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
811                                             LPROCFS_STATS_FLAG_NONE);
812         if (sbi->ll_stats == NULL)
813                 GOTO(out, err = -ENOMEM);
814         /* do counter init */
815         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
816                 __u32 type = llite_opcode_table[id].type;
817                 void *ptr = NULL;
818                 if (type & LPROCFS_TYPE_REGS)
819                         ptr = "regs";
820                 else if (type & LPROCFS_TYPE_BYTES)
821                         ptr = "bytes";
822                 else if (type & LPROCFS_TYPE_PAGES)
823                         ptr = "pages";
824                 lprocfs_counter_init(sbi->ll_stats,
825                                      llite_opcode_table[id].opcode,
826                                      (type & LPROCFS_CNTR_AVGMINMAX),
827                                      llite_opcode_table[id].opname, ptr);
828         }
829         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
830         if (err)
831                 GOTO(out, err);
832
833         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
834         if (err)
835                 GOTO(out, err);
836
837         /* MDC info */
838         obd = class_name2obd(mdc);
839
840         LASSERT(obd != NULL);
841         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
842         LASSERT(obd->obd_type->typ_name != NULL);
843
844         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
845                  obd->obd_type->typ_name);
846         lvars[0].read_fptr = lprocfs_rd_name;
847         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
848         if (err)
849                 GOTO(out, err);
850
851         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
852         lvars[0].read_fptr = lprocfs_rd_uuid;
853         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
854         if (err)
855                 GOTO(out, err);
856
857         /* OSC */
858         obd = class_name2obd(osc);
859
860         LASSERT(obd != NULL);
861         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
862         LASSERT(obd->obd_type->typ_name != NULL);
863
864         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
865                  obd->obd_type->typ_name);
866         lvars[0].read_fptr = lprocfs_rd_name;
867         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
868         if (err)
869                 GOTO(out, err);
870
871         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
872         lvars[0].read_fptr = lprocfs_rd_uuid;
873         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
874 out:
875         if (err) {
876                 lprocfs_remove(&sbi->ll_proc_root);
877                 lprocfs_free_stats(&sbi->ll_stats);
878         }
879         RETURN(err);
880 }
881
882 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
883 {
884         if (sbi->ll_proc_root) {
885                 lprocfs_remove(&sbi->ll_proc_root);
886                 lprocfs_free_stats(&sbi->ll_ra_stats);
887                 lprocfs_free_stats(&sbi->ll_stats);
888         }
889 }
890 #undef MAX_STRING_SIZE
891
892 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
893                 if (test_bit(PG_##flag, &(page)->flags)) {              \
894                         if (!has_flags)                                 \
895                                 has_flags = 1;                          \
896                         else                                            \
897                                 seq_putc(seq, '|');                     \
898                         seq_puts(seq, #flag);                           \
899                 }                                                       \
900         } while(0);
901
902 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
903 {
904         struct ll_async_page *dummy_llap = seq->private;
905
906         if (dummy_llap->llap_magic == 2)
907                 return NULL;
908
909         return (void *)1;
910 }
911
912 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
913 {
914         struct ll_async_page *llap, *dummy_llap = seq->private;
915         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
916         struct ll_pglist_data *pd;
917         int cpu = dummy_llap->llap_pglist_cpu;
918
919         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
920          * it in our own state */
921         if (dummy_llap->llap_magic == 0) {
922                 seq_printf(seq, "gener |  llap  cookie  origin wq du wb | page "
923                                 "inode index count [ page flags ]\n");
924                 return 0;
925         }
926
927         pd = ll_pglist_cpu_lock(sbi, cpu);
928         llap = llite_pglist_next_llap(&pd->llpd_list,
929                                       &dummy_llap->llap_pglist_item);
930         if (llap != NULL)  {
931                 int has_flags = 0, i;
932                 struct page *page = llap->llap_page;
933                 unsigned long gen = 0UL;
934
935                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
936                          llap->llap_origin);
937
938                 for_each_online_cpu(i)
939                          gen += LL_PGLIST_DATA_CPU(sbi, i)->llpd_gen;
940
941                 seq_printf(seq," %5lu | %p %p %s %s %s %s | %p %lu/%u(%p) "
942                            "%lu %u [",
943                            gen,
944                            llap, llap->llap_cookie,
945                            llap_origins[llap->llap_origin],
946                            llap->llap_write_queued ? "wq" : "- ",
947                            llap->llap_defer_uptodate ? "du" : "- ",
948                            PageWriteback(page) ? "wb" : "-",
949                            page, page->mapping->host->i_ino,
950                            page->mapping->host->i_generation,
951                            page->mapping->host, page->index,
952                            page_count(page));
953                 seq_page_flag(seq, page, locked, has_flags);
954                 seq_page_flag(seq, page, error, has_flags);
955                 seq_page_flag(seq, page, referenced, has_flags);
956                 seq_page_flag(seq, page, uptodate, has_flags);
957                 seq_page_flag(seq, page, dirty, has_flags);
958 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
959                 seq_page_flag(seq, page, highmem, has_flags);
960 #endif
961                 seq_page_flag(seq, page, writeback, has_flags);
962                 if (!has_flags)
963                         seq_puts(seq, "-]\n");
964                 else
965                         seq_puts(seq, "]\n");
966         }
967         ll_pglist_cpu_unlock(sbi, cpu);
968
969         return 0;
970 }
971
972 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v,
973                                          loff_t *pos)
974 {
975         struct ll_async_page *llap, *dummy_llap = seq->private;
976         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
977         struct ll_pglist_data *pd, *next;
978         int cpu = dummy_llap->llap_pglist_cpu;
979
980         /* bail if we just displayed the banner */
981         if (dummy_llap->llap_magic == 0) {
982                 dummy_llap->llap_magic = 1;
983                 return dummy_llap;
984         }
985
986         /* we've just displayed the llap that is after us in the list.
987          * we advance to a position beyond it, returning null if there
988          * isn't another llap in the list beyond that new position. */
989         pd = ll_pglist_cpu_lock(sbi, cpu);
990         llap = llite_pglist_next_llap(&pd->llpd_list,
991                         &dummy_llap->llap_pglist_item);
992         list_del_init(&dummy_llap->llap_pglist_item);
993         if (llap) {
994                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
995                 llap = llite_pglist_next_llap(&pd->llpd_list,
996                                 &dummy_llap->llap_pglist_item);
997         }
998         if (llap == NULL) {
999                 int i = cpu + 1;
1000                 for (next = NULL; i < num_possible_cpus(); i++, next = NULL) {
1001                         next = ll_pglist_cpu_lock(sbi, i);
1002                         if (!list_empty(&next->llpd_list))
1003                                 break;
1004                         ll_pglist_cpu_unlock(sbi, i);
1005                 }
1006                 if (next != NULL) {
1007                         list_move(&dummy_llap->llap_pglist_item,
1008                                   &next->llpd_list);
1009                         dummy_llap->llap_pglist_cpu = i;
1010                         ll_pglist_cpu_unlock(sbi, cpu);
1011                         llap = llite_pglist_next_llap(&next->llpd_list,
1012                                         &dummy_llap->llap_pglist_item);
1013                         LASSERT(llap);
1014                         cpu = i;
1015                 }
1016         }
1017         ll_pglist_cpu_unlock(sbi, cpu);
1018
1019         ++*pos;
1020         if (llap == NULL) {
1021                 dummy_llap->llap_magic = 2;
1022                 return NULL;
1023         }
1024         return dummy_llap;
1025 }
1026
1027 static void null_stop(struct seq_file *seq, void *v)
1028 {
1029 }
1030
1031 struct seq_operations llite_dump_pgcache_seq_sops = {
1032         .start = llite_dump_pgcache_seq_start,
1033         .stop = null_stop,
1034         .next = llite_dump_pgcache_seq_next,
1035         .show = llite_dump_pgcache_seq_show,
1036 };
1037
1038 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
1039  * while we walk the entire list, and we don't want to have to seek into
1040  * the right position in the list as an app advances with many syscalls.  we
1041  * allocate a dummy llap and hang it off file->private.  its position in
1042  * the list records where the app is currently displaying.  this way our
1043  * seq .start and .stop don't actually do anything.  .next returns null
1044  * when the dummy hits the end of the list which eventually leads to .release
1045  * where we tear down.  this kind of displaying is super-racey, so we put
1046  * a generation counter on the list so the output shows when the list
1047  * changes between reads.
1048  */
1049 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
1050 {
1051         struct proc_dir_entry *dp = PDE(inode);
1052         struct ll_async_page *dummy_llap;
1053         struct seq_file *seq;
1054         struct ll_sb_info *sbi = dp->data;
1055         struct ll_pglist_data *pd;
1056         int rc = -ENOMEM;
1057
1058         LPROCFS_ENTRY_AND_CHECK(dp);
1059
1060         OBD_ALLOC_PTR_WAIT(dummy_llap);
1061         if (dummy_llap == NULL)
1062                 GOTO(out, rc);
1063
1064         dummy_llap->llap_page = NULL;
1065         dummy_llap->llap_cookie = sbi;
1066         dummy_llap->llap_magic = 0;
1067         dummy_llap->llap_pglist_cpu = 0;
1068
1069         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
1070         if (rc) {
1071                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1072                 GOTO(out, rc);
1073         }
1074         seq = file->private_data;
1075         seq->private = dummy_llap;
1076
1077         pd = ll_pglist_cpu_lock(sbi, 0);
1078         list_add(&dummy_llap->llap_pglist_item, &pd->llpd_list);
1079         ll_pglist_cpu_unlock(sbi, 0);
1080
1081 out:
1082         if (rc)
1083                 LPROCFS_EXIT();
1084         return rc;
1085 }
1086
1087 static int llite_dump_pgcache_seq_release(struct inode *inode,
1088                                           struct file *file)
1089 {
1090         struct seq_file *seq = file->private_data;
1091         struct ll_async_page *dummy_llap = seq->private;
1092         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
1093         int cpu = dummy_llap->llap_pglist_cpu;
1094
1095         ll_pglist_cpu_lock(sbi, cpu);
1096         if (!list_empty(&dummy_llap->llap_pglist_item))
1097                 list_del_init(&dummy_llap->llap_pglist_item);
1098         ll_pglist_cpu_unlock(sbi, cpu);
1099         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1100
1101         return lprocfs_seq_release(inode, file);
1102 }
1103
1104 struct file_operations llite_dump_pgcache_fops = {
1105         .owner   = THIS_MODULE,
1106         .open    = llite_dump_pgcache_seq_open,
1107         .read    = seq_read,
1108         .release = llite_dump_pgcache_seq_release,
1109 };
1110
1111 #define pct(a,b) (b ? a * 100 / b : 0)
1112
1113 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1114                                    struct seq_file *seq, int which)
1115 {
1116         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1117         unsigned long start, end, r, w;
1118         char *unitp = "KMGTPEZY";
1119         int i, units = 10;
1120         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1121
1122         read_cum = 0;
1123         write_cum = 0;
1124         start = 0;
1125
1126         for(i = 0; i < LL_HIST_MAX; i++) {
1127                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1128                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1129         }
1130
1131         for(i = 0; i < LL_HIST_MAX; i++) {
1132                 r = pp_info->pp_r_hist.oh_buckets[i];
1133                 w = pp_info->pp_w_hist.oh_buckets[i];
1134                 read_cum += r;
1135                 write_cum += w;
1136                 end = 1 << (i + LL_HIST_START - units);
1137                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1138                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1139                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1140                            r, pct(r, read_tot), pct(read_cum, read_tot),
1141                            w, pct(w, write_tot), pct(write_cum, write_tot));
1142                 start = end;
1143                 if (start == 1<<10) {
1144                         start = 1;
1145                         units += 10;
1146                         unitp++;
1147                 }
1148                 if (read_cum == read_tot && write_cum == write_tot)
1149                         break;
1150         }
1151 }
1152
1153 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1154 {
1155         struct timeval now;
1156         struct ll_sb_info *sbi = seq->private;
1157         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1158         int k;
1159
1160         do_gettimeofday(&now);
1161
1162         if (!sbi->ll_rw_stats_on) {
1163                 seq_printf(seq, "disabled\n"
1164                                 "write anything in this file to activate, "
1165                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1166                 return 0;
1167         }
1168         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1169                    now.tv_sec, now.tv_usec);
1170         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1171         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1172                    "extents", "calls", "%", "cum%",
1173                    "calls", "%", "cum%");
1174         spin_lock(&sbi->ll_pp_extent_lock);
1175         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1176                 if(io_extents->pp_extents[k].pid != 0) {
1177                         seq_printf(seq, "\nPID: %d\n",
1178                                    io_extents->pp_extents[k].pid);
1179                         ll_display_extents_info(io_extents, seq, k);
1180                 }
1181         }
1182         spin_unlock(&sbi->ll_pp_extent_lock);
1183         return 0;
1184 }
1185
1186 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1187                                                 const char *buf, size_t len,
1188                                                 loff_t *off)
1189 {
1190         struct seq_file *seq = file->private_data;
1191         struct ll_sb_info *sbi = seq->private;
1192         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1193         int i;
1194         int value = 1, rc = 0;
1195
1196         rc = lprocfs_write_helper(buf, len, &value);
1197         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1198                        strcmp(buf, "Disabled") == 0))
1199                 value = 0;
1200
1201         if (value == 0)
1202                 sbi->ll_rw_stats_on = 0;
1203         else
1204                 sbi->ll_rw_stats_on = 1;
1205
1206         spin_lock(&sbi->ll_pp_extent_lock);
1207         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1208                 io_extents->pp_extents[i].pid = 0;
1209                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1210                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1211         }
1212         spin_unlock(&sbi->ll_pp_extent_lock);
1213         return len;
1214 }
1215
1216 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1217
1218 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1219 {
1220         struct timeval now;
1221         struct ll_sb_info *sbi = seq->private;
1222         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1223
1224         do_gettimeofday(&now);
1225
1226         if (!sbi->ll_rw_stats_on) {
1227                 seq_printf(seq, "disabled\n"
1228                                 "write anything in this file to activate, "
1229                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1230                 return 0;
1231         }
1232         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1233                    now.tv_sec, now.tv_usec);
1234
1235         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1236         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1237                    "extents", "calls", "%", "cum%",
1238                    "calls", "%", "cum%");
1239         spin_lock(&sbi->ll_lock);
1240         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1241         spin_unlock(&sbi->ll_lock);
1242
1243         return 0;
1244 }
1245
1246 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1247                                         size_t len, loff_t *off)
1248 {
1249         struct seq_file *seq = file->private_data;
1250         struct ll_sb_info *sbi = seq->private;
1251         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1252         int i;
1253         int value = 1, rc = 0;
1254
1255         rc = lprocfs_write_helper(buf, len, &value);
1256         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1257                        strcmp(buf, "Disabled") == 0))
1258                 value = 0;
1259
1260         if (value == 0)
1261                 sbi->ll_rw_stats_on = 0;
1262         else
1263                 sbi->ll_rw_stats_on = 1;
1264         spin_lock(&sbi->ll_pp_extent_lock);
1265         for(i = 0; i <= LL_PROCESS_HIST_MAX; i++)
1266         {
1267                 io_extents->pp_extents[i].pid = 0;
1268                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1269                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1270         }
1271         spin_unlock(&sbi->ll_pp_extent_lock);
1272
1273         return len;
1274 }
1275
1276 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1277
1278 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1279                                *file, size_t count, int rw)
1280 {
1281         int i, cur = -1;
1282         struct ll_rw_process_info *process;
1283         struct ll_rw_process_info *offset;
1284         int *off_count = &sbi->ll_rw_offset_entry_count;
1285         int *process_count = &sbi->ll_offset_process_count;
1286         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1287
1288         if(!sbi->ll_rw_stats_on)
1289                 return;
1290         process = sbi->ll_rw_process_info;
1291         offset = sbi->ll_rw_offset_info;
1292
1293         spin_lock(&sbi->ll_pp_extent_lock);
1294         /* Extent statistics */
1295         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1296                 if(io_extents->pp_extents[i].pid == pid) {
1297                         cur = i;
1298                         break;
1299                 }
1300         }
1301
1302         if (cur == -1) {
1303                 /* new process */
1304                 sbi->ll_extent_process_count =
1305                         (sbi->ll_extent_process_count+1) % LL_PROCESS_HIST_MAX;
1306                 cur = sbi->ll_extent_process_count;
1307                 io_extents->pp_extents[cur].pid = pid;
1308                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1309                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1310         }
1311
1312         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1313              (i < (LL_HIST_MAX - 1)); i++);
1314         if (rw == 0) {
1315                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1316                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1317         } else {
1318                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1319                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1320         }
1321         spin_unlock(&sbi->ll_pp_extent_lock);
1322
1323         spin_lock(&sbi->ll_process_lock);
1324         /* Offset statistics */
1325         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1326                 if (process[i].rw_pid == pid) {
1327                         if (process[i].rw_last_file != file) {
1328                                 process[i].rw_range_start = file->f_pos;
1329                                 process[i].rw_last_file_pos =
1330                                                         file->f_pos + count;
1331                                 process[i].rw_smallest_extent = count;
1332                                 process[i].rw_largest_extent = count;
1333                                 process[i].rw_offset = 0;
1334                                 process[i].rw_last_file = file;
1335                                 spin_unlock(&sbi->ll_process_lock);
1336                                 return;
1337                         }
1338                         if (process[i].rw_last_file_pos != file->f_pos) {
1339                                 *off_count =
1340                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1341                                 offset[*off_count].rw_op = process[i].rw_op;
1342                                 offset[*off_count].rw_pid = pid;
1343                                 offset[*off_count].rw_range_start =
1344                                         process[i].rw_range_start;
1345                                 offset[*off_count].rw_range_end =
1346                                         process[i].rw_last_file_pos;
1347                                 offset[*off_count].rw_smallest_extent =
1348                                         process[i].rw_smallest_extent;
1349                                 offset[*off_count].rw_largest_extent =
1350                                         process[i].rw_largest_extent;
1351                                 offset[*off_count].rw_offset =
1352                                         process[i].rw_offset;
1353                                 process[i].rw_op = rw;
1354                                 process[i].rw_range_start = file->f_pos;
1355                                 process[i].rw_smallest_extent = count;
1356                                 process[i].rw_largest_extent = count;
1357                                 process[i].rw_offset = file->f_pos -
1358                                         process[i].rw_last_file_pos;
1359                         }
1360                         if(process[i].rw_smallest_extent > count)
1361                                 process[i].rw_smallest_extent = count;
1362                         if(process[i].rw_largest_extent < count)
1363                                 process[i].rw_largest_extent = count;
1364                         process[i].rw_last_file_pos = file->f_pos + count;
1365                         spin_unlock(&sbi->ll_process_lock);
1366                         return;
1367                 }
1368         }
1369         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1370         process[*process_count].rw_pid = pid;
1371         process[*process_count].rw_op = rw;
1372         process[*process_count].rw_range_start = file->f_pos;
1373         process[*process_count].rw_last_file_pos = file->f_pos + count;
1374         process[*process_count].rw_smallest_extent = count;
1375         process[*process_count].rw_largest_extent = count;
1376         process[*process_count].rw_offset = 0;
1377         process[*process_count].rw_last_file = file;
1378         spin_unlock(&sbi->ll_process_lock);
1379 }
1380
1381 char lpszt[] = LPSZ;
1382
1383 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1384 {
1385         struct timeval now;
1386         struct ll_sb_info *sbi = seq->private;
1387         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1388         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1389         char format[50];
1390         int i;
1391
1392         do_gettimeofday(&now);
1393
1394         if (!sbi->ll_rw_stats_on) {
1395                 seq_printf(seq, "disabled\n"
1396                                 "write anything in this file to activate, "
1397                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1398                 return 0;
1399         }
1400         spin_lock(&sbi->ll_process_lock);
1401
1402         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1403                    now.tv_sec, now.tv_usec);
1404         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1405                    "R/W", "PID", "RANGE START", "RANGE END",
1406                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1407         sprintf(format, "%s%s%s%s%s\n",
1408                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1409         /* We stored the discontiguous offsets here; print them first */
1410         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1411                 if (offset[i].rw_pid != 0)
1412                         /* Is there a way to snip the '%' off of LPSZ? */
1413                         seq_printf(seq, format,
1414                                    offset[i].rw_op ? 'W' : 'R',
1415                                    offset[i].rw_pid,
1416                                    offset[i].rw_range_start,
1417                                    offset[i].rw_range_end,
1418                                    offset[i].rw_smallest_extent,
1419                                    offset[i].rw_largest_extent,
1420                                    offset[i].rw_offset);
1421         }
1422         /* Then print the current offsets for each process */
1423         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1424                 if (process[i].rw_pid != 0)
1425                         seq_printf(seq, format,
1426                                    process[i].rw_op ? 'W' : 'R',
1427                                    process[i].rw_pid,
1428                                    process[i].rw_range_start,
1429                                    process[i].rw_last_file_pos,
1430                                    process[i].rw_smallest_extent,
1431                                    process[i].rw_largest_extent,
1432                                    process[i].rw_offset);
1433         }
1434         spin_unlock(&sbi->ll_process_lock);
1435
1436         return 0;
1437 }
1438
1439 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1440                                        size_t len, loff_t *off)
1441 {
1442         struct seq_file *seq = file->private_data;
1443         struct ll_sb_info *sbi = seq->private;
1444         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1445         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1446         int value = 1, rc = 0;
1447
1448         rc = lprocfs_write_helper(buf, len, &value);
1449
1450         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1451                            strcmp(buf, "Disabled") == 0))
1452                 value = 0;
1453
1454         if (value == 0)
1455                 sbi->ll_rw_stats_on = 0;
1456         else
1457                 sbi->ll_rw_stats_on = 1;
1458
1459         spin_lock(&sbi->ll_process_lock);
1460         sbi->ll_offset_process_count = 0;
1461         sbi->ll_rw_offset_entry_count = 0;
1462         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1463                LL_PROCESS_HIST_MAX);
1464         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1465                LL_OFFSET_HIST_MAX);
1466         spin_unlock(&sbi->ll_process_lock);
1467
1468         return len;
1469 }
1470
1471 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1472
1473 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1474 {
1475     lvars->module_vars  = NULL;
1476     lvars->obd_vars     = lprocfs_llite_obd_vars;
1477 }
1478 #endif /* LPROCFS */