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
#![recursion_limit="100"]
#[macro_use]
extern crate nom;

use std::error;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::str::FromStr;

use self::nom::{le_u8, is_digit, space, newline};

#[cfg(test)]
mod tests {
    use nom;

    #[test]
    fn it_parses_a_u32() {
        let input = b"12345";
        let result = super::take_u32(input);
        match result {
            nom::IResult::Done(_, f) => assert_eq!(f, 12345u32),
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_a_u32_with_whitespace() {
        let input = b"12345 ";
        let result = super::take_u32(input);
        match result {
            nom::IResult::Done(_, f) => assert_eq!(f, 12345u32),
            _ => unreachable!(),
        }

    }

    #[test]
    fn it_parses_extent_allocation() {
        let example_output = b"extent_alloc 4260849 125170297 4618726 131131897";
        match super::extent_alloc(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.allocated_extents, 4260849);
                assert_eq!(result.freed_blocks, 131131897);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_allocation_btree() {
        let example_output = b"abt 29491162 337391304 11257328 11133039";
        match super::abt(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.inserts, 11257328);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_block_mapping() {
        let example_output = b"blk_map 381213360 115456141 10903633 69612322 7448401 507596777 0";
        match super::blk_map(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.list_delete, 7448401);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_block_map_btree() {
        let example_output = b"bmbt 771328 6236258 602114 86646";
        match super::bmbt(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.deletes, 86646);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_directory_operations() {
        let example_output = b"dir 21253907 6921870 6969079 779205554";
        match super::dir(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.lookups, 21253907);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_transactions() {
        let example_output = b"trans 126946406 38184616 6342392";
        match super::trans(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.waited, 126946406);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_inode_operations() {
        let example_output = b"ig 17754368 2019571 102 15734797 0 15672217 3962470";
        match super::ig(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.cache_lookups, 17754368);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_log_operations() {
        let example_output = b"log 129491915 3992515264 458018 153771989 127040250";
        match super::log(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.log_writes, 129491915);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_tail_pushing_stats() {
        let example_output = b"push_ail 171473415 0 6896837 3324292 8069877 65884 1289485 0 22535 7337";
        match super::push_ail(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.logspace, 171473415);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_io_map_write_convert() {
        let example_output = b"xstrat 4140059 0";
        match super::xstrat(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.quick, 4140059);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_read_write_stats() {
        let example_output = b"rw 1595677950 1046884251";
        match super::rw(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.write, 1595677950);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_attribute_operations() {
        let example_output = b"attr 194724197 0 7 0";
        match super::attr(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.get, 194724197);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_inode_clustering() {
        let example_output = b"icluster 20772185 2488203 13909520";
        match super::icluster(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.count, 20772185);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_vnode_statistics() {
        let example_output = b"vnodes 62578 15959666 0 0 15897088 15897088 15897088 0";
        match super::vnodes(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.active, 62578);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_buf_statistics() {
        let example_output = b"buf 2090581631 1972536890 118044776 225145 9486625 0 0 2000152616 809762";
        match super::buf(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.get, 2090581631);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_extended_precision_counters() {
        let example_output = b"xpc 6908312903680 67735504884757 19760115252482";
        match super::xpc(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result.xstrat_bytes, 6908312903680);
            }
            _ => unreachable!(),
        }
    }

     #[test]
    fn it_parses_debug() {
        let example_output = b"debug 0";
        match super::debug(example_output) {
            nom::IResult::Done(_, result) => {
                assert_eq!(result, false);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn it_parses_example() {
        let example_output = b"extent_alloc 4260849 125170297 4618726 131131897
abt 29491162 337391304 11257328 11133039
blk_map 381213360 115456141 10903633 69612322 7448401 507596777 0
bmbt 771328 6236258 602114 86646
dir 21253907 6921870 6969079 779205554
trans 126946406 38184616 6342392
ig 17754368 2019571 102 15734797 0 15672217 3962470
log 129491915 3992515264 458018 153771989 127040250
push_ail 171473415 0 6896837 3324292 8069877 65884 1289485 0 22535 7337
xstrat 4140059 0
rw 1595677950 1046884251
attr 194724197 0 7 0
icluster 20772185 2488203 13909520
vnodes 62578 15959666 0 0 15897088 15897088 15897088 0
buf 2090581631 1972536890 118044776 225145 9486625 0 0 2000152616 809762
xpc 6908312903680 67735504884757 19760115252482
debug 0";
        let result = super::parse(example_output).unwrap();

        assert_eq!(result.extent_allocation.freed_extents, 4618726);
        assert_eq!(result.allocation_btree.lookups, 29491162);
        assert_eq!(result.block_mapping.unmap, 10903633);
        assert_eq!(result.block_map_btree.inserts, 602114);
        assert_eq!(result.directory_operations.get_dents, 779205554);
        assert_eq!(result.transactions.empty, 6342392);
        assert_eq!(result.inode_operations.inode_attr_changes, 3962470);
        assert_eq!(result.log_operations.force_sleep, 127040250);
        assert_eq!(result.tail_pushing_stats.push_ail_flush, 7337);
        assert_eq!(result.io_map_write_convert.split, 0);
        assert_eq!(result.read_write_stats.read, 1046884251);
        assert_eq!(result.attribute_operations.list, 0);
        assert_eq!(result.inode_clustering.flushinode, 13909520);
        assert_eq!(result.vnode_statistics.free, 0);
        assert_eq!(result.buf_statistics.get_read, 809762);
        assert_eq!(result.extended_precision_counters.read_bytes, 19760115252482);
        assert_eq!(result.debug, false);
    }

    #[test]
    fn it_parses_newer_version_with_extra_fields() {
        let example_output = b"extent_alloc 0 0 0 0
abt 0 0 0 0
blk_map 0 0 0 0 0 0 0
bmbt 0 0 0 0
dir 0 0 0 0
trans 0 0 0
ig 0 0 0 0 0 0 0
log 0 0 0 0 0
push_ail 0 0 0 0 0 0 0 0 0 0
xstrat 0 0
rw 0 0
attr 0 0 0 0
icluster 0 0 0
vnodes 0 0 0 0 0 0 0 0
buf 0 0 0 0 0 0 0 0 0
abtb2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
abtc2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
bmbt2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ibt2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
fibt2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
qm 0 0 0 0 0 0 0 0
xpc 0 0 0
debug 0
";
        let result = super::parse(example_output).unwrap();
        assert_eq!(result.extent_allocation.freed_extents, 0);
        assert_eq!(result.allocation_btree.lookups, 0);
        assert_eq!(result.block_mapping.unmap, 0);
        assert_eq!(result.block_map_btree.inserts, 0);
        assert_eq!(result.directory_operations.get_dents, 0);
        assert_eq!(result.transactions.empty, 0);
        assert_eq!(result.inode_operations.inode_attr_changes, 0);
        assert_eq!(result.log_operations.force_sleep, 0);
        assert_eq!(result.tail_pushing_stats.push_ail_flush, 0);
        assert_eq!(result.io_map_write_convert.split, 0);
        assert_eq!(result.read_write_stats.read, 0);
        assert_eq!(result.attribute_operations.list, 0);
        assert_eq!(result.inode_clustering.flushinode, 0);
        assert_eq!(result.vnode_statistics.free, 0);
        assert_eq!(result.buf_statistics.get_read, 0);
        assert_eq!(result.extended_precision_counters.read_bytes, 0);
        assert_eq!(result.debug, false);
    }
}

pub struct XfsStat {
    pub extent_allocation: ExtentAllocation,
    pub allocation_btree: AllocationBTree,
    pub block_mapping: BlockMapping,
    pub block_map_btree: BlockMapBTree,
    pub directory_operations: DirectoryOperations,
    pub transactions: Transactions,
    pub inode_operations: InodeOperations,
    pub log_operations: LogOperations,
    pub tail_pushing_stats: TailPushingStats,
    pub io_map_write_convert: IoMapWriteConvert,
    pub read_write_stats: ReadWriteStats,
    pub attribute_operations: AttributeOperations,
    pub inode_clustering: InodeClustering,
    pub vnode_statistics: VnodeStatistics,
    pub buf_statistics: BufStatistics,
    pub extended_precision_counters: ExtendedPrecisionCounters,
    pub debug: bool,
}

pub struct ExtentAllocation {
    /// Number of file system extents allocated over all XFS filesystems.
    pub allocated_extents: u32,
    /// Number of file system blocks allocated over all XFS filesystems.
    pub allocated_blocks: u32,
    /// Number of file system extents freed over all XFS filesystems.
    pub freed_extents: u32,
    /// Number of file system blocks freed over all XFS filesystems.
    pub freed_blocks: u32,
}

pub struct AllocationBTree {
    /// Number of lookup operations in XFS filesystem allocation btrees.
    pub lookups: u32,
    /// Number of compares in XFS filesystem allocation btree lookups.
    pub compares: u32,
    /// Number of extent records inserted into XFS filesystem allocation btrees.
    pub inserts: u32,
    /// Number of extent records deleted from XFS filesystem allocation btrees.
    pub deletes: u32,
}

pub struct BlockMapping {
    /// Number of block map for read operations performed on XFS files.
    pub map_read: u32,
    /// Number of block map for write operations performed on XFS files.
    pub map_write: u32,
    /// Number of block unmap (delete) operations performed on XFS files.
    pub unmap: u32,
    /// Number of extent list insertion operations for XFS files.
    pub list_insert: u32,
    /// Number of extent list deletion operations for XFS files.
    pub list_delete: u32,
    /// Number of extent list lookup operations for XFS files.
    pub list_lookup: u32,
    /// Number of extent list comparisons in XFS extent list lookups.
    pub list_compare: u32,
}

pub struct BlockMapBTree {
    /// Number of block map btree lookup operations on XFS files.
    pub lookups: u32,
    /// Number of block map btree compare operations in XFS block map lookups.
    pub compares: u32,
    /// Number of block map btree records inserted for XFS files.
    pub inserts: u32,
    /// Number of block map btree records deleted for XFS files.
    pub deletes: u32,
}

pub struct DirectoryOperations {
    /// This is a count of the number of file name directory lookups in XFS
    /// filesystems. It counts only those lookups which miss in the operating
    /// system's directory name lookup cache and must search the real directory
    /// structure for the name in question. The count is incremented once for each
    /// level of a pathname search that results in a directory lookup.
    pub lookups: u32,
    /// This is the number of times a new directory entry was created in XFS filesystems. Each time that a new file, directory, link, symbolic link, or special file is created in the directory hierarchy the count is incremented.
    pub creates: u32,
    /// This is the number of times an existing directory entry was removed in XFS filesystems. Each time that a file, directory, link, symbolic link, or special file is removed from the directory hierarchy the count is incremented.
    pub removes: u32,
    /// This is the number of times the XFS directory getdents operation was performed. The getdents operation is used by programs to read the contents of directories in a file system independent fashion. This count corresponds exactly to the number of times the getdents(2) system call was successfully used on an XFS directory.
    pub get_dents: u32,
}

pub struct Transactions {
    /// This is the number of meta-data transactions which waited to be committed to the on-disk log before allowing the process performing the transaction to continue. These transactions are slower and more expensive than asynchronous transactions, because they force the in memory log buffers to be forced to disk more often and they wait for the completion of the log buffer writes. Synchronous transactions include file truncations and all directory updates when the file system is mounted with the 'wsync' option.
    pub waited: u32,
    /// This is the number of meta-data transactions which did not wait to be committed to the on-disk log before allowing the process performing the transaction to continue. These transactions are faster and more efficient than synchronous transactions, because they commit their data to the in memory log buffers without forcing those buffers to be written to disk. This allows multiple asynchronous transactions to be committed to disk in a single log buffer write. Most transactions used in XFS file systems are asynchronous.
    pub async: u32,
    /// This is the number of meta-data transactions which did not actually change anything. These are transactions which were started for some purpose, but in the end it turned out that no change was necessary.
    pub empty: u32,
}

pub struct InodeOperations {
    /// This is the number of times the operating system looked for an XFS inode in the inode cache. Whether the inode was found in the cache or needed to be read in from the disk is not indicated here, but this can be computed from the ig_found and ig_missed counts.
    pub cache_lookups: u32,
    /// This is the number of times the operating system looked for an XFS inode in the inode cache and found it. The closer this count is to the ig_attempts count the better the inode cache is performing.
    pub cache_hits: u32,
    /// This is the number of times the operating system looked for an XFS inode in the inode cache and saw that it was there but was unable to use the in memory inode because it was being recycled by another process.
    pub cache_recycle: u32,
    /// This is the number of times the operating system looked for an XFS inode in the inode cache and the inode was not there. The further this count is from the ig_attempts count the better.
    pub cache_missed: u32,
    /// This is the number of times the operating system looked for an XFS inode in the inode cache and found that it was not there but upon attempting to add the inode to the cache found that another process had already inserted it.
    pub cache_dup: u32,
    /// This is the number of times the operating system recycled an XFS inode from the inode cache in order to use the memory for that inode for another purpose. Inodes are recycled in order to keep the inode cache from growing without bound. If the reclaim rate is high it may be beneficial to raise the vnode_free_ratio kernel tunable variable to increase the size of the inode cache.
    pub cache_reclaime: u32,
    /// This is the number of times the operating system explicitly changed the attributes of an XFS inode. For example, this could be to change the inode's owner, the inode's size, or the inode's timestamps.
    pub inode_attr_changes: u32,
}

pub struct LogOperations {
    /// This variable counts the number of log buffer writes going to the physical log partitions of all XFS filesystems. Log data traffic is proportional to the level of meta-data updating. Log buffer writes get generated when they fill up or external syncs occur.
    pub log_writes: u32,
    /// This variable counts (in 512-byte units) the information being written to the physical log partitions of all XFS filesystems. Log data traffic is proportional to the level of meta-data updating. The rate with which log data gets written depends on the size of internal log buffers and disk write speed. Therefore, filesystems with very high meta-data updating may need to stripe the log partition or put the log partition on a separate drive.
    pub log_blocks: u32,
    /// This variable keeps track of times when a logged transaction can not get any log buffer space. When this occurs, all of the internal log buffers are busy flushing their data to the physical on-disk log.
    pub noiclogs: u32,
    /// The number of times the in-core log is forced to disk. It is equivalent to the number of successful calls to the function xfs_log_force().
    pub log_forced: u32,
    /// Value exported from the xs_log_force_sleep field of struct xfsstats.
    pub force_sleep: u32,
}

pub struct TailPushingStats {
    /// Value from the xs_try_logspace field of struct xfsstats.
    pub logspace: u32,
    /// Value from the xs_sleep_logspace field of struct xfsstats.
    pub sleep_logspace: u32,
    /// The number of times the tail of the AIL is moved forward. It is equivalent to the number of successful calls to the function xfs_trans_push_ail().
    pub push_ails: u32,
    /// Value from xs_push_ail_success field of struct xfsstats.
    pub push_ail_success: u32,
    /// Value from xs_push_ail_pushbuf field of struct xfsstats.
    pub push_ail_pushbuf: u32,
    /// Value from xs_push_ail_pinned field of struct xfsstats.
    pub push_ail_pinned: u32,
    /// Value from xs_push_ail_locked field of struct xfsstats.
    pub push_ail_locked: u32,
    /// Value from xs_push_ail_flushing field of struct xfsstats.
    pub push_ail_flushing: u32,
    /// Value from xs_push_ail_restarts field of struct xfsstats.
    pub push_ail_restarts: u32,
    /// Value from xs_push_ail_flush field of struct xfsstats.
    pub push_ail_flush: u32,
}

pub struct IoMapWriteConvert {
    /// This is the number of buffers flushed out by the XFS flushing daemons which are written to contiguous space on disk. The buffers handled by the XFS daemons are delayed allocation buffers, so this count gives an indication of the success of the XFS daemons in allocating contiguous disk space for the data being flushed to disk.
    pub quick: u32,
    /// This is the number of buffers flushed out by the XFS flushing daemons which are written to non-contiguous space on disk. The buffers handled by the XFS daemons are delayed allocation buffers, so this count gives an indication of the failure of the XFS daemons in allocating contiguous disk space for the data being flushed to disk. Large values in this counter indicate that the file system has become fragmented.
    pub split: u32,
}

pub struct ReadWriteStats {
    /// This is the number of write(2) system calls made to files in XFS file systems.
    pub write: u32,
    /// This is the number of read(2) system calls made to files in XFS file systems.
    pub read: u32,
}

pub struct AttributeOperations {
    /// The number of "get" operations performed on extended file attributes within XFS filesystems. The "get" operation retrieves the value of an extended attribute.
    pub get: u32,
    /// The number of "set" operations performed on extended file attributes within XFS filesystems. The "set" operation creates and sets the value of an extended attribute.
    pub set: u32,
    /// The number of "remove" operations performed on extended file attributes within XFS filesystems. The "remove" operation deletes an extended attribute.
    pub remove: u32,
    /// The number of "list" operations performed on extended file attributes within XFS filesystems. The "list" operation retrieves the set of extended attributes associated with a file.
    pub list: u32,
}

pub struct InodeClustering {
    /// This is the number of calls to xfs_iflush which gets called when an inode is being flushed (such as by bdflush or tail pushing). xfs_iflush searches for other inodes in the same cluster which are dirty and flushable.
    pub count: u32,
    /// Value from xs_icluster_flushcnt field of struct xfsstats.
    pub flushcnt: u32,
    /// This is the number of times that the inode clustering was not able to flush anything but the one inode it was called with.
    pub flushinode: u32,
}

pub struct VnodeStatistics {
    /// Number of vnodes not on free lists.
    pub active: u32,
    /// Number of times vn_alloc called.
    pub alloc: u32,
    /// Number of times vn_get called.
    pub get: u32,
    /// Number of times vn_hold called.
    pub hold: u32,
    /// Number of times vn_rele called.
    pub rele: u32,
    /// Number of times vn_reclaim called.
    pub reclaim: u32,
    /// Number of times vn_remove called.
    pub remove: u32,
    /// Number of times vn_free called.
    pub free: u32,
}

pub struct BufStatistics {
    pub get: u32,
    pub create: u32,
    pub get_locked: u32,
    pub get_locked_waited: u32,
    pub busy_locked: u32,
    pub miss_locked: u32,
    pub page_retries: u32,
    pub page_found: u32,
    pub get_read: u32,
}

pub struct ExtendedPrecisionCounters {
    /// This is a count of bytes of file data flushed out by the XFS flushing daemons.
    pub xstrat_bytes: u64,
    /// This is a count of bytes written via write(2) system calls to files in XFS file systems. It can be used in conjunction with the write_calls count to calculate the average size of the write operations to files in XFS file systems.
    pub write_bytes: u64,
    /// This is a count of bytes read via read(2) system calls to files in XFS file systems. It can be used in conjunction with the read_calls count to calculate the average size of the read operations to files in XFS file systems.
    pub read_bytes: u64,
}

#[derive(Debug)]
pub enum XfsError {
    /// We encounter an error reading from /proc/fs/xfs/stat
    Io(io::Error),
    /// We don't have enough information for a complete parse
    Incomplete,
    /// We encounter an error with the data wer're parsing
    Parse,
}

impl fmt::Display for XfsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            // Both underlying errors already impl `Display`, so we defer to
            // their implementations.
            XfsError::Io(ref err) => write!(f, "IO error: {}", err),
            XfsError::Incomplete => write!(f, "Not enough data for XFS parse"),
            XfsError::Parse => write!(f, "Parse error"),
        }
    }
}

impl error::Error for XfsError {
    fn description(&self) -> &str {
        // Both underlying errors already impl `Error`, so we defer to their
        // implementations.
        match *self {
            XfsError::Io(ref err) => err.description(),
            XfsError::Incomplete => "There is not enough data for parsing",
            XfsError::Parse => "There was an error parsing",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            // N.B. Both of these implicitly cast `err` from their concrete
            // types (either `&io::Error` or `&num::ParseIntError`)
            // to a trait object `&Error`. This works because both error types
            // implement `Error`.
            XfsError::Io(ref err) => Some(err),
            XfsError::Incomplete => None,
            XfsError::Parse => None,
        }
    }
}

impl From<io::Error> for XfsError {
    fn from(err: io::Error) -> XfsError {
        XfsError::Io(err)
    }
}

pub fn parse(input: &[u8]) -> Result<XfsStat, XfsError> {
    match xfs_stat(input) {
        nom::IResult::Done(_, stat) => Ok(stat),
        nom::IResult::Error(_) => {
            Err(XfsError::Parse)
        },
        nom::IResult::Incomplete(_) => {
            Err(XfsError::Incomplete)
        },
        // _ => None,
    }
}

pub fn read() -> Result<String, XfsError> {
    let mut f = try!(File::open("/proc/fs/xfs/stat"));
    let mut s = String::new();
    try!(f.read_to_string(&mut s));
    Ok(s)
}

pub fn get() -> Result<XfsStat, XfsError> {
    let contents = try!(read());
    parse((&contents[..]).as_bytes())
}

named!(xfs_stat <XfsStat>,
  chain!(
    extent_alloc: extent_alloc ~
    newline ~
    abt: abt ~
    newline ~
    blk_map: blk_map ~
    newline ~
    block_map_btree: bmbt ~
    newline ~
    directory_operations: dir ~
    newline ~
    transactions: trans ~
    newline ~
    inode_operations: ig ~
    newline ~
    log_operations: log ~
    newline ~
    tail_pushing_stats: push_ail ~
    newline ~
    io_map_write_convert: xstrat ~
    newline ~
    read_write_stats: rw ~
    newline ~
    attribute_operations: attr ~
    newline ~
    inode_clustering: icluster ~
    newline ~
    vnode_statistics: vnodes ~
    newline ~
    buf_statistics: buf ~
    newline ~
    extended_precision_counters: xpc ~
    newline ~
    dbg: debug,
    || {
      XfsStat {
        extent_allocation: extent_alloc,
        allocation_btree: abt,
        block_mapping: blk_map,
        block_map_btree: block_map_btree,
        directory_operations: directory_operations,
        transactions: transactions,
        inode_operations: inode_operations,
        log_operations:log_operations,
        tail_pushing_stats: tail_pushing_stats,
        io_map_write_convert: io_map_write_convert,
        read_write_stats: read_write_stats,
        attribute_operations: attribute_operations,
        inode_clustering: inode_clustering,
        vnode_statistics: vnode_statistics,
        buf_statistics: buf_statistics,
        extended_precision_counters: extended_precision_counters,
        debug: dbg,
      }
    }
  )
);

named!(debug <bool>,
    chain!(
        tag!("debug") ~
        space ~
        dbg: le_u8,
        || {
            if dbg == 1 {
                true
            } else {
                false
            }
        }
    )
);

named!(take_u32 <u32>,
  chain!(
    uint_slice: take_while!(is_digit) ~
    opt!(space),
    || {
      let int_str = String::from_utf8_lossy(uint_slice);
      u32::from_str(&int_str[..]).unwrap()
    }
  )
);

named!(take_u64 <u64>,
  chain!(
    uint_slice: take_while!(is_digit) ~
    opt!(space),
    || {
      let int_str = String::from_utf8_lossy(uint_slice);
      u64::from_str(&int_str[..]).unwrap()
    }
  )
);


named!(extent_alloc <ExtentAllocation>,
  chain!(
    tag!("extent_alloc") ~
    space ~
    allocx: take_u32 ~
    allocb: take_u32 ~
    freex: take_u32 ~
    freeb: take_u32,
    || {
      ExtentAllocation {
        allocated_extents: allocx,
        allocated_blocks: allocb,
        freed_extents: freex,
        freed_blocks: freeb,
      }
    }
  )
);

named!(abt <AllocationBTree>,
  chain!(
    tag!("abt") ~
    space ~
    lookups: take_u32 ~
    compares: take_u32 ~
    inserts: take_u32 ~
    deletes: take_u32,
    || {
      AllocationBTree {
        lookups: lookups,
        compares: compares,
        inserts: inserts,
        deletes: deletes,
      }
    }
  )
);

named!(blk_map <BlockMapping>,
  chain!(
    tag!("blk_map") ~
    space ~
    map_read: take_u32 ~
    map_write: take_u32 ~
    unmap: take_u32 ~
    list_insert: take_u32 ~
    list_delete: take_u32 ~
    list_lookup: take_u32 ~
    list_compare: take_u32,
    ||{
      BlockMapping {
        map_read: map_read,
        map_write: map_write,
        unmap: unmap,
        list_insert: list_insert,
        list_delete: list_delete,
        list_lookup: list_lookup,
        list_compare: list_compare,
      }
    }
  )
);

named!(bmbt <BlockMapBTree>,
  chain!(
    tag!("bmbt") ~
    space ~
    lookup: take_u32 ~
    compare: take_u32 ~
    insrec: take_u32 ~
    delrec: take_u32,
    || {
      BlockMapBTree {
        lookups: lookup,
        compares: compare,
        inserts: insrec,
        deletes: delrec,
      }
    }
  )
);

named!(dir <DirectoryOperations>,
    chain!(
        tag!("dir") ~
        space ~
        lookups: take_u32 ~
        creates: take_u32 ~
        removes: take_u32 ~
        get_dents: take_u32,
        || {
            DirectoryOperations {
                lookups: lookups,
                creates: creates,
                removes: removes,
                get_dents: get_dents,
            }
        }
    )
);

named!(trans <Transactions>,
  chain!(
    tag!("trans") ~
    space ~
    waited: take_u32 ~
    async: take_u32 ~
    empty: take_u32,
    ||{
      Transactions {
        waited: waited,
        async: async,
        empty: empty,
      }
    }
  )
);

named!(ig <InodeOperations>,
  chain!(
    tag!("ig") ~
    space ~
    cache_lookups: take_u32 ~
    cache_hits: take_u32 ~
    cache_recycle: take_u32 ~
    cache_missed: take_u32 ~
    cache_dup: take_u32 ~
    cache_reclaime: take_u32 ~
    inode_attr_changes: take_u32,
    || {
      InodeOperations {
        cache_lookups: cache_lookups,
        cache_hits: cache_hits,
        cache_recycle: cache_recycle,
        cache_missed: cache_missed,
        cache_dup: cache_dup,
        cache_reclaime: cache_reclaime,
        inode_attr_changes: inode_attr_changes,
      }
    }
  )
);

named!(log <LogOperations>,
  chain!(
    tag!("log") ~
    space ~
    log_writes: take_u32 ~
    log_blocks: take_u32 ~
    noiclogs: take_u32 ~
    log_forced: take_u32 ~
    force_sleep: take_u32,
    || {
      LogOperations {
          log_writes: log_writes,
          log_blocks: log_blocks,
          noiclogs: noiclogs,
          log_forced: log_forced,
          force_sleep: force_sleep,
      }
    }
  )
);

named!(push_ail <TailPushingStats>,
    chain!(
        tag!("push_ail") ~
        space ~
        logspace: take_u32 ~
        sleep_logspace: take_u32 ~
        push_ails: take_u32 ~
        push_ail_success: take_u32 ~
        push_ail_pushbuf: take_u32 ~
        push_ail_pinned: take_u32 ~
        push_ail_locked: take_u32 ~
        push_ail_flushing: take_u32 ~
        push_ail_restarts: take_u32 ~
        push_ail_flush: take_u32,
        || {
            TailPushingStats {
                logspace: logspace,
                sleep_logspace: sleep_logspace,
                push_ails: push_ails,
                push_ail_success: push_ail_success,
                push_ail_pushbuf: push_ail_pushbuf,
                push_ail_pinned: push_ail_pinned,
                push_ail_locked: push_ail_locked,
                push_ail_flushing: push_ail_flushing,
                push_ail_restarts: push_ail_restarts,
                push_ail_flush: push_ail_flush,
            }
        }
    )
);

named!(xstrat <IoMapWriteConvert>,
    chain!(
        tag!("xstrat") ~
        space ~
        quick: take_u32 ~
        split: take_u32,
        || {
            IoMapWriteConvert {
                quick: quick,
                split: split,
            }
        }
    )
);

named!(rw <ReadWriteStats>,
    chain!(
        tag!("rw") ~
        space ~
        write: take_u32 ~
        read: take_u32,
        || {
            ReadWriteStats {
                write: write,
                read: read,
            }
        }
    )
);

named!(attr <AttributeOperations>,
    chain!(
        tag!("attr") ~
        space  ~
        get: take_u32 ~
        set: take_u32 ~
        remove: take_u32 ~
        list: take_u32,
        || {
            AttributeOperations {
                get: get,
                set: set,
                remove: remove,
                list: list,
            }
        }
    )
);

named!(icluster <InodeClustering>,
    chain!(
        tag!("icluster") ~
        space ~
        count: take_u32 ~
        flushcnt: take_u32 ~
        flushinode: take_u32,
        || {
            InodeClustering {
                count: count,
                flushcnt: flushcnt,
                flushinode: flushinode,
            }
        }
    )
);

named!(vnodes <VnodeStatistics>,
    chain!(
        tag!("vnodes") ~
        space ~
        active: take_u32 ~
        alloc: take_u32 ~
        get: take_u32 ~
        hold: take_u32 ~
        rele: take_u32 ~
        reclaim: take_u32 ~
        remove: take_u32 ~
        free: take_u32,
        || {
            VnodeStatistics {
                active: active,
                alloc: alloc,
                get: get,
                hold: hold,
                rele: rele,
                reclaim: reclaim,
                remove: remove,
                free: free,
            }

        }
    )
);

named!(buf <BufStatistics>,
    chain!(
        tag!("buf") ~
        space ~
        get: take_u32 ~
        create: take_u32 ~
        get_locked: take_u32 ~
        get_locked_waited: take_u32 ~
        busy_locked: take_u32 ~
        miss_locked: take_u32 ~
        page_retries: take_u32 ~
        page_found: take_u32 ~
        get_read: take_u32,
        || {
            BufStatistics {
                get: get,
                create: create,
                get_locked: get_locked,
                get_locked_waited: get_locked_waited,
                busy_locked: busy_locked,
                miss_locked: miss_locked,
                page_retries: page_retries,
                page_found: page_found,
                get_read: get_read,
            }

        }
    )
);

named!(xpc <ExtendedPrecisionCounters>,
    chain!(
        take_until!("xpc") ~
        tag!("xpc") ~
        space ~
        xstrat_bytes: take_u64 ~
        write_bytes: take_u64 ~
        read_bytes: take_u64,
        ||{
            ExtendedPrecisionCounters {
                xstrat_bytes: xstrat_bytes,
                write_bytes: write_bytes,
                read_bytes: read_bytes,
            }
        }
    )
);