-
Notifications
You must be signed in to change notification settings - Fork 8
/
trace.go
128 lines (113 loc) · 3.73 KB
/
trace.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
package onnxruntime
import (
"context"
"encoding/json"
"fmt"
"sort"
"time"
"github.com/c3sr/tracer"
opentracing "github.com/opentracing/opentracing-go"
)
type TraceEvent struct {
Category string `json:"cat,omitempty"`
Name string `json:"name,omitempty"`
Phase string `json:"ph,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Duration int64 `json:"dur,omitempty"`
ProcessID int64 `json:"pid,omitempty"`
ThreadID int64 `json:"tid,omitempty"`
Arguments map[string]string `json:"args,omitempty"`
Start int64 `json:"-"`
End int64 `json:"-"`
StartTime time.Time `json:"-"`
EndTime time.Time `json:"-"`
}
func (t TraceEvent) ID() string {
return fmt.Sprintf("%s/%v", t.Name, t.ThreadID)
}
type TraceEvents []TraceEvent
func (t TraceEvents) Len() int { return len(t) }
func (t TraceEvents) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t TraceEvents) Less(i, j int) bool { return t[i].Start < t[j].Start }
type Trace struct {
StartTime time.Time
TraceEvents TraceEvents
}
func (t Trace) Len() int { return t.TraceEvents.Len() }
func (t Trace) Swap(i, j int) { t.TraceEvents.Swap(i, j) }
func (t Trace) Less(i, j int) bool { return t.TraceEvents.Less(i, j) }
func SplitTrace(t *Trace, startSlice []int64, endSlice []int64) ([]*Trace, error) {
batchNum := 0
tSlice := []*Trace{}
tmpTrace := new(Trace)
tSlice = append(tSlice, tmpTrace)
tmpTrace.StartTime = time.Unix(0, startSlice[batchNum])
for _, event := range t.TraceEvents {
if event.End < endSlice[batchNum] {
if event.Start > startSlice[batchNum] {
tmpTrace.TraceEvents = append(tmpTrace.TraceEvents, event)
}
} else {
batchNum++
tmpTrace = new(Trace)
tSlice = append(tSlice, tmpTrace)
tmpTrace.StartTime = time.Unix(0, startSlice[batchNum])
tmpTrace.TraceEvents = append(tmpTrace.TraceEvents, event)
}
}
return tSlice, nil
}
func NewTrace(data string, start_time int64) (*Trace, error) {
trace := new(Trace)
err := json.Unmarshal([]byte(data), &trace.TraceEvents)
if err != nil {
return nil, err
}
trace.StartTime = time.Unix(0, start_time)
for i, event := range trace.TraceEvents {
trace.TraceEvents[i].Start = start_time + event.Timestamp*1000
trace.TraceEvents[i].StartTime = time.Unix(0, trace.TraceEvents[i].Start)
trace.TraceEvents[i].End = start_time + event.Timestamp*1000 + event.Duration*1000
trace.TraceEvents[i].EndTime = time.Unix(0, trace.TraceEvents[i].End)
}
sort.Sort(trace.TraceEvents)
return trace, nil
}
func (event *TraceEvent) Publish(ctx context.Context, lvl tracer.Level, opts ...opentracing.StartSpanOption) error {
// skip the events for loading model and setting environments in onnxruntime
if event.Name == "model_loading_from_uri" || event.Name == "session_initialization" || event.Name == "model_run" || event.Name == "SequentialExecutor::Execute" {
return nil
}
tags := opentracing.Tags{
"category": event.Category,
"phase": event.Phase,
"process_id": event.ProcessID,
"thread_id": event.ThreadID,
"arguments": event.Arguments,
}
s, _ := tracer.StartSpanFromContext(
ctx,
lvl,
event.Name,
opentracing.StartTime(event.StartTime),
tags,
)
if s == nil {
log.WithField("event_name", event.Name).
WithField("tags", tags).
Error("failed to create span from context")
return nil
}
s.FinishWithOptions(opentracing.FinishOptions{
FinishTime: event.EndTime,
})
return nil
}
func (t *Trace) Publish(ctx context.Context, lvl tracer.Level, opts ...opentracing.StartSpanOption) error {
for _, event := range t.TraceEvents {
if err := event.Publish(ctx, lvl, opts...); err != nil {
return err
}
}
return nil
}