1#!/usr/bin/env python3 2# -*- coding: UTF-8 -*- 3# Copyright (c) 2021 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import os 17import sys 18import subprocess 19 20 21def summary_ccache_new(ccache_log): 22 hit_dir_num = 0 23 hit_pre_num = 0 24 mis_num = 0 25 hit_rate = 0 26 mis_rate = 0 27 hit_dir_str = "Result: direct_cache_hit" 28 hit_pre_str = "Result: preprocessed_cache_hit" 29 mis_str = "Result: cache_miss" 30 if os.path.exists(ccache_log): 31 cmd = "grep -c \'{}\' {}".format(hit_dir_str, ccache_log) 32 hit_dir_num = int( 33 subprocess.Popen(cmd, shell=True, 34 stdout=subprocess.PIPE).communicate()[0]) 35 cmd = "grep -c \'{}\' {}".format(hit_pre_str, ccache_log) 36 hit_pre_num = int( 37 subprocess.Popen(cmd, shell=True, 38 stdout=subprocess.PIPE).communicate()[0]) 39 cmd = "grep -c \'{}\' {}".format(mis_str, ccache_log) 40 mis_num = int( 41 subprocess.Popen(cmd, shell=True, 42 stdout=subprocess.PIPE).communicate()[0]) 43 sum_ccache = hit_dir_num + hit_pre_num + mis_num 44 if sum_ccache != 0: 45 hit_rate = (float(hit_dir_num) + 46 float(hit_pre_num)) / float(sum_ccache) 47 mis_rate = float(mis_num) / float(sum_ccache) 48 return hit_rate, mis_rate, hit_dir_num, hit_pre_num, mis_num 49 50 51def main(): 52 if len(sys.argv) < 2: 53 print("Error, please input the ccache log file path.") 54 exit(-1) 55 56 ccache_log = sys.argv[1] 57 hit_rate = 0 58 mis_rate = 0 59 hit_dir_num = 0 60 hit_pre_num = 0 61 mis_num = 0 62 if os.path.exists(ccache_log): 63 hit_rate, mis_rate, hit_dir_num, hit_pre_num, mis_num = summary_ccache_new( 64 ccache_log) 65 66 print("---------------------------------------------") 67 print("ccache summary:") 68 print("cache hit (direct) : " + str(hit_dir_num)) 69 print("cache hit (preprocessed) : " + str(hit_pre_num)) 70 print("cache miss : " + str(mis_num)) 71 print("hit rate: %.2f%% " % (hit_rate * 100)) 72 print("mis rate: %.2f%% " % (mis_rate * 100)) 73 print("---------------------------------------------") 74 75 76if __name__ == "__main__": 77 main() 78