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