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