]> granicus.if.org Git - sysstat/blob - sa.h
44d4b7252e14d7ff90dc1e78165aa35e992398a1
[sysstat] / sa.h
1 /*
2  * sar/sadc: Report system activity
3  * (C) 1999-2017 by Sebastien Godard (sysstat <at> orange.fr)
4  */
5
6 #ifndef _SA_H
7 #define _SA_H
8
9 #include <stdio.h>
10
11 #include "common.h"
12 #include "rd_stats.h"
13 #include "rd_sensors.h"
14
15 /*
16  ***************************************************************************
17  * Activity identification values.
18  ***************************************************************************
19  */
20
21 /* Number of activities */
22 #define NR_ACT          39
23 /* The value below is used for sanity check */
24 #define MAX_NR_ACT      256
25
26 /* Number of functions used to count items */
27 #define NR_F_COUNT      11
28
29 /* Activities */
30 #define A_CPU           1
31 #define A_PCSW          2
32 #define A_IRQ           3
33 #define A_SWAP          4
34 #define A_PAGE          5
35 #define A_IO            6
36 #define A_MEMORY        7
37 #define A_KTABLES       8
38 #define A_QUEUE         9
39 #define A_SERIAL        10
40 #define A_DISK          11
41 #define A_NET_DEV       12
42 #define A_NET_EDEV      13
43 #define A_NET_NFS       14
44 #define A_NET_NFSD      15
45 #define A_NET_SOCK      16
46 #define A_NET_IP        17
47 #define A_NET_EIP       18
48 #define A_NET_ICMP      19
49 #define A_NET_EICMP     20
50 #define A_NET_TCP       21
51 #define A_NET_ETCP      22
52 #define A_NET_UDP       23
53 #define A_NET_SOCK6     24
54 #define A_NET_IP6       25
55 #define A_NET_EIP6      26
56 #define A_NET_ICMP6     27
57 #define A_NET_EICMP6    28
58 #define A_NET_UDP6      29
59 #define A_PWR_CPUFREQ   30
60 #define A_PWR_FAN       31
61 #define A_PWR_TEMP      32
62 #define A_PWR_IN        33
63 #define A_HUGE          34
64 #define A_PWR_WGHFREQ   35
65 #define A_PWR_USB       36
66 #define A_FILESYSTEM    37
67 #define A_NET_FC        38
68 #define A_NET_SOFT      39
69
70
71 /* Macro used to flag an activity that should be collected */
72 #define COLLECT_ACTIVITY(m)     act[get_activity_position(act, m, EXIT_IF_NOT_FOUND)]->options |= AO_COLLECTED
73
74 /* Macro used to flag an activity that should be selected */
75 #define SELECT_ACTIVITY(m)      act[get_activity_position(act, m, EXIT_IF_NOT_FOUND)]->options |= AO_SELECTED
76
77
78 /*
79  ***************************************************************************
80  * Flags.
81  ***************************************************************************
82  */
83
84 #define S_F_SINCE_BOOT          0x00000001
85 #define S_F_SA_ROTAT            0x00000002
86 #define S_F_DEV_PRETTY          0x00000004
87 #define S_F_FORCE_FILE          0x00000008
88 #define S_F_INTERVAL_SET        0x00000010
89 #define S_F_TRUE_TIME           0x00000020
90 #define S_F_LOCK_FILE           0x00000040
91 #define S_F_SEC_EPOCH           0x00000080
92 #define S_F_HDR_ONLY            0x00000100
93 #define S_F_FILE_LOCKED         0x00000200
94 #define S_F_SA_YYYYMMDD         0x00000400
95 #define S_F_HORIZONTALLY        0x00000800
96 #define S_F_COMMENT             0x00001000
97 #define S_F_PERSIST_NAME        0x00002000
98 #define S_F_LOCAL_TIME          0x00004000
99 #define S_F_PREFD_TIME_OUTPUT   0x00008000
100 #define S_F_SVG_SKIP            0x00010000
101 /* Same value as S_F_SVG_SKIP above. Used for a different output format */
102 #define S_F_RAW_SHOW_HINTS      0x00010000
103 #define S_F_SVG_AUTOSCALE       0x00020000
104 #define S_F_SVG_ONE_DAY         0x00040000
105 #define S_F_SVG_SHOW_IDLE       0x00080000
106 #define S_F_UNIT                0x00100000
107 #define S_F_SVG_HEIGHT          0x00200000
108 #define S_F_SVG_PACKED          0x00400000
109 #define S_F_SVG_SHOW_INFO       0x00800000
110
111 #define WANT_SINCE_BOOT(m)              (((m) & S_F_SINCE_BOOT)   == S_F_SINCE_BOOT)
112 #define WANT_SA_ROTAT(m)                (((m) & S_F_SA_ROTAT)     == S_F_SA_ROTAT)
113 #define USE_PRETTY_OPTION(m)            (((m) & S_F_DEV_PRETTY)   == S_F_DEV_PRETTY)
114 #define FORCE_FILE(m)                   (((m) & S_F_FORCE_FILE)   == S_F_FORCE_FILE)
115 #define INTERVAL_SET(m)                 (((m) & S_F_INTERVAL_SET) == S_F_INTERVAL_SET)
116 #define PRINT_TRUE_TIME(m)              (((m) & S_F_TRUE_TIME)    == S_F_TRUE_TIME)
117 #define LOCK_FILE(m)                    (((m) & S_F_LOCK_FILE)    == S_F_LOCK_FILE)
118 #define PRINT_SEC_EPOCH(m)              (((m) & S_F_SEC_EPOCH)    == S_F_SEC_EPOCH)
119 #define DISPLAY_HDR_ONLY(m)             (((m) & S_F_HDR_ONLY)     == S_F_HDR_ONLY)
120 #define FILE_LOCKED(m)                  (((m) & S_F_FILE_LOCKED)  == S_F_FILE_LOCKED)
121 #define USE_SA_YYYYMMDD(m)              (((m) & S_F_SA_YYYYMMDD)  == S_F_SA_YYYYMMDD)
122 #define DISPLAY_HORIZONTALLY(m)         (((m) & S_F_HORIZONTALLY) == S_F_HORIZONTALLY)
123 #define DISPLAY_COMMENT(m)              (((m) & S_F_COMMENT)      == S_F_COMMENT)
124 #define DISPLAY_PERSIST_NAME_S(m)       (((m) & S_F_PERSIST_NAME) == S_F_PERSIST_NAME)
125 #define PRINT_LOCAL_TIME(m)             (((m) & S_F_LOCAL_TIME)   == S_F_LOCAL_TIME)
126 #define USE_PREFD_TIME_OUTPUT(m)        (((m) & S_F_PREFD_TIME_OUTPUT)   == S_F_PREFD_TIME_OUTPUT)
127 #define SKIP_EMPTY_VIEWS(m)             (((m) & S_F_SVG_SKIP)     == S_F_SVG_SKIP)
128 #define DISPLAY_HINTS(m)                (((m) & S_F_RAW_SHOW_HINTS) == S_F_RAW_SHOW_HINTS)
129 #define AUTOSCALE_ON(m)                 (((m) & S_F_SVG_AUTOSCALE) == S_F_SVG_AUTOSCALE)
130 #define DISPLAY_ONE_DAY(m)              (((m) & S_F_SVG_ONE_DAY)   == S_F_SVG_ONE_DAY)
131 #define DISPLAY_IDLE(m)                 (((m) & S_F_SVG_SHOW_IDLE) == S_F_SVG_SHOW_IDLE)
132 #define DISPLAY_INFO(m)                 (((m) & S_F_SVG_SHOW_INFO) == S_F_SVG_SHOW_INFO)
133 #define DISPLAY_UNIT(m)                 (((m) & S_F_UNIT) == S_F_UNIT)
134 #define SET_CANVAS_HEIGHT(m)            (((m) & S_F_SVG_HEIGHT) == S_F_SVG_HEIGHT)
135 #define PACK_VIEWS(m)                   (((m) & S_F_SVG_PACKED) == S_F_SVG_PACKED)
136
137 #define AO_F_NULL               0x00000000
138
139 /* Output flags for options -r / -S */
140 #define AO_F_MEMORY             0x00000001
141 #define AO_F_SWAP               0x00000002
142 /* AO_F_MEM_ALL: See opt_flags in struct activity below */
143 #define AO_F_MEM_ALL            (AO_F_MEMORY << 8)
144
145 #define DISPLAY_MEMORY(m)       (((m) & AO_F_MEMORY)    == AO_F_MEMORY)
146 #define DISPLAY_SWAP(m)         (((m) & AO_F_SWAP)      == AO_F_SWAP)
147 #define DISPLAY_MEM_ALL(m)      (((m) & AO_F_MEM_ALL)   == AO_F_MEM_ALL)
148
149 /* Output flags for option -u [ ALL ] */
150 #define AO_F_CPU_DEF            0x00000001
151 #define AO_F_CPU_ALL            0x00000002
152
153 #define DISPLAY_CPU_DEF(m)      (((m) & AO_F_CPU_DEF)     == AO_F_CPU_DEF)
154 #define DISPLAY_CPU_ALL(m)      (((m) & AO_F_CPU_ALL)     == AO_F_CPU_ALL)
155
156 /* Output flags for option -d */
157 #define AO_F_DISK_PART          0x00000001
158
159 #define COLLECT_PARTITIONS(m)   (((m) & AO_F_DISK_PART)   == AO_F_DISK_PART)
160
161 /* Output flags for option -F */
162 #define AO_F_FILESYSTEM         0x00000001
163 #define AO_F_MOUNT              0x00000002
164
165 #define DISPLAY_MOUNT(m)        (((m) & AO_F_MOUNT)       == AO_F_MOUNT)
166
167 /*
168  ***************************************************************************
169  * Various keywords and constants.
170  ***************************************************************************
171  */
172
173 /* Keywords */
174 #define K_XALL          "XALL"
175 #define K_SUM           "SUM"
176 #define K_DEV           "DEV"
177 #define K_EDEV          "EDEV"
178 #define K_NFS           "NFS"
179 #define K_NFSD          "NFSD"
180 #define K_SOCK          "SOCK"
181 #define K_IP            "IP"
182 #define K_EIP           "EIP"
183 #define K_ICMP          "ICMP"
184 #define K_EICMP         "EICMP"
185 #define K_TCP           "TCP"
186 #define K_ETCP          "ETCP"
187 #define K_UDP           "UDP"
188 #define K_SOCK6         "SOCK6"
189 #define K_IP6           "IP6"
190 #define K_EIP6          "EIP6"
191 #define K_ICMP6         "ICMP6"
192 #define K_EICMP6        "EICMP6"
193 #define K_UDP6          "UDP6"
194 #define K_CPU           "CPU"
195 #define K_FAN           "FAN"
196 #define K_TEMP          "TEMP"
197 #define K_IN            "IN"
198 #define K_FREQ          "FREQ"
199 #define K_MOUNT         "MOUNT"
200 #define K_FC            "FC"
201 #define K_SOFT          "SOFT"
202
203 #define K_INT           "INT"
204 #define K_DISK          "DISK"
205 #define K_XDISK         "XDISK"
206 #define K_SNMP          "SNMP"
207 #define K_IPV6          "IPV6"
208 #define K_POWER         "POWER"
209 #define K_USB           "USB"
210
211 #define K_SKIP_EMPTY    "skipempty"
212 #define K_AUTOSCALE     "autoscale"
213 #define K_ONEDAY        "oneday"
214 #define K_SHOWIDLE      "showidle"
215 #define K_SHOWINFO      "showinfo"
216 #define K_SHOWHINTS     "showhints"
217 #define K_HEIGHT        "height="
218 #define K_PACKED        "packed"
219
220 /* Groups of activities */
221 #define G_DEFAULT       0x00
222 #define G_INT           0x01
223 #define G_DISK          0x02
224 #define G_SNMP          0x04
225 #define G_IPV6          0x08
226 #define G_POWER         0x10
227 #define G_XDISK         0x20
228
229 /* sadc program */
230 #define SADC            "sadc"
231
232 /* Time must have the format HH:MM:SS with HH in 24-hour format */
233 #define DEF_TMSTART     "08:00:00"
234 #define DEF_TMEND       "18:00:00"
235
236 #define UTSNAME_LEN     65
237 #define HEADER_LINE_LEN 512
238
239 /*
240  * Define upper limit for various items.
241  * Made necessary because we have to check the number of
242  * items for each activity when we read a (possibly tainted)
243  * sa data file.
244  */
245 #define MAX_NR_SERIAL_LINES     65536
246 #define MAX_NR_DISKS            (65536 * 4096)
247 #define MAX_NR_IFACES           65536
248 #define MAX_NR_FANS             4096
249 #define MAX_NR_TEMP_SENSORS     4096
250 #define MAX_NR_IN_SENSORS       4096
251 #define MAX_NR_USB              65536
252 #define MAX_NR_FS               (65536 * 4096)
253 #define MAX_NR_FCHOSTS          65536
254
255 /* NR_MAX is the upper limit used for unknown activities */
256 #define NR_MAX          (65536 * 4096)
257 #define NR2_MAX         1024
258
259 /* Maximum number of args that can be passed to sadc */
260 #define MAX_ARGV_NR     32
261
262 /* Miscellaneous constants */
263 #define USE_SADC                0
264 #define USE_SA_FILE             1
265 #define NO_TM_START             0
266 #define NO_TM_END               0
267 #define NO_RESET                0
268 #define NON_FATAL               0
269 #define FATAL                   1
270 #define C_SAR                   0
271 #define C_SADF                  1
272 #define ALL_ACTIVITIES          ~0U
273 #define EXIT_IF_NOT_FOUND       1
274 #define RESUME_IF_NOT_FOUND     0
275
276 #define SOFT_SIZE       0
277 #define HARD_SIZE       1
278
279 #define FIRST   0
280 #define SECOND  1
281
282 #define CLOSE_XML_MARKUP        0
283 #define OPEN_XML_MARKUP         1
284
285 #define CLOSE_JSON_MARKUP       0
286 #define OPEN_JSON_MARKUP        1
287
288 #define COUNT_ACTIVITIES        0
289 #define COUNT_OUTPUTS           1
290
291 /* Type for all functions counting items */
292 #define __nr_t          int
293 /* Type for all functions reading statistics */
294 #define __read_funct_t  void
295 /* Type for all functions displaying statistics */
296 #define __print_funct_t void
297
298 /* Structure for SVG specific parameters */
299 struct svg_parm {
300         unsigned long long dt;                  /* Interval of time for current sample */
301         unsigned long long ust_time_ref;        /* X axis start time in seconds since the epoch */
302         unsigned long long ust_time_end;        /* X axis end time in seconds since the epoch */
303         unsigned long long ust_time_first;      /* Time (in seconds since the epoch) for first sample */
304         int graph_no;                           /* Total number of views already displayed */
305         int restart;                            /* TRUE if we have just met a RESTART record */
306         struct file_header *file_hdr;           /* Pointer on file header structure */
307 };
308
309 /* Structure used when displaying SVG header */
310 struct svg_hdr_parm {
311         int graph_nr;      /* Number of rows of views to display or canvas height entered on the command line */
312         int views_per_row; /* Maximum number of views on a single row */
313 };
314
315 /*
316  ***************************************************************************
317  * Definitions of header structures.
318  *
319  * The rule is: "strict writing, broad reading", meaning that sar/sadc can
320  * only append data to a datafile whose format is strictly the same as that
321  * of current version (checking FORMAT_MAGIC is not enough), but sar/sadf
322  * can read data from different versions, providing that FORMAT_MAGIC value
323  * has not changed.
324  *
325  * Format of system activity data files:
326  *       __
327  *      |
328  *      | file_magic structure
329  *      |
330  *      |--
331  *      |
332  *      | file_header structure
333  *      |
334  *      |--                         --|
335  *      |                             |
336  *      | file_activity structure     | * sa_act_nr
337  *      |                             |
338  *      |--                         --|
339  *      |                             |
340  *      | record_header structure     |
341  *      |                             |
342  *      |--                           | * <count>
343  *      |                             |
344  *      | Statistics structures...(*) |
345  *      |                             |
346  *      |--                         --|
347  *
348  * (*)Note: If it's a special record, we may find a comment instead of
349  * statistics (R_COMMENT record type) or, if it's a R_RESTART record type,
350  * <sa_nr_vol_act> structures (of type file_activity) for the volatile
351  * activities.
352  ***************************************************************************
353  */
354
355 /*
356  * Sysstat magic number. Should never be modified.
357  * Indicate that the file was created by sysstat.
358  */
359 #define SYSSTAT_MAGIC   0xd596
360 #define SYSSTAT_MAGIC_SWAPPED   (((SYSSTAT_MAGIC << 8) | (SYSSTAT_MAGIC >> 8)) & 0xffff)
361
362 /*
363  * Datafile format magic number.
364  * Modified to indicate that the format of the file is
365  * no longer compatible with that of previous sysstat versions.
366  */
367 #define FORMAT_MAGIC    0x2175
368 #define FORMAT_MAGIC_SWAPPED    (((FORMAT_MAGIC << 8) | (FORMAT_MAGIC >> 8)) & 0xffff)
369
370 /* Previous datafile format magic number used by older sysstat versions */
371 #define PREVIOUS_FORMAT_MAGIC   0x2173
372
373 /* Padding in file_magic structure. See below. */
374 #define FILE_MAGIC_PADDING      48
375
376 /* Structure for file magic header data */
377 struct file_magic {
378         /*
379          * This field identifies the file as a file created by sysstat.
380          */
381         unsigned short sysstat_magic;
382         /*
383          * The value of this field varies whenever datafile format changes.
384          */
385         unsigned short format_magic;
386         /*
387          * Sysstat version used to create the file.
388          */
389         unsigned char sysstat_version;
390         unsigned char sysstat_patchlevel;
391         unsigned char sysstat_sublevel;
392         unsigned char sysstat_extraversion;
393 #define FILE_MAGIC_ULL_NR       0       /* Nr of unsigned long long below */
394 #define FILE_MAGIC_UL_NR        0       /* Nr of unsigned long below */
395 #define FILE_MAGIC_U_NR         5       /* Nr of [unsigned] int below */
396         /*
397          * Size of file's header (size of file_header structure used by file).
398          */
399         unsigned int header_size;
400         /*
401          * Set to non zero if data file has been converted with "sadf -c" from
402          * an old format (version x.y.z) to a newest format (version X.Y.Z).
403          * In this case, the value is: Y*256 + Z + 1.
404          * The FORMAT_MAGIC value of the file can be used to determine X.
405          */
406         unsigned int upgraded;
407         /*
408          * Description of the file_header structure
409          * (nr of "long long", nr of "long" and nr of "int").
410          */
411         unsigned int hdr_types_nr[3];
412         /*
413          * Padding. Reserved for future use while avoiding a format change.
414          * sysstat always reads a number of bytes which is that expected for
415          * current sysstat version (FILE_MAGIC_SIZE). We cannot guess if we
416          * are going to read a file from current, an older or a newer version.
417          */
418         unsigned char pad[FILE_MAGIC_PADDING];
419 };
420
421 #define FILE_MAGIC_SIZE (sizeof(struct file_magic))
422
423 /* Header structure for system activity data file */
424 struct file_header {
425         /*
426          * Timestamp in seconds since the epoch.
427          */
428         unsigned long long sa_ust_time;
429         /*
430          * Number of jiffies per second.
431          */
432         unsigned long sa_hz             __attribute__ ((aligned (8)));
433         /*
434          * Number of CPU items (1 .. CPU_NR + 1) for the last sample in file.
435          */
436         unsigned int sa_last_cpu_nr     __attribute__ ((aligned (8)));
437         /*
438          * Number of [online or offline] CPU (1 .. CPU_NR + 1)
439          * when the datafile has been created.
440          * When reading a datafile, this value is updated whenever
441          * a RESTART record is found.
442          */
443         unsigned int sa_cpu_nr;
444         /*
445          * Number of activities saved in file.
446          */
447         unsigned int sa_act_nr;
448         /*
449          * Number of volatile activities in file. This is the number of
450          * file_activity structures saved after each restart mark in file.
451          */
452         unsigned int sa_vol_act_nr;
453         /*
454          * Current year.
455          */
456         int sa_year;
457         /*
458          * Description of the file_activity and record_header structures
459          * (nr of "long long", nr of "long" and nr of "int").
460          */
461         unsigned int act_types_nr[3];
462         unsigned int rec_types_nr[3];
463         /*
464          * Size of file_activity and record_header structures used by file.
465          */
466         unsigned int act_size;
467         unsigned int rec_size;
468         /*
469          * Current day and month.
470          * No need to save DST (Daylight Saving Time) flag, since it is not taken
471          * into account by the strftime() function used to print the timestamp.
472          */
473         unsigned char sa_day;
474         unsigned char sa_month;
475         /*
476          * Size of a long integer. Useful to know the architecture on which
477          * the datafile was created.
478          */
479         char sa_sizeof_long;
480         /*
481          * Operating system name.
482          */
483         char sa_sysname[UTSNAME_LEN];
484         /*
485          * Machine hostname.
486          */
487         char sa_nodename[UTSNAME_LEN];
488         /*
489          * Operating system release number.
490          */
491         char sa_release[UTSNAME_LEN];
492         /*
493          * Machine architecture.
494          */
495         char sa_machine[UTSNAME_LEN];
496 };
497
498 #define FILE_HEADER_SIZE        (sizeof(struct file_header))
499 #define FILE_HEADER_ULL_NR      1       /* Nr of unsigned long long in file_header structure */
500 #define FILE_HEADER_UL_NR       1       /* Nr of unsigned long in file_header structure */
501 #define FILE_HEADER_U_NR        13      /* Nr of [unsigned] int in file_header structure */
502 /* The values below are used for sanity check */
503 #define MIN_FILE_HEADER_SIZE    0
504 #define MAX_FILE_HEADER_SIZE    8192
505
506
507 /*
508  * Base magical number for activities.
509  */
510 #define ACTIVITY_MAGIC_BASE     0x8a
511 /*
512  * Magical value used for activities with
513  * unknown format (used for sadf -H only).
514  */
515 #define ACTIVITY_MAGIC_UNKNOWN  0x89
516
517 /* List of activities saved in file */
518 struct file_activity {
519         /*
520          * Identification value of activity.
521          */
522         unsigned int id;
523         /*
524          * Activity magical number.
525          */
526         unsigned int magic;
527         /*
528          * Number of items for this activity.
529          */
530         __nr_t nr;
531         /*
532          * Number of sub-items for this activity.
533          */
534         __nr_t nr2;
535         /*
536          * Size of an item structure.
537          */
538         int size;
539         /*
540          * Description of the structure containing statistics for the
541          * given activity (nr of "long long", nr of "long" and nr of "int").
542          */
543         unsigned int types_nr[3];
544 };
545
546 #define FILE_ACTIVITY_SIZE      (sizeof(struct file_activity))
547 #define MAX_FILE_ACTIVITY_SIZE  1024    /* Used for sanity check */
548 #define FILE_ACTIVITY_ULL_NR    0       /* Nr of unsigned long long in file_activity structure */
549 #define FILE_ACTIVITY_UL_NR     0       /* Nr of unsigned long in file_activity structure */
550 #define FILE_ACTIVITY_U_NR      8       /* Nr of [unsigned] int in file_activity structure */
551
552
553 /* Record type */
554 /*
555  * R_STATS means that this is a record of statistics.
556  */
557 #define R_STATS         1
558 /*
559  * R_RESTART means that this is a special record containing
560  * a LINUX RESTART message.
561  */
562 #define R_RESTART       2
563 /*
564  * R_LAST_STATS warns sar that this is the last record to be written
565  * to file before a file rotation, and that the next data to come will
566  * be a header file.
567  * Such a record is tagged R_STATS anyway before being written to file.
568  */
569 #define R_LAST_STATS    3
570 /*
571  * R_COMMENT means that this is a special record containing
572  * a comment.
573  */
574 #define R_COMMENT       4
575
576 /* Maximum length of a comment */
577 #define MAX_COMMENT_LEN 64
578
579 /* Header structure for every record */
580 struct record_header {
581         /*
582          * Machine uptime in 1/100th of a second.
583          */
584         unsigned long long uptime_cs;
585         /*
586          * Timestamp (number of seconds since the epoch).
587          */
588         unsigned long long ust_time;
589         /*
590          * Record type: R_STATS, R_RESTART,...
591          */
592         unsigned char record_type;
593         /*
594          * Timestamp: Hour (0-23), minute (0-59) and second (0-59).
595          * Used to determine TRUE time (immutable, non locale dependent time).
596          */
597         unsigned char hour;
598         unsigned char minute;
599         unsigned char second;
600 };
601
602 #define RECORD_HEADER_SIZE      (sizeof(struct record_header))
603 #define MAX_RECORD_HEADER_SIZE  512     /* Used for sanity check */
604 #define RECORD_HEADER_ULL_NR    2       /* Nr of unsigned long long in record_header structure */
605 #define RECORD_HEADER_UL_NR     0       /* Nr of unsigned long in record_header structure */
606 #define RECORD_HEADER_U_NR      0       /* Nr of unsigned int in record_header structure */
607
608
609 /*
610  ***************************************************************************
611  * Generic description of an activity.
612  ***************************************************************************
613  */
614
615 /* Activity options */
616 #define AO_NULL                 0x00
617 /*
618  * Indicate that corresponding activity should be collected by sadc.
619  */
620 #define AO_COLLECTED            0x01
621 /*
622  * Indicate that corresponding activity should be displayed by sar.
623  */
624 #define AO_SELECTED             0x02
625 /*
626  * When appending data to a file, the number of items (for every activity)
627  * is forced to that of the file (number of network interfaces, serial lines,
628  * etc.) Exceptions are volatile activities (like A_CPU) whose number of items
629  * is related to the number of CPUs: If current machine has a different number
630  * of CPU than that of the file (but is equal to sa_last_cpu_nr) then data
631  * will be appended with a number of items equal to that of the machine.
632  */
633 #define AO_VOLATILE             0x04
634 /*
635  * 0x08: Unused.
636  */
637 /*
638  * This flag should be set for every activity closing a markup used
639  * by several activities. Used by sadf f_xml_print() functions to
640  * display XML output.
641  */
642 #define AO_CLOSE_MARKUP         0x10
643 /*
644  * Indicate that corresponding activity has multiple different
645  * output formats. This is the case for example for memory activity
646  * with options -r and -R.
647  * PS: Such an activity should appear in the list of activities that
648  * sar -A is supposed to display.
649  */
650 #define AO_MULTIPLE_OUTPUTS     0x20
651 /*
652  * Indicate that one (SVG) graph will be displayed for each
653  * distinct item for this activity (sadf -g).
654  */
655 #define AO_GRAPH_PER_ITEM       0x40
656 /*
657  * Indicate that this activity may have sub-items.
658  */
659 #define AO_MATRIX               0x80
660
661 #define IS_COLLECTED(m)         (((m) & AO_COLLECTED)        == AO_COLLECTED)
662 #define IS_SELECTED(m)          (((m) & AO_SELECTED)         == AO_SELECTED)
663 #define IS_VOLATILE(m)          (((m) & AO_VOLATILE)         == AO_VOLATILE)
664 #define CLOSE_MARKUP(m)         (((m) & AO_CLOSE_MARKUP)     == AO_CLOSE_MARKUP)
665 #define HAS_MULTIPLE_OUTPUTS(m) (((m) & AO_MULTIPLE_OUTPUTS) == AO_MULTIPLE_OUTPUTS)
666 #define ONE_GRAPH_PER_ITEM(m)   (((m) & AO_GRAPH_PER_ITEM)   == AO_GRAPH_PER_ITEM)
667 #define IS_MATRIX(m)            (((m) & AO_MATRIX)           == AO_MATRIX)
668
669 #define _buf0   buf[0]
670
671 /* Structure used to define a bitmap needed by an activity */
672 struct act_bitmap {
673         /*
674          * Bitmap for activities that need one. Remember to allocate it
675          * before use!
676          */
677         unsigned char *b_array;
678         /*
679          * Size of the bitmap in bits. In fact, bitmap is sized to b_size + 1
680          * to take into account CPU "all"
681          */
682         int b_size;
683 };
684
685 /*
686  * Structure used to define an activity.
687  * Note: This structure can be modified without changing the format of data files.
688  */
689 struct activity {
690         /*
691          * This variable contains the identification value (A_...) for this activity.
692          */
693         unsigned int id;
694         /*
695          * Activity options (AO_...)
696          */
697         unsigned int options;
698         /*
699          * Activity magical number. This number changes when activity format in file
700          * is no longer compatible with the format of that same activity from
701          * previous versions.
702          */
703         unsigned int magic;
704         /*
705          * An activity belongs to a group (and only one).
706          * Groups are those selected with option -S of sadc.
707          */
708         unsigned int group;
709         /*
710          * Index in f_count[] array to determine function used to count
711          * the number of items (serial lines, network interfaces, etc.) -> @nr
712          * Such a function should _always_ return a value greater than
713          * or equal to 0.
714          *
715          * A value of -1 indicates that the number of items
716          * is a constant (and @nr is set to this value).
717          *
718          * These functions are called even if corresponding activities have not
719          * been selected, to make sure that all items have been calculated
720          * (including #CPU, etc.)
721          */
722         int f_count_index;
723         /*
724          * The f_count2() function is used to count the number of
725          * sub-items -> @nr2
726          * Such a function should _always_ return a value greater than
727          * or equal to 0.
728          *
729          * A NULL value for this function pointer indicates that the number of items
730          * is a constant (and @nr2 is set to this value).
731          */
732         __nr_t (*f_count2) (struct activity *);
733         /*
734          * This function reads the relevant file and fill the buffer
735          * with statistics corresponding to given activity.
736          */
737         __read_funct_t (*f_read) (struct activity *);
738         /*
739          * This function displays activity statistics onto the screen.
740          */
741         __print_funct_t (*f_print) (struct activity *, int, int, unsigned long long);
742         /*
743          * This function displays average activity statistics onto the screen.
744          */
745         __print_funct_t (*f_print_avg) (struct activity *, int, int, unsigned long long);
746         /*
747          * This function is used by sadf to display activity in a format that can
748          * easily be ingested by a relational database, or a format that can be
749          * handled by pattern processing commands like "awk".
750          */
751         __print_funct_t (*f_render) (struct activity *, int, char *, int, unsigned long long);
752         /*
753          * This function is used by sadf to display activity statistics in XML.
754          */
755         __print_funct_t (*f_xml_print) (struct activity *, int, int, unsigned long long);
756         /*
757          * This function is used by sadf to display activity statistics in JSON.
758          */
759         __print_funct_t (*f_json_print) (struct activity *, int, int, unsigned long long);
760         /*
761          * This function is used by sadf to display activity statistics in SVG.
762          */
763         __print_funct_t (*f_svg_print) (struct activity *, int, int, struct svg_parm *,
764                                         unsigned long long, struct record_header *);
765         /*
766          * This function is used by sadf to display activity statistics in raw format.
767          */
768         __print_funct_t (*f_raw_print) (struct activity *, char *, int);
769         /*
770          * Header string displayed by sadf -d.
771          * Header lines for each output (for activities with multiple outputs) are
772          * separated with a '|' character.
773          * For a given output, the first field corresponding to extended statistics
774          * (eg. -r ALL) begins with a '&' character.
775          */
776         char *hdr_line;
777         /*
778          * Name of activity.
779          */
780         char *name;
781         /*
782          * Description of the corresponding structure containing statistics (as defined
783          * in rd_stats.h or rd_sensors.h). Such a structure has 0+ fields of type
784          * "long long", followed by 0+ fields of type "long", followed by 0+ fields of
785          * type "int", followed by 0+ other fields (e.g. of type char). The array below
786          * gives the number of "long long" fields composing the structure, then the number
787          * of "long" fields, then the number of "int" fields.
788          */
789         unsigned int gtypes_nr[3];
790         /*
791          * This array has the same meaning as @gtypes_nr[] above, but the values are those
792          * read from current data file. They may be different from those of @gtypes_nr[]
793          * because we can read data from a different sysstat version (older or newer).
794          */
795         unsigned int ftypes_nr[3];
796         /*
797          * Number of SVG graphs for this activity. The total number of graphs for
798          * the activity can be greater though if flag AO_GRAPH_PER_ITEM is set, in
799          * which case the total number will  be @g_nr * @nr.
800          */
801         int g_nr;
802         /*
803          * Number of items on the system.
804          * A negative value (-1) is the default value and indicates that this number
805          * has still not been calculated by the f_count() function.
806          * A value of 0 means that this number has been calculated, but no items have
807          * been found.
808          * A positive value (>0) has either been calculated or is a constant.
809          */
810         __nr_t nr;
811         /*
812          * Number of sub-items on the system.
813          * @nr2 is in fact the second dimension of a matrix of items, the first
814          * one being @nr. @nr is the number of lines, and @nr2 the number of columns.
815          * A negative value (-1) is the default value and indicates that this number
816          * has still not been calculated by the f_count2() function.
817          * A value of 0 means that this number has been calculated, but no sub-items have
818          * been found.
819          * A positive value (>0) has either been calculated or is a constant.
820          * Rules:
821          * 1) IF @nr2 = 0 THEN @nr = 0
822          *    Note: If @nr = 0, then @nr2 is undetermined (may be -1, 0 or >0).
823          * 2) IF @nr > 0 THEN @nr2 > 0.
824          *    Note: If @nr2 > 0 then @nr is undetermined (may be -1, 0 or >0).
825          * 3) IF @nr <= 0 THEN @nr2 = -1 (this is the default value for @nr2,
826          * meaning that it has not been calculated).
827          */
828         __nr_t nr2;
829         /*
830          * Maximum number of elements that sar can handle for this item.
831          * NB: The maximum number of elements that sar can handle for sub-items
832          * is NR2_MAX.
833          */
834         __nr_t nr_max;
835         /*
836          * Size of an item.
837          * This is the size of the corresponding structure, as read from or written
838          * to a file, or read from or written by the data collector.
839          */
840         int fsize;
841         /*
842          * Size of an item.
843          * This is the size of the corresponding structure as mapped into memory.
844          * @msize can be different from @fsize when data are read from or written to
845          * a data file from a different sysstat version.
846          */
847         int msize;
848         /*
849          * Optional flags for activity. This is eg. used when AO_MULTIPLE_OUTPUTS
850          * option is set.
851          * 0x0001 - 0x0080 : Multiple outputs (eg. AO_F_MEMORY, AO_F_SWAP...)
852          * 0x0100 - 0x8000 : If bit set then display complete header (hdr_line) for
853          *                   corresponding output
854          * 0x010000+       : Optional flags
855          */
856         unsigned int opt_flags;
857         /*
858          * Buffers that will contain the statistics read. Its size is @nr * @nr2 * @size each.
859          * [0]: used by sadc.
860          * [0] and [1]: current/previous statistics values (used by sar).
861          * [2]: Used by sar to save first collected stats (used later to
862          * compute average).
863          */
864         void *buf[3];
865         /*
866          * Bitmap for activities that need one. Such a bitmap is needed by activity
867          * if @bitmap is not NULL.
868          */
869         struct act_bitmap *bitmap;
870 };
871
872
873 /*
874  ***************************************************************************
875  * Generic description of an output format for sadf (and sar).
876  ***************************************************************************
877  */
878
879 /* Type for all functions used by sadf to display stats in various formats */
880 #define __printf_funct_t void
881 #define __tm_funct_t char *
882
883 /*
884  * Structure used to define a report.
885  * A XML-like report has the following format:
886  *       __
887  *      |
888  *      | Header block
889  *      |  __
890  *      | |
891  *      | | Statistics block
892  *      | |  __
893  *      | | |
894  *      | | | Timestamp block
895  *      | | |  __
896  *      | | | |
897  *      | | | | Activity #1
898  *      | | | |__
899  *      | | | |
900  *      | | | | ...
901  *      | | | |__
902  *      | | | |
903  *      | | | | Activity #n
904  *      | | | |__
905  *      | | |__
906  *      | |__
907  *      | |
908  *      | | Restart messages block
909  *      | |__
910  *      | |
911  *      | | Comments block
912  *      | |__
913  *      |__
914  */
915 struct report_format {
916         /*
917          * This variable contains the identification value (F_...) for this report format.
918          */
919         unsigned int id;
920         /*
921          * Format options (FO_...).
922          */
923         unsigned int options;
924         /*
925          * This function displays the report header
926          * (data displayed once at the beginning of the report).
927          */
928         __printf_funct_t (*f_header) (void *, int, char *, struct file_magic *, struct file_header *,
929                                       struct activity * [], unsigned int []);
930         /*
931          * This function defines the statistics part of the report.
932          * Used only with textual (XML-like) reports.
933          */
934         __printf_funct_t (*f_statistics) (int *, int);
935         /*
936          * This function defines the timestamp part of the report.
937          * Used only with textual (XML-like) reports.
938          */
939         __tm_funct_t (*f_timestamp) (void *, int, char *, char *, unsigned long long,
940                                      struct file_header *, unsigned int);
941         /*
942          * This function displays the restart messages.
943          */
944         __printf_funct_t (*f_restart) (int *, int, char *, char *, int, struct file_header *);
945         /*
946          * This function displays the comments.
947          */
948         __printf_funct_t (*f_comment) (int *, int, char *, char *, int, char *, struct file_header *);
949 };
950
951 /* Possible actions for functions used to display reports */
952 #define F_BEGIN 0x01
953 #define F_MAIN  0x02
954 #define F_END   0x04
955
956 /*
957  ***************************************************************************
958  * SVG output definitions
959  ***************************************************************************
960  */
961
962 /*
963  *   ^
964  * 1 | General header
965  *   v
966  *   ^   ^   ^
967  *   |   | 4 | Graph title
968  *   |   |   v
969  *   |   |   ^    |                                Caption
970  *   | 3 |   |    |
971  *   |   |   |  G |Y
972  * 2 |   | 5 |  r |
973  *   |   |   |  a |A
974  *   |   |   |  d |x
975  *   |   |   |  . |i
976  *   |   |   |    |s          X Axis
977  *   |   |   v    |-------------------------------
978  *   |   |              Grad.
979  *   |   v   <---><------------------------------>
980  *   |         6                8
981  *   | Gap
982  *   v<---------------------------------------------------------------> Gap
983  *                                    7
984  *    <--------------------------------------------------------------------->
985  *                                      8
986  */
987
988 /* #8 */
989 #define SVG_G_XSIZE     720
990 /* #6 */
991 #define SVG_M_XSIZE     70
992 /* #7 */
993 #define SVG_V_XSIZE     1050
994 /* #8 */
995 #define SVG_T_XSIZE     1060
996
997 /* #5 */
998 #define SVG_G_YSIZE     200
999 /* #1 */
1000 #define SVG_H_YSIZE     50
1001 /* #4 */
1002 #define SVG_M_YSIZE     50
1003 /* #2 */
1004 #define SVG_T_YSIZE     310
1005 /* #3 */
1006 #define SVG_V_YSIZE     300
1007
1008 /* Grid: Nr of horizontal lines */
1009 #define SVG_H_GRIDNR    3
1010 /* Grid: Nr of vertical lines */
1011 #define SVG_V_GRIDNR    10
1012
1013 /* Block size used to allocate arrays for graphs data */
1014 #define CHUNKSIZE       4096
1015
1016 /* Maximum number of views on a single row */
1017 #define MAX_VIEWS_ON_A_ROW      6
1018
1019 #define SVG_LINE_GRAPH  1
1020 #define SVG_BAR_GRAPH   2
1021
1022 /* Maximum number of horizontal lines for the background grid */
1023 #define MAX_HLINES_NR   10
1024
1025 #define MAYBE   0x80
1026
1027 /*
1028  ***************************************************************************
1029  * Macro functions definitions.
1030  *
1031  * Note: Using 'do ... while' makes the macros safer to use
1032  * (remember that macro use is followed by a semicolon).
1033  ***************************************************************************
1034  */
1035
1036 /* Close file descriptors */
1037 #define CLOSE_ALL(_fd_)         do {                    \
1038                                         close(_fd_[0]); \
1039                                         close(_fd_[1]); \
1040                                 } while (0)
1041
1042 #define CLOSE(_fd_)             if (_fd_ >= 0)          \
1043                                         close(_fd_)
1044
1045
1046 /*
1047  ***************************************************************************
1048  * Various structure definitions.
1049  ***************************************************************************
1050  */
1051
1052 /* Structure for timestamps */
1053 struct tstamp {
1054         int tm_sec;
1055         int tm_min;
1056         int tm_hour;
1057         int use;
1058 };
1059
1060
1061 /*
1062  ***************************************************************************
1063  * Functions prototypes.
1064  ***************************************************************************
1065  */
1066
1067 /* Functions used to count number of items */
1068 __nr_t wrap_get_cpu_nr
1069         (struct activity *);
1070 __nr_t wrap_get_irq_nr
1071         (struct activity *);
1072 __nr_t wrap_get_serial_nr
1073         (struct activity *);
1074 __nr_t wrap_get_disk_nr
1075         (struct activity *);
1076 __nr_t wrap_get_iface_nr
1077         (struct activity *);
1078 __nr_t wrap_get_fan_nr
1079         (struct activity *);
1080 __nr_t wrap_get_temp_nr
1081         (struct activity *);
1082 __nr_t wrap_get_in_nr
1083         (struct activity *);
1084 __nr_t wrap_get_freq_nr
1085         (struct activity *);
1086 __nr_t wrap_get_usb_nr
1087         (struct activity *);
1088 __nr_t wrap_get_filesystem_nr
1089         (struct activity *);
1090 __nr_t wrap_get_fchost_nr
1091         (struct activity *);
1092
1093 /* Functions used to read activities statistics */
1094 __read_funct_t wrap_read_stat_cpu
1095         (struct activity *);
1096 __read_funct_t wrap_read_stat_pcsw
1097         (struct activity *);
1098 __read_funct_t wrap_read_stat_irq
1099         (struct activity *);
1100 __read_funct_t wrap_read_swap
1101         (struct activity *);
1102 __read_funct_t wrap_read_paging
1103         (struct activity *);
1104 __read_funct_t wrap_read_io
1105         (struct activity *);
1106 __read_funct_t wrap_read_meminfo
1107         (struct activity *);
1108 __read_funct_t wrap_read_kernel_tables
1109         (struct activity *);
1110 __read_funct_t wrap_read_loadavg
1111         (struct activity *);
1112 __read_funct_t wrap_read_tty_driver_serial
1113         (struct activity *);
1114 __read_funct_t wrap_read_disk
1115         (struct activity *);
1116 __read_funct_t wrap_read_net_dev
1117         (struct activity *);
1118 __read_funct_t wrap_read_net_edev
1119         (struct activity *);
1120 __read_funct_t wrap_read_net_nfs
1121         (struct activity *);
1122 __read_funct_t wrap_read_net_nfsd
1123         (struct activity *);
1124 __read_funct_t wrap_read_net_sock
1125         (struct activity *);
1126 __read_funct_t wrap_read_net_ip
1127         (struct activity *);
1128 __read_funct_t wrap_read_net_eip
1129         (struct activity *);
1130 __read_funct_t wrap_read_net_icmp
1131         (struct activity *);
1132 __read_funct_t wrap_read_net_eicmp
1133         (struct activity *);
1134 __read_funct_t wrap_read_net_tcp
1135         (struct activity *);
1136 __read_funct_t wrap_read_net_etcp
1137         (struct activity *);
1138 __read_funct_t wrap_read_net_udp
1139         (struct activity *);
1140 __read_funct_t wrap_read_net_sock6
1141         (struct activity *);
1142 __read_funct_t wrap_read_net_ip6
1143         (struct activity *);
1144 __read_funct_t wrap_read_net_eip6
1145         (struct activity *);
1146 __read_funct_t wrap_read_net_icmp6
1147         (struct activity *);
1148 __read_funct_t wrap_read_net_eicmp6
1149         (struct activity *);
1150 __read_funct_t wrap_read_net_udp6
1151         (struct activity *);
1152 __read_funct_t wrap_read_cpuinfo
1153         (struct activity *);
1154 __read_funct_t wrap_read_fan
1155         (struct activity *);
1156 __read_funct_t wrap_read_temp
1157         (struct activity *);
1158 __read_funct_t wrap_read_in
1159         (struct activity *);
1160 __read_funct_t wrap_read_meminfo_huge
1161         (struct activity *);
1162 __read_funct_t wrap_read_time_in_state
1163         (struct activity *);
1164 __read_funct_t wrap_read_bus_usb_dev
1165         (struct activity *);
1166 __read_funct_t wrap_read_filesystem
1167         (struct activity *);
1168 __read_funct_t wrap_read_fchost
1169         (struct activity *);
1170 __read_funct_t wrap_read_softnet
1171         (struct activity *);
1172
1173 /* Other functions */
1174 void allocate_bitmaps
1175         (struct activity * []);
1176 void allocate_structures
1177         (struct activity * []);
1178 int check_alt_sa_dir
1179         (char *, int, int);
1180 int check_disk_reg
1181         (struct activity *, int, int, int);
1182 void check_file_actlst
1183         (int *, char *, struct activity * [], struct file_magic *, struct file_header *,
1184          struct file_activity **, unsigned int [], int, int *, int *);
1185 int check_net_dev_reg
1186         (struct activity *, int, int, int);
1187 int check_net_edev_reg
1188         (struct activity *, int, int, int);
1189 double compute_ifutil
1190         (struct stats_net_dev *, double, double);
1191 void copy_structures
1192         (struct activity * [], unsigned int [], struct record_header [], int, int);
1193 int datecmp
1194         (struct tm *, struct tstamp *);
1195 void display_sa_file_version
1196         (FILE *, struct file_magic *);
1197 void enum_version_nr
1198         (struct file_magic *);
1199 void free_bitmaps
1200         (struct activity * []);
1201 void free_structures
1202         (struct activity * []);
1203 int get_activity_nr
1204         (struct activity * [], unsigned int, int);
1205 int get_activity_position
1206         (struct activity * [], unsigned int, int);
1207 char *get_devname
1208         (unsigned int, unsigned int, int);
1209 void get_file_timestamp_struct
1210         (unsigned int, struct tm *, struct file_header *);
1211 void get_itv_value
1212         (struct record_header *, struct record_header *, unsigned long long *);
1213 void handle_invalid_sa_file
1214         (int *, struct file_magic *, char *, int);
1215 int next_slice
1216         (unsigned long long, unsigned long long, int, long);
1217 int parse_sar_opt
1218         (char * [], int *, struct activity * [], unsigned int *, int);
1219 int parse_sar_I_opt
1220         (char * [], int *, struct activity * []);
1221 int parse_sa_P_opt
1222         (char * [], int *, unsigned int *, struct activity * []);
1223 int parse_sar_m_opt
1224         (char * [], int *, struct activity * []);
1225 int parse_sar_n_opt
1226         (char * [], int *, struct activity * []);
1227 int parse_timestamp
1228         (char * [], int *, struct tstamp *, const char *);
1229 void print_report_hdr
1230         (unsigned int, struct tm *, struct file_header *);
1231 void print_sar_comment
1232         (int *, int, char *, char *, int, char *, struct file_header *);
1233 __printf_funct_t print_sar_restart
1234         (int *, int, char *, char *, int, struct file_header *);
1235 int print_special_record
1236         (struct record_header *, unsigned int, struct tstamp *, struct tstamp *,
1237          int, int, struct tm *, struct tm *, char *, int, struct file_magic *,
1238          struct file_header *, struct activity * [], struct report_format *, int, int);
1239 void read_file_stat_bunch
1240         (struct activity * [], int, int, int, struct file_activity *, int, int);
1241 int read_record_hdr
1242         (int, void *, struct record_header *, struct file_header *, int, int);
1243 __nr_t read_vol_act_structures
1244         (int, struct activity * [], char *, struct file_magic *, unsigned int, int, int);
1245 int reallocate_vol_act_structures
1246         (struct activity * [], unsigned int, unsigned int);
1247 void remap_struct
1248         (unsigned int [], unsigned int [], void *, unsigned int);
1249 void replace_nonprintable_char
1250         (int, char *);
1251 int sa_fread
1252         (int, void *, int, int);
1253 int sa_get_record_timestamp_struct
1254         (unsigned int, struct record_header *, struct tm *, struct tm *);
1255 int sa_open_read_magic
1256         (int *, char *, struct file_magic *, int, int *);
1257 void select_all_activities
1258         (struct activity * []);
1259 void select_default_activity
1260         (struct activity * []);
1261 void set_bitmap
1262         (unsigned char [], unsigned char, unsigned int);
1263 void set_default_file
1264         (char *, int, int);
1265 void set_hdr_rectime
1266         (unsigned int, struct tm *, struct file_header *);
1267 void set_record_timestamp_string
1268         (unsigned int, struct record_header *, char *, char *, int, struct tm *);
1269 void swap_struct
1270         (unsigned int [], void *, int);
1271
1272 #endif  /* _SA_H */