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