-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot.c
More file actions
213 lines (182 loc) · 5.41 KB
/
dot.c
File metadata and controls
213 lines (182 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include "dot.h"
#include "util.h"
static double
calc_dot_weight(uint64_t total_nhits, uint64_t nhits)
{
assert(nhits <= total_nhits);
double weight = sqrt((double)nhits / (double)total_nhits);
assert(0.0 <= weight && weight <= 1.0);
return weight;
}
static int
calc_dot_font_size(double weight)
{
#define DOT_WEAK_FONT_SIZE 14
#define DOT_STRONG_FONT_SIZE (3 * DOT_WEAK_FONT_SIZE)
#define DOT_FONT_SCALE (DOT_STRONG_FONT_SIZE - DOT_WEAK_FONT_SIZE)
int font_size = DOT_WEAK_FONT_SIZE + (int)(weight * DOT_FONT_SCALE);
assert(DOT_WEAK_FONT_SIZE <= font_size && font_size <= DOT_STRONG_FONT_SIZE);
return font_size;
}
static double
calc_dot_pen_width(double weight)
{
#define DOT_WEAK_PEN_WIDTH 2.0
#define DOT_STRONG_PEN_WIDTH 6.0
#define DOT_PEN_WIDTH_SCALE (DOT_STRONG_PEN_WIDTH - DOT_WEAK_PEN_WIDTH)
double pen_width = DOT_WEAK_PEN_WIDTH + weight * DOT_PEN_WIDTH_SCALE;
assert(DOT_WEAK_PEN_WIDTH <= pen_width && pen_width <= DOT_STRONG_PEN_WIDTH);
return pen_width;
}
typedef uint32_t color_t;
#define COLOR_R(c) (((c) >> 16) & 0xFF)
#define COLOR_G(c) (((c) >> 8) & 0xFF)
#define COLOR_B(c) (((c) >> 0) & 0xFF)
static color_t
mkcolor(uint8_t r, uint8_t g, uint8_t b)
{
color_t c = (r << 16) | (g << 8) | b;
assert(c < (1 << 24));
return c;
}
static color_t
hash_to_node_color(uint64_t hash)
{
uint8_t r = 0x80 | (0xFF & (hash >> 16));
uint8_t g = 0x80 | (0xFF & (hash >> 8));
uint8_t b = 0x80 | (0xFF & (hash >> 0));
return mkcolor(r, g, b);
}
static color_t
node_to_edge_color(color_t c, double mult)
{
uint8_t r = MAX(COLOR_R(c) * mult, 0x00);
uint8_t g = MAX(COLOR_G(c) * mult, 0x00);
uint8_t b = MAX(COLOR_B(c) * mult, 0x00);
return mkcolor(r, g, b);
}
#define COLOR_FMT "\"#%06" PRIx32 "\""
void
output_dot_graph(FILE *out, struct path_graph *pg, struct request_table *rt)
{
assert(out != NULL);
assert(pg != NULL);
assert(rt != NULL);
struct path_graph_vertex *vertex;
request_id_t rid;
const char *request_data;
uint64_t request_hash;
fprintf(out,
"digraph apathy_graph {\n"
" nodesep=1.0;\n"
" rankdir=LR;\n"
" ranksep=1.0;\n"
"\n");
uint64_t prev_depth = 0, cur_depth = 0;
int first = 1;
int open_subgraph = 1;
/* Declare nodes with labels, and rank by minimum depth */
size_t v = 0;
uint64_t subgraph_id = 0;
while (v < pg->capvertices) {
vertex = &pg->vertices[v];
if (is_null_vertex(vertex))
continue;
if (open_subgraph) {
fprintf(out,
" subgraph s%" PRIu64 " {\n"
" rank = same;\n",
subgraph_id);
open_subgraph = 0;
subgraph_id++;
}
if (first) {
cur_depth = vertex->min_depth;
prev_depth = cur_depth;
first = 0;
} else {
prev_depth = cur_depth;
cur_depth = vertex->min_depth;
if (prev_depth != cur_depth)
goto close_subgraph;
}
rid = vertex->rid;
request_data = rt->requests[rid];
request_hash = rt->hashes[rid];
double pct_in = 100 * ((double)vertex->total_nhits_in / (double)pg->total_nhits);
double pct_out = 100 * ((double)vertex->total_nhits_out / (double)vertex->total_nhits_in);
double weight = calc_dot_weight(pg->total_nhits, vertex->total_nhits_in);
int font_size = calc_dot_font_size(weight);
double pen_width = calc_dot_pen_width(weight);
color_t node_color = hash_to_node_color(request_hash);
fprintf(out,
" r%" PRIuRID " [label=\"%s\\n(in %.2lf%% (%" PRIu64 "), out %.2lf%% (%" PRIu64 "))\", "
"fontsize=%d, "
"style=filled, "
"fillcolor=" COLOR_FMT ", "
"penwidth=%lf];\n",
rid, request_data, pct_in, vertex->total_nhits_in,
pct_out, vertex->total_nhits_out,
font_size, node_color, pen_width);
v++;
close_subgraph:
if (prev_depth != cur_depth) {
fprintf(out,
" }\n\n");
open_subgraph = 1;
}
}
if (!open_subgraph) {
fprintf(out,
" }\n\n");
}
/* Link nodes */
for (v = 0; v < pg->capvertices; v++) {
vertex = &pg->vertices[v];
if (is_null_vertex(vertex))
continue;
rid = vertex->rid;
request_hash = rt->hashes[rid];
for (size_t e = 0; e < vertex->nedges; e++) {
struct path_graph_edge *edge = &vertex->edges[e];
double pct = 100 *
((double)edge->nhits / (double)pg->total_edge_nhits);
double weight = calc_dot_weight(pg->total_nhits,
edge->nhits);
int font_size = calc_dot_font_size(weight);
double pen_width = calc_dot_pen_width(weight);
request_id_t edge_rid = edge->rid;
struct path_graph_vertex *edge_vertex = &pg->vertices[edge_rid];
assert(!is_null_vertex(edge_vertex));
const char *style;
if (rid == edge_rid)
style = "dotted";
else if (vertex->min_depth <= edge_vertex->min_depth)
style = "solid";
else
style = "dashed";
color_t node_color = hash_to_node_color(request_hash);
double edge_mult = 0.8;
double edge_label_mult = 0.6;
color_t edge_color =
node_to_edge_color(node_color, edge_mult);
color_t edge_label_color =
node_to_edge_color(node_color, edge_label_mult);
double duration_sec = edge->duration_cma / 1000.0;
fprintf(out,
" r%" PRIuRID " -> r%" PRIuRID " [xlabel=\"%.2lf%% (%" PRIu64 ")\\n%.1lfs\", "
"fontsize=%d, "
"style=\"%s\", "
"color=" COLOR_FMT ", "
"fontcolor=" COLOR_FMT ", "
"penwidth=%lf];\n",
rid, edge->rid, pct, edge->nhits, duration_sec,
font_size, style, edge_color, edge_label_color,
pen_width);
}
}
fprintf(out, "}\n");
}