forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_stats.c
5204 lines (4791 loc) · 175 KB
/
svg_stats.c
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* svg_stats.c: Funtions used by sadf to display statistics in SVG format.
* (C) 2016-2018 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
***************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <float.h>
#include "sa.h"
#include "ioconf.h"
#include "svg_stats.h"
#ifdef USE_NLS
#include <locale.h>
#include <libintl.h>
#define _(string) gettext(string)
#else
#define _(string) (string)
#endif
extern unsigned int flags;
unsigned int svg_colors[] = {0x00cc00, 0xff00bf, 0x00ffff, 0xff0000,
0xe85f00, 0x0000ff, 0x006020, 0x7030a0,
0xffff00, 0x666635, 0xd60093, 0x00bfbf,
0xcc3300, 0x50040f, 0xffffbf, 0x193d55};
#define SVG_COLORS_IDX_MASK 0x0f
/*
***************************************************************************
* Compare the values of a statistics sample with the max and min values
* already found in previous samples for this same activity. If some new
* min or max values are found, then save them.
* Assume values cannot be negative.
* The structure containing the statistics sample is composed of @llu_nr
* unsigned long long fields, followed by @lu_nr unsigned long fields, then
* followed by @u_nr unsigned int fields.
*
* IN:
* @types_nr Number of fields whose type is "long long", "long" and "int"
* composing the structure.
* @cs Pointer on current sample statistics structure.
* @ps Pointer on previous sample statistics structure (may be NULL).
* @itv Interval of time in 1/100th of a second.
* @spmin Array containing min values already found for this activity.
* @spmax Array containing max values already found for this activity.
* @g_fields Index in spmin/spmax arrays where extrema values for each
* activity metric will be saved. As a consequence spmin/spmax
* arrays may contain values in a different order than that of
* the fields in the statistics structure.
*
* OUT:
* @spmin Array containg the possible new min values for current activity.
* @spmax Array containg the possible new max values for current activity.
***************************************************************************
*/
void save_extrema(unsigned int types_nr[], void *cs, void *ps, unsigned long long itv,
double *spmin, double *spmax, int g_fields[])
{
unsigned long long *lluc, *llup;
unsigned long *luc, *lup;
unsigned int *uc, *up;
double val;
int i, m = 0;
/* Compare unsigned long long fields */
lluc = (unsigned long long *) cs;
llup = (unsigned long long *) ps;
for (i = 0; i < types_nr[0]; i++, m++) {
if (ps) {
val = *lluc < *llup ? 0.0 : S_VALUE(*llup, *lluc, itv);
}
else {
/*
* If no pointer on previous sample has been given
* then the value is not a per-second one.
*/
val = (double) *lluc;
}
if (val < *(spmin + g_fields[m])) {
*(spmin + g_fields[m]) = val;
}
if (val > *(spmax + g_fields[m])) {
*(spmax + g_fields[m]) = val;
}
lluc = (unsigned long long *) ((char *) lluc + ULL_ALIGNMENT_WIDTH);
if (ps) {
llup = (unsigned long long *) ((char *) llup + ULL_ALIGNMENT_WIDTH);
}
}
/* Compare unsigned long fields */
luc = (unsigned long *) lluc;
lup = (unsigned long *) llup;
for (i = 0; i < types_nr[1]; i++, m++) {
if (ps) {
val = *luc < *lup ? 0.0 : S_VALUE(*lup, *luc, itv);
}
else {
val = (double) *luc;
}
if (val < *(spmin + g_fields[m])) {
*(spmin + g_fields[m]) = val;
}
if (val > *(spmax + g_fields[m])) {
*(spmax + g_fields[m]) = val;
}
luc = (unsigned long *) ((char *) luc + UL_ALIGNMENT_WIDTH);
if (ps) {
lup = (unsigned long *) ((char *) lup + UL_ALIGNMENT_WIDTH);
}
}
/* Compare unsigned int fields */
uc = (unsigned int *) luc;
up = (unsigned int *) lup;
for (i = 0; i < types_nr[2]; i++, m++) {
if (ps) {
val = *uc < *up ? 0.0 : S_VALUE(*up, *uc, itv);
}
else {
val = (double) *uc;
}
if (val < *(spmin + g_fields[m])) {
*(spmin + g_fields[m]) = val;
}
if (val > *(spmax + g_fields[m])) {
*(spmax + g_fields[m]) = val;
}
uc = (unsigned int *) ((char *) uc + U_ALIGNMENT_WIDTH);
if (ps) {
up = (unsigned int *) ((char *) up + U_ALIGNMENT_WIDTH);
}
}
}
/*
***************************************************************************
* Find the min and max values of all the graphs that will be drawn in the
* same view. The graphs have their own min and max values in
* spmin[pos...pos+n-1] and spmax[pos...pos+n-1].
*
* IN:
* @pos Position in array for the first graph extrema value.
* @n Number of graphs to scan.
* @spmin Array containing min values for graphs.
* @spmax Array containing max values for graphs.
*
* OUT:
* @gmin Global min value found.
* @gmax Global max value found.
***************************************************************************
*/
void get_global_extrema(int pos, int n, double *spmin, double *spmax,
double *gmin, double *gmax)
{
int i;
*gmin = *(spmin + pos);
*gmax = *(spmax + pos);
for (i = 1; i < n; i++) {
if (*(spmin + pos + i) < *gmin) {
*gmin = *(spmin + pos + i);
}
if (*(spmax + pos + i) > *gmax) {
*gmax = *(spmax + pos + i);
}
}
}
/*
***************************************************************************
* Allocate arrays used to save graphs data, min and max values.
* @n arrays of chars are allocated for @n graphs to draw. A pointer on this
* array is returned. This is equivalent to "char data[][n]" where each
* element is of indeterminate size and will contain the graph data (eg.
* << path d="M12,14 L13,16..." ... >>.
* The size of element data[i] is given by outsize[i].
* Also allocate an array to save min values (equivalent to "double spmin[n]")
* and an array for max values (equivalent to "double spmax[n]").
*
* IN:
* @n Number of graphs to draw for current activity.
*
* OUT:
* @outsize Array that will contain the sizes of each element in array
* of chars. Equivalent to "int outsize[n]" with
* outsize[n] = sizeof(data[][n]).
* @spmin Array that will contain min values for current activity.
* @spmax Array that will contain max values for current activity.
*
* RETURNS:
* Pointer on array of arrays of chars that will contain the graphs data.
*
* NB: @min and @max arrays contain values in the same order as the fields
* in the statistics structure.
***************************************************************************
*/
char **allocate_graph_lines(int n, int **outsize, double **spmin, double **spmax)
{
char **out;
char *out_p;
int i;
/*
* Allocate an array of pointers. Each of these pointers will
* be an array of chars.
*/
if ((out = (char **) malloc(n * sizeof(char *))) == NULL) {
perror("malloc");
exit(4);
}
/* Allocate array that will contain the size of each array of chars */
if ((*outsize = (int *) malloc(n * sizeof(int))) == NULL) {
perror("malloc");
exit(4);
}
/* Allocate array that will contain the min value of each graph */
if ((*spmin = (double *) malloc(n * sizeof(double))) == NULL) {
perror("malloc");
exit(4);
}
/* Allocate array that will contain the max value of each graph */
if ((*spmax = (double *) malloc(n * sizeof(double))) == NULL) {
perror("malloc");
exit(4);
}
/* Allocate arrays of chars that will contain graphs data */
for (i = 0; i < n; i++) {
if ((out_p = (char *) malloc(CHUNKSIZE * sizeof(char))) == NULL) {
perror("malloc");
exit(4);
}
*(out + i) = out_p;
*out_p = '\0'; /* Reset string so that it can be safely strncat()'d later */
*(*outsize + i) = CHUNKSIZE; /* Each array of chars has a default size of CHUNKSIZE */
*(*spmin + i) = DBL_MAX; /* Init min and max values */
*(*spmax + i) = -DBL_MAX;
}
return out;
}
/*
***************************************************************************
* Save SVG code for current graph.
*
* IN:
* @data SVG code to append to current graph definition.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
*
* OUT:
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
***************************************************************************
*/
void save_svg_data(char *data, char **out, int *outsize)
{
char *out_p;
int len;
out_p = *out;
/* Determine space left in array */
len = *outsize - strlen(out_p) - 1;
if (strlen(data) >= len) {
/*
* If current array of chars doesn't have enough space left
* then reallocate it with CHUNKSIZE more bytes.
*/
SREALLOC(out_p, char, *outsize + CHUNKSIZE);
*out = out_p;
*outsize += CHUNKSIZE;
len += CHUNKSIZE;
}
strncat(out_p, data, len);
}
/*
***************************************************************************
* Update line graph definition by appending current X,Y coordinates.
*
* IN:
* @timetag Timestamp in seconds since the epoch for current sample
* stats. Will be used as X coordinate.
* @value Value of current sample metric. Will be used as Y coordinate.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
* @restart Set to TRUE if a RESTART record has been read since the last
* statistics sample.
*
* OUT:
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
***************************************************************************
*/
void lnappend(unsigned long long timetag, double value, char **out, int *outsize,
int restart)
{
char data[128];
/* Prepare additional graph definition data */
snprintf(data, 128, " %c%llu,%.2f", restart ? 'M' : 'L', timetag, value);
data[127] = '\0';
save_svg_data(data, out, outsize);
}
/*
***************************************************************************
* Update line graph definition by appending current X,Y coordinates. Use
* (unsigned long) integer values here.
*
* IN:
* @timetag Timestamp in seconds since the epoch for current sample
* stats. Will be used as X coordinate.
* @value Value of current sample metric. Will be used as Y coordinate.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
* @restart Set to TRUE if a RESTART record has been read since the last
* statistics sample.
*
* OUT:
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
***************************************************************************
*/
void lniappend(unsigned long long timetag, unsigned long long value, char **out,
int *outsize, int restart)
{
char data[128];
/* Prepare additional graph definition data */
snprintf(data, 128, " %c%llu,%llu", restart ? 'M' : 'L', timetag, value);
data[127] = '\0';
save_svg_data(data, out, outsize);
}
/*
***************************************************************************
* Update bar graph definition by adding a new rectangle.
*
* IN:
* @timetag Timestamp in seconds since the epoch for current sample
* stats. Will be used as X coordinate.
* @value Value of current sample metric. Will be used as rectangle
* height.
* @offset Offset for Y coordinate.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
* @dt Interval of time in seconds between current and previous
* sample.
*
* OUT:
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
***************************************************************************
*/
void brappend(unsigned long long timetag, double offset, double value, char **out,
int *outsize, unsigned long long dt)
{
char data[128];
unsigned long long t = 0;
/* Prepare additional graph definition data */
if ((value == 0.0) || (dt == 0))
/* Don't draw a flat rectangle! */
return;
if (dt < timetag) {
t = timetag -dt;
}
snprintf(data, 128, "<rect x=\"%llu\" y=\"%.2f\" height=\"%.2f\" width=\"%llu\"/>",
t, MINIMUM(offset, 100.0), MINIMUM(value, (100.0 - offset)), dt);
data[127] = '\0';
save_svg_data(data, out, outsize);
}
/*
***************************************************************************
* Update CPU graph and min/max values for each metric.
*
* IN:
* @timetag Timestamp in seconds since the epoch for current sample
* stats. Will be used as X coordinate.
* @offset Offset for Y coordinate.
* @value Value of current CPU metric. Will be used as rectangle
* height.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
* @dt Interval of time in seconds between current and previous
* sample.
* @spmin Min value already found for this CPU metric.
* @spmax Max value already found for this CPU metric.
*
* OUT:
* @offset New offset value, to use to draw next rectangle
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
***************************************************************************
*/
void cpuappend(unsigned long long timetag, double *offset, double value, char **out,
int *outsize, unsigned long long dt, double *spmin, double *spmax)
{
/* Save min and max values */
if (value < *spmin) {
*spmin = value;
}
if (value > *spmax) {
*spmax = value;
}
/* Prepare additional graph definition data */
brappend(timetag, *offset, value, out, outsize, dt);
*offset += value;
}
/*
***************************************************************************
* Update rectangular graph and min/max values.
*
* IN:
* @timetag Timestamp in seconds since the epoch for current sample
* stats. Will be used as X coordinate.
* @p_value Metric value for previous sample
* @value Metric value for current sample.
* @out Pointer on array of chars for current graph definition.
* @outsize Size of array of chars for current graph definition.
* @restart Set to TRUE if a RESTART record has been read since the last
* statistics sample.
* @dt Interval of time in seconds between current and previous
* sample.
* @spmin Min value already found for this metric.
* @spmax Max value already found for this metric.
*
* OUT:
* @out Pointer on array of chars for current graph definition that
* has been updated with the addition of current sample data.
* @outsize Array that containing the (possibly new) sizes of each
* element in array of chars.
* @spmin Min value for this metric.
* @spmax Max value for this metric.
***************************************************************************
*/
void recappend(unsigned long long timetag, double p_value, double value, char **out,
int *outsize, int restart, unsigned long long dt,
double *spmin, double *spmax)
{
char data[512], data1[128], data2[128];
unsigned long long t = 0;
/* Save min and max values */
if (value < *spmin) {
*spmin = value;
}
if (value > *spmax) {
*spmax = value;
}
if (dt < timetag) {
t = timetag -dt;
}
/* Prepare additional graph definition data */
if (restart) {
snprintf(data1, sizeof(data1), " M%llu,%.2f", t, p_value);
data1[sizeof(data1) - 1] = '\0';
}
if (p_value != value) {
snprintf(data2, sizeof(data2), " L%llu,%.2f", timetag, value);
data2[sizeof(data2) - 1] = '\0';
}
snprintf(data, sizeof(data), "%s L%llu,%.2f%s", restart ? data1 : "", timetag, p_value,
p_value != value ? data2 : "");
data[sizeof(data) - 1] = '\0';
save_svg_data(data, out, outsize);
}
/*
***************************************************************************
* Calculate 10 raised to the power of n.
*
* IN:
* @n Power number to use.
*
* RETURNS:
* 10 raised to the power of n.
***************************************************************************
*/
unsigned int pwr10(int n)
{
int i;
unsigned int e = 1;
for (i = 0; i < n; i++) {
e = e * 10;
}
return e;
}
/*
***************************************************************************
* Autoscale graphs of a given view.
*
* IN:
* @asf_nr (Maximum) number of autoscale factors.
* @group Number of graphs in current view.
* @g_type Type of graph (SVG_LINE_GRAPH, SVG_BAR_GRAPH).
* @pos Position in array for the first graph in view.
* @gmax Global max value for all graphs in view.
* @spmax Array containing max values for graphs.
*
* OUT:
* @asfactor Autoscale factors (one for each graph).
***************************************************************************
*/
void gr_autoscaling(unsigned int asfactor[], int asf_nr, int group, int g_type, int pos,
double gmax, double *spmax)
{
int j;
char val[32];
for (j = 0; j < asf_nr; j++) {
/* Init autoscale factors */
asfactor[j] = 1;
}
if (AUTOSCALE_ON(flags) && (group > 1) && gmax && (g_type == SVG_LINE_GRAPH)) {
/* Autoscaling... */
for (j = 0; (j < group) && (j < asf_nr); j++) {
if (!*(spmax + pos + j) || (*(spmax + pos + j) == gmax))
continue;
snprintf(val, 32, "%u", (unsigned int) (gmax / *(spmax + pos + j)));
if (strlen(val) > 0) {
asfactor[j] = pwr10(strlen(val) - 1);
}
}
}
}
/*
***************************************************************************
* Display background grid (horizontal lines) and corresponding graduations.
*
* IN:
* @ypos Gap between two horizontal lines.
* @yfactor Scaling factor on Y axis.
* @lmax Max value for current view.
* @dp Number of decimal places for graduations.
***************************************************************************
*/
void display_hgrid(double ypos, double yfactor, double lmax, int dp)
{
int j = 0;
char stmp[32];
do {
/* Display horizontal lines (except on X axis) */
if (j > 0) {
printf("<polyline points=\"0,%.2f %d,%.2f\" style=\"vector-effect: non-scaling-stroke; "
"stroke: #202020\" transform=\"scale(1,%f)\"/>\n",
ypos * j, SVG_G_XSIZE, ypos * j, yfactor);
}
/*
* Display graduations.
* Use same rounded value for graduation numbers as for grid lines
* to make sure they are properly aligned.
*/
sprintf(stmp, "%.2f", ypos * j);
printf("<text x=\"0\" y=\"%ld\" style=\"fill: white; stroke: none; font-size: 12px; "
"text-anchor: end\">%.*f.</text>\n",
(long) (atof(stmp) * yfactor), dp, ypos * j);
j++;
}
while ((ypos * j <= lmax) && (j < MAX_HLINES_NR));
}
/*
***************************************************************************
* Display background grid (vertical lines) and corresponding graduations.
*
* IN:
* @xpos Gap between two vertical lines.
* @xfactor Scaling factor on X axis.
* @v_gridnr Default number of vertical lines to display. The actual
* number may vary between this value and 2 times this value.
* @svg_p SVG specific parameters (see draw_activity_graphs() function).
***************************************************************************
*/
void display_vgrid(long int xpos, double xfactor, int v_gridnr, struct svg_parm *svg_p)
{
struct record_header stamp;
struct tm rectime;
char cur_time[TIMESTAMP_LEN];
int j;
stamp.ust_time = svg_p->ust_time_ref; /* Only ust_time field needs to be set. TRUE_TIME not allowed */
/*
* What really matters to know when we should stop drawing vertical lines
* is the time end. v_gridnr is only informative and used to calculate
* the gap between two lines.
*/
for (j = 0; (j <= (2 * v_gridnr)) && (stamp.ust_time <= svg_p->ust_time_end); j++) {
/* Display vertical lines */
sa_get_record_timestamp_struct(flags, &stamp, &rectime, NULL);
set_record_timestamp_string(flags, &stamp, NULL, cur_time, TIMESTAMP_LEN, &rectime);
printf("<polyline points=\"%ld,0 %ld,%d\" style=\"vector-effect: non-scaling-stroke; "
"stroke: #202020\" transform=\"scale(%f,1)\"/>\n",
xpos * j, xpos * j, -SVG_G_YSIZE, xfactor);
/*
* Display graduations.
* NB: We may have tm_min != 0 if we have more than 24H worth of data in one datafile.
* In this case, we should rather display the exact time instead of only the hour.
*/
if (DISPLAY_ONE_DAY(flags) && (rectime.tm_min == 0)) {
printf("<text x=\"%ld\" y=\"15\" style=\"fill: white; stroke: none; font-size: 14px; "
"text-anchor: start\">%2d:00</text>\n",
(long) (xpos * j * xfactor) - 15, rectime.tm_hour);
}
else {
printf("<text x=\"%ld\" y=\"10\" style=\"fill: white; stroke: none; font-size: 12px; "
"text-anchor: start\" transform=\"rotate(45,%ld,0)\">%s</text>\n",
(long) (xpos * j * xfactor), (long) (xpos * j * xfactor), cur_time);
}
stamp.ust_time += xpos;
}
if (!PRINT_LOCAL_TIME(flags)) {
printf("<text x=\"-10\" y=\"30\" style=\"fill: yellow; stroke: none; font-size: 12px; "
"text-anchor: end\">UTC</text>\n");
}
}
/*
***************************************************************************
* Calculate the value on the Y axis between two horizontal lines that will
* make the graph background grid.
*
* IN:
* @lmax Max value reached for this graph.
*
* OUT:
* @dp Number of decimal places for Y graduations.
*
* RETURNS:
* Value between two horizontal lines.
***************************************************************************
*/
double ygrid(double lmax, int *dp)
{
char val[32];
int l;
unsigned int e;
long n = 0;
*dp = 0;
if (lmax == 0) {
lmax = 1;
}
n = (long) (lmax / SVG_H_GRIDNR);
if (!n) {
*dp = 2;
return (lmax / SVG_H_GRIDNR);
}
snprintf(val, 32, "%ld", n);
val[31] = '\0';
l = strlen(val);
if (l < 2)
return n;
e = pwr10(l - 1);
return ((double) (((long) (n / e)) * e));
}
/*
***************************************************************************
* Calculate the value on the X axis between two vertical lines that will
* make the graph background grid.
*
* IN:
* @timestart First data timestamp (X coordinate of the first data point).
* @timeend Last data timestamp (X coordinate of the last data point).
* @v_gridnr Number of vertical lines to display. Its value is normally
* SVG_V_GRIDNR, except when option "oneday" is used, in which
* case it is set to 12.
*
* RETURNS:
* Value between two vertical lines.
***************************************************************************
*/
long int xgrid(unsigned long timestart, unsigned long timeend, int v_gridnr)
{
if ((timeend - timestart) <= v_gridnr)
return 1;
else
return ((timeend - timestart) / v_gridnr);
}
/*
***************************************************************************
* Free global graphs structures.
*
* IN:
* @out Pointer on array of chars for each graph definition.
* @outsize Size of array of chars for each graph definition.
* @spmin Array containing min values for graphs.
* @spmax Array containing max values for graphs.
***************************************************************************
*/
void free_graphs(char **out, int *outsize, double *spmin, double *spmax)
{
if (out) {
free(out);
}
if (outsize) {
free(outsize);
}
if (spmin) {
free(spmin);
}
if (spmax) {
free(spmax);
}
}
/*
***************************************************************************
* Skip current view where all graphs have only zero values. This function
* is called when option "skipempty" has been used, or when "No data" have
* been found for current view.
*
* IN:
* @out Pointer on array of chars for each graph definition.
* @pos Position of current view in the array of graphs definitions.
* @group Number of graphs in current view.
*
* OUT:
* @pos Position of next view in the array of graphs definitions.
***************************************************************************
*/
void skip_current_view(char **out, int *pos, int group)
{
int j;
char *out_p;
for (j = 0; j < group; j++) {
out_p = *(out + *pos + j);
if (out_p) {
/* Even if not displayed, current graph data have to be freed */
free(out_p);
}
}
*pos += group;
}
/*
***************************************************************************
* Display all graphs for current activity.
*
* IN:
* @g_nr Number of views to display.
* @g_type Type of graph (SVG_LINE_GRAPH, SVG_BAR_GRAPH) for each view.
* @title Titles for each set of graphs.
* @g_title Titles for each graph.
* @item_name Item (network interface, etc.) name.
* @group Indicate how graphs are grouped together to make sets.
* @spmin Array containing min values for graphs.
* @spmax Array containing max values for graphs.
* @out Pointer on array of chars for each graph definition.
* @outsize Size of array of chars for each graph definition.
* @svg_p SVG specific parameters: Current views row number (.@graph_no),
* time for the first sample of stats (.@ust_time_first), and
* times used as start and end values on the X axis
* (.@ust_time_ref and .@ust_time_end).
* @record_hdr Pointer on record header of current stats sample.
* @skip_void Set to <> 0 if graphs with no data should be skipped.
* This is typicallly used to not display CPU offline on the
* whole period.
* @id Current activity id.
* @xid Current activity extra id number.
*
* RETURNS:
* TRUE if at least one graph has been displayed.
***************************************************************************
*/
int draw_activity_graphs(int g_nr, int g_type[], char *title[], char *g_title[], char *item_name,
int group[], double *spmin, double *spmax, char **out, int *outsize,
struct svg_parm *svg_p, struct record_header *record_hdr, int skip_void,
unsigned int id, unsigned int xid)
{
char *out_p;
int i, j, dp, pos = 0, views_nr = 0, displayed = FALSE;
int v_gridnr, xv, yv;
unsigned int asfactor[16];
long int xpos;
double lmax, xfactor, yfactor, ypos, gmin, gmax;
char val[32], cur_date[TIMESTAMP_LEN];
struct tm rectime;
/* For each view which is part of current activity */
for (i = 0; i < g_nr; i++) {
/* Get global min and max value for current view */
get_global_extrema(pos, group[i], spmin, spmax, &gmin, &gmax);
/* Don't display empty views if requested */
if (SKIP_EMPTY_VIEWS(flags) && (gmax < 0.005)) {
skip_current_view(out, &pos, group[i]);
continue;
}
/* Skip void graphs */
if (skip_void && ((*(spmin + pos) == DBL_MAX) || (*(spmax + pos) == -DBL_MIN)))
continue;
if (!displayed) {
/* Translate to proper position for current activity */
printf("<g id=\"g%d-%d\" transform=\"translate(0,%d)\">\n",
id, xid,
SVG_H_YSIZE +
SVG_C_YSIZE * (DISPLAY_TOC(flags) ? svg_p->nr_act_dispd : 0) +
SVG_T_YSIZE * svg_p->graph_no);
displayed = TRUE;
}
/* Increment number of views actually displayed */
views_nr++;
/* Compute top left position of view */
if (PACK_VIEWS(flags)) {
xv = (views_nr - 1) * SVG_T_XSIZE;
yv = 0;
}
else {
xv = 0;
yv = (views_nr - 1) * SVG_T_YSIZE;
}
/* Graph background */
printf("<rect x=\"%d\" y=\"%d\" height=\"%d\" width=\"%d\"/>\n",
xv, yv, SVG_V_YSIZE, SVG_V_XSIZE);
/* Graph title */
printf("<text x=\"%d\" y=\"%d\" style=\"fill: yellow; stroke: none\">%s",
xv, 20 + yv, title[i]);
if (item_name) {
printf(" [%s]", item_name);
}
printf("\n");
printf("<tspan x=\"%d\" y=\"%d\" style=\"fill: yellow; stroke: none; font-size: 12px\">"
"(Min, Max values)</tspan>\n</text>\n",
xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + 25);
/*
* At least two samples are needed.
* And a min and max value should have been found.
*/
if ((record_hdr->ust_time == svg_p->ust_time_first) ||
(*(spmin + pos) == DBL_MAX) || (*(spmax + pos) == -DBL_MIN)) {
/* No data found */
printf("<text x=\"%d\" y=\"%d\" style=\"fill: red; stroke: none\">No data</text>\n",
xv, yv + SVG_M_YSIZE);
skip_current_view(out, &pos, group[i]);
continue;
}
/* X and Y axis */
printf("<polyline points=\"%d,%d %d,%d %d,%d\" stroke=\"white\" stroke-width=\"2\"/>\n",
xv + SVG_M_XSIZE, yv + SVG_M_YSIZE,
xv + SVG_M_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE,
xv + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE);
/* Autoscaling graphs if needed */
gr_autoscaling(asfactor, 16, group[i], g_type[i], pos, gmax, spmax);
/* Caption */
for (j = 0; j < group[i]; j++) {
/* Set dp to TRUE (1) if current metric is based on integer values */
dp = (g_title[pos + j][0] == '~');
snprintf(val, 32, "x%u ", asfactor[j]);
printf("<text x=\"%d\" y=\"%d\" style=\"fill: #%06x; stroke: none; font-size: 12px\">"
"%s %s(%.*f, %.*f)</text>\n",
xv + 5 + SVG_M_XSIZE + SVG_G_XSIZE, yv + SVG_M_YSIZE + j * 15,
svg_colors[(pos + j) & SVG_COLORS_IDX_MASK], g_title[pos + j] + dp,
asfactor[j] == 1 ? "" : val,
!dp * 2, *(spmin + pos + j) * asfactor[j],
!dp * 2, *(spmax + pos + j) * asfactor[j]);
}
if (DISPLAY_INFO(flags)) {
/* Display additional info (hostname, date) */
printf("<text x=\"%d\" y=\"%d\" "
"style=\"fill: yellow; text-anchor: end; stroke: none; font-size: 14px\">"
"%s\n",
xv + SVG_V_XSIZE - 5, yv + SVG_M_YSIZE + SVG_G_YSIZE,
svg_p->file_hdr->sa_nodename);
/* Get report date */
set_report_date(localtime_r((const time_t *) &(svg_p->file_hdr->sa_ust_time), &rectime),
cur_date, sizeof(cur_date));
printf("<tspan x=\"%d\" y=\"%d\" "
"style=\"fill: yellow; text-anchor: end; stroke: none; font-size: 14px\">"
"%s</tspan>\n</text>\n",
xv + SVG_V_XSIZE - 5, yv + SVG_M_YSIZE + SVG_G_YSIZE + 14,
cur_date);
}
/* Translate to proper position for current graph within current activity */
printf("<g transform=\"translate(%d,%d)\">\n",
xv + SVG_M_XSIZE, yv + SVG_M_YSIZE + SVG_G_YSIZE);
/* Grid */
if (g_type[i] == SVG_LINE_GRAPH) {
/* For line graphs */
if (!gmax) {
/* If all values are zero then set current max value to 1 */
lmax = 1.0;
}
else {
lmax = gmax;
}
/* Max value cannot be too small, else Y graduations will be meaningless */
if (lmax < SVG_H_GRIDNR * 0.01) {
lmax = SVG_H_GRIDNR * 0.01;
}
ypos = ygrid(lmax, &dp);
}
else {
/* For bar graphs (used for %values) */
ypos = 25.0; /* Draw lines at 25%, 50%, 75% and 100% */
dp = 0; /* No decimals */
/* Max should be always 100% except for percentage values greater than 100% */
if (gmax > 100.0) {
lmax = gmax;
}
else {
lmax = 100.0;
}
}
yfactor = (double) -SVG_G_YSIZE / lmax;
/* Display horizontal lines and graduations */
display_hgrid(ypos, yfactor, lmax, dp);
/* Set number of vertical lines to 12 when option "oneday" is used */
v_gridnr = DISPLAY_ONE_DAY(flags) ? 12 : SVG_V_GRIDNR;
xpos = xgrid(svg_p->ust_time_ref, svg_p->ust_time_end, v_gridnr);
xfactor = (double) SVG_G_XSIZE / (svg_p->ust_time_end - svg_p->ust_time_ref);
/* Display vertical lines and graduations */
display_vgrid(xpos, xfactor, v_gridnr, svg_p);
/* Draw current graphs set */
for (j = 0; j < group[i]; j++) {
out_p = *(out + pos + j);
if (g_type[i] == SVG_LINE_GRAPH) {
/* Line graphs */
printf("<path d=\"%s\" "
"style=\"vector-effect: non-scaling-stroke; "
"stroke: #%06x; stroke-width: 1; fill-opacity: 0\" "
"transform=\"scale(%f,%f)\"/>\n",
out_p,