-
Notifications
You must be signed in to change notification settings - Fork 3
/
statistics_pre37.go
145 lines (126 loc) · 4.53 KB
/
statistics_pre37.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2018-2022 VirtualTam.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package ccache
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/alecthomas/units"
)
var pre37StatisticsRules = map[string]*regexp.Regexp{
"statsZeroTime": regexp.MustCompile(`stats zero( time|ed)\s+(.*)`),
"cacheHitDirect": regexp.MustCompile(`cache hit \(direct\)\s+(\d+)`),
"cacheHitPreprocessed": regexp.MustCompile(`cache hit \(preprocessed\)\s+(\d+)`),
"cacheMiss": regexp.MustCompile(`cache miss\s+(\d+)`),
"cacheHitRate": regexp.MustCompile(`cache hit rate\s+(\d+(\.\d+)?) %`),
"calledForLink": regexp.MustCompile(`called for link\s+(\d+)`),
"calledForPreprocessing": regexp.MustCompile(`called for preprocessing\s+(\d+)`),
"unsupportedCodeDirective": regexp.MustCompile(`unsupported code directive\s+(\d+)`),
"noInputFile": regexp.MustCompile(`no input file\s+(\d+)`),
"cleanupsPerformed": regexp.MustCompile(`cleanups performed\s+(\d+)`),
"filesInCache": regexp.MustCompile(`files in cache\s+(\d+)`),
"cacheSize": regexp.MustCompile(`cache size\s+(.+)`),
}
// ParsePre37Statistics reads ccache configuration and statistics as formatted by the `ccache --show-stats` command.
//
// Starting with ccache 3.7, this command was overhauled to print human-readable
// statistics, with `ccache --print-stats` being the new command to get
// machine-readable statistics.
func ParsePre37Statistics(text string) (*Statistics, error) {
stats := &Statistics{}
var err error
// now's the time
stats.StatsTime = time.Now()
// assume stats originate from the local host
matches := pre37StatisticsRules["statsZeroTime"].FindStringSubmatch(text)
if len(matches) == 3 {
statsZeroTime := pre37StatisticsRules["statsZeroTime"].FindStringSubmatch(text)[2]
stats.StatsZeroTime, err = time.ParseInLocation("Mon Jan 2 15:04:05 2006", statsZeroTime, stats.StatsTime.Location())
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cacheHitDirect"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CacheHitDirect, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cacheHitPreprocessed"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CacheHitPreprocessed, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cacheMiss"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CacheMiss, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cacheHitRate"].FindStringSubmatch(text)
if len(matches) == 3 {
stats.CacheHitRate, err = strconv.ParseFloat(matches[1], 64)
stats.CacheHitRatio = stats.CacheHitRate / 100
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["calledForLink"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CalledForLink, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["calledForPreprocessing"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CalledForPreprocessing, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["unsupportedCodeDirective"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.UnsupportedCodeDirective, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["noInputFile"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.NoInputFile, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cleanupsPerformed"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CleanupsPerformed, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["filesInCache"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.FilesInCache, err = strconv.Atoi(matches[1])
if err != nil {
return &Statistics{}, err
}
}
matches = pre37StatisticsRules["cacheSize"].FindStringSubmatch(text)
if len(matches) == 2 {
stats.CacheSize = matches[1]
sanitizedCacheSize := strings.Replace(strings.ToUpper(stats.CacheSize), " ", "", -1)
stats.CacheSizeBytes, err = units.ParseMetricBytes(sanitizedCacheSize)
if err != nil {
return &Statistics{}, err
}
}
return stats, nil
}