• Stars
    star
    1,951
  • Rank 22,922 (Top 0.5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Concurrency tracer and visualizer for Go (Golang) programming language

GoTrace - Go Concurrency 3D Tracer

GoTrace is a 3D WebGL visualizer of Go concurrency. It analyzes trace produced by go tool trace and renders visualization of concurrency flow.

Original article: https://divan.github.io/posts/go_concurrency_visualize/

Slides from GopherCon'16: http://divan.github.io/talks/2016/gophercon/

Intro

This tool generates 3D visualization of Go concurrency flow by analyzing its execution trace. To view the visualization you need a modern browser with WebGL support (pretty much any browser nowadays).

Its primary goal is to be an educational tool for Go concurrency. It works well with small programs that produce short traces (see Scale issues below).

Usage

First, install gotrace:

go get -u github.com/divan/gotrace

Second, use patched Go runtime to produce trace and binary. There are two ways to do it - use a docker container or apply the patch locally.

Quick example using pre-made docker image (jump to detailed instructions):

docker run --rm -it \
	-e GOOS=darwin \
	-v $(pwd):/src divan/golang:gotrace \
		go build -o /src/binary /src/examples/hello.go
./binary 2> trace.out
gotrace ./trace.out ./binary

Or, using local patched Go installation (jump to detailed instructions):

gotrace examples/hello.go

Prepare your program

Now, please learn some important things before trying your own code. Feel free to play first with code in examples/ folder.

Theoretically, gotrace should do all the magic itself and be able to handle any Go program. That's the goal, but at the present moment, if you want to get good/meaningful visualization, you should follow some rules and suggestions.

Make it short

The height of program visualization currently is a fixed value, so any trace fits into the screen height. This means, that example running 1 minute will be visualized at different scale from program running 1 second.

Depending on what you try to see, but rule of thumb is - the shorter execution time, the better. See examples/ dir for good samples that produce nice visualizations.

Insert runtime/trace yourself

In order to produce the trace, your program should be instrumented with special code. gotrace can do this automatically for you, but in some cases it's wiser to put this code by yourself. Here is a typical example:

package main

import (
	"os"
	"runtime/trace"
)

func main() {
	trace.Start(os.Stderr)
	...
	trace.Stop()
}

Currently it's important to write trace into os.Stderr. See issue #X if your example uses stderr for other needs.

Consider inserting very short time.Sleep() calls

If you are trying to visualize some things that happen at nanosecond/microsecond level, it could be wise to insert time.Sleep(1 * time.Millisecond) calls to get more clear visualization.

For example, if your code runs 100 goroutines in a loop, their IDs and their start order probably would be different, resulting in slightly messed picture. So, changing:

	for i := 0; i < 100; i++ {
		go player(table)
	}

to

	for i := 0; i < 100; i++ {
		time.Sleep(1 * time.Millisecond)
		go player(table)
	}

will help to make better visualization.

Try to keep number of goroutines/events small

The fewer objects that will be rendered, the better. If you have many things to render, WebGL will just hang your browser. Also, keep in mind, that point of visualization is to express something. So running 1024 workers will result in a heavy visualization where you will not see separate goroutines. Setting this value to, say, 36 will produce much more clear picture.

Detailed instructions

The next step is to build your program. You will need to build using the patched Go runtime. So if you patched it yourself (see Appendix A), you just have to run go build, or, even simpler, let gotrace do it for you. But most people, probably wouldn't want to do this and prefer using Docker for it.

Using Docker

You will need Docker installed and running.

Then pull the image from Docker Hub:

docker pull divan/golang:gotrace

or build it yourself:

docker build -t "divan/golang:gotrace" -f runtime/Dockerfile runtime/

If everything went ok, you should have divan/golang:gotrace image in your local docker (check with docker images command).

Now, use this command to produce the binary:

MacOS X
docker run --rm -it \
	-e GOOS=darwin \
	-v $(pwd):/src divan/golang:gotrace \
		go build -o /src/binary /src/examples/hello.go
Linux
docker run --rm -it \
	-v $(pwd):/src divan/golang:gotrace \
		go build -o /src/binary /src/examples/hello.go
Windows
docker run --rm -it \
	-e GOOS=windows \
	-v $(pwd):/src divan/golang:gotrace \
		go build -o /src/binary.exe /src/examples/hello.go

3. Run it and save the trace.

Once you have the binary, you can run it and save the trace:

./binary 2> trace.out

4. Run gotrace (finally)

Now, it's time to run gotrace and feed the binary and the trace to produce the visualization:

gotrace ./trace.out ./binary

It should start the browser and render visualization.

Visualization

Colors

Different colors of goroutines represent different states.

  • red - blocked state
  • green - unblocked
  • yellow - unblocked and using CPU

Colors of goroutines' connections and sendings over the channels are the same.

Hotkeys

You can use the mouse/trackpad to zoom/rotate/pan visualization. On MacOS X you use single tap and move to rotate, double-finger touch to zoom, and double-finger tap and move to pan.

You may also try it with a Leap Motion controller for zooming and rotating with hands - switch to leap branch.

Also there are some useful hotkeys:

  • r - restart
  • p - pause
  • 1, 2, 3, 4, 0 - highlight modes (0 - default)
  • +/- - increase/decrease width of lines
  • s/f - slower/faster animation

Limits/Known issues

  • Value of variable being sent to the channel is supported only for integer types. (see Issue #3)
  • Buffered channels doesn't work (yet) (see Issue #2)

Appendix A - patching Go locally

If you really want to play around with gotrace, you may want to patch Go runtime yourself. It will allow you to run gotrace as easy as gotrace main.go without all intermediate steps described above.

Here are instructions on how to do it (MacOS X and Linux).

  1. Assuming your Go installation is in /usr/local/go (default), download Go 1.6.3 and unpack it into /usr/local/go163.

     sudo -i
     mkdir -p /usr/local/go163
     curl https://storage.googleapis.com/golang/go1.6.3.src.tar.gz | tar -xz -C /usr/local/go163
    
  2. Then, copy patch and apply it:

     sudo patch -p1 -d /usr/local/go163/go < runtime/go1.6.3-tracenew.patch
    
  3. Build new runtime:

     sudo -i
     cd /usr/local/go163/go/src
     export GOROOT_BOOTSTRAP=/usr/local/go # or choose yours
     ./make.bash
    
  4. Finally, export PATH or use ln -s command to make this Go version actual in your system:

     export PATH=/usr/local/go163/go/bin:$PATH
    

or (assuming your PATH set to use /usr/local/go)

	sudo mv /usr/local/go /usr/local/go-orig
	sudo ln -nsf /usr/local/go163/go /usr/local/go

NOTE: return your previous installation by sudo ln -nsf /usr/local/go-orig /usr/local/go

Now, you should be able to run gotrace main.go and get the result.

License

MIT License

More Repositories

1

txqr

Transfer data via animated QR codes
Go
2,855
star
2

expvarmon

TermUI based monitor for Go apps using expvars (/debug/vars). Quickest way to monitor your Go app(s).
Go
2,000
star
3

gobenchui

UI for overview of your Golang package benchmarks progress.
Go
533
star
4

depscheck

Dependency checker for Golang (Go) packages. Prints stats and suggests to remove small LeftPad-like imports if any.
Go
426
star
5

gofresh

Keep your Go package dependencies fresh. Console tool for checking and updating package dependencies (imports).
Go
141
star
6

txqr-reader

TXQR (Animated QR data transfer) demo reader app for iOS
Swift
90
star
7

gorilla-xmlrpc

Gorilla XML RPC implementation (Golang/Go)
Go
71
star
8

qrlogo

QR with logo overlay generator for Go (Golang)
Go
65
star
9

num2words

num2words - Numbers to words converter in Go (Golang)
Go
50
star
10

whispervis

Whisper p2p messaging visualizing and analysis tool
Go
42
star
11

goabm

Go ABM (Agent-Based Modelling) library
Go
26
star
12

divan.github.io

Personal blog
JavaScript
19
star
13

graphx

Graph layout and display library.
Go
19
star
14

go2nodebinding

Go 2 Node.js Addon C++ bindings generator
Go
13
star
15

psh

Пщ - комманд аліас фо Go
Go
12
star
16

blog

Personal blog sources
JavaScript
10
star
17

ankiqml

Anki QML
Python
8
star
18

txqr-tester-ios

iOS version of txqr-tester client
Swift
8
star
19

hackertyper

HackerTyper for Maemo/Harmattan
Python
6
star
20

qechoprint

Qt-based music identify app powered by EchoNest
C++
4
star
21

talks

JavaScript
3
star
22

wgames

WebOS Games Manager for Maemo
C
3
star
23

dotfiles

.dotfiles
Vim Script
3
star
24

errfreq

Disttribution of 'if err !=' checks per function counter
Go
3
star
25

status_flutter

PoC of Flutter version of Status UI
Dart
2
star
26

hclust

Quick rewrite of Agglomerative hierarchical clustering for Node JS
Go
2
star
27

qt-worldclock

WIP Simple Qt fullscreen worldclock app
QMake
2
star
28

kyivcity

Demo repository for working on documents
HTML
2
star
29

incognito

Incognito Color Scheme for Vim
Vim Script
2
star
30

simulation

Misc simulation code for p2p messaging protocols
Go
2
star
31

vkontakteqml

Vkontante QML client
JavaScript
2
star
32

locust2graphite

Send Locust metrics to Graphite
Go
2
star
33

graph-experiments

Misc graph related experiments and snippets
JavaScript
1
star
34

interviews

Convert txt to xls for interviews
Go
1
star
35

skating.science

Skating.science website stub
HTML
1
star
36

go-gce-debug-demo

Demo project to test GCE Debugger for Go
Go
1
star
37

whisper-gopherjs

Demo POC of WhisperV6 encryption converted to JS using GopherJS
JavaScript
1
star
38

qml-screens

Screens implementation for QT-podcast contest
1
star
39

dep-issue-example

Simple CGo program with .c files in src/, for dep issue demonstration.
Go
1
star
40

vecty-visibility-demo

Small vecty project to check visibilitychange event handler in vecty programs
JavaScript
1
star