xref: /dragonfly/usr.bin/kcollect/gnuplot.c (revision 9afa2da7)
1f9c8e4acSMatthew Dillon /*
2f9c8e4acSMatthew Dillon  * Copyright (c) 2017 The DragonFly Project.  All rights reserved.
3f9c8e4acSMatthew Dillon  *
4f9c8e4acSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5f9c8e4acSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6f9c8e4acSMatthew Dillon  *
7f9c8e4acSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8f9c8e4acSMatthew Dillon  * modification, are permitted provided that the following conditions
9f9c8e4acSMatthew Dillon  * are met:
10f9c8e4acSMatthew Dillon  *
11f9c8e4acSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12f9c8e4acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13f9c8e4acSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14f9c8e4acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15f9c8e4acSMatthew Dillon  *    the documentation and/or other materials provided with the
16f9c8e4acSMatthew Dillon  *    distribution.
17f9c8e4acSMatthew Dillon  *
18f9c8e4acSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f9c8e4acSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20f9c8e4acSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21f9c8e4acSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22f9c8e4acSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23f9c8e4acSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24f9c8e4acSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25f9c8e4acSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26f9c8e4acSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27f9c8e4acSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28f9c8e4acSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29f9c8e4acSMatthew Dillon  * SUCH DAMAGE.
30f9c8e4acSMatthew Dillon  */
31f9c8e4acSMatthew Dillon 
32f9c8e4acSMatthew Dillon #include "kcollect.h"
33f9c8e4acSMatthew Dillon 
34f9c8e4acSMatthew Dillon void
start_gnuplot(int ac __unused,char ** av __unused,const char * plotfile)3556160b9cSMatthew Dillon start_gnuplot(int ac __unused, char **av __unused, const char *plotfile)
36f9c8e4acSMatthew Dillon {
37f9c8e4acSMatthew Dillon 	OutFP = popen("gnuplot", "w");
38f9c8e4acSMatthew Dillon 	if (OutFP == NULL) {
39f9c8e4acSMatthew Dillon 		fprintf(stderr, "can't find gnuplot\n");
40f9c8e4acSMatthew Dillon 		exit(1);
41f9c8e4acSMatthew Dillon 	}
42f9c8e4acSMatthew Dillon 
43f9c8e4acSMatthew Dillon 	/*
44f9c8e4acSMatthew Dillon 	 * If plotfile is specified allow .jpg or .JPG or .png or .PNG
45f9c8e4acSMatthew Dillon 	 */
46f9c8e4acSMatthew Dillon 	if (plotfile) {
47f9c8e4acSMatthew Dillon 		const char *ext;
48f9c8e4acSMatthew Dillon 
49f9c8e4acSMatthew Dillon 		if ((ext = strrchr(plotfile, '.')) == NULL) {
50f9c8e4acSMatthew Dillon 			ext = "";
51f9c8e4acSMatthew Dillon 		} else {
52f9c8e4acSMatthew Dillon 			++ext;
53f9c8e4acSMatthew Dillon 		}
54f9c8e4acSMatthew Dillon 		if (strcmp(ext, "jpg") == 0 ||
55f9c8e4acSMatthew Dillon 		    strcmp(ext, "JPG") == 0) {
56f9c8e4acSMatthew Dillon 			fprintf(OutFP, "set terminal jpeg size %d,%d\n",
57f9c8e4acSMatthew Dillon 				OutputWidth, OutputHeight);
58f9c8e4acSMatthew Dillon 		} else if (strcmp(ext, "png") == 0 ||
59f9c8e4acSMatthew Dillon 			   strcmp(ext, "PNG") == 0) {
60f9c8e4acSMatthew Dillon 			fprintf(OutFP, "set terminal png size %d,%d\n",
61f9c8e4acSMatthew Dillon 				OutputWidth, OutputHeight);
62f9c8e4acSMatthew Dillon 		} else {
63f9c8e4acSMatthew Dillon 			fprintf(stderr, "plotfile must be .jpg or .png\n");
64f9c8e4acSMatthew Dillon 			exit(1);
65f9c8e4acSMatthew Dillon 		}
66f9c8e4acSMatthew Dillon 		fprintf(OutFP, "set output \"%s\"\n", plotfile);
67f9c8e4acSMatthew Dillon 	} else {
68f9c8e4acSMatthew Dillon 		fprintf(OutFP, "set terminal x11 persist size %d,%d\n",
69f9c8e4acSMatthew Dillon 			OutputWidth, OutputHeight);
70f9c8e4acSMatthew Dillon 	}
7156160b9cSMatthew Dillon }
7256160b9cSMatthew Dillon 
7356160b9cSMatthew Dillon void
dump_gnuplot(kcollect_t * ary,size_t count)7456160b9cSMatthew Dillon dump_gnuplot(kcollect_t *ary, size_t count)
7556160b9cSMatthew Dillon {
7656160b9cSMatthew Dillon 	int plot1[] = { KCOLLECT_MEMFRE, KCOLLECT_MEMCAC,
7756160b9cSMatthew Dillon 			KCOLLECT_MEMINA, KCOLLECT_MEMACT,
7856160b9cSMatthew Dillon 			KCOLLECT_MEMWIR, KCOLLECT_LOAD };
7956160b9cSMatthew Dillon 	int plot2[] = { KCOLLECT_IDLEPCT, KCOLLECT_INTRPCT,
8056160b9cSMatthew Dillon 			KCOLLECT_SYSTPCT, KCOLLECT_USERPCT,
81c25e6c22SMatthew Dillon 			KCOLLECT_SWAPPCT,
82*9afa2da7SMatthew Dillon 			KCOLLECT_VMFAULT, -1/*KCOLLECT_SYSCALLS*/,
83*9afa2da7SMatthew Dillon 			KCOLLECT_NLOOKUP };
8456160b9cSMatthew Dillon 	const char *id1[] = {
8556160b9cSMatthew Dillon 			"free", "cache",
8656160b9cSMatthew Dillon 			"inact", "active",
8756160b9cSMatthew Dillon 			"wired", "load" };
8856160b9cSMatthew Dillon 	const char *id2[] = {
8956160b9cSMatthew Dillon 			"idle", "intr", "system", "user",
90c25e6c22SMatthew Dillon 			"swap",
9156160b9cSMatthew Dillon 			"faults", "syscalls", "nlookups" };
9256160b9cSMatthew Dillon 	struct tm *tmv;
9356160b9cSMatthew Dillon 	char buf[64];
9456160b9cSMatthew Dillon 	uint64_t value;
9556160b9cSMatthew Dillon 	time_t t;
9656160b9cSMatthew Dillon 	double dv;
9756160b9cSMatthew Dillon 	double smoothed_dv;
9856160b9cSMatthew Dillon 	int i;
9956160b9cSMatthew Dillon 	int j;
10056160b9cSMatthew Dillon 	int jj;
10156160b9cSMatthew Dillon 	int k;
102f9c8e4acSMatthew Dillon 
103f9c8e4acSMatthew Dillon 	/*
104f9c8e4acSMatthew Dillon 	 * NOTE: be sure to reset any fields adjusted by the second plot,
105f9c8e4acSMatthew Dillon 	 *	 in case we are streaming plots with -f.
106f9c8e4acSMatthew Dillon 	 */
107f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set xdata time\n");
108f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set timefmt \"%%d-%%b-%%Y %%H:%%M:%%S\"\n");
109f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set style fill solid 1.0\n");
110f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set multiplot layout 2,1\n");
111f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set key outside\n");
112f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set lmargin 10\n");
113f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set rmargin 25\n");
114f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set xtics rotate\n");
115f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set format x '%%H:%%M'\n");
116f9c8e4acSMatthew Dillon 
117f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set ylabel \"GB\"\n");
118f9c8e4acSMatthew Dillon 
119f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set yrange [0:%d]\n",
120f9c8e4acSMatthew Dillon 		(int)((KCOLLECT_GETSCALE(ary[0].data[KCOLLECT_MEMFRE]) +
121f9c8e4acSMatthew Dillon 		       999999) / 1000000000));
122f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set autoscale y2\n");
123f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set y2label \"Load\"\n");
124f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set ytics nomirror\n");
125f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set y2tics nomirror\n");
126f9c8e4acSMatthew Dillon 
127f9c8e4acSMatthew Dillon 	fprintf(OutFP,
128f9c8e4acSMatthew Dillon 		"plot "
1293ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1303ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1313ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1323ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1333ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1343ac52c49SMatthew Dillon 		"\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1\n",
1353ac52c49SMatthew Dillon 		id1[0], id1[1], id1[2], id1[3], id1[4], id1[5]);
136f9c8e4acSMatthew Dillon 
137f9c8e4acSMatthew Dillon 	for (jj = 0; jj < (int)(sizeof(plot1) / sizeof(plot1[0])); ++jj) {
138f9c8e4acSMatthew Dillon 		j = plot1[jj];
139f9c8e4acSMatthew Dillon 
140b038db3dSMatthew Dillon 		smoothed_dv = 0.0;
141f9c8e4acSMatthew Dillon 		for (i = count - 1; i >= 2; --i) {
142f9c8e4acSMatthew Dillon 			/*
143f9c8e4acSMatthew Dillon 			 * Timestamp
144f9c8e4acSMatthew Dillon 			 */
145f9c8e4acSMatthew Dillon 			t = ary[i].realtime.tv_sec;
146f9c8e4acSMatthew Dillon 			if (t < 1000)
147f9c8e4acSMatthew Dillon 				continue;
148f9c8e4acSMatthew Dillon 			if (UseGMT)
149f9c8e4acSMatthew Dillon 				tmv = gmtime(&t);
150f9c8e4acSMatthew Dillon 			else
151f9c8e4acSMatthew Dillon 				tmv = localtime(&t);
152f9c8e4acSMatthew Dillon 			strftime(buf, sizeof(buf), "%d-%b-%Y %H:%M:%S", tmv);
153f9c8e4acSMatthew Dillon 			value = ary[i].data[j];
154f9c8e4acSMatthew Dillon 			if (jj <= 4) {
155f9c8e4acSMatthew Dillon 				for (k = jj + 1; k <= 4; ++k)
156f9c8e4acSMatthew Dillon 					value += ary[i].data[plot1[k]];
157f9c8e4acSMatthew Dillon 				dv = (double)value / 1e9;
158f9c8e4acSMatthew Dillon 			} else {
159f9c8e4acSMatthew Dillon 				dv = (double)value / 100.0;
160f9c8e4acSMatthew Dillon 			}
161b038db3dSMatthew Dillon 			if (SmoothOpt) {
162b038db3dSMatthew Dillon 				if (i == (int)(count - 1)) {
163b038db3dSMatthew Dillon 					smoothed_dv = dv;
164b038db3dSMatthew Dillon 				} else if (smoothed_dv < dv) {
165b038db3dSMatthew Dillon 					smoothed_dv =
166b038db3dSMatthew Dillon 					 (smoothed_dv * 5.0 + 5 * dv) /
167b038db3dSMatthew Dillon 					 10.0;
168b038db3dSMatthew Dillon 				} else {
169b038db3dSMatthew Dillon 					smoothed_dv =
170b038db3dSMatthew Dillon 					 (smoothed_dv * 9.0 + 1 * dv) /
171b038db3dSMatthew Dillon 					 10.0;
172b038db3dSMatthew Dillon 				}
173b038db3dSMatthew Dillon 				dv = smoothed_dv;
174b038db3dSMatthew Dillon 			}
175f9c8e4acSMatthew Dillon 			fprintf(OutFP, "%s %6.2f\n", buf, dv);
176f9c8e4acSMatthew Dillon 		}
177f9c8e4acSMatthew Dillon 		fprintf(OutFP, "e\n");
178f9c8e4acSMatthew Dillon 	}
179f9c8e4acSMatthew Dillon 
1806e316fcdSMatthew Dillon 	int ncpu = 4;
1816e316fcdSMatthew Dillon 	size_t ncpu_size = sizeof(ncpu);
1826e316fcdSMatthew Dillon 	sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
1836e316fcdSMatthew Dillon 
184f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set ylabel \"Cpu Utilization\"\n");
185f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set y2label \"MOps/sec (smoothed)\"\n");
186f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set ytics nomirror\n");
187f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set y2tics nomirror\n");
188f9c8e4acSMatthew Dillon 	fprintf(OutFP, "set yrange [0:105]\n");
1896e316fcdSMatthew Dillon 	fprintf(OutFP, "set y2range [0:%d.0]\n",
1906e316fcdSMatthew Dillon 		(93750 * ncpu + 999999) / 1000000);
191f9c8e4acSMatthew Dillon 
192f9c8e4acSMatthew Dillon 	fprintf(OutFP,
193f9c8e4acSMatthew Dillon 		"plot "
1943ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1953ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1963ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
1973ac52c49SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with boxes lw 1, "
198c25e6c22SMatthew Dillon 		"\"-\" using 1:3 title \"%s\" with lines lw 1, "
1993ac52c49SMatthew Dillon 		"\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1, "
2003ac52c49SMatthew Dillon 		"\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1, "
2013ac52c49SMatthew Dillon 		"\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1\n",
202c25e6c22SMatthew Dillon 		id2[0], id2[1], id2[2], id2[3], id2[4], id2[5], id2[6], id2[7]);
203f9c8e4acSMatthew Dillon 
204f9c8e4acSMatthew Dillon 	for (jj = 0; jj < (int)(sizeof(plot2) / sizeof(plot2[0])); ++jj) {
205f9c8e4acSMatthew Dillon 		j = plot2[jj];
206f9c8e4acSMatthew Dillon 
207*9afa2da7SMatthew Dillon 		/*
208*9afa2da7SMatthew Dillon 		 * Skip if disabled
209*9afa2da7SMatthew Dillon 		 */
210*9afa2da7SMatthew Dillon 		if (j < 0) {
211*9afa2da7SMatthew Dillon 			fprintf(OutFP, "e\n");
212*9afa2da7SMatthew Dillon 			continue;
213*9afa2da7SMatthew Dillon 		}
214*9afa2da7SMatthew Dillon 
215*9afa2da7SMatthew Dillon 		/*
216*9afa2da7SMatthew Dillon 		 * Dump points
217*9afa2da7SMatthew Dillon 		 */
218f9c8e4acSMatthew Dillon 		smoothed_dv = 0.0;
219f9c8e4acSMatthew Dillon 		for (i = count - 1; i >= 2; --i) {
220f9c8e4acSMatthew Dillon 			/*
221f9c8e4acSMatthew Dillon 			 * Timestamp
222f9c8e4acSMatthew Dillon 			 */
223f9c8e4acSMatthew Dillon 			t = ary[i].realtime.tv_sec;
224f9c8e4acSMatthew Dillon 			if (t < 1000)
225f9c8e4acSMatthew Dillon 				continue;
226f9c8e4acSMatthew Dillon 			if (UseGMT)
227f9c8e4acSMatthew Dillon 				tmv = gmtime(&t);
228f9c8e4acSMatthew Dillon 			else
229f9c8e4acSMatthew Dillon 				tmv = localtime(&t);
230f9c8e4acSMatthew Dillon 			strftime(buf, sizeof(buf), "%d-%b-%Y %H:%M:%S", tmv);
231f9c8e4acSMatthew Dillon 			value = ary[i].data[j];
232f9c8e4acSMatthew Dillon 
233f9c8e4acSMatthew Dillon 			if (jj <= 3) {
234c25e6c22SMatthew Dillon 				/*
235c25e6c22SMatthew Dillon 				 * intr/sys/user/idle percentages
236c25e6c22SMatthew Dillon 				 */
237f9c8e4acSMatthew Dillon 				for (k = jj + 1; k <= 3; ++k)
238f9c8e4acSMatthew Dillon 					value += ary[i].data[plot2[k]];
239f9c8e4acSMatthew Dillon 				dv = (double)value / 100.0;
240b038db3dSMatthew Dillon 				if (SmoothOpt) {
241b038db3dSMatthew Dillon 					if (i == (int)(count - 1)) {
242b038db3dSMatthew Dillon 						smoothed_dv = dv;
243b038db3dSMatthew Dillon 					} else if (smoothed_dv < dv) {
244b038db3dSMatthew Dillon 						smoothed_dv =
245b038db3dSMatthew Dillon 						 (smoothed_dv * 5.0 + 5 * dv) /
246b038db3dSMatthew Dillon 					         10.0;
247b038db3dSMatthew Dillon 					} else {
248b038db3dSMatthew Dillon 						smoothed_dv =
249b038db3dSMatthew Dillon 						 (smoothed_dv * 9.0 + 1 * dv) /
250b038db3dSMatthew Dillon 					         10.0;
251b038db3dSMatthew Dillon 					}
252b038db3dSMatthew Dillon 					dv = smoothed_dv;
253b038db3dSMatthew Dillon 				}
254f9c8e4acSMatthew Dillon 			} else {
255c25e6c22SMatthew Dillon 				if (jj >= 5) {
256c25e6c22SMatthew Dillon 					/* fault counters */
257f9c8e4acSMatthew Dillon 					dv = (double)value / KCOLLECT_INTERVAL;
258f9c8e4acSMatthew Dillon 					dv = dv / 1e6;
259c25e6c22SMatthew Dillon 				} else {
260c25e6c22SMatthew Dillon 					/* swap percentage (line graph) */
261c25e6c22SMatthew Dillon 					dv = (double)value / 100.0;
262c25e6c22SMatthew Dillon 				}
263b038db3dSMatthew Dillon 				if (i == (int)(count - 1)) {
2643ac52c49SMatthew Dillon 					smoothed_dv = dv;
265b038db3dSMatthew Dillon 				} else if (smoothed_dv < dv) {
266b038db3dSMatthew Dillon 					smoothed_dv =
267b038db3dSMatthew Dillon 					 (smoothed_dv * 5.0 + 5 * dv) /
268b038db3dSMatthew Dillon 					 10.0;
269b038db3dSMatthew Dillon 				} else {
2703ac52c49SMatthew Dillon 					smoothed_dv = (smoothed_dv * 9.0 + dv) /
2713ac52c49SMatthew Dillon 						      10.0;
272b038db3dSMatthew Dillon 				}
273f9c8e4acSMatthew Dillon 				dv = smoothed_dv;
274f9c8e4acSMatthew Dillon 			}
275f9c8e4acSMatthew Dillon 			fprintf(OutFP, "%s %6.2f\n", buf, dv);
276f9c8e4acSMatthew Dillon 		}
277f9c8e4acSMatthew Dillon 		fprintf(OutFP, "e\n");
278f9c8e4acSMatthew Dillon 	}
279f9c8e4acSMatthew Dillon 	fflush(OutFP);
280f9c8e4acSMatthew Dillon }
281