-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
433 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dataviz | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strconv" | ||
"time" | ||
|
||
"gorgonia.org/gorgonia" | ||
xvm "gorgonia.org/gorgonia/x/vm" | ||
) | ||
|
||
// DumpTrace suitable for https://github.com/vasturiano/timelines-chart | ||
func DumpTrace(traces []xvm.Trace, g *gorgonia.ExprGraph, w io.Writer) error { | ||
var zerotime time.Time | ||
groups := make(map[string]group) | ||
// generate all labels | ||
for _, trace := range traces { | ||
if trace.End == zerotime { | ||
continue | ||
} | ||
if _, ok := groups[trace.StateFunction]; !ok { | ||
groups[trace.StateFunction] = group{ | ||
Group: trace.StateFunction, | ||
} | ||
} | ||
label := dataLabel{ | ||
TimeRange: []time.Time{ | ||
trace.Start, | ||
trace.End, | ||
}, | ||
Val: strconv.Itoa(int(trace.ID)), | ||
} | ||
dGroup := dataGroup{ | ||
Label: g.Node(trace.ID).(*gorgonia.Node).Name(), | ||
Data: []dataLabel{ | ||
label, | ||
}, | ||
} | ||
g := groups[trace.StateFunction] | ||
g.Data = append(g.Data, dGroup) | ||
groups[trace.StateFunction] = g | ||
} | ||
grps := make([]group, 0, len(groups)) | ||
for _, grp := range groups { | ||
grps = append(grps, grp) | ||
} | ||
enc := json.NewEncoder(w) | ||
return enc.Encode(grps) | ||
} | ||
|
||
type group struct { | ||
Group string `json:"group"` | ||
Data []dataGroup `json:"data"` | ||
} | ||
|
||
type dataGroup struct { | ||
Label string `json:"label"` | ||
Data []dataLabel `json:"data"` | ||
} | ||
|
||
type dataLabel struct { | ||
TimeRange []time.Time `json:"timeRange"` | ||
Val interface{} `json:"val"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dataviz | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"gorgonia.org/gorgonia" | ||
xvm "gorgonia.org/gorgonia/x/vm" | ||
) | ||
|
||
func ExampleDumpTrace() { | ||
g := gorgonia.NewGraph() | ||
// Add elements | ||
ctx, traceC := xvm.WithTracing(context.Background()) | ||
defer xvm.CloseTracing(ctx) | ||
traces := make([]xvm.Trace, 0) | ||
go func() { | ||
for v := range traceC { | ||
traces = append(traces, v) | ||
} | ||
}() | ||
machine := xvm.NewMachine(g) | ||
err := machine.Run(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
machine.Close() | ||
DumpTrace(traces, g, os.Stdout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package xvm | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
// Trace the nodes states | ||
type Trace struct { | ||
//fmt.Println(runtime.FuncForPC(reflect.ValueOf(state).Pointer()).Name()) | ||
StateFunction string | ||
ID int64 | ||
Start time.Time | ||
End time.Time `json:",omitempty"` | ||
} | ||
|
||
type chanTracerContextKey int | ||
|
||
const ( | ||
globalTracerContextKey chanTracerContextKey = 0 | ||
) | ||
|
||
// WithTracing initializes a tracing channel and adds it to the context | ||
func WithTracing(parent context.Context) (context.Context, <-chan Trace) { | ||
c := make(chan Trace, 0) | ||
return context.WithValue(parent, globalTracerContextKey, c), c | ||
} | ||
|
||
// CloseTracing the tracing channel to avoid context leak. | ||
// it is a nil op if context does not carry tracing information | ||
func CloseTracing(ctx context.Context) { | ||
c := extractTracingChannel(ctx) | ||
if c != nil { | ||
close(c) | ||
} | ||
} | ||
|
||
func extractTracingChannel(ctx context.Context) chan<- Trace { | ||
if ctx == nil { | ||
return nil | ||
} | ||
if c := ctx.Value(globalTracerContextKey); c != nil { | ||
return c.(chan Trace) | ||
} | ||
return nil | ||
} | ||
|
||
var now = time.Now | ||
|
||
func trace(ctx context.Context, t *Trace, n *node, state stateFn) *Trace { | ||
traceC := extractTracingChannel(ctx) | ||
if traceC == nil { | ||
return t | ||
} | ||
if t == nil { | ||
t = &Trace{ | ||
ID: n.id, | ||
StateFunction: runtime.FuncForPC(reflect.ValueOf(state).Pointer()).Name(), | ||
Start: now(), | ||
} | ||
} else { | ||
t.End = now() | ||
} | ||
select { | ||
case traceC <- *t: | ||
case <-ctx.Done(): | ||
} | ||
return t | ||
} |
Oops, something went wrong.