From 66b826a8e117d57b683cf5f01aa0846755bf663e Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 22 Jan 2024 13:58:00 -0500 Subject: [PATCH] pgraph, lang: funcs: dage: Add an awkward graphviz interface This refactors the code to make it not depend on the specific engine. --- lang/funcs/dage/dage.go | 8 ++++---- lang/funcs/dage/txn.go | 6 +++--- pgraph/graphviz.go | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lang/funcs/dage/dage.go b/lang/funcs/dage/dage.go index 5f588ac5..8aa2233a 100644 --- a/lang/funcs/dage/dage.go +++ b/lang/funcs/dage/dage.go @@ -1451,10 +1451,10 @@ func (obj *Engine) Stats() string { return obj.stats.String() } -// Graphviz writes out the diagram of a graph to be used for visualization and -// debugging. You must not modify the graph (eg: during Lock) when calling this -// method. -func (obj *Engine) Graphviz(dir string) error { +// ExecGraphviz writes out the diagram of a graph to be used for visualization +// and debugging. You must not modify the graph (eg: during Lock) when calling +// this method. +func (obj *Engine) ExecGraphviz(dir string) error { // XXX: would this deadlock if we added this? //obj.graphMutex.Lock() // XXX: should this be a RLock? //defer obj.graphMutex.Unlock() // XXX: should this be an RUnlock? diff --git a/lang/funcs/dage/txn.go b/lang/funcs/dage/txn.go index 07ecd61a..32c0e5b7 100644 --- a/lang/funcs/dage/txn.go +++ b/lang/funcs/dage/txn.go @@ -498,12 +498,12 @@ func (obj *graphTxn) commit() error { // XXX: running this on each commit has a huge performance hit. // XXX: we could write out the .dot files and run graphviz afterwards - if engine, ok := obj.GraphAPI.(*Engine); ok && GraphvizDebug { + if g, ok := obj.GraphAPI.(pgraph.Graphvizable); ok && GraphvizDebug { //d := time.Now().Unix() - //if err := engine.graph.ExecGraphviz(fmt.Sprintf("/tmp/txn-graphviz-%d.dot", d)); err != nil { + //if err := g.ExecGraphviz(fmt.Sprintf("/tmp/txn-graphviz-%d.dot", d)); err != nil { // panic("no graphviz") //} - if err := engine.Graphviz(""); err != nil { + if err := g.ExecGraphviz(""); err != nil { panic(err) // XXX: improve me } diff --git a/pgraph/graphviz.go b/pgraph/graphviz.go index ced93aa8..2ed61446 100644 --- a/pgraph/graphviz.go +++ b/pgraph/graphviz.go @@ -40,6 +40,23 @@ const ( ptrLabelsSize = 10 ) +// Graphvizable is a simple interface to handle the common signature for this +// useful graphviz exec method that is used in debugging. Expect that this +// signature might change if the authors find a different variant more useful. +type Graphvizable interface { + + // ExecGraphviz runs graphviz and stores the result at this absolute + // path. It (awkwardly) can be used by someone expecting either a + // filename, or a directory. The filename scenario should be used if you + // are expecting a single .dot output file. The directory scenario + // should be used if you are expecting a series of .dot graphs. + // Directories must end with a trailing slash. A filename passed will + // get this location overwritten if there is something already there. If + // the string is empty, it might create a file in a temporary directory + // somewhere. + ExecGraphviz(filename string) error +} + // Graphviz adds some visualization features for pgraph. type Graphviz struct { // Name is the display name of the graph. If specified it overrides an