01
Why Graphviz?Visualising large directory structures such as the Linux kernel can be really practical. The Linux kernel root directory contains over 2,000 other directories and 37,000 fi les that would otherwise be very diffi cult to picture. The output of our Perl script can optionally show the included fi les as well as their sizes. Also, the Graphviz knowledge you’ll get by visualising directories can be used for visualising networks, traceroute paths, function calls etc. And there are plenty of other benefi ts.
Note: For huge directory structures such as the
Linux kernel, it is better not to visualise all at once but to split into smaller parts.
02
Installing and running GraphvizYour Linux distribution probably includes a ready-to-install Graphviz package that you can use. For a Debian 7 system, you just have to run the following command to download and install Graphviz:
# apt-get install graphviz
After installing Graphviz, try to compile the following Graphviz code:
digraph G {
03
The Perl Graphviz moduleMany programming languages, including Python, Ruby, C++ and Perl, provide their own interface for creating Graphviz fi les. The Perl module is presented as an alternative way of generating Graphviz code.
The important thing to remember when using the Graphviz module is that if you want to get the output as a PNG fi le, the last line of your program should be:
print $graph->as_png;
Similarly, if you want to get the output as plain text, you should use the following line instead: print $graph->as_text;
04
Basic Graphviz informationA graph G(V,E) is a fi nite, non-empty set of vertices V (or nodes) and a set of edges E. A graph contains nodes and edges, each of them having attributes.
Graphviz has its own dialect that you will have to learn. The language may be simple and elegant but it is also very powerful. The good thing about Graphviz is that you can write its code using a simple plain text editor – a wonderful side effect of it is that you can easily write scripts that generate Graphviz code.
By reading some Graphviz code, you will soon realise that lines beginning with # or // are considered comments.
“Hello world!”; }
Use the following command for the compilation: $ dot -Tps hw.dot -o hw.ps
The aforementioned command will produce a PostScript fi le called hw.ps that you can view. The word digraph means that a directed graph is going to be created. For creating an undirected graph, the word graph should have been used instead. For such a simplistic example, however, it does not make any difference if the graph is either directed or undirected.
Although the PostScript format used to be very popular, it is recommended to use the PDF format because it is faster to render and display. Additionally, PDF fi les can be zoomed in more before losing their clarity.
05
A simple Graphviz exampleThe following Graphviz code draws a simple directory structure that includes fi les. It also displays the size of a fi le:
digraph Widget { size=”16,6”; nodesep=0.05; rankdir = LR; rotate = 90; edge[len=5]; node[style=filled, shape=record, fontsize=8]; node[height=0.20, width=0.20, color=gray]; “Graphviz Tsoukalos. doc”[label=”Graphviz Tsoukalos.doc 43520b”];
“Graphviz Tsoukalos.doc” -> “/home/ mtsouk/docs/article/working/Graphviz.LUD”; “/home/mtsouk/docs/article/working/ Graphviz.LUD”[label=”Graphviz.LUD”]; “._Graphviz Tsoukalos.doc”[label=”._ Graphviz Tsoukalos.doc 4096b”];
“._Graphviz Tsoukalos.doc” -> “/home/ mtsouk/docs/article/working/Graphviz.LUD”; “/home/mtsouk/docs/article/working/ Graphviz.LUD/code”[label=”code”]; “/home/mtsouk/docs/article/working/ Graphviz.LUD/code” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD”; “articles.viz”[label=”articles.viz 0b”]; “articles.viz” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “visDir.pl”[label=”visDir.pl 3626b”]; “visDir.pl” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “Docs.pdf”[label=”Docs.pdf 48063b”]; “Docs.pdf” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “code.viz”[label=”code.viz 3713b”]; “code.viz” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “code.pdf”[label=”code.pdf 15268b”]; “code.pdf” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “Docs.viz”[label=”Docs.viz 0b”]; “Docs.viz” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/code”; “/home/mtsouk/docs/article/working/ Graphviz.LUD/figures”[label=”figures”]; “/home/mtsouk/docs/article/working/ Graphviz.LUD/figures” -> “/home/mtsouk/ docs/article/working/Graphviz.LUD”; “tsoukalos.jpg”[label=”tsoukalos.jpg 286780b”]; “tsoukalos.jpg” -> “/home/mtsouk/docs/ article/working/Graphviz.LUD/figures”; }
The code (articles.viz) can be compiled using the following command:
$ dot -Tpdf articles.viz -o articles.pdf The presented visDir.pl Perl script generates similar Graphviz code.
06
A complicated Graphviz exampleYou can visualise the Linux kernel directory structure with the help of the visDir.pl script using the following two commands: $ ./visDir.pl -d ~/kernel/linux-source-3.2
> kernel.viz
$ neato -Tpdf kernel.viz -o kernel.pdf As you already know, the Graphviz suite contains many tools for creating graphs. For graphs with a large number of nodes, you should experiment to fi nd the suitable tool and parameters for the job. The output of the dot tool for the Linux kernel directory structure is not as pretty as the graph created using the neato tool.
07
The Perl scriptThe Perl script, called visDir.pl, requires one command-line option and one argument. The argument is the path of the directory that is going to be visualised. The command-line option must be -d (for including directories only) or -f (for also including fi les). If none of them is found, the script prints an explanatory message and stops execution. Please note that the directory argument must not contain a ‘/’ at the end; so the following command will not work properly: $ ./program_name.pl /usr/