1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * svghelper.c - helper functions for outputting svg
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright 2009 Intel Corporation
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Authors:
8*4882a593Smuzhiyun * Arjan van de Ven <arjan@linux.intel.com>
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <inttypes.h>
12*4882a593Smuzhiyun #include <stdio.h>
13*4882a593Smuzhiyun #include <stdlib.h>
14*4882a593Smuzhiyun #include <unistd.h>
15*4882a593Smuzhiyun #include <string.h>
16*4882a593Smuzhiyun #include <linux/bitmap.h>
17*4882a593Smuzhiyun #include <linux/string.h>
18*4882a593Smuzhiyun #include <linux/time64.h>
19*4882a593Smuzhiyun #include <linux/zalloc.h>
20*4882a593Smuzhiyun #include <internal/cpumap.h>
21*4882a593Smuzhiyun #include <perf/cpumap.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun #include "env.h"
24*4882a593Smuzhiyun #include "svghelper.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static u64 first_time, last_time;
27*4882a593Smuzhiyun static u64 turbo_frequency, max_freq;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #define SLOT_MULT 30.0
31*4882a593Smuzhiyun #define SLOT_HEIGHT 25.0
32*4882a593Smuzhiyun #define SLOT_HALF (SLOT_HEIGHT / 2)
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun int svg_page_width = 1000;
35*4882a593Smuzhiyun u64 svg_highlight;
36*4882a593Smuzhiyun const char *svg_highlight_name;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define MIN_TEXT_SIZE 0.01
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static u64 total_height;
41*4882a593Smuzhiyun static FILE *svgfile;
42*4882a593Smuzhiyun
cpu2slot(int cpu)43*4882a593Smuzhiyun static double cpu2slot(int cpu)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun return 2 * cpu + 1;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun static int *topology_map;
49*4882a593Smuzhiyun
cpu2y(int cpu)50*4882a593Smuzhiyun static double cpu2y(int cpu)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun if (topology_map)
53*4882a593Smuzhiyun return cpu2slot(topology_map[cpu]) * SLOT_MULT;
54*4882a593Smuzhiyun else
55*4882a593Smuzhiyun return cpu2slot(cpu) * SLOT_MULT;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
time2pixels(u64 __time)58*4882a593Smuzhiyun static double time2pixels(u64 __time)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun double X;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun X = 1.0 * svg_page_width * (__time - first_time) / (last_time - first_time);
63*4882a593Smuzhiyun return X;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun * Round text sizes so that the svg viewer only needs a discrete
68*4882a593Smuzhiyun * number of renderings of the font
69*4882a593Smuzhiyun */
round_text_size(double size)70*4882a593Smuzhiyun static double round_text_size(double size)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun int loop = 100;
73*4882a593Smuzhiyun double target = 10.0;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun if (size >= 10.0)
76*4882a593Smuzhiyun return size;
77*4882a593Smuzhiyun while (loop--) {
78*4882a593Smuzhiyun if (size >= target)
79*4882a593Smuzhiyun return target;
80*4882a593Smuzhiyun target = target / 2.0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun return size;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
open_svg(const char * filename,int cpus,int rows,u64 start,u64 end)85*4882a593Smuzhiyun void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun int new_width;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun svgfile = fopen(filename, "w");
90*4882a593Smuzhiyun if (!svgfile) {
91*4882a593Smuzhiyun fprintf(stderr, "Cannot open %s for output\n", filename);
92*4882a593Smuzhiyun return;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun first_time = start;
95*4882a593Smuzhiyun first_time = first_time / 100000000 * 100000000;
96*4882a593Smuzhiyun last_time = end;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * if the recording is short, we default to a width of 1000, but
100*4882a593Smuzhiyun * for longer recordings we want at least 200 units of width per second
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun new_width = (last_time - first_time) / 5000000;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (new_width > svg_page_width)
105*4882a593Smuzhiyun svg_page_width = new_width;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT;
108*4882a593Smuzhiyun fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n");
109*4882a593Smuzhiyun fprintf(svgfile, "<!DOCTYPE svg SYSTEM \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
110*4882a593Smuzhiyun fprintf(svgfile, "<svg width=\"%i\" height=\"%" PRIu64 "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", svg_page_width, total_height);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun fprintf(svgfile, " rect { stroke-width: 1; }\n");
115*4882a593Smuzhiyun fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n");
116*4882a593Smuzhiyun fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
117*4882a593Smuzhiyun fprintf(svgfile, " rect.process3 { fill:rgb(180,180,180); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
118*4882a593Smuzhiyun fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
119*4882a593Smuzhiyun fprintf(svgfile, " rect.sample_hi{ fill:rgb(255,128, 0); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
120*4882a593Smuzhiyun fprintf(svgfile, " rect.error { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
121*4882a593Smuzhiyun fprintf(svgfile, " rect.net { fill:rgb( 0,128, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
122*4882a593Smuzhiyun fprintf(svgfile, " rect.disk { fill:rgb( 0, 0,255); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
123*4882a593Smuzhiyun fprintf(svgfile, " rect.sync { fill:rgb(128,128, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
124*4882a593Smuzhiyun fprintf(svgfile, " rect.poll { fill:rgb( 0,128,128); fill-opacity:0.2; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
125*4882a593Smuzhiyun fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
126*4882a593Smuzhiyun fprintf(svgfile, " rect.waiting { fill:rgb(224,214, 0); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
127*4882a593Smuzhiyun fprintf(svgfile, " rect.WAITING { fill:rgb(255,214, 48); fill-opacity:0.6; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
128*4882a593Smuzhiyun fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
129*4882a593Smuzhiyun fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
130*4882a593Smuzhiyun fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
131*4882a593Smuzhiyun fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
132*4882a593Smuzhiyun fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
133*4882a593Smuzhiyun fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
134*4882a593Smuzhiyun fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
135*4882a593Smuzhiyun fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n");
136*4882a593Smuzhiyun fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n");
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
normalize_height(double height)141*4882a593Smuzhiyun static double normalize_height(double height)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun if (height < 0.25)
144*4882a593Smuzhiyun return 0.25;
145*4882a593Smuzhiyun else if (height < 0.50)
146*4882a593Smuzhiyun return 0.50;
147*4882a593Smuzhiyun else if (height < 0.75)
148*4882a593Smuzhiyun return 0.75;
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun return 0.100;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
svg_ubox(int Yslot,u64 start,u64 end,double height,const char * type,int fd,int err,int merges)153*4882a593Smuzhiyun void svg_ubox(int Yslot, u64 start, u64 end, double height, const char *type, int fd, int err, int merges)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun double w = time2pixels(end) - time2pixels(start);
156*4882a593Smuzhiyun height = normalize_height(height);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun if (!svgfile)
159*4882a593Smuzhiyun return;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
162*4882a593Smuzhiyun fprintf(svgfile, "<title>fd=%d error=%d merges=%d</title>\n", fd, err, merges);
163*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"%s\"/>\n",
164*4882a593Smuzhiyun time2pixels(start),
165*4882a593Smuzhiyun w,
166*4882a593Smuzhiyun Yslot * SLOT_MULT,
167*4882a593Smuzhiyun SLOT_HALF * height,
168*4882a593Smuzhiyun type);
169*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
svg_lbox(int Yslot,u64 start,u64 end,double height,const char * type,int fd,int err,int merges)172*4882a593Smuzhiyun void svg_lbox(int Yslot, u64 start, u64 end, double height, const char *type, int fd, int err, int merges)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun double w = time2pixels(end) - time2pixels(start);
175*4882a593Smuzhiyun height = normalize_height(height);
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun if (!svgfile)
178*4882a593Smuzhiyun return;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
181*4882a593Smuzhiyun fprintf(svgfile, "<title>fd=%d error=%d merges=%d</title>\n", fd, err, merges);
182*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"%s\"/>\n",
183*4882a593Smuzhiyun time2pixels(start),
184*4882a593Smuzhiyun w,
185*4882a593Smuzhiyun Yslot * SLOT_MULT + SLOT_HEIGHT - SLOT_HALF * height,
186*4882a593Smuzhiyun SLOT_HALF * height,
187*4882a593Smuzhiyun type);
188*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
svg_fbox(int Yslot,u64 start,u64 end,double height,const char * type,int fd,int err,int merges)191*4882a593Smuzhiyun void svg_fbox(int Yslot, u64 start, u64 end, double height, const char *type, int fd, int err, int merges)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun double w = time2pixels(end) - time2pixels(start);
194*4882a593Smuzhiyun height = normalize_height(height);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (!svgfile)
197*4882a593Smuzhiyun return;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
200*4882a593Smuzhiyun fprintf(svgfile, "<title>fd=%d error=%d merges=%d</title>\n", fd, err, merges);
201*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"%s\"/>\n",
202*4882a593Smuzhiyun time2pixels(start),
203*4882a593Smuzhiyun w,
204*4882a593Smuzhiyun Yslot * SLOT_MULT + SLOT_HEIGHT - SLOT_HEIGHT * height,
205*4882a593Smuzhiyun SLOT_HEIGHT * height,
206*4882a593Smuzhiyun type);
207*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
svg_box(int Yslot,u64 start,u64 end,const char * type)210*4882a593Smuzhiyun void svg_box(int Yslot, u64 start, u64 end, const char *type)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun if (!svgfile)
213*4882a593Smuzhiyun return;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"%s\"/>\n",
216*4882a593Smuzhiyun time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun static char *time_to_string(u64 duration);
svg_blocked(int Yslot,int cpu,u64 start,u64 end,const char * backtrace)220*4882a593Smuzhiyun void svg_blocked(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun if (!svgfile)
223*4882a593Smuzhiyun return;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
226*4882a593Smuzhiyun fprintf(svgfile, "<title>#%d blocked %s</title>\n", cpu,
227*4882a593Smuzhiyun time_to_string(end - start));
228*4882a593Smuzhiyun if (backtrace)
229*4882a593Smuzhiyun fprintf(svgfile, "<desc>Blocked on:\n%s</desc>\n", backtrace);
230*4882a593Smuzhiyun svg_box(Yslot, start, end, "blocked");
231*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
svg_running(int Yslot,int cpu,u64 start,u64 end,const char * backtrace)234*4882a593Smuzhiyun void svg_running(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun double text_size;
237*4882a593Smuzhiyun const char *type;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (!svgfile)
240*4882a593Smuzhiyun return;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (svg_highlight && end - start > svg_highlight)
243*4882a593Smuzhiyun type = "sample_hi";
244*4882a593Smuzhiyun else
245*4882a593Smuzhiyun type = "sample";
246*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun fprintf(svgfile, "<title>#%d running %s</title>\n",
249*4882a593Smuzhiyun cpu, time_to_string(end - start));
250*4882a593Smuzhiyun if (backtrace)
251*4882a593Smuzhiyun fprintf(svgfile, "<desc>Switched because:\n%s</desc>\n", backtrace);
252*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"%s\"/>\n",
253*4882a593Smuzhiyun time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT,
254*4882a593Smuzhiyun type);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun text_size = (time2pixels(end)-time2pixels(start));
257*4882a593Smuzhiyun if (cpu > 9)
258*4882a593Smuzhiyun text_size = text_size/2;
259*4882a593Smuzhiyun if (text_size > 1.25)
260*4882a593Smuzhiyun text_size = 1.25;
261*4882a593Smuzhiyun text_size = round_text_size(text_size);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (text_size > MIN_TEXT_SIZE)
264*4882a593Smuzhiyun fprintf(svgfile, "<text x=\"%.8f\" y=\"%.8f\" font-size=\"%.8fpt\">%i</text>\n",
265*4882a593Smuzhiyun time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
time_to_string(u64 duration)270*4882a593Smuzhiyun static char *time_to_string(u64 duration)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun static char text[80];
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun text[0] = 0;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (duration < NSEC_PER_USEC) /* less than 1 usec */
277*4882a593Smuzhiyun return text;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun if (duration < NSEC_PER_MSEC) { /* less than 1 msec */
280*4882a593Smuzhiyun sprintf(text, "%.1f us", duration / (double)NSEC_PER_USEC);
281*4882a593Smuzhiyun return text;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun sprintf(text, "%.1f ms", duration / (double)NSEC_PER_MSEC);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun return text;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
svg_waiting(int Yslot,int cpu,u64 start,u64 end,const char * backtrace)288*4882a593Smuzhiyun void svg_waiting(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun char *text;
291*4882a593Smuzhiyun const char *style;
292*4882a593Smuzhiyun double font_size;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun if (!svgfile)
295*4882a593Smuzhiyun return;
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun style = "waiting";
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (end-start > 10 * NSEC_PER_MSEC) /* 10 msec */
300*4882a593Smuzhiyun style = "WAITING";
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun text = time_to_string(end-start);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun font_size = 1.0 * (time2pixels(end)-time2pixels(start));
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (font_size > 3)
307*4882a593Smuzhiyun font_size = 3;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun font_size = round_text_size(font_size);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\">\n", time2pixels(start), Yslot * SLOT_MULT);
312*4882a593Smuzhiyun fprintf(svgfile, "<title>#%d waiting %s</title>\n", cpu, time_to_string(end - start));
313*4882a593Smuzhiyun if (backtrace)
314*4882a593Smuzhiyun fprintf(svgfile, "<desc>Waiting on:\n%s</desc>\n", backtrace);
315*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"0\" width=\"%.8f\" y=\"0\" height=\"%.1f\" class=\"%s\"/>\n",
316*4882a593Smuzhiyun time2pixels(end)-time2pixels(start), SLOT_HEIGHT, style);
317*4882a593Smuzhiyun if (font_size > MIN_TEXT_SIZE)
318*4882a593Smuzhiyun fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%.8fpt\"> %s</text>\n",
319*4882a593Smuzhiyun font_size, text);
320*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
cpu_model(void)323*4882a593Smuzhiyun static char *cpu_model(void)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun static char cpu_m[255];
326*4882a593Smuzhiyun char buf[256];
327*4882a593Smuzhiyun FILE *file;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun cpu_m[0] = 0;
330*4882a593Smuzhiyun /* CPU type */
331*4882a593Smuzhiyun file = fopen("/proc/cpuinfo", "r");
332*4882a593Smuzhiyun if (file) {
333*4882a593Smuzhiyun while (fgets(buf, 255, file)) {
334*4882a593Smuzhiyun if (strstr(buf, "model name")) {
335*4882a593Smuzhiyun strlcpy(cpu_m, &buf[13], 255);
336*4882a593Smuzhiyun break;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun fclose(file);
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun /* CPU type */
343*4882a593Smuzhiyun file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", "r");
344*4882a593Smuzhiyun if (file) {
345*4882a593Smuzhiyun while (fgets(buf, 255, file)) {
346*4882a593Smuzhiyun unsigned int freq;
347*4882a593Smuzhiyun freq = strtoull(buf, NULL, 10);
348*4882a593Smuzhiyun if (freq > max_freq)
349*4882a593Smuzhiyun max_freq = freq;
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun fclose(file);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun return cpu_m;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun
svg_cpu_box(int cpu,u64 __max_freq,u64 __turbo_freq)356*4882a593Smuzhiyun void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun char cpu_string[80];
359*4882a593Smuzhiyun if (!svgfile)
360*4882a593Smuzhiyun return;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun max_freq = __max_freq;
363*4882a593Smuzhiyun turbo_frequency = __turbo_freq;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\" class=\"cpu\"/>\n",
368*4882a593Smuzhiyun time2pixels(first_time),
369*4882a593Smuzhiyun time2pixels(last_time)-time2pixels(first_time),
370*4882a593Smuzhiyun cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun sprintf(cpu_string, "CPU %i", (int)cpu);
373*4882a593Smuzhiyun fprintf(svgfile, "<text x=\"%.8f\" y=\"%.8f\">%s</text>\n",
374*4882a593Smuzhiyun 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun fprintf(svgfile, "<text transform=\"translate(%.8f,%.8f)\" font-size=\"1.25pt\">%s</text>\n",
377*4882a593Smuzhiyun 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun
svg_process(int cpu,u64 start,u64 end,int pid,const char * name,const char * backtrace)382*4882a593Smuzhiyun void svg_process(int cpu, u64 start, u64 end, int pid, const char *name, const char *backtrace)
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun double width;
385*4882a593Smuzhiyun const char *type;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun if (!svgfile)
388*4882a593Smuzhiyun return;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (svg_highlight && end - start >= svg_highlight)
391*4882a593Smuzhiyun type = "sample_hi";
392*4882a593Smuzhiyun else if (svg_highlight_name && strstr(name, svg_highlight_name))
393*4882a593Smuzhiyun type = "sample_hi";
394*4882a593Smuzhiyun else
395*4882a593Smuzhiyun type = "sample";
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\">\n", time2pixels(start), cpu2y(cpu));
398*4882a593Smuzhiyun fprintf(svgfile, "<title>%d %s running %s</title>\n", pid, name, time_to_string(end - start));
399*4882a593Smuzhiyun if (backtrace)
400*4882a593Smuzhiyun fprintf(svgfile, "<desc>Switched because:\n%s</desc>\n", backtrace);
401*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"0\" width=\"%.8f\" y=\"0\" height=\"%.1f\" class=\"%s\"/>\n",
402*4882a593Smuzhiyun time2pixels(end)-time2pixels(start), SLOT_MULT+SLOT_HEIGHT, type);
403*4882a593Smuzhiyun width = time2pixels(end)-time2pixels(start);
404*4882a593Smuzhiyun if (width > 6)
405*4882a593Smuzhiyun width = 6;
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun width = round_text_size(width);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun if (width > MIN_TEXT_SIZE)
410*4882a593Smuzhiyun fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%.8fpt\">%s</text>\n",
411*4882a593Smuzhiyun width, name);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
svg_cstate(int cpu,u64 start,u64 end,int type)416*4882a593Smuzhiyun void svg_cstate(int cpu, u64 start, u64 end, int type)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun double width;
419*4882a593Smuzhiyun char style[128];
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun if (!svgfile)
422*4882a593Smuzhiyun return;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (type > 6)
428*4882a593Smuzhiyun type = 6;
429*4882a593Smuzhiyun sprintf(style, "c%i", type);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun fprintf(svgfile, "<rect class=\"%s\" x=\"%.8f\" width=\"%.8f\" y=\"%.1f\" height=\"%.1f\"/>\n",
432*4882a593Smuzhiyun style,
433*4882a593Smuzhiyun time2pixels(start), time2pixels(end)-time2pixels(start),
434*4882a593Smuzhiyun cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun width = (time2pixels(end)-time2pixels(start))/2.0;
437*4882a593Smuzhiyun if (width > 6)
438*4882a593Smuzhiyun width = 6;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun width = round_text_size(width);
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun if (width > MIN_TEXT_SIZE)
443*4882a593Smuzhiyun fprintf(svgfile, "<text x=\"%.8f\" y=\"%.8f\" font-size=\"%.8fpt\">C%i</text>\n",
444*4882a593Smuzhiyun time2pixels(start), cpu2y(cpu)+width, width, type);
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun
HzToHuman(unsigned long hz)449*4882a593Smuzhiyun static char *HzToHuman(unsigned long hz)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun static char buffer[1024];
452*4882a593Smuzhiyun unsigned long long Hz;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun memset(buffer, 0, 1024);
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun Hz = hz;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* default: just put the Number in */
459*4882a593Smuzhiyun sprintf(buffer, "%9lli", Hz);
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun if (Hz > 1000)
462*4882a593Smuzhiyun sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun if (Hz > 1500000)
465*4882a593Smuzhiyun sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (Hz == turbo_frequency)
468*4882a593Smuzhiyun sprintf(buffer, "Turbo");
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun return buffer;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun
svg_pstate(int cpu,u64 start,u64 end,u64 freq)473*4882a593Smuzhiyun void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun double height = 0;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun if (!svgfile)
478*4882a593Smuzhiyun return;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun if (max_freq)
483*4882a593Smuzhiyun height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
484*4882a593Smuzhiyun height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
485*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" x2=\"%.8f\" y1=\"%.1f\" y2=\"%.1f\" class=\"pstate\"/>\n",
486*4882a593Smuzhiyun time2pixels(start), time2pixels(end), height, height);
487*4882a593Smuzhiyun fprintf(svgfile, "<text x=\"%.8f\" y=\"%.8f\" font-size=\"0.25pt\">%s</text>\n",
488*4882a593Smuzhiyun time2pixels(start), height+0.9, HzToHuman(freq));
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun
svg_partial_wakeline(u64 start,int row1,char * desc1,int row2,char * desc2,const char * backtrace)494*4882a593Smuzhiyun void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2, const char *backtrace)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun double height;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun if (!svgfile)
499*4882a593Smuzhiyun return;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun fprintf(svgfile, "<title>%s wakes up %s</title>\n",
505*4882a593Smuzhiyun desc1 ? desc1 : "?",
506*4882a593Smuzhiyun desc2 ? desc2 : "?");
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun if (backtrace)
509*4882a593Smuzhiyun fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun if (row1 < row2) {
512*4882a593Smuzhiyun if (row1) {
513*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
514*4882a593Smuzhiyun time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
515*4882a593Smuzhiyun if (desc2)
516*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s ></text></g>\n",
517*4882a593Smuzhiyun time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun if (row2) {
520*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
521*4882a593Smuzhiyun time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
522*4882a593Smuzhiyun if (desc1)
523*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s ></text></g>\n",
524*4882a593Smuzhiyun time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun } else {
527*4882a593Smuzhiyun if (row2) {
528*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
529*4882a593Smuzhiyun time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
530*4882a593Smuzhiyun if (desc1)
531*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s <</text></g>\n",
532*4882a593Smuzhiyun time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun if (row1) {
535*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
536*4882a593Smuzhiyun time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT);
537*4882a593Smuzhiyun if (desc2)
538*4882a593Smuzhiyun fprintf(svgfile, "<g transform=\"translate(%.8f,%.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s <</text></g>\n",
539*4882a593Smuzhiyun time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun height = row1 * SLOT_MULT;
543*4882a593Smuzhiyun if (row2 > row1)
544*4882a593Smuzhiyun height += SLOT_HEIGHT;
545*4882a593Smuzhiyun if (row1)
546*4882a593Smuzhiyun fprintf(svgfile, "<circle cx=\"%.8f\" cy=\"%.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
547*4882a593Smuzhiyun time2pixels(start), height);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
svg_wakeline(u64 start,int row1,int row2,const char * backtrace)552*4882a593Smuzhiyun void svg_wakeline(u64 start, int row1, int row2, const char *backtrace)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun double height;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun if (!svgfile)
557*4882a593Smuzhiyun return;
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun if (backtrace)
563*4882a593Smuzhiyun fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun if (row1 < row2)
566*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
567*4882a593Smuzhiyun time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
568*4882a593Smuzhiyun else
569*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
570*4882a593Smuzhiyun time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun height = row1 * SLOT_MULT;
573*4882a593Smuzhiyun if (row2 > row1)
574*4882a593Smuzhiyun height += SLOT_HEIGHT;
575*4882a593Smuzhiyun fprintf(svgfile, "<circle cx=\"%.8f\" cy=\"%.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
576*4882a593Smuzhiyun time2pixels(start), height);
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
svg_interrupt(u64 start,int row,const char * backtrace)581*4882a593Smuzhiyun void svg_interrupt(u64 start, int row, const char *backtrace)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun if (!svgfile)
584*4882a593Smuzhiyun return;
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun fprintf(svgfile, "<title>Wakeup from interrupt</title>\n");
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun if (backtrace)
591*4882a593Smuzhiyun fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
592*4882a593Smuzhiyun
593*4882a593Smuzhiyun fprintf(svgfile, "<circle cx=\"%.8f\" cy=\"%.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
594*4882a593Smuzhiyun time2pixels(start), row * SLOT_MULT);
595*4882a593Smuzhiyun fprintf(svgfile, "<circle cx=\"%.8f\" cy=\"%.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
596*4882a593Smuzhiyun time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
svg_text(int Yslot,u64 start,const char * text)601*4882a593Smuzhiyun void svg_text(int Yslot, u64 start, const char *text)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun if (!svgfile)
604*4882a593Smuzhiyun return;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun fprintf(svgfile, "<text x=\"%.8f\" y=\"%.8f\">%s</text>\n",
607*4882a593Smuzhiyun time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun
svg_legenda_box(int X,const char * text,const char * style)610*4882a593Smuzhiyun static void svg_legenda_box(int X, const char *text, const char *style)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun double boxsize;
613*4882a593Smuzhiyun boxsize = SLOT_HEIGHT / 2;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun fprintf(svgfile, "<rect x=\"%i\" width=\"%.8f\" y=\"0\" height=\"%.1f\" class=\"%s\"/>\n",
616*4882a593Smuzhiyun X, boxsize, boxsize, style);
617*4882a593Smuzhiyun fprintf(svgfile, "<text transform=\"translate(%.8f, %.8f)\" font-size=\"%.8fpt\">%s</text>\n",
618*4882a593Smuzhiyun X + boxsize + 5, boxsize, 0.8 * boxsize, text);
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
svg_io_legenda(void)621*4882a593Smuzhiyun void svg_io_legenda(void)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun if (!svgfile)
624*4882a593Smuzhiyun return;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
627*4882a593Smuzhiyun svg_legenda_box(0, "Disk", "disk");
628*4882a593Smuzhiyun svg_legenda_box(100, "Network", "net");
629*4882a593Smuzhiyun svg_legenda_box(200, "Sync", "sync");
630*4882a593Smuzhiyun svg_legenda_box(300, "Poll", "poll");
631*4882a593Smuzhiyun svg_legenda_box(400, "Error", "error");
632*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
svg_legenda(void)635*4882a593Smuzhiyun void svg_legenda(void)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun if (!svgfile)
638*4882a593Smuzhiyun return;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun fprintf(svgfile, "<g>\n");
641*4882a593Smuzhiyun svg_legenda_box(0, "Running", "sample");
642*4882a593Smuzhiyun svg_legenda_box(100, "Idle","c1");
643*4882a593Smuzhiyun svg_legenda_box(200, "Deeper Idle", "c3");
644*4882a593Smuzhiyun svg_legenda_box(350, "Deepest Idle", "c6");
645*4882a593Smuzhiyun svg_legenda_box(550, "Sleeping", "process2");
646*4882a593Smuzhiyun svg_legenda_box(650, "Waiting for cpu", "waiting");
647*4882a593Smuzhiyun svg_legenda_box(800, "Blocked on IO", "blocked");
648*4882a593Smuzhiyun fprintf(svgfile, "</g>\n");
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun
svg_time_grid(double min_thickness)651*4882a593Smuzhiyun void svg_time_grid(double min_thickness)
652*4882a593Smuzhiyun {
653*4882a593Smuzhiyun u64 i;
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (!svgfile)
656*4882a593Smuzhiyun return;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun i = first_time;
659*4882a593Smuzhiyun while (i < last_time) {
660*4882a593Smuzhiyun int color = 220;
661*4882a593Smuzhiyun double thickness = 0.075;
662*4882a593Smuzhiyun if ((i % 100000000) == 0) {
663*4882a593Smuzhiyun thickness = 0.5;
664*4882a593Smuzhiyun color = 192;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun if ((i % 1000000000) == 0) {
667*4882a593Smuzhiyun thickness = 2.0;
668*4882a593Smuzhiyun color = 128;
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun if (thickness >= min_thickness)
672*4882a593Smuzhiyun fprintf(svgfile, "<line x1=\"%.8f\" y1=\"%.2f\" x2=\"%.8f\" y2=\"%" PRIu64 "\" style=\"stroke:rgb(%i,%i,%i);stroke-width:%.3f\"/>\n",
673*4882a593Smuzhiyun time2pixels(i), SLOT_MULT/2, time2pixels(i),
674*4882a593Smuzhiyun total_height, color, color, color, thickness);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun i += 10000000;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun
svg_close(void)680*4882a593Smuzhiyun void svg_close(void)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun if (svgfile) {
683*4882a593Smuzhiyun fprintf(svgfile, "</svg>\n");
684*4882a593Smuzhiyun fclose(svgfile);
685*4882a593Smuzhiyun svgfile = NULL;
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun #define cpumask_bits(maskp) ((maskp)->bits)
690*4882a593Smuzhiyun typedef struct { DECLARE_BITMAP(bits, MAX_NR_CPUS); } cpumask_t;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun struct topology {
693*4882a593Smuzhiyun cpumask_t *sib_core;
694*4882a593Smuzhiyun int sib_core_nr;
695*4882a593Smuzhiyun cpumask_t *sib_thr;
696*4882a593Smuzhiyun int sib_thr_nr;
697*4882a593Smuzhiyun };
698*4882a593Smuzhiyun
scan_thread_topology(int * map,struct topology * t,int cpu,int * pos,int nr_cpus)699*4882a593Smuzhiyun static void scan_thread_topology(int *map, struct topology *t, int cpu,
700*4882a593Smuzhiyun int *pos, int nr_cpus)
701*4882a593Smuzhiyun {
702*4882a593Smuzhiyun int i;
703*4882a593Smuzhiyun int thr;
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun for (i = 0; i < t->sib_thr_nr; i++) {
706*4882a593Smuzhiyun if (!test_bit(cpu, cpumask_bits(&t->sib_thr[i])))
707*4882a593Smuzhiyun continue;
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun for_each_set_bit(thr, cpumask_bits(&t->sib_thr[i]), nr_cpus)
710*4882a593Smuzhiyun if (map[thr] == -1)
711*4882a593Smuzhiyun map[thr] = (*pos)++;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun
scan_core_topology(int * map,struct topology * t,int nr_cpus)715*4882a593Smuzhiyun static void scan_core_topology(int *map, struct topology *t, int nr_cpus)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun int pos = 0;
718*4882a593Smuzhiyun int i;
719*4882a593Smuzhiyun int cpu;
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun for (i = 0; i < t->sib_core_nr; i++)
722*4882a593Smuzhiyun for_each_set_bit(cpu, cpumask_bits(&t->sib_core[i]), nr_cpus)
723*4882a593Smuzhiyun scan_thread_topology(map, t, cpu, &pos, nr_cpus);
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun
str_to_bitmap(char * s,cpumask_t * b,int nr_cpus)726*4882a593Smuzhiyun static int str_to_bitmap(char *s, cpumask_t *b, int nr_cpus)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun int i;
729*4882a593Smuzhiyun int ret = 0;
730*4882a593Smuzhiyun struct perf_cpu_map *m;
731*4882a593Smuzhiyun int c;
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun m = perf_cpu_map__new(s);
734*4882a593Smuzhiyun if (!m)
735*4882a593Smuzhiyun return -1;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun for (i = 0; i < m->nr; i++) {
738*4882a593Smuzhiyun c = m->map[i];
739*4882a593Smuzhiyun if (c >= nr_cpus) {
740*4882a593Smuzhiyun ret = -1;
741*4882a593Smuzhiyun break;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun set_bit(c, cpumask_bits(b));
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun perf_cpu_map__put(m);
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun return ret;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
svg_build_topology_map(struct perf_env * env)752*4882a593Smuzhiyun int svg_build_topology_map(struct perf_env *env)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun int i, nr_cpus;
755*4882a593Smuzhiyun struct topology t;
756*4882a593Smuzhiyun char *sib_core, *sib_thr;
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun nr_cpus = min(env->nr_cpus_online, MAX_NR_CPUS);
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun t.sib_core_nr = env->nr_sibling_cores;
761*4882a593Smuzhiyun t.sib_thr_nr = env->nr_sibling_threads;
762*4882a593Smuzhiyun t.sib_core = calloc(env->nr_sibling_cores, sizeof(cpumask_t));
763*4882a593Smuzhiyun t.sib_thr = calloc(env->nr_sibling_threads, sizeof(cpumask_t));
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun sib_core = env->sibling_cores;
766*4882a593Smuzhiyun sib_thr = env->sibling_threads;
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun if (!t.sib_core || !t.sib_thr) {
769*4882a593Smuzhiyun fprintf(stderr, "topology: no memory\n");
770*4882a593Smuzhiyun goto exit;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun for (i = 0; i < env->nr_sibling_cores; i++) {
774*4882a593Smuzhiyun if (str_to_bitmap(sib_core, &t.sib_core[i], nr_cpus)) {
775*4882a593Smuzhiyun fprintf(stderr, "topology: can't parse siblings map\n");
776*4882a593Smuzhiyun goto exit;
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun sib_core += strlen(sib_core) + 1;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun for (i = 0; i < env->nr_sibling_threads; i++) {
783*4882a593Smuzhiyun if (str_to_bitmap(sib_thr, &t.sib_thr[i], nr_cpus)) {
784*4882a593Smuzhiyun fprintf(stderr, "topology: can't parse siblings map\n");
785*4882a593Smuzhiyun goto exit;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun sib_thr += strlen(sib_thr) + 1;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun topology_map = malloc(sizeof(int) * nr_cpus);
792*4882a593Smuzhiyun if (!topology_map) {
793*4882a593Smuzhiyun fprintf(stderr, "topology: no memory\n");
794*4882a593Smuzhiyun goto exit;
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun for (i = 0; i < nr_cpus; i++)
798*4882a593Smuzhiyun topology_map[i] = -1;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun scan_core_topology(topology_map, &t, nr_cpus);
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun return 0;
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun exit:
805*4882a593Smuzhiyun zfree(&t.sib_core);
806*4882a593Smuzhiyun zfree(&t.sib_thr);
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun return -1;
809*4882a593Smuzhiyun }
810