File: parse.py

package info (click to toggle)
nfsometer 1.9-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: python: 4,928; sh: 156; javascript: 32; makefile: 5
file content (1108 lines) | stat: -rw-r--r-- 37,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
"""
Copyright 2012 NetApp, Inc. All Rights Reserved,
contribution by Weston Andros Adamson <dros@netapp.com>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
"""

import os
import re
#import six

from .config import *

#
# Regular Expressions section
#  these are precompiled so they are only compiled once
#
def _re(regex):
    """ short-hand wrapper for regex compilation """
    return re.compile(regex)

RE = {
  'time_real': _re('^real\s+([\d]+)m([\d.]+)s'),
  'time_user': _re('^user\s+([\d]+)m([\d.]+)s'),
  'time_sys':  _re('^sys\s+([\d]+)m([\d.]+)s'),

  'ms_mount_opts': _re('^\s+NFS mount options:\s+(.*)$'),

  'ms_read_norm':  _re('^\s+applications read (\d+) bytes via read'),
  'ms_write_norm': _re('^\s+applications wrote (\d+) bytes via write'),
  'ms_read_odir':  _re('^\s+applications read (\d+) bytes via O_DIRECT'),
  'ms_write_odir': _re('^\s+applications wrote (\d+) bytes via O_DIRECT'),
  'ms_read_nfs':   _re('^\s+client read (\d+) bytes via NFS READ'),
  'ms_write_nfs':  _re('^\s+client wrote (\d+) bytes via NFS WRITE'),

  'ms_rpc_line':    _re('^\s+(\d+) RPC requests sent, (\d+) RPC ' \
                              + 'replies received \((\d)+ XIDs not found\)'),
  'ms_rpc_backlog': _re('^\s+average backlog queue length: (\d)'),

  'ms_ops_header': _re('^(\S+):$'),
  'ms_ops_line1':  _re('^\s+(\d+) ops \((\d+)%\)\s+(-?\d+) retrans '
                             + '\((-?\d+)%\)\s+(\d+) major timeouts'),
  'ms_ops_line2':  _re('^\s+avg bytes sent per op:\s+(\d+)\s+avg bytes received per op:\s+(\d+)'),
  'ms_ops_line3':  _re('^\s+backlog wait:\s+(\d+\.\d+)\s+RTT:\s+(\d+\.\d+)\s+total execute time:\s+(\d+\.\d+)\s+'),

  'pms_xprt_tcp':  _re('^\s+xprt:\s+tcp\s+(.*)'),
  'pms_xprt_udp':  _re('^\s+xprt:\s+udp\s+(.*)'),

  'nio_infoline': _re('^.* mounted on (\S+):'),
  'nio_readhdr':  _re('^read:\s+'),
  'nio_writehdr': _re('^write:\s+'),
  'nio_numbers':  _re('^\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)\s+(-?\d+)\s+\((-?[\d.]+)%\)\s+([\d.]+)\s+([\d.]+)'),

  'filebench_stats': _re('^.*IO Summary:\s+(\d+)\s+ops,\s+([\d.]+)\s+ops/s,\s+\((\d+)/(\d+)\s+r/w\),\s+([\d.]+)mb/s,\s+(\d+)us\s+cpu/op,\s+([\d.]+)ms\s+latency'),

  'ns_count_title': _re('^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)'),
  'ns_count_data':  _re('^(\d+)\s+\d+%\s+(\d+)\s+\d+%\s+(\d+)\s+\d+%\s+(\d+)\s+\d+%\s+(\d+)\s+\d+%\s+(\d+)\s+\d+%'),
  'ns_count_newsection': _re('^Client nfs'),

  'ns_rpc_title': _re('^Client rpc stats:'),
  'ns_rpc_data':  _re('^(\d+)\s+(\d+)\s+(\d+)'),

  # proc_mounstats
  # events - 27 values
  'pms_events': _re('^\s+events:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)'),

  # iozone output
  #'iozone_report_hdr': _re('^"(.*) report"'),
}


