日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Ubuntu 14.04 64bit上玩转graphviz绘图工具

發(fā)布時(shí)間:2023/11/27 生活经验 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ubuntu 14.04 64bit上玩转graphviz绘图工具 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

關(guān)于Graphviz: Graphviz(英文:Graph Visualization Software的縮寫)是一個(gè)由AT&T實(shí)驗(yàn)室啟動(dòng)的開源工具包,用于繪制DOT語(yǔ)言腳本描述的圖形。它也提供了供其它軟件使用的庫(kù)。Graphviz是一個(gè)自由軟件,其授權(quán)為Eclipse Public License。其Mac版本曾經(jīng)獲得2004年的蘋果設(shè)計(jì)獎(jiǎng)。

Graphviz是大名鼎鼎的貝爾實(shí)驗(yàn)室的幾位牛人開發(fā)的一個(gè)畫圖工具。它的理念和一般的“所見即所得”的畫圖工具不一樣,是“所想即所得”。
Graphviz提供了dot語(yǔ)言來(lái)編寫繪圖腳本。dot的語(yǔ)言專門用來(lái)定義關(guān)系。我們必須先在某個(gè)文件中寫上代碼,最后調(diào)用命令行生成。沒(méi)有找到可邊寫邊改的集成環(huán)境。
關(guān)于dot的具體語(yǔ)法參看官方文檔,這里就不多說(shuō)。這里僅用來(lái)入門。

http://www.graphviz.org/

另外也有專門渲染DOT腳本的瀏覽器xdo,直接在窗口渲染,不需要生成圖像文件,強(qiáng)烈推薦。

很多圖都比較適合用DOT來(lái)定義,比如流程圖,組織關(guān)系圖,類圖,狀態(tài)轉(zhuǎn)換圖等等。

在剛接觸DOT不到3天的時(shí)間里,我就分別在代碼性能優(yōu)化,協(xié)議棧狀態(tài)機(jī)定義兩件事情用到了DOT,感覺(jué)非常方便。

性能優(yōu)化:gprof可以對(duì)一個(gè)程序運(yùn)行時(shí)進(jìn)行profiling,生成報(bào)告,包括函數(shù)調(diào)用關(guān)系,每個(gè)函數(shù)被調(diào)用的次數(shù),花費(fèi)的時(shí)間。但這個(gè)報(bào)告是文本的,不夠直觀。可以通過(guò)一個(gè)gprof2dot.py腳本,把gprof的輸出翻譯成一個(gè)DOT文件,然后再用Graphviz轉(zhuǎn)成圖片或者直接用xdot來(lái)查看。

安裝graphviz?????
sudo apt-get install graphviz graphviz-doc 這樣會(huì)安裝dot語(yǔ)言的執(zhí)行文件,執(zhí)行文件路徑在 /usr/bin/dot 繪圖方法 先根據(jù)業(yè)務(wù)需要編寫dot文件,參見下面的示例部分,然后編譯運(yùn)行。輸出格式可以根據(jù)自己的需要來(lái)靈活選擇 例如test.dot, 產(chǎn)生圖片: dot -Tpng test.dot -o test.png
dot -Tsvg test.dot -o test.svg dot test.dot -Tpdf -o test.pdf

典型示例(更多例子可以參見官方文檔)

1.Fancy Graph

digraph G { edge[fontname="FangSong"];node[peripheries=2, fontname="FangSong"];size="16, 16";main[shape="box"];main[label="從這里開始"]main->parse[weight=8]parse->execute;main->init[style=dotted];                                                                                                                                             main->cleanup;execute->{make_string; printf;}edge[color=red];init->make_string[label="1000次的感動(dòng)"];main->printf[label="make a \n string"];node[shape=box,style=filled,color=".7 .3 1.0"];execute->compare;
}       
效果圖


2.Polygon Graph

digraph G{size = "4, 4"a->b->c;b->d;a[shape=polygon, sides=5, peripheries=3, color=lightblue, style=filled];c[shape=polygon, sides=4, skew=0.4, label="hello world"];d[shape=invtriangle];                                                                                                                                                 e[shape=polygon, side=4, distortion=.7];}   


