kcollect - Add a smoothing option (-s)
[dragonfly.git] / usr.bin / kcollect / gnuplot.c
blob703f488b901eeb611abc74d4f0fb0e393963086c
1 /*
2 * Copyright (c) 2017 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include "kcollect.h"
34 void
35 start_gnuplot(int ac __unused, char **av __unused)
37 OutFP = popen("gnuplot", "w");
38 if (OutFP == NULL) {
39 fprintf(stderr, "can't find gnuplot\n");
40 exit(1);
44 void
45 dump_gnuplot(kcollect_t *ary, size_t count, const char *plotfile)
47 int plot1[] = { KCOLLECT_MEMFRE, KCOLLECT_MEMCAC,
48 KCOLLECT_MEMINA, KCOLLECT_MEMACT,
49 KCOLLECT_MEMWIR, KCOLLECT_LOAD };
50 int plot2[] = { KCOLLECT_IDLEPCT, KCOLLECT_INTRPCT,
51 KCOLLECT_SYSTPCT, KCOLLECT_USERPCT,
52 KCOLLECT_VMFAULT, KCOLLECT_SYSCALLS, KCOLLECT_NLOOKUP };
53 const char *id1[] = {
54 "free", "cache",
55 "inact", "active",
56 "wired", "load" };
57 const char *id2[] = {
58 "idle", "intr", "system", "user",
59 "faults", "syscalls", "nlookups" };
60 struct tm *tmv;
61 char buf[64];
62 uint64_t value;
63 time_t t;
64 double dv;
65 double smoothed_dv;
66 int i;
67 int j;
68 int jj;
69 int k;
72 * If plotfile is specified allow .jpg or .JPG or .png or .PNG
74 if (plotfile) {
75 const char *ext;
77 if ((ext = strrchr(plotfile, '.')) == NULL) {
78 ext = "";
79 } else {
80 ++ext;
82 if (strcmp(ext, "jpg") == 0 ||
83 strcmp(ext, "JPG") == 0) {
84 fprintf(OutFP, "set terminal jpeg size %d,%d\n",
85 OutputWidth, OutputHeight);
86 } else if (strcmp(ext, "png") == 0 ||
87 strcmp(ext, "PNG") == 0) {
88 fprintf(OutFP, "set terminal png size %d,%d\n",
89 OutputWidth, OutputHeight);
90 } else {
91 fprintf(stderr, "plotfile must be .jpg or .png\n");
92 exit(1);
94 fprintf(OutFP, "set output \"%s\"\n", plotfile);
95 } else {
96 fprintf(OutFP, "set terminal x11 persist size %d,%d\n",
97 OutputWidth, OutputHeight);
101 * NOTE: be sure to reset any fields adjusted by the second plot,
102 * in case we are streaming plots with -f.
104 fprintf(OutFP, "set xdata time\n");
105 fprintf(OutFP, "set timefmt \"%%d-%%b-%%Y %%H:%%M:%%S\"\n");
106 fprintf(OutFP, "set style fill solid 1.0\n");
107 fprintf(OutFP, "set multiplot layout 2,1\n");
108 fprintf(OutFP, "set key outside\n");
109 fprintf(OutFP, "set lmargin 10\n");
110 fprintf(OutFP, "set rmargin 25\n");
111 fprintf(OutFP, "set xtics rotate\n");
112 fprintf(OutFP, "set format x '%%H:%%M'\n");
114 fprintf(OutFP, "set ylabel \"GB\"\n");
116 fprintf(OutFP, "set yrange [0:%d]\n",
117 (int)((KCOLLECT_GETSCALE(ary[0].data[KCOLLECT_MEMFRE]) +
118 999999) / 1000000000));
119 fprintf(OutFP, "set autoscale y2\n");
120 fprintf(OutFP, "set y2label \"Load\"\n");
121 fprintf(OutFP, "set ytics nomirror\n");
122 fprintf(OutFP, "set y2tics nomirror\n");
124 fprintf(OutFP,
125 "plot "
126 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
127 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
128 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
129 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
130 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
131 "\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1\n",
132 id1[0], id1[1], id1[2], id1[3], id1[4], id1[5]);
134 for (jj = 0; jj < (int)(sizeof(plot1) / sizeof(plot1[0])); ++jj) {
135 j = plot1[jj];
137 smoothed_dv = 0.0;
138 for (i = count - 1; i >= 2; --i) {
140 * Timestamp
142 t = ary[i].realtime.tv_sec;
143 if (t < 1000)
144 continue;
145 if (UseGMT)
146 tmv = gmtime(&t);
147 else
148 tmv = localtime(&t);
149 strftime(buf, sizeof(buf), "%d-%b-%Y %H:%M:%S", tmv);
150 value = ary[i].data[j];
151 if (jj <= 4) {
152 for (k = jj + 1; k <= 4; ++k)
153 value += ary[i].data[plot1[k]];
154 dv = (double)value / 1e9;
155 } else {
156 dv = (double)value / 100.0;
158 if (SmoothOpt) {
159 if (i == (int)(count - 1)) {
160 smoothed_dv = dv;
161 } else if (smoothed_dv < dv) {
162 smoothed_dv =
163 (smoothed_dv * 5.0 + 5 * dv) /
164 10.0;
165 } else {
166 smoothed_dv =
167 (smoothed_dv * 9.0 + 1 * dv) /
168 10.0;
170 dv = smoothed_dv;
172 fprintf(OutFP, "%s %6.2f\n", buf, dv);
174 fprintf(OutFP, "e\n");
177 fprintf(OutFP, "set ylabel \"Cpu Utilization\"\n");
178 fprintf(OutFP, "set y2label \"MOps/sec (smoothed)\"\n");
179 fprintf(OutFP, "set ytics nomirror\n");
180 fprintf(OutFP, "set y2tics nomirror\n");
181 fprintf(OutFP, "set yrange [0:105]\n");
182 fprintf(OutFP, "set y2range [0:1.0]\n");
184 fprintf(OutFP,
185 "plot "
186 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
187 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
188 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
189 "\"-\" using 1:3 title \"%s\" with boxes lw 1, "
190 "\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1, "
191 "\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1, "
192 "\"-\" using 1:3 axes x1y2 title \"%s\" with lines lw 1\n",
193 id2[0], id2[1], id2[2], id2[3], id2[4], id2[5], id2[6]);
195 for (jj = 0; jj < (int)(sizeof(plot2) / sizeof(plot2[0])); ++jj) {
196 j = plot2[jj];
198 smoothed_dv = 0.0;
199 for (i = count - 1; i >= 2; --i) {
201 * Timestamp
203 t = ary[i].realtime.tv_sec;
204 if (t < 1000)
205 continue;
206 if (UseGMT)
207 tmv = gmtime(&t);
208 else
209 tmv = localtime(&t);
210 strftime(buf, sizeof(buf), "%d-%b-%Y %H:%M:%S", tmv);
211 value = ary[i].data[j];
213 if (jj <= 3) {
214 for (k = jj + 1; k <= 3; ++k)
215 value += ary[i].data[plot2[k]];
216 dv = (double)value / 100.0;
217 if (SmoothOpt) {
218 if (i == (int)(count - 1)) {
219 smoothed_dv = dv;
220 } else if (smoothed_dv < dv) {
221 smoothed_dv =
222 (smoothed_dv * 5.0 + 5 * dv) /
223 10.0;
224 } else {
225 smoothed_dv =
226 (smoothed_dv * 9.0 + 1 * dv) /
227 10.0;
229 dv = smoothed_dv;
231 } else {
232 dv = (double)value / KCOLLECT_INTERVAL;
233 dv = dv / 1e6;
234 if (i == (int)(count - 1)) {
235 smoothed_dv = dv;
236 } else if (smoothed_dv < dv) {
237 smoothed_dv =
238 (smoothed_dv * 5.0 + 5 * dv) /
239 10.0;
240 } else {
241 smoothed_dv = (smoothed_dv * 9.0 + dv) /
242 10.0;
244 dv = smoothed_dv;
246 fprintf(OutFP, "%s %6.2f\n", buf, dv);
248 fprintf(OutFP, "e\n");
250 fflush(OutFP);