##
# Bucket Definitions
##

BUCKET_OTHER='Other'

class BucketDef:
    """
        Used to define buckets
    """
    def __init__(self):
        self._key2bucket = {}
        self._key2display = {}
        self._other_keys = set()

    def key_to_bucket(self, key):
        b = self._key2bucket.get(key, None)

        if not b:
            return BUCKET_OTHER

        return b

    def bucket_names(self):
        """ return all buckets """
        r = list(set([ x for x in self._key2bucket.values()]))
        r.sort()

        other = BUCKET_OTHER
        try:
            r.remove(other)
        except ValueError:
            pass

        r.append(other)

        return r

    def add_key(self, bucket_name, key, display):
        if key in self._key2bucket or key in self._other_keys:
            return

        if display:
            self._key2display[key] = display

        if bucket_name:
            self._key2bucket[key] = bucket_name
        else:
            self._other_keys.add(key)

    def keys(self):
        r = set(self._key2bucket.keys())
        r = r.union(self._other_keys)
        return tuple(r)

    def key_display(self, key):
        return self._key2display.get(key, key)
    
# global bucket definitions
wall_times_bucket_def = BucketDef()
exec_times_bucket_def = BucketDef()
nfsstat_bucket_def = BucketDef()
mountstat_exec_time_bucket_def = BucketDef()
mountstat_rtt_bucket_def = BucketDef()
mountstat_bytes_sent_bucket_def = BucketDef()
mountstat_bytes_received_bucket_def = BucketDef()
iozone_bucket_def = BucketDef()

nfsstat_op_map_def = {
  'Creation and Deletion':
    ('create', 'open', 'open_conf', 'open_dgrd', 'open_noat', 'mkdir',
     'rmdir', 'remove', 'close', 'mknod',
    ),

  'File Metadata':
    ('access', 'lookup', 'lookup_root', 'rename', 'link', 'readlink',
     'symlink',
    ),

  'Readdir':
    ('readdir', 'readdirplus',
    ),

  'Getattr and Setattr':
    ('getattr', 'setattr',
    ),

  'FS Metadata':
    ('fsstat', 'fsinfo', 'statfs',
    ),

  'Locks and Delegations':
    ('lock', 'lockt', 'locku', 'rel_lkowner', 'delegreturn', 'get_lease_t',
    ),

  'Write':
    ('write', 'commit', 'ds_write',
    ),

  'Read':
    ('read',),

  'PNFS':
    ('getdevinfo', 'getdevlist', 'layoutget', 'layoutcommit', 'layoutreturn',
    ),

  'Getacl and Setacl':
    ('getacl', 'setacl',
    ),

  'Session':
    ('create_ses', 'destroy_ses', 'exchange_id',
    ),
}
nfsstat_op_map = {}
for b, ops in nfsstat_op_map_def.items():
    for o in ops:
        nfsstat_op_map[o] = b


mountstat_op_map_def = {
  'Creation and Deletion':
    ('CREATE', 'OPEN', 'MKDIR', 'RMDIR', 'REMOVE', 'CLOSE', 'OPEN_CONFIRM',
     'OPEN_DOWNGRADE',
    ),

  'File Metadata':
    ('ACCESS', 'LOOKUP', 'LOOKUP_ROOT', 'RENAME', 'LINK', 'READLINK',
     'SYMLINK',
    ),

  'Readdir':
    ('READDIR', 'READDIRPLUS',
    ),

  'Getattr and Setattr':
    ('GETATTR', 'SETATTR',
    ),

  'FS Metadata':
    ('FSSTAT', 'FSINFO', 'STATFS',
    ),

  'Locks and Delegations':
    ('LOCK', 'LOCKT', 'LOCKU', 'RELEASE_LOCKOWNER', 'DELEGRETURN',
     'GET_LEASE_TIME',
    ),

  'Write':
    ('WRITE', 'COMMIT',
    ),

  'Read':
    ('READ',
    ),

  'PNFS':
    ('GETDEVICEINFO', 'GETDEVICELIST', 'LAYOUTGET', 'LAYOUTCOMMIT',
     'LAYOUTRETURN',
    ),

  'Getacl and Setacl':
    ('GETACL', 'SETACL',
    ),

  'Session':
    ('CREATE_SESSION', 'DESTROY_SESSION', 'EXCHANGE_ID',
    ),
}

