• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: str):
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 summary_ccache_size():
52    cache_size, ccache_version = "", ""
53    ccache_exec = os.environ.get('CCACHE_EXEC')
54    ccache_dir = os.environ.get('CCACHE_DIR')
55    print("ccache_dir = {}, ccache_exec = {}".format(ccache_dir, ccache_exec))
56    if ccache_exec is None or ccache_dir is None:
57        print("ccache_exec or ccache_dir is None, summary ccache size failed...")
58        return cache_size, ccache_version
59    cmd = [ccache_exec, '-s', '-V']
60    process = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0].decode("utf-8").split("\n")
61    cache_size_str = "Cache size"
62    ccache_version_str = "ccache version"
63    for line in process:
64        if cache_size_str in line:
65            cache_size = line.split(":")[1].strip()
66        if ccache_version_str in line:
67            ccache_version = line.split(" ")[2].strip()
68    return cache_size, ccache_version
69
70
71def main():
72    if len(sys.argv) < 2:
73        print("Error, please input the ccache log file path.")
74        exit(-1)
75
76    ccache_log = sys.argv[1]
77    hit_rate = 0
78    miss_rate = 0
79    hit_dir_num = 0
80    hit_pre_num = 0
81    miss_num = 0
82    cache_size = ""
83    ccache_version = ""
84    if os.path.exists(ccache_log):
85        hit_rate, miss_rate, hit_dir_num, hit_pre_num, miss_num = summary_ccache_new(
86            ccache_log)
87        cache_size, ccache_version = summary_ccache_size()
88
89    print(f"--------------------------------------------\n" +
90          "ccache summary:\n" +
91          "ccache version: " + ccache_version + "\n" +
92          "cache hit (direct): " + str(hit_dir_num) + "\n" +
93          "cache hit (preprocessed): " + str(hit_pre_num) + "\n" +
94          "cache miss: " + str(miss_num) + "\n" +
95          "hit rate: %.2f%% " % (hit_rate * 100) + "\n" +
96          "miss rate: %.2f%% " % (miss_rate * 100) + "\n" +
97          "Cache size (GB): " + cache_size + "\n" +
98          "---------------------------------------------")
99
100
101if __name__ == "__main__":
102    main()
103