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