效果圖


3.list Graph

digraph G{node[shape=record, height=.1];node0[label="<f0> |<f1> G|<f2> "];node1[label="<f0> |<f1> E|<f2> "];node2[label="<f0> |<f1> B|<f2> "];node3[label="<f0> |<f1> F|<f2> "];node4[label="<f0> |<f1> R|<f2> "];node5[label="<f0> |<f1> H|<f2> "];node6[label="<f0> |<f1> Y|<f2> "];node7[label="<f0> |<f1> A|<f2> "];node8[label="<f0> |<f1> C|<f2> "];                                                                                                                                    "node0":f2->"node4":f1;"node0":f0->"node1":f1;"node1":f0->"node2":f1;"node1":f2->"node3":f1;"node2":f2->"node8":f1;"node2":f0->"node7":f1;"node4":f2->"node6":f1;"node4":f0->"node5":f1;
}       



4.hash table Graph

digraph G { nodesep=.05;rankdir=LR;node[shape=record, width=.1, height=.1];node0[label="<f0> |<f1> |<f2> |<f3> |<f4> |<f5> |<f6> |", height=2.5]node[width=1.5];node1[label="{<n> n14 | 719 |<p>}"];node2[label="{<n> a1 | 719 |<p>}"];node3[label="{<n> i9 | 512 |<p>}"];node4[label="{<n> e5 | 632 |<p>}"];node5[label="{<n> t20 | 959 |<p>}"];node6[label="{<n> o15 | 794 |<p>}"];node7[label="{<n> s19 | 659 |<p>}"];node0:f0->node1:n;node0:f1->node2:n;node0:f2->node3:n;node0:f5->node4:n;node0:f6->node5:n;node2:p->node6:n;node4:p->node7:n;                                                                                                                                                     
}     


5.Process Graph

digraph G { subgraph cluster0 {node[style=filled, color=white];style=filled;color=lightgrey;a0->a1->a2->a3;label="process #1";}   subgraph cluster1 {node[style=filled];color=blue;b0->b1->b2->b3;label="process #2";}   start->a0;start->b0;a1->b3;b2->a3;a3->end;b3->end;start[shape=Mdiamond];end[shape=Msquare];                                                                                                                                                   
}       


6.directed Graph

digraph G { {a b c} -> {d e f g}                                                                                                                                                  
} 


7.化學(xué)分子式

graph s{C_0 -- H_0;C_0 -- H_1;C_0 -- H_2;C_0 -- C_1;C_1 -- H_3;C_1 -- H_4;C_1 -- H_5;
}

8.函數(shù)調(diào)用關(guān)系圖

digraph s {node [shape=ellipse, style=filled, color="#40e0d0"];edge [color="#606060", penwidth=3];main [color=green];main -> init[color=blue, label="hello, I'm \llable of edge"];main -> mainloop;main -> exit;init -> a_init;init -> b_init;init -> c_init;mainloop -> select;
}

這個(gè)是有向圖(directional graph), 腳本的第一個(gè)詞需要換成"digraph"。常用的屬性有color, style, shape, penwidth, label等。label中 \r, \l, \n 都表示換行,但對(duì)齊方式分別是 右對(duì)齊,左對(duì)齊,居中


9.ATS中插件collapsed_connection的流程圖,一個(gè)更復(fù)雜的例子

dot -Tpng collapsed_connection.dot -o collapsed_connection.png

