1#!/usr/bin/python3
2# -*- coding: UTF-8 -*-
3
4"""
5This is the main script of arnalyzer, which:
6- parse the options
7- call a specific script to do analysis
8"""
9
10import sys
11import getopt
12import os
13from vmexit_analyze import analyze_vm_exit
14from irq_analyze import analyze_irq
15from cpuusage_analyze import analyze_cpu_usage
16
17def usage():
18    """print the usage of the script
19    Args: NA
20    Returns: None
21    Raises: NA
22    """
23    print ('''
24    [Usage] acrnalyze.py [options] [value] ...
25
26    [options]
27    -h: print this message
28    -i, --ifile=[string]: input file
29    -o, --ofile=[string]: output file
30    -f, --frequency=[unsigned int]: TSC frequency in MHz
31    --vm_exit: to generate vm_exit report
32    --irq: to generate irq related report
33    --cpu_usage: to generate cpu_usage report
34    ''')
35
36def do_analysis(ifile, ofile, analyzer, freq):
37    """do the specific analysis
38
39    Args:
40        ifile: input trace data file
41        ofile: output analysis report file
42        analyzer: a function do the specific analysis
43        freq: TSC frequency of the host where we capture the trace data
44    Returns:
45        None
46    Raises:
47        NA
48    """
49    for alyer in analyzer:
50        alyer(ifile, ofile, freq)
51
52def main(argv):
53    """Main enterance function
54
55    Args:
56        argv: arguments string
57    Returns:
58        None
59    Raises:
60        GetoptError
61    """
62    inputfile = ''
63    outputfile = ''
64    # Default TSC frequency of MRB in MHz
65    freq = 1881.6
66    opts_short = "hi:o:f:"
67    opts_long = ["ifile=", "ofile=", "frequency=", "vm_exit", "irq", "cpu_usage"]
68    analyzer = []
69
70    try:
71        opts, args = getopt.getopt(argv, opts_short, opts_long)
72    except getopt.GetoptError:
73        usage()
74        sys.exit(1)
75
76    for opt, arg in opts:
77        if opt == '-h':
78            usage()
79            sys.exit()
80        elif opt in ("-i", "--ifile"):
81            inputfile = arg
82        elif opt in ("-o", "--ofile"):
83            outputfile = arg
84        elif opt in ("-f", "--frequency"):
85            freq = arg
86        elif opt == "--vm_exit":
87            analyzer.append(analyze_vm_exit)
88        elif opt == "--irq":
89            analyzer.append(analyze_irq)
90        elif opt == "--cpu_usage":
91            analyzer.append(analyze_cpu_usage)
92        else:
93            assert False, "unhandled option"
94
95    assert inputfile != '', "input file is required"
96    assert outputfile != '', "output file is required"
97    assert analyzer != '', 'MUST contain one of analyzer: ''vm_exit'
98
99    do_analysis(inputfile, outputfile, analyzer, freq)
100
101if __name__ == "__main__":
102    main(sys.argv[1:])
103