mountstat_op_map = {}
for b, ops in mountstat_op_map_def.items():
    for o in ops:
        mountstat_op_map[o] = b


def gather_buckets(collection, tracestat):
    gather_bucket(collection, tracestat, wall_times_bucket_def,
                  'Average wall-clock time of workload')
    gather_bucket(collection, tracestat, exec_times_bucket_def,
                  'Exececution times of workload')
    gather_bucket(collection, tracestat, nfsstat_bucket_def,
                  'Count of NFS operations for group %(bucket)s')
    gather_bucket(collection, tracestat, mountstat_exec_time_bucket_def,
                  'Average operation execution time for group %(bucket)s')
    gather_bucket(collection, tracestat, mountstat_rtt_bucket_def,
                  'Operation round trip time for group %(bucket)s')
    gather_bucket(collection, tracestat, mountstat_bytes_sent_bucket_def,
                  'Average bytes sent per operation for group %(bucket)s')
    gather_bucket(collection, tracestat, mountstat_bytes_received_bucket_def,
                  'Average bytes received per operation for group %(bucket)s')
    #gather_bucket(collection, tracestat, iozone_bucket_def,
    #              'Average KB/s for iozone %(bucket)s')

def gather_bucket(collection, tracestat, bucket_def, descr):
    keys = [ x for x in bucket_def.keys() ]

    for k in keys:
        stat = tracestat.get_stat(k)
        if stat == None:
            continue
        b = bucket_def.key_to_bucket(k)

        if b != None:
            fmtmap = {'bucket': b}
            descr_fmt = descr % fmtmap
            tracestat.add_bucket(b, stat, descr_fmt)

def parse_tracedir(collection, tracestat, tracedir, attrs):
    parsers = (
        (parse_time,            True),
        (parse_mountstats,      True),
        (parse_nfsiostat,       True),
        (parse_nfsstats,        True),
        (parse_proc_mountstats, (int(attrs['orig_tracedir_version']) > 4)),
        #(parse_iozone,          (attrs['workload'].startswith('iozone'))),
        (parse_filebench,       (attrs['workload'].startswith('filebench_'))),
    )

    for p, cond in parsers:
        if not cond:
            continue

        try:
            p(tracestat, tracedir, attrs)
        except Exception as e:
            collection.warn(tracedir, str(e))


def parse_time(tracestat, tracedir, attrs):
    prefix = 'times:'
    stat_desc = 'output of time(1)'
    filename = 'test.time'

    path = os.path.join(tracedir, filename)

    lines = [ x.strip() for x in open(path) if x.strip() ]
    assert len(lines) == 3

    def _parse_time(minutes, seconds):
        return (float(minutes) * 60.0) + float(seconds)

    WALLTIME_BUCKET='Wall Times Time'
    EXECTIME_BUCKET='Exec Times Time'

    m = RE['time_real'].match(lines[0])
    time_real = _parse_time(m.group(1), m.group(2))
    tracestat.add_stat(prefix + 'Real Time',
                time_real, 's',
                'Wall-clock time of workload execution',
                BETTER_ALWAYS_LESS,
                (wall_times_bucket_def, WALLTIME_BUCKET),
                filename,
                tracedir)

    tracetime = float(attrs['stoptime']) - float(attrs['starttime'])
    tracestat.add_stat(prefix + 'Trace Time',
                tracetime, 's',
                'Wall-clock time of mount, workload execution, unmount and '
                'flushing of dirty data',
                BETTER_ALWAYS_LESS,
                None,
                filename,
                tracedir)

    diff = tracetime - float(time_real)
    tracestat.add_stat(prefix + 'Sync Time',
                diff, 's',
                'Wall-clock time of mount, workload execution, unmount and '
                'flushing of dirty data',
                BETTER_ALWAYS_LESS,
                (wall_times_bucket_def, WALLTIME_BUCKET),
                filename,
                tracedir)

    m = RE['time_user'].match(lines[1])
    time_user = _parse_time(m.group(1), m.group(2))
    tracestat.add_stat(prefix + 'User Time',
                time_user, 's',
                'Time spent executing the workload in the user context',
                BETTER_ALWAYS_LESS,
                (exec_times_bucket_def, EXECTIME_BUCKET),
                filename,
                tracedir)

    m = RE['time_sys'].match(lines[2])
    time_sys = _parse_time(m.group(1), m.group(2))
    tracestat.add_stat(prefix + 'Sys Time',
                time_sys, 's',
                'Time spent executing the workload in the kernel context',
                BETTER_ALWAYS_LESS,
                (exec_times_bucket_def, EXECTIME_BUCKET),
                filename,
                tracedir)

