-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
254 lines (226 loc) · 6.22 KB
/
parse.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cudnn_log_parser
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"time"
"gopkg.in/yaml.v2"
"github.com/atotto/clipboard"
"github.com/gocarina/gocsv"
"github.com/k0kubun/pp"
"github.com/Unknwon/com"
"github.com/cydev/zero"
"github.com/pkg/errors"
"github.com/iancoleman/strcase"
)
var verboseDebug = false
func indent(n int) string {
if n == 0 {
return ""
}
s := make([]byte, 2*n)
for ii := range s {
s[ii] = ' '
}
return string(s)
}
func parseFunctionName(line string) (string, error) {
if !strings.HasPrefix(line, "I! CuDNN") {
return "", errors.New("not a function line")
}
params := getRegexParams(`I!.*function\s+(?P<functionName>\w+)\(.*`, line)
if f, ok := params["functionName"]; ok {
return f, nil
}
return "", errors.New("cannot find function")
}
func parseTimeStamp(line string) (time.Time, error) {
if !strings.HasPrefix(line, "i! Time") {
return time.Time{}, errors.New("not a function line")
}
params := getRegexParams(`i!\s+Time:\s+(?P<time>[\d-:T.]+)+\s+.*`, line)
if t, ok := params["time"]; ok {
// 2019-06-04T18:01:43.967769
return time.Parse("2006-01-02T15:04:05.999999999", t)
}
return time.Time{}, errors.New("cannot find time")
}
func processLine(info *Info, line string) {
// indentN := indentOf(line)
if t, err := parseTimeStamp(line); err == nil {
info.TimeStamp = t
return
}
if f, err := parseFunctionName(line); err == nil {
info.FunctionKind = strings.TrimPrefix(f, "cudnn")
info.FunctionName = f
return
}
// var re = regexp.MustCompile(`(?m)i!\s+(.*):\s((.*)=(.*);)*`)
// for i, match := range re.FindAllString(line, -1) {
// fmt.Println(match, "found at index", i)
// }
}
var indentattion = " "
var nestIndentation = "i!" + indentattion
func processNest(info *Info, log string) {
lines := strings.Split(log, "\n")
for ii, line := range lines {
lines[ii] = strings.TrimPrefix(line, nestIndentation)
}
// procesedLines := []string{}
// if err := copier.Copy(procesedLines, lines); err != nil {
// panic("unable to copy lines")
// }
endOfLineColonRe := regexp.MustCompile(`(?m).*:.*:$`)
typeAssignmentRe := regexp.MustCompile(`(?m)\s+type=`)
processedLines := []string{}
for _, line := range lines {
indentN := indentOf(line)
nextIndent := "\n" + indent(indentN+1)
if endOfLineColonRe.MatchString(line) {
line = strings.TrimSuffix(line, ":")
}
if strings.Contains(line, ": ") {
line = strings.ReplaceAll(line, ": ", ": {") + "}"
}
if indentN == 0 {
line = strings.ReplaceAll(line, ": {", ":"+nextIndent+indent(indentN+1)+"opinfo: {")
}
line = strings.ReplaceAll(line, "=", ": ")
line = strings.ReplaceAll(line, ";", ", ")
line = strings.ReplaceAll(line, ", }", " }")
if false {
if typeAssignmentRe.MatchString(line) {
line = typeAssignmentRe.ReplaceAllString(line, nextIndent+"type: ")
} else {
line = strings.ReplaceAll(line, ":", ":"+nextIndent)
}
if endOfLineColonRe.MatchString(line) {
line = strings.TrimSuffix(line, ":")
// line = strings.ReplaceAll(line, " type=", "type: ")
fmt.Println(line)
fmt.Println("----------------------")
}
line = strings.ReplaceAll(line, "=", ": ")
line = strings.ReplaceAll(line, ";", nextIndent)
}
for _, newLine := range strings.Split(line, "\n") {
if strings.TrimSpace(newLine) == "" {
continue
}
processedLines = append(processedLines, newLine)
}
}
ymlBlock := strings.Join(processedLines, "\n")
// clipboard.WriteAll(ymlBlock)
attributes := new(Attributes)
err := yaml.Unmarshal([]byte(ymlBlock), &attributes)
if err != nil {
clipboard.WriteAll(ymlBlock)
panic(err)
}
if verboseDebug {
pp.Println(attributes)
yml, err := yaml.Marshal(&attributes)
if err != nil {
panic(err)
}
_ = yml
}
info.Attributes = attributes
}
func toInfos(log string) (Infos, error) {
lines := strings.Split(log, "\n")
nst := ""
infos := make([]Info, 0, len(lines))
var currentInfo Info
for _, line := range lines {
if strings.HasPrefix(line, "I! CuDNN") && !zero.IsZero(currentInfo) {
infos = append(infos, currentInfo)
}
if !strings.HasPrefix(line, nestIndentation) {
processLine(¤tInfo, line)
continue
}
nst += line + "\n"
}
if nst != "" {
processNest(¤tInfo, nst)
if currentInfo.Attributes == nil {
currentInfo.Attributes = new(Attributes)
}
(*currentInfo.Attributes)[strcase.ToSnake("FunctionKind")] = currentInfo.FunctionKind
(*currentInfo.Attributes)[strcase.ToSnake("FunctionName")] = currentInfo.FunctionName
(*currentInfo.Attributes)[strcase.ToSnake("Duration")] = currentInfo.Duration
(*currentInfo.Attributes)[strcase.ToSnake("TimeStamp")] = currentInfo.TimeStamp
}
infos = append(infos, currentInfo)
return infos, nil
}
func (infos0 *Infos) computeDurations() {
infos := *infos0
for ii := 0; ii < len(infos)-1; ii++ {
nextTimeStamp := infos[ii+1].TimeStamp
currTimeStamp := infos[ii].TimeStamp
infos[ii].Duration = Duration(nextTimeStamp.Sub(currTimeStamp))
}
infos = infos[1:]
}
func (infos Infos) ToCSV(filename string) error {
os.MkdirAll(path.Dir(filename), os.ModePerm)
cvsFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil {
panic(err)
}
defer cvsFile.Close()
return gocsv.MarshalFile(infos, cvsFile)
}
func (infos Infos) ToJSON(filename string) error {
bts, err := json.MarshalIndent(infos, "", " ")
if err != nil {
panic(err)
}
return com.WriteFile(filename, bts)
}
func ParseBlock(log string) (Infos, error) {
infos, err := toInfos(log)
if err != nil {
return nil, errors.Wrap(err, "cannot parse into infos")
}
infos.computeDurations()
return infos, err
}
func Parse(log string) (Infos, error) {
lines := strings.Split(log, "\n")
var infos Infos
buf := ""
for _, line := range lines {
if strings.TrimSpace(line) == "" && buf != "" {
blk, err := toInfos(buf)
if err != nil {
return nil, errors.Wrap(err, "cannot parse into infos")
}
infos = append(infos, blk...)
buf = ""
continue
}
buf += line + "\n"
}
infos.computeDurations()
return infos, nil
}
func ParseFile(file string) ([]Info, error) {
if !com.IsFile(file) {
return nil, errors.Errorf("unable to find %v", file)
}
bts, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
return Parse(string(bts))
}