xref: /XiangShan/scripts/statistics.py (revision 708ceed4afe43fb0ea3a52407e46b2794c573634)
1#***************************************************************************************
2# Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3# Copyright (c) 2020-2021 Peng Cheng Laboratory
4#
5# XiangShan is licensed under Mulan PSL v2.
6# You can use this software according to the terms and conditions of the Mulan PSL v2.
7# You may obtain a copy of Mulan PSL v2 at:
8#          http://license.coscl.org.cn/MulanPSL2
9#
10# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13#
14# See the Mulan PSL v2 for more details.
15#***************************************************************************************
16
17import os
18import argparse
19
20def printMap(mp):
21    len_key = max(map(lambda s: len(s), mp.keys()))
22    len_value = max(map(lambda v: len(str(v)), mp.values()))
23    pattern = "{:<" +str(len_key) + "} {:<" +str(len_value)+ "} {:<7}%"
24    total = sum(mp.values())
25    for k,v in sorted(mp.items(), key=lambda x:x[1], reverse=True):
26        print(
27            pattern.format(k, v, round(v*100.0/total, 3))
28        )
29
30
31def analyzeVerilog(filename):
32    mymap = {}
33    last = ""
34    with open(filename, "r") as f:
35        line = f.readline()
36        cnt = 0
37        while(line):
38            if "module " in line:
39                if last!="" :
40                    mymap[last] = cnt
41                last = line[7:-2]
42                cnt = 1
43            else:
44                cnt = cnt + 1
45            line = f.readline()
46        mymap[last] = cnt
47        printMap(mymap)
48
49logLevels = ['ALL', 'DEBUG', 'INFO', 'WARN', 'ERROR']
50
51def listToStr(lst):
52    acc = ''
53    for l in lst:
54        acc += '|' + str(l) if acc else str(l)
55    return acc
56
57def lineStrip(line):
58    return line.replace('\n', '')
59
60def getNumLogLines(filename, modules, ll=logLevels):
61    cmd = "grep -E '\[({0}).*\]\[time=.*\] ({1}):' {2} | wc -l".format(
62        listToStr(ll),
63        listToStr(modules),
64        filename
65    )
66    res = os.popen(cmd)
67    return int(lineStrip(res.readline()), 10)
68
69def analyzeLog(filename):
70    cmd = "grep -E '\[time=.*\]' {0} ".format(filename) + " | awk -F '(:)' {'print $1'} | awk  {'print $NF'} | sort | uniq"
71    res = os.popen(cmd)
72    modules = list(map(lineStrip, res.readlines()))
73    mymap = {}
74    for m in modules:
75        mymap[m] = getNumLogLines(filename, [m])
76    printMap(mymap)
77
78def main():
79    parser = argparse.ArgumentParser()
80    parser.add_argument("-v", "--verilogFile", help="verilog file path", type=str)
81    parser.add_argument("-l", "--logFile", help="log file path", type=str)
82    args = parser.parse_args()
83
84    if args.verilogFile:
85        analyzeVerilog(args.verilogFile)
86
87    if args.logFile:
88        analyzeLog(args.logFile)
89
90    if not args.verilogFile and not args.logFile:
91        parser.print_help()
92
93if __name__ == '__main__':
94    main()
95
96