def parse_mountstats(tracestat, tracedir, attrs):
    prefix = 'mountstats:'
    stat_desc = 'output of mountstats(1)'
    filename = 'mountstats'

    path = os.path.join(tracedir, filename)

    f = open(path)
    for line in f:
        found = False

        m = RE['ms_mount_opts'].match(line)
        if m:
            tracestat.add_attr('mount_options', m.group(1))
            continue

        m = RE['ms_read_norm'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'read_normal',
                        val, 'B',
                        'Bytes read through the read() syscall',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_write_norm'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'write_normal',
                        val, 'B',
                        'Bytes written through write() syscall',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_read_odir'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'read_odirect',
                        val, 'B',
                        'Bytes read through read(O_DIRECT) syscall',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_write_odir'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'write_odirect',
                        val, 'B',
                        'Bytes written through write(O_DIRECT) syscall',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_read_nfs'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'read_nfs',
                        val, 'B',
                        'Bytes read via NFS RPCs',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_write_nfs'].match(line)
        if m:
            val = int(m.group(1))
            tracestat.add_stat(prefix + 'write_nfs',
                        val, 'B',
                        'Bytes written via NFS RPCs',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_rpc_line'].match(line)
        if m:
            tracestat.add_stat(prefix + 'rpc_requests',
                        int(m.group(1)), 'RPCs',
                        'Count of RPC requests',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'rpc_replies',
                        int(m.group(2)), 'RPCs',
                        'Count of RPC replies',
                        BETTER_LESS_IF_IO_BOUND,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'xid_not_found',
                        int(m.group(3)), 'RPCs',
                        'Count of RPC replies that couldn\'t be matched ' +
                        'with a request',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['ms_rpc_backlog'].match(line)
        if m:
            tracestat.add_stat(prefix + 'backlog_queue_avg',
                        int(m.group(1)), 'RPCs',
                        'Average number of outgoing requests on the backlog ' +
                        'queue',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            break

    # now read nfs ops
    op = None
    oplineno = 0
    for line in f:
        ls = line.strip()
        m = RE['ms_ops_header'].match(ls)
        if m:
            #assert op is None,"failed op==None, m==%s" % m
            op = m.group(1)
            op_bucket = mountstat_op_map.get(op, BUCKET_OTHER)
            oplineno = 1
            continue

        if oplineno == 1:
            m = RE['ms_ops_line1'].match(line)
            if m:
                assert op is not None,"failed op != None"
                oplineno += 1
                continue

        elif oplineno == 2:
            m = RE['ms_ops_line2'].match(line)
            if m:
                tracestat.add_stat(prefix + op + ' Bytes Sent',
                            m.group(1), 'B',
                            'Average bytes sent for %s operations' % op,
                            BETTER_ALWAYS_MORE,
                            (mountstat_bytes_sent_bucket_def,
                             op_bucket + ' Bytes Sent', op),
                            filename,
                            tracedir)
                tracestat.add_stat(prefix + op + ' Bytes Received',
                            m.group(2), 'B',
                            'Average bytes received for %s operations' % op,
                            BETTER_ALWAYS_MORE,
                            (mountstat_bytes_received_bucket_def,
                             op_bucket + ' Bytes Received', op),
                            filename,
                            tracedir)

                oplineno += 1

        elif oplineno == 3:
            m = RE['ms_ops_line3'].match(line)
            if m:

                tracestat.add_stat(prefix + op + ' RTT',
                  m.group(2), 'ms',
                  'Average round trip time of %s operations' % op,
                  BETTER_ALWAYS_LESS,
                  (mountstat_rtt_bucket_def, op_bucket + ' RTT', op),
                  filename,
                  tracedir)
                tracestat.add_stat(prefix + op + ' Exec Time',
                  m.group(3), '&mu;s',
                  'Average execution time of %s operations' % op,
                  BETTER_ALWAYS_LESS,
                  (mountstat_exec_time_bucket_def, op_bucket + ' Exec Time', op),
                  filename,
                  tracedir)

                op = None
                oplineno = 0
                continue

        elif op:
            raise ParseError("Didn't match line: %s" % line)

    f.close()

def parse_nfsiostat(tracestat, tracedir, attrs):
    prefix = 'nfsiostat:'
    stat_desc = 'output of nfsiostat(1)'
    filename = 'nfsiostat'

    path = os.path.join(tracedir, filename)

    lines = open(path).readlines()

    # skip until we find our mount
    name=None
    found_mnt = False
    warn = True
    got_read = False
    got_write = False

    for line in lines:
        if not found_mnt:
            m = RE['nio_infoline'].match(line)

            if m and m.group(1) == attrs['localpath']:
                found_mnt = True
            elif warn and line.strip():
                tracestat.collection.warn(tracedir,
                    "More than one NFS mount found, "
                    "this will skew global stats like nfsstats")
                warn = False
            continue

        if got_read and got_write:
            break

        m = RE['nio_readhdr'].match(line)
        if m:
            name='read'
            continue

        m = RE['nio_writehdr'].match(line)
        if m:
            name='write'
            continue

        if name:
            m = RE['nio_numbers'].match(line)
            assert m, "Cant match line: %s" % line

            # name is 'read' or 'write'
            plural = name + 's'

            tracestat.add_stat(prefix + '%s_ops_per_sec' % name,
                        m.group(1), 'ops/s',
                        'Operations per second of of NFS %s' % plural,
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + '%s_kb_per_sec' % name,
                        m.group(2), 'KB/s',
                        'KB per second of NFS %s' % plural,
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + '%s_kb_per_op' % name,
                        m.group(3), 'KB/op',
                        'KB per operation of NFS %s' % plural,
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + '%s_avg_rtt_ms' % name,
                        m.group(6), 'ms',
                        'Average round trip time of NFS %s' % plural,
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)

            if name == "read":
                got_read = True
            elif name == "write":
                got_write = True

            name=None
            continue

def parse_nfsstats(tracestat, tracedir, attrs):
    prefix = 'nfsstats:'
    stat_desc = 'output of nfsstats(1)'
    filename = 'nfsstats'

    path = os.path.join(tracedir, filename)

    lines = open(path).readlines()

    m = RE['ns_rpc_title'].match(lines[0])

    if m:
        parse_idx = 4
        m = RE['ns_rpc_data'].match(lines[2])

    else:
        parse_idx = 8
        m = RE['ns_rpc_title'].match(lines[4])

        if m:
            m = RE['ns_rpc_data'].match(lines[6])

    if not m:
        raise ParseError("Can't find RPC call count")

    tracestat.add_stat(prefix + 'rpc_calls',
                int(m.group(1)), 'Calls',
                'Count of RPC calls',
                BETTER_LESS_IF_IO_BOUND,
                None,
                filename,
                tracedir)

    op_counts = {}
    titles = None

    # handle bug in nfsstats not clearing v4 stats... :-/
    sections = 0

    for line in lines[parse_idx:]:
        m = RE['ns_count_newsection'].match(line)

        if m:
            sections += 1

            if sections > 1:
                break
            else:
                continue

        if not titles:
            m = RE['ns_count_title'].match(line)

            if m:
                titles = m.groups()[0:]

        else:
            m = RE['ns_count_data'].match(line)
            if m:
                for i, t in enumerate(titles):
                    assert t not in op_counts, "dup op count %s" % t
                    op_counts[t] = int(m.group(i+1))

            titles = None

    for op, count in op_counts.items():
        if count:
            op_bucket = nfsstat_op_map.get(op, BUCKET_OTHER)
            tracestat.add_stat(prefix + op.upper() + ' Count',
                        count, 'Calls',
                        'Count of %s operations' % op.upper(),
                        BETTER_LESS_IF_IO_BOUND,
                        (nfsstat_bucket_def, op_bucket + ' Count', op),
                        filename,
                        tracedir)

def parse_filebench(tracestat, tracedir, attrs):
    prefix = 'filebench:'
    stat_desc = 'output of the filebench test suite'
    filename = 'test.log'

    path = os.path.join(tracedir, filename)

    # NOTE: BETTER_* based on fact that filebench output is only ever time bound
    found = False
    with open(path) as f:
      for line in f:
        m = RE['filebench_stats'].match(line)
        if m:
            tracestat.add_stat(prefix + 'op_count',
                        m.group(1), 'fbops',
                        'Count of filebench operations',
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'ops_per_second',
                        m.group(2), 'fbops/s',
                        'Filebench operations per second',
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'mb_per_second',
                        m.group(5), 'MB/s',
                        'MB per second throughput',
                        BETTER_ALWAYS_MORE,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'cpu_per_op',
                        m.group(6), 'CPU/FBop',
                        'CPU usage per filebench operation',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            tracestat.add_stat(prefix + 'latency_ms',
                        m.group(7), 'ms',
                        'Filebench measured latency',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            found = True
            break

    assert found, "Couldn't match filebench line: %s" % path

def parse_proc_mountstats(tracestat, tracedir, attrs):
    prefix = 'proc_mountstats:'
    stat_desc = '/proc/self/mountstats after the test run'
    filename = 'proc_mountstats.stop'

    path = os.path.join(tracedir, filename)

    with open(path) as f:
      found = False
      for line in f:
        m = RE['pms_events'].match(line)
        if m:
            found = True

            tracestat.add_stat(prefix + 'inode_revalidate',
                        int(m.group(1)), 'events',
                        'Count of inode_revalidate events',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'dentry_revalidate',
                        int(m.group(2)), 'events',
                        'Count of dentry_revalidate events',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'data_invalidate',
                        int(m.group(3)), 'events',
                        'Count of data_invalidate events',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'attr_invalidate',
                        int(m.group(4)), 'events',
                        'Count of attr_invalidate events',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_open',
                        int(m.group(5)), 'events',
                        'Count of file and directory opens',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_lookup',
                        int(m.group(6)), 'events',
                        'Count of lookups',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_access',
                        int(m.group(7)), 'events',
                        'Count of access calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_updatepage',
                        int(m.group(8)), 'events',
                        'Count of updatepage calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_readpage',
                        int(m.group(9)), 'events',
                        'Count of readpage calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_readpages',
                        int(m.group(10)), 'events',
                        'Count of readpages calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_writepage',
                        int(m.group(11)), 'events',
                        'Count of writepage calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_writepages',
                        int(m.group(12)), 'events',
                        'Count of writepages calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_getdents',
                        int(m.group(13)), 'events',
                        'Count of getdents calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_setattr',
                        int(m.group(14)), 'events',
                        'Count of setattr calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_flush',
                        int(m.group(15)), 'events',
                        'Count of flush calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_fsync',
                        int(m.group(16)), 'events',
                        'Count of fsync calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_lock',
                        int(m.group(17)), 'events',
                        'Count of lock calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'vfs_release',
                        int(m.group(18)), 'events',
                        'Count of release calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'congestion_wait',
                        int(m.group(19)), 'events',
                        'Count of congestion_wait',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'setattr_trunc',
                        int(m.group(20)), 'events',
                        'Count of setattr_trunc',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'extend_write',
                        int(m.group(21)), 'events',
                        'Count of extend_write',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'silly_rename',
                        int(m.group(22)), 'events',
                        'Count of silly_rename',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'short_read',
                        int(m.group(23)), 'events',
                        'Count of short_read',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'short_write',
                        int(m.group(24)), 'events',
                        'Count of short_write',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'delay',
                        int(m.group(25)), 'events',
                        'Count of delays (v3: JUKEBOX, v4: ERR_DELAY, grace period, key expired)',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'pnfs_read',
                        int(m.group(26)), 'events',
                        'Count of pnfs_read calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)

            tracestat.add_stat(prefix + 'pnfs_write',
                        int(m.group(27)), 'events',
                        'Count of pnfs_write calls',
                        BETTER_ALWAYS_LESS | BETTER_NO_VARIANCE,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['pms_xprt_tcp'].match(line)
        if m:
            values = [ x for x in m.group(1).split(' ') if x ]

            if len(values) > 10:
                # older mountstats don't have so many values
                tracestat.add_stat(prefix + 'xprt_max_slots',
                        int(values[10]), 'slots',
                        'Max slots used by rpc transport',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            continue

        m = RE['pms_xprt_udp'].match(line)
        if m:
            values = [ x for x in m.group(1).split(' ') if x ]

            if len(values) > 8:
                # older mountstats don't have so many values
                tracestat.add_stat(prefix + 'xprt_max_slots',
                        int(values[8]), 'slots',
                        'Max slots used by rpc transport',
                        BETTER_ALWAYS_LESS,
                        None,
                        filename,
                        tracedir)
            continue


    assert found


def parse_iozone(tracestats, tracedir, attrs):
    prefix = 'iozone:'
    stat_desc = 'output of the iozone test suite'
    filename = 'test.log'

    path = os.path.join(tracedir, filename)

    rpt_name = None
    rpt_col_hdr = []

    # maps name -> (%u_%u) -> value
    newkeys = []

    with open(path) as f:
      for line in f:
        line = line.strip()
        if rpt_name:
            if not line:
                # pop report
                rpt_name = None
                rpt_col_hdr = []
                continue

            if not rpt_col_hdr:
                rpt_col_hdr = []
                for x in line.split(' '):
                    if x.strip():
                        assert x.startswith('"') and x.endswith('"')
                        rpt_col_hdr.append(x[1:-1])
            else:
                newrow = [ x for x in line.split(' ' ) if x.strip() ]

                row_hdr = newrow.pop(0)
                assert row_hdr.startswith('"') and row_hdr.endswith('"')
                row_hdr = row_hdr[1:-1]

                for i, val in enumerate(newrow):
                    key = '%s_%u_%u' % (rpt_name, int(row_hdr),
                                        int(rpt_col_hdr[i]))
                    newkeys.append((key.lower(), val))

        else:
            m = RE['iozone_report_hdr'].match(line)
            if m:
                rpt_name = m.group(1)
                continue

    for key, value in newkeys:
        skey = key.split('_')
        report = '_'.join(skey[:-2])
        x = int(skey[-2])
        y = int(skey[-1])

        tracestat.add_stat(prefix + key + ' iozone',
            int(value), 'KB/s',
            '%s: size kb: %u, reclen: %u' % (report, x, y),
            BETTER_ALWAYS_MORE,
            (iozone_bucket_def, report + ' iozone'),
                        filename,
            tracedir)