Whamcloud - gitweb
Branch HEAD
[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_statahead_max(char *page, char **start, off_t off,
464                                int count, int *eof, void *data)
465 {
466         struct super_block *sb = data;
467         struct ll_sb_info *sbi = ll_s2sbi(sb);
468
469         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
470 }
471
472 static int ll_wr_statahead_max(struct file *file, const char *buffer,
473                                unsigned long count, void *data)
474 {
475         struct super_block *sb = data;
476         struct ll_sb_info *sbi = ll_s2sbi(sb);
477         int val, rc;
478
479         rc = lprocfs_write_helper(buffer, count, &val);
480         if (rc)
481                 return rc;
482
483         if (val >= 0 && val <= LL_SA_RPC_MAX)
484                 sbi->ll_sa_max = val;
485         else
486                 CERROR("Bad statahead_max value %d. Valid values are in the "
487                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
488
489         return count;
490 }
491
492 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
493                                  int count, int *eof, void *data)
494 {
495         struct super_block *sb = data;
496         struct ll_sb_info *sbi = ll_s2sbi(sb);
497
498         return snprintf(page, count,
499                         "statahead wrong: %u\n"
500                         "statahead total: %u\n"
501                         "ls blocked:      %llu\n"
502                         "ls cached:       %llu\n"
503                         "hit count:       %llu\n"
504                         "miss count:      %llu\n",
505                         sbi->ll_sa_wrong,
506                         sbi->ll_sa_total,
507                         sbi->ll_sa_blocked,
508                         sbi->ll_sa_cached,
509                         sbi->ll_sa_hit,
510                         sbi->ll_sa_miss);
511 }
512
513 static int ll_rd_contention_time(char *page, char **start, off_t off,
514                                  int count, int *eof, void *data)
515 {
516         struct super_block *sb = data;
517
518         *eof = 1;
519         return snprintf(page, count, "%u\n", ll_s2sbi(sb)->ll_contention_time);
520
521 }
522
523 static int ll_wr_contention_time(struct file *file, const char *buffer,
524                                  unsigned long count, void *data)
525 {
526         struct super_block *sb = data;
527         struct ll_sb_info *sbi = ll_s2sbi(sb);
528
529         return lprocfs_write_helper(buffer, count,&sbi->ll_contention_time) ?:
530                 count;
531 }
532
533 static int ll_rd_lockless_truncate(char *page, char **start, off_t off,
534                                    int count, int *eof, void *data)
535 {
536         struct super_block *sb = data;
537
538         *eof = 1;
539         return snprintf(page, count, "%u\n",
540                         ll_s2sbi(sb)->ll_lockless_truncate_enable);
541 }
542
543 static int ll_wr_lockless_truncate(struct file *file, const char *buffer,
544                                    unsigned long count, void *data)
545 {
546         struct super_block *sb = data;
547         struct ll_sb_info *sbi = ll_s2sbi(sb);
548
549         return lprocfs_write_helper(buffer, count,
550                                     &sbi->ll_lockless_truncate_enable)
551                                     ?: count;
552 }
553
554 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
555         { "uuid",         ll_rd_sb_uuid,          0, 0 },
556         //{ "mntpt_path",   ll_rd_path,             0, 0 },
557         { "fstype",       ll_rd_fstype,           0, 0 },
558         { "blocksize",    ll_rd_blksize,          0, 0 },
559         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
560         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
561         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
562         { "filestotal",   ll_rd_filestotal,       0, 0 },
563         { "filesfree",    ll_rd_filesfree,        0, 0 },
564         { "client_type",  ll_rd_client_type,      0, 0 },
565         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
566         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
567                                ll_wr_max_readahead_mb, 0 },
568         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
569                                      ll_wr_max_read_ahead_whole_mb, 0 },
570         { "max_cached_mb",    ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
571         { "checksum_pages",   ll_rd_checksum, ll_wr_checksum, 0 },
572         { "max_rw_chunk",     ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
573         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
574         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
575         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
576         { "statahead_max",    ll_rd_statahead_max, ll_wr_statahead_max, 0 },
577         { "statahead_stats",  ll_rd_statahead_stats, 0, 0 },
578         { "contention_seconds", ll_rd_contention_time, ll_wr_contention_time, 0},
579         { "lockless_truncate", ll_rd_lockless_truncate,
580                                ll_wr_lockless_truncate, 0},
581         { 0 }
582 };
583
584 #define MAX_STRING_SIZE 128
585
586 struct llite_file_opcode {
587         __u32       opcode;
588         __u32       type;
589         const char *opname;
590 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
591         /* file operation */
592         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
593         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
594         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
595                                    "writeback_from_writepage" },
596         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
597                                    "writeback_from_pressure" },
598         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
599                                    "writeback_ok_pages" },
600         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
601                                    "writeback_failed_pages" },
602         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
603                                    "read_bytes" },
604         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
605                                    "write_bytes" },
606         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
607                                    "brw_read" },
608         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
609                                    "brw_write" },
610
611         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
612         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
613         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
614         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
615         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
616         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
617         /* inode operation */
618         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
619         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
620         { LPROC_LL_LOCKLESS_TRUNC, LPROCFS_TYPE_REGS, "lockless_truncate"},
621         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
622         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
623         /* special inode operation */
624         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
625         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
626         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
627         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
628         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
629         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
630         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
631         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
632                                    "direct_read" },
633         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
634                                    "direct_write" },
635         { LPROC_LL_LOCKLESS_READ,  LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
636                                    "lockless_read_bytes" },
637         { LPROC_LL_LOCKLESS_WRITE, LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
638                                    "lockless_write_bytes" },
639
640 };
641
642 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
643 {
644         if (!sbi->ll_stats)
645                 return;
646         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
647                 lprocfs_counter_add(sbi->ll_stats, op, count);
648         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
649                  sbi->ll_stats_track_id == current->pid)
650                 lprocfs_counter_add(sbi->ll_stats, op, count);
651         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
652                  sbi->ll_stats_track_id == current->p_pptr->pid)
653                 lprocfs_counter_add(sbi->ll_stats, op, count);
654         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
655                  sbi->ll_stats_track_id == current->gid)
656                 lprocfs_counter_add(sbi->ll_stats, op, count);
657 }
658 EXPORT_SYMBOL(ll_stats_ops_tally);
659
660 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
661                                 struct super_block *sb, char *osc, char *mdc)
662 {
663         struct lprocfs_vars lvars[2];
664         struct lustre_sb_info *lsi = s2lsi(sb);
665         struct ll_sb_info *sbi = ll_s2sbi(sb);
666         struct obd_device *obd;
667         char name[MAX_STRING_SIZE + 1], *ptr;
668         int err, id, len, rc;
669         ENTRY;
670
671         memset(lvars, 0, sizeof(lvars));
672
673         name[MAX_STRING_SIZE] = '\0';
674         lvars[0].name = name;
675
676         LASSERT(sbi != NULL);
677         LASSERT(mdc != NULL);
678         LASSERT(osc != NULL);
679
680         /* Get fsname */
681         len = strlen(lsi->lsi_lmd->lmd_profile);
682         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
683         if (ptr && (strcmp(ptr, "-client") == 0))
684                 len -= 7; 
685         
686         /* Mount info */
687         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
688                  lsi->lsi_lmd->lmd_profile, sb);
689         
690         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
691         if (IS_ERR(sbi->ll_proc_root)) {
692                 err = PTR_ERR(sbi->ll_proc_root);
693                 sbi->ll_proc_root = NULL;
694                 RETURN(err);
695         }
696
697
698         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
699                                 &llite_dump_pgcache_fops, sbi);
700         if (rc)
701                 CWARN("Error adding the dump_page_cache file\n");
702
703         rc = lprocfs_seq_create(sbi->ll_proc_root, "read_ahead_stats", 0644,
704                                 &ll_ra_stats_fops, sbi);
705         if (rc)
706                 CWARN("Error adding the read_ahead_stats file\n");
707
708         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
709                                 &ll_rw_extents_stats_fops, sbi);
710         if (rc)
711                 CWARN("Error adding the extent_stats file\n");
712
713         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
714                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
715         if (rc)
716                 CWARN("Error adding the extents_stats_per_process file\n");
717
718         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
719                                 &ll_rw_offset_stats_fops, sbi);
720         if (rc)
721                 CWARN("Error adding the offset_stats file\n");
722
723         /* File operations stats */
724         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES, 
725                                             LPROCFS_STATS_FLAG_PERCPU);
726         if (sbi->ll_stats == NULL)
727                 GOTO(out, err = -ENOMEM);
728         /* do counter init */
729         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
730                 __u32 type = llite_opcode_table[id].type;
731                 void *ptr = NULL;
732                 if (type & LPROCFS_TYPE_REGS)
733                         ptr = "regs";
734                 else if (type & LPROCFS_TYPE_BYTES)
735                         ptr = "bytes";
736                 else if (type & LPROCFS_TYPE_PAGES)
737                         ptr = "pages";
738                 lprocfs_counter_init(sbi->ll_stats,
739                                      llite_opcode_table[id].opcode,
740                                      (type & LPROCFS_CNTR_AVGMINMAX),
741                                      llite_opcode_table[id].opname, ptr);
742         }
743         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
744         if (err)
745                 GOTO(out, err);
746
747         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
748         if (err)
749                 GOTO(out, err);
750
751         /* MDC info */
752         obd = class_name2obd(mdc);
753
754         LASSERT(obd != NULL);
755         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
756         LASSERT(obd->obd_type->typ_name != NULL);
757
758         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
759                  obd->obd_type->typ_name);
760         lvars[0].read_fptr = lprocfs_rd_name;
761         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
762         if (err)
763                 GOTO(out, err);
764
765         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
766         lvars[0].read_fptr = lprocfs_rd_uuid;
767         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
768         if (err)
769                 GOTO(out, err);
770
771         /* OSC */
772         obd = class_name2obd(osc);
773
774         LASSERT(obd != NULL);
775         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
776         LASSERT(obd->obd_type->typ_name != NULL);
777
778         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
779                  obd->obd_type->typ_name);
780         lvars[0].read_fptr = lprocfs_rd_name;
781         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
782         if (err)
783                 GOTO(out, err);
784
785         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
786         lvars[0].read_fptr = lprocfs_rd_uuid;
787         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
788 out:
789         if (err) {
790                 lprocfs_remove(&sbi->ll_proc_root);
791                 lprocfs_free_stats(&sbi->ll_stats);
792         }
793         RETURN(err);
794 }
795
796 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
797 {
798         if (sbi->ll_proc_root) {
799                 lprocfs_remove(&sbi->ll_proc_root);
800                 lprocfs_free_stats(&sbi->ll_stats);
801         }
802 }
803 #undef MAX_STRING_SIZE
804
805 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
806                 if (test_bit(PG_##flag, &(page)->flags)) {              \
807                         if (!has_flags)                                 \
808                                 has_flags = 1;                          \
809                         else                                            \
810                                 seq_putc(seq, '|');                     \
811                         seq_puts(seq, #flag);                           \
812                 }                                                       \
813         } while(0);
814
815 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
816 {
817         struct ll_async_page *dummy_llap = seq->private;
818
819         if (dummy_llap->llap_magic == 2)
820                 return NULL;
821
822         return (void *)1;
823 }
824
825 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
826 {
827         struct ll_async_page *llap, *dummy_llap = seq->private;
828         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
829
830         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
831          * it in our own state */
832         if (dummy_llap->llap_magic == 0) {
833                 seq_printf(seq, "gener |  llap  cookie  origin wq du wb | page "
834                                 "inode index count [ page flags ]\n");
835                 return 0;
836         }
837
838         spin_lock(&sbi->ll_lock);
839
840         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
841         if (llap != NULL)  {
842                 int has_flags = 0;
843                 struct page *page = llap->llap_page;
844
845                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
846                          llap->llap_origin);
847
848                 seq_printf(seq," %5lu | %p %p %s %s %s %s | %p %lu/%u(%p) "
849                            "%lu %u [",
850                            sbi->ll_pglist_gen,
851                            llap, llap->llap_cookie,
852                            llap_origins[llap->llap_origin],
853                            llap->llap_write_queued ? "wq" : "- ",
854                            llap->llap_defer_uptodate ? "du" : "- ",
855                            PageWriteback(page) ? "wb" : "-",
856                            page, page->mapping->host->i_ino,
857                            page->mapping->host->i_generation,
858                            page->mapping->host, page->index,
859                            page_count(page));
860                 seq_page_flag(seq, page, locked, has_flags);
861                 seq_page_flag(seq, page, error, has_flags);
862                 seq_page_flag(seq, page, referenced, has_flags);
863                 seq_page_flag(seq, page, uptodate, has_flags);
864                 seq_page_flag(seq, page, dirty, has_flags);
865 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
866                 seq_page_flag(seq, page, highmem, has_flags);
867 #endif
868                 seq_page_flag(seq, page, writeback, has_flags);
869                 if (!has_flags)
870                         seq_puts(seq, "-]\n");
871                 else
872                         seq_puts(seq, "]\n");
873         }
874
875         spin_unlock(&sbi->ll_lock);
876
877         return 0;
878 }
879
880 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v, 
881                                          loff_t *pos)
882 {
883         struct ll_async_page *llap, *dummy_llap = seq->private;
884         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
885
886         /* bail if we just displayed the banner */
887         if (dummy_llap->llap_magic == 0) {
888                 dummy_llap->llap_magic = 1;
889                 return dummy_llap;
890         }
891
892         /* we've just displayed the llap that is after us in the list.
893          * we advance to a position beyond it, returning null if there
894          * isn't another llap in the list beyond that new position. */
895         spin_lock(&sbi->ll_lock);
896         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
897         list_del_init(&dummy_llap->llap_pglist_item);
898         if (llap) {
899                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
900                 llap =llite_pglist_next_llap(sbi,&dummy_llap->llap_pglist_item);
901         }
902         spin_unlock(&sbi->ll_lock);
903
904         ++*pos;
905         if (llap == NULL) {
906                 dummy_llap->llap_magic = 2;
907                 return NULL;
908         }
909         return dummy_llap;
910 }
911
912 static void null_stop(struct seq_file *seq, void *v)
913 {
914 }
915
916 struct seq_operations llite_dump_pgcache_seq_sops = {
917         .start = llite_dump_pgcache_seq_start,
918         .stop = null_stop,
919         .next = llite_dump_pgcache_seq_next,
920         .show = llite_dump_pgcache_seq_show,
921 };
922
923 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
924  * while we walk the entire list, and we don't want to have to seek into
925  * the right position in the list as an app advances with many syscalls.  we
926  * allocate a dummy llap and hang it off file->private.  its position in
927  * the list records where the app is currently displaying.  this way our
928  * seq .start and .stop don't actually do anything.  .next returns null
929  * when the dummy hits the end of the list which eventually leads to .release
930  * where we tear down.  this kind of displaying is super-racey, so we put
931  * a generation counter on the list so the output shows when the list
932  * changes between reads.
933  */
934 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
935 {
936         struct proc_dir_entry *dp = PDE(inode);
937         struct ll_async_page *dummy_llap;
938         struct seq_file *seq;
939         struct ll_sb_info *sbi = dp->data;
940         int rc = -ENOMEM;
941
942         LPROCFS_ENTRY_AND_CHECK(dp);
943
944         OBD_ALLOC_PTR_WAIT(dummy_llap);
945         if (dummy_llap == NULL)
946                 GOTO(out, rc);
947         dummy_llap->llap_page = NULL;
948         dummy_llap->llap_cookie = sbi;
949         dummy_llap->llap_magic = 0;
950
951         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
952         if (rc) {
953                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
954                 GOTO(out, rc);
955         }
956         seq = file->private_data;
957         seq->private = dummy_llap;
958
959         spin_lock(&sbi->ll_lock);
960         list_add(&dummy_llap->llap_pglist_item, &sbi->ll_pglist);
961         spin_unlock(&sbi->ll_lock);
962
963 out:
964         if (rc)
965                 LPROCFS_EXIT();
966         return rc;
967 }
968
969 static int llite_dump_pgcache_seq_release(struct inode *inode,
970                                           struct file *file)
971 {
972         struct seq_file *seq = file->private_data;
973         struct ll_async_page *dummy_llap = seq->private;
974         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
975
976         spin_lock(&sbi->ll_lock);
977         if (!list_empty(&dummy_llap->llap_pglist_item))
978                 list_del_init(&dummy_llap->llap_pglist_item);
979         spin_unlock(&sbi->ll_lock);
980         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
981
982         return lprocfs_seq_release(inode, file);
983 }
984
985 struct file_operations llite_dump_pgcache_fops = {
986         .owner   = THIS_MODULE,
987         .open    = llite_dump_pgcache_seq_open,
988         .read    = seq_read,
989         .release = llite_dump_pgcache_seq_release,
990 };
991
992 static int ll_ra_stats_seq_show(struct seq_file *seq, void *v)
993 {
994         struct timeval now;
995         struct ll_sb_info *sbi = seq->private;
996         struct ll_ra_info *ra = &sbi->ll_ra_info;
997         int i;
998         static char *ra_stat_strings[] = {
999                 [RA_STAT_HIT] = "hits",
1000                 [RA_STAT_MISS] = "misses",
1001                 [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
1002                 [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
1003                 [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
1004                 [RA_STAT_FAILED_MATCH] = "failed lock match",
1005                 [RA_STAT_DISCARDED] = "read but discarded",
1006                 [RA_STAT_ZERO_LEN] = "zero length file",
1007                 [RA_STAT_ZERO_WINDOW] = "zero size window",
1008                 [RA_STAT_EOF] = "read-ahead to EOF",
1009                 [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
1010                 [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
1011         };
1012
1013         do_gettimeofday(&now);
1014
1015         spin_lock(&sbi->ll_lock);
1016
1017         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1018                    now.tv_sec, now.tv_usec);
1019         seq_printf(seq, "pending issued pages:           %lu\n",
1020                    ra->ra_cur_pages);
1021
1022         for(i = 0; i < _NR_RA_STAT; i++)
1023                 seq_printf(seq, "%-25s %lu\n", ra_stat_strings[i], 
1024                            ra->ra_stats[i]);
1025
1026         spin_unlock(&sbi->ll_lock);
1027
1028         return 0;
1029 }
1030
1031 static ssize_t ll_ra_stats_seq_write(struct file *file, const char *buf,
1032                                        size_t len, loff_t *off)
1033 {
1034         struct seq_file *seq = file->private_data;
1035         struct ll_sb_info *sbi = seq->private;
1036         struct ll_ra_info *ra = &sbi->ll_ra_info;
1037
1038         spin_lock(&sbi->ll_lock);
1039         memset(ra->ra_stats, 0, sizeof(ra->ra_stats));
1040         spin_unlock(&sbi->ll_lock);
1041
1042         return len;
1043 }
1044
1045 LPROC_SEQ_FOPS(ll_ra_stats);
1046
1047 #define pct(a,b) (b ? a * 100 / b : 0)
1048
1049 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1050                                    struct seq_file *seq, int which)
1051 {
1052         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1053         unsigned long start, end, r, w;
1054         char *unitp = "KMGTPEZY";
1055         int i, units = 10;
1056         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1057
1058         read_cum = 0;
1059         write_cum = 0;
1060         start = 0;
1061
1062         for(i = 0; i < LL_HIST_MAX; i++) {
1063                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1064                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1065         }
1066
1067         for(i = 0; i < LL_HIST_MAX; i++) {
1068                 r = pp_info->pp_r_hist.oh_buckets[i];
1069                 w = pp_info->pp_w_hist.oh_buckets[i];
1070                 read_cum += r;
1071                 write_cum += w;
1072                 end = 1 << (i + LL_HIST_START - units);
1073                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1074                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1075                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1076                            r, pct(r, read_tot), pct(read_cum, read_tot),
1077                            w, pct(w, write_tot), pct(write_cum, write_tot));
1078                 start = end;
1079                 if (start == 1<<10) {
1080                         start = 1;
1081                         units += 10;
1082                         unitp++;
1083                 }
1084                 if (read_cum == read_tot && write_cum == write_tot)
1085                         break;
1086         }
1087 }
1088
1089 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1090 {
1091         struct timeval now;
1092         struct ll_sb_info *sbi = seq->private;
1093         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1094         int k;
1095
1096         do_gettimeofday(&now);
1097
1098         if (!sbi->ll_rw_stats_on) {
1099                 seq_printf(seq, "Disabled\n"
1100                                 "Write anything in this file to activate\n");
1101                 return 0;
1102         }
1103         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1104                    now.tv_sec, now.tv_usec);
1105         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1106         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
1107                    "extents", "calls", "%", "cum%",
1108                    "calls", "%", "cum%");
1109         spin_lock(&sbi->ll_pp_extent_lock);
1110         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1111                 if(io_extents->pp_extents[k].pid != 0) {
1112                         seq_printf(seq, "\nPID: %d\n",
1113                                    io_extents->pp_extents[k].pid);
1114                         ll_display_extents_info(io_extents, seq, k);
1115                 }
1116         }
1117         spin_unlock(&sbi->ll_pp_extent_lock);
1118         return 0;
1119 }
1120
1121 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1122                                                 const char *buf, size_t len,
1123                                                 loff_t *off)
1124 {
1125         struct seq_file *seq = file->private_data;
1126         struct ll_sb_info *sbi = seq->private;
1127         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1128         int i;
1129
1130         sbi->ll_rw_stats_on = 1;
1131         spin_lock(&sbi->ll_pp_extent_lock);
1132         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1133                 io_extents->pp_extents[i].pid = 0;
1134                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1135                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1136         }
1137         spin_unlock(&sbi->ll_pp_extent_lock);
1138         return len;
1139 }
1140
1141 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1142
1143 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1144 {
1145         struct timeval now;
1146         struct ll_sb_info *sbi = seq->private;
1147         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1148
1149         do_gettimeofday(&now);
1150
1151         if (!sbi->ll_rw_stats_on) {
1152                 seq_printf(seq, "Disabled\n"
1153                                 "Write anything in this file to activate\n");
1154                 return 0;
1155         }
1156         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1157                    now.tv_sec, now.tv_usec);
1158
1159         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1160         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
1161                    "extents", "calls", "%", "cum%",
1162                    "calls", "%", "cum%");
1163         spin_lock(&sbi->ll_lock);
1164         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1165         spin_unlock(&sbi->ll_lock);
1166
1167         return 0;
1168 }
1169
1170 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1171                                         size_t len, loff_t *off)
1172 {
1173         struct seq_file *seq = file->private_data;
1174         struct ll_sb_info *sbi = seq->private;
1175         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1176         int i;
1177
1178         sbi->ll_rw_stats_on = 1;
1179         spin_lock(&sbi->ll_pp_extent_lock);
1180         for(i = 0; i <= LL_PROCESS_HIST_MAX; i++)
1181         {
1182                 io_extents->pp_extents[i].pid = 0;
1183                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1184                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1185         }
1186         spin_unlock(&sbi->ll_pp_extent_lock);
1187
1188         return len;
1189 }
1190
1191 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1192
1193 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1194                                *file, size_t count, int rw)
1195 {
1196         int i, cur = -1;
1197         struct ll_rw_process_info *process;
1198         struct ll_rw_process_info *offset;
1199         int *off_count = &sbi->ll_rw_offset_entry_count;
1200         int *process_count = &sbi->ll_offset_process_count;
1201         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1202
1203         if(!sbi->ll_rw_stats_on)
1204                 return;
1205         process = sbi->ll_rw_process_info;
1206         offset = sbi->ll_rw_offset_info;
1207
1208         spin_lock(&sbi->ll_pp_extent_lock);
1209         /* Extent statistics */
1210         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1211                 if(io_extents->pp_extents[i].pid == pid) {
1212                         cur = i;
1213                         break;
1214                 }
1215         }
1216
1217         if (cur == -1) {
1218                 /* new process */
1219                 sbi->ll_extent_process_count = 
1220                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1221                 cur = sbi->ll_extent_process_count;
1222                 io_extents->pp_extents[cur].pid = pid;
1223                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1224                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1225         }
1226
1227         for(i = 0; (count >= (1 << LL_HIST_START << i)) && 
1228              (i < (LL_HIST_MAX - 1)); i++);
1229         if (rw == 0) {
1230                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1231                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1232         } else {
1233                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1234                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1235         }
1236         spin_unlock(&sbi->ll_pp_extent_lock);
1237
1238         spin_lock(&sbi->ll_process_lock);
1239         /* Offset statistics */
1240         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1241                 if (process[i].rw_pid == pid) {
1242                         if (process[i].rw_last_file != file) {
1243                                 process[i].rw_range_start = file->f_pos;
1244                                 process[i].rw_last_file_pos =
1245                                                         file->f_pos + count;
1246                                 process[i].rw_smallest_extent = count;
1247                                 process[i].rw_largest_extent = count;
1248                                 process[i].rw_offset = 0;
1249                                 process[i].rw_last_file = file;
1250                                 spin_unlock(&sbi->ll_process_lock);
1251                                 return;
1252                         }
1253                         if (process[i].rw_last_file_pos != file->f_pos) {
1254                                 *off_count =
1255                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1256                                 offset[*off_count].rw_op = process[i].rw_op;
1257                                 offset[*off_count].rw_pid = pid;
1258                                 offset[*off_count].rw_range_start =
1259                                         process[i].rw_range_start;
1260                                 offset[*off_count].rw_range_end =
1261                                         process[i].rw_last_file_pos;
1262                                 offset[*off_count].rw_smallest_extent =
1263                                         process[i].rw_smallest_extent;
1264                                 offset[*off_count].rw_largest_extent =
1265                                         process[i].rw_largest_extent;
1266                                 offset[*off_count].rw_offset =
1267                                         process[i].rw_offset;
1268                                 process[i].rw_op = rw;
1269                                 process[i].rw_range_start = file->f_pos;
1270                                 process[i].rw_smallest_extent = count;
1271                                 process[i].rw_largest_extent = count;
1272                                 process[i].rw_offset = file->f_pos -
1273                                         process[i].rw_last_file_pos;
1274                         }
1275                         if(process[i].rw_smallest_extent > count)
1276                                 process[i].rw_smallest_extent = count;
1277                         if(process[i].rw_largest_extent < count)
1278                                 process[i].rw_largest_extent = count;
1279                         process[i].rw_last_file_pos = file->f_pos + count;
1280                         spin_unlock(&sbi->ll_process_lock);
1281                         return;
1282                 }
1283         }
1284         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1285         process[*process_count].rw_pid = pid;
1286         process[*process_count].rw_op = rw;
1287         process[*process_count].rw_range_start = file->f_pos;
1288         process[*process_count].rw_last_file_pos = file->f_pos + count;
1289         process[*process_count].rw_smallest_extent = count;
1290         process[*process_count].rw_largest_extent = count;
1291         process[*process_count].rw_offset = 0;
1292         process[*process_count].rw_last_file = file;
1293         spin_unlock(&sbi->ll_process_lock);
1294 }
1295
1296 char lpszt[] = LPSZ;
1297
1298 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1299 {
1300         struct timeval now;
1301         struct ll_sb_info *sbi = seq->private;
1302         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1303         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1304         char format[50];
1305         int i;
1306
1307         do_gettimeofday(&now);
1308
1309         if (!sbi->ll_rw_stats_on) {
1310                 seq_printf(seq, "Disabled\n"
1311                                 "Write anything in this file to activate\n");
1312                 return 0;
1313         }
1314         spin_lock(&sbi->ll_process_lock);
1315
1316         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1317                    now.tv_sec, now.tv_usec);
1318         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1319                    "R/W", "PID", "RANGE START", "RANGE END",
1320                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1321         sprintf(format, "%s%s%s%s%s\n",
1322                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1323         /* We stored the discontiguous offsets here; print them first */
1324         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1325                 if (offset[i].rw_pid != 0)
1326                         /* Is there a way to snip the '%' off of LPSZ? */
1327                         seq_printf(seq, format,
1328                                    offset[i].rw_op ? 'W' : 'R',
1329                                    offset[i].rw_pid,
1330                                    offset[i].rw_range_start,
1331                                    offset[i].rw_range_end,
1332                                    offset[i].rw_smallest_extent,
1333                                    offset[i].rw_largest_extent,
1334                                    offset[i].rw_offset);
1335         }
1336         /* Then print the current offsets for each process */
1337         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1338                 if (process[i].rw_pid != 0)
1339                         seq_printf(seq, format,
1340                                    process[i].rw_op ? 'W' : 'R',
1341                                    process[i].rw_pid,
1342                                    process[i].rw_range_start,
1343                                    process[i].rw_last_file_pos,
1344                                    process[i].rw_smallest_extent,
1345                                    process[i].rw_largest_extent,
1346                                    process[i].rw_offset);
1347         }
1348         spin_unlock(&sbi->ll_process_lock);
1349
1350         return 0;
1351 }
1352
1353 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1354                                        size_t len, loff_t *off)
1355 {
1356         struct seq_file *seq = file->private_data;
1357         struct ll_sb_info *sbi = seq->private;
1358         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1359         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1360
1361         sbi->ll_rw_stats_on = 1;
1362
1363         spin_lock(&sbi->ll_process_lock);
1364         sbi->ll_offset_process_count = 0;
1365         sbi->ll_rw_offset_entry_count = 0;
1366         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1367                LL_PROCESS_HIST_MAX);
1368         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1369                LL_OFFSET_HIST_MAX);
1370         spin_unlock(&sbi->ll_process_lock);
1371
1372         return len;
1373 }
1374
1375 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1376
1377 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1378 {
1379     lvars->module_vars  = NULL;
1380     lvars->obd_vars     = lprocfs_llite_obd_vars;
1381 }
1382 #endif /* LPROCFS */