digraph collapsed_connection {  accept -> TS_HTTP_POST_REMAP_HOOK;TS_HTTP_POST_REMAP_HOOK -> "check request method (and header)";"check request method (and header)" -> "get CacheUrl hash_key using MurmurHash3" [label = "GET request (required_header present)"];"check request method (and header)" -> "pass request" [label = "others"];"get CacheUrl hash_key using MurmurHash3" -> "check hash_key from hashTable";"check hash_key from hashTable" -> "lock URL in hashTable" [label = "not found"];"check hash_key from hashTable" -> "pass request" [label = "found, but marked pass"];"check hash_key from hashTable" -> "check hash_key from hashTable" [label = "locked or unable to get mutex, wait insert_lock_retry_time"];"lock URL in hashTable" -> TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK;TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK -> "remove URL from hashTable(1)" [label = "hit_fresh or skipped"];TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK -> "request origin server" [label = "miss or stale"];"request origin server" -> TS_HTTP_READ_RESPONSE_HDR_HOOK;TS_HTTP_READ_RESPONSE_HDR_HOOK -> "remove URL from hashTable(1)" [label = "not 200/OK response"];TS_HTTP_READ_RESPONSE_HDR_HOOK -> "check read_while_writer config";"check read_while_writer config" -> "remove URL from hashTable(1)" [label = "enabled"];"check read_while_writer config" -> TS_HTTP_TXN_CLOSE_HOOK [label = "disabled"];TS_HTTP_READ_RESPONSE_HDR_HOOK -> "mark pass in hashTable" [label = "non-cacheable"];"remove URL from hashTable(1)" -> TS_HTTP_TXN_CLOSE_HOOK;"mark pass in hashTable" -> TS_HTTP_TXN_CLOSE_HOOK;"pass request" -> TS_HTTP_TXN_CLOSE_HOOK;TS_HTTP_TXN_CLOSE_HOOK -> "remove URL from hashTable(2)";TS_HTTP_TXN_CLOSE_HOOK -> "check keep_pass_record_time" [label = "non-cacheable"];"check keep_pass_record_time" -> "add into KeepPassList" [label = "> 0"];"check keep_pass_record_time" -> "remove URL from hashTable(2)" [label = "= 0"];"add into KeepPassList" -> "transaction close";"remove URL from hashTable(2)" -> "transaction close";"transaction close" -> accept;                                                                                                                                         TS_HTTP_POST_REMAP_HOOK [shape = box];TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK[shape = box];TS_HTTP_READ_RESPONSE_HDR_HOOK [shape = box];TS_HTTP_TXN_CLOSE_HOOK [shape = box];"check request method (and header)" [shape = diamond];"check hash_key from hashTable" [shape = diamond];"check read_while_writer config" [shape = diamond];"check keep_pass_record_time" [shape = diamond];
} 



10.使用Makefile文件來(lái)批量執(zhí)行

在閱讀源碼過(guò)程中,如果我們編寫了大量的dot文件,需要處理和展示,如果逐個(gè)編譯和操作,未免太為繁瑣,我們這里推薦使用一個(gè)Makefile來(lái)將所有的dot文件都包括過(guò)來(lái)。

生成圖像命令

make png

清除圖像命令

make clean

下面是Makefile文件

SVG_FILES = \HttpProxyServer-callgraph.svg \HttpSM-callgraph.svg \HttpTransact-callgraph.svg \HttpTransact-state-diagram.svg \HttpTransactAndSM-callgraph.svgPNG_FILES = $(SVG_FILES:.svg=.png)PDF_FILES = $(SVG_FILES:.svg=.pdf)svg: ${SVG_FILES}png: ${PNG_FILES}pdf: ${PDF_FILES}clean:rm -f ${SVG_FILES} ${PNG_FILES} ${PDF_FILES}.SUFFIXES: .dot .svg .png .pdf.dot.svg:dot -Tsvg $< -o $@.dot.png:dot -Tpng $< -o $@.dot.pdf:dot -Tpdf $< -o $@

截圖如下



注意事項(xiàng)

1.中文亂碼

graphviz默認(rèn)情況下對(duì)中文支持不好,如果代碼中有中文,則必須使用UTF-8的格式保存文件,并且在代碼中指定字體。

進(jìn)一步深究可以查看官方文檔

http://www.graphviz.org/Documentation.php

下面給出幾個(gè)頗具震撼力的效果圖,來(lái)自官網(wǎng)文檔



總結(jié)

以上是生活随笔為你收集整理的Ubuntu 14.04 64bit上玩转graphviz绘图工具的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。