]> granicus.if.org Git - sysstat/blob - common.h
sar/sadc: Dereference nr array pointer in struct activity
[sysstat] / common.h
1 /*
2  * sysstat: System performance tools for Linux
3  * (C) 1999-2020 by Sebastien Godard (sysstat <at> orange.fr)
4  */
5
6 #ifndef _COMMON_H
7 #define _COMMON_H
8
9 /* Maximum length of sensors device name */
10 #define MAX_SENSORS_DEV_LEN     20
11
12 #include <time.h>
13 #include <sched.h>      /* For __CPU_SETSIZE */
14 #include <limits.h>
15 #include <stdlib.h>
16 #include "systest.h"
17
18 #ifdef HAVE_SYS_SYSMACROS_H
19 /* Needed on some non-glibc environments */
20 #include <sys/sysmacros.h>
21 #endif
22
23 /*
24  ***************************************************************************
25  * Various keywords and constants
26  ***************************************************************************
27  */
28
29 #define FALSE   0
30 #define TRUE    1
31
32 #define PLAIN_OUTPUT    0
33
34 #define DISP_HDR        1
35
36 /* Index in units array (see common.c) */
37 #define NO_UNIT         -1
38 #define UNIT_SECTOR     0
39 #define UNIT_BYTE       1
40 #define UNIT_KILOBYTE   2
41
42 #define NR_UNITS        8
43
44 /* Timestamp buffer length */
45 #define TIMESTAMP_LEN   64
46
47 /* Number of seconds per day */
48 #define SEC_PER_DAY     3600 * 24
49
50 /* Maximum number of CPUs */
51 #if defined(__CPU_SETSIZE) && __CPU_SETSIZE > 8192
52 #define NR_CPUS         __CPU_SETSIZE
53 #else
54 #define NR_CPUS         8192
55 #endif
56
57 /* Maximum number of interrupts */
58 #define NR_IRQS         4096
59
60 /* Size of /proc/interrupts line, CPU data excluded */
61 #define INTERRUPTS_LINE 128
62
63 /* Keywords */
64 #define K_ISO           "ISO"
65 #define K_ALL           "ALL"
66 #define K_LOWERALL      "all"
67 #define K_UTC           "UTC"
68 #define K_JSON          "JSON"
69
70 /* Files */
71 #define __DISKSTATS             "diskstats"
72 #define __BLOCK                 "block"
73 #define __DEV_BLOCK             "dev/block"
74 #define SLASH_SYS               PRE "/sys"
75 #define SLASH_DEV               PRE "/dev/"
76 #define STAT                    PRE "/proc/stat"
77 #define UPTIME                  PRE "/proc/uptime"
78 #define DISKSTATS               PRE "/proc/" __DISKSTATS
79 #define INTERRUPTS              PRE "/proc/interrupts"
80 #define MEMINFO                 PRE "/proc/meminfo"
81 #define SYSFS_BLOCK             SLASH_SYS "/" __BLOCK
82 #define SYSFS_DEV_BLOCK         SLASH_SYS "/" __DEV_BLOCK
83 #define SYSFS_DEVCPU            PRE "/sys/devices/system/cpu"
84 #define SYSFS_TIME_IN_STATE     "cpufreq/stats/time_in_state"
85 #define S_STAT                  "stat"
86 #define DEVMAP_DIR              PRE "/dev/mapper"
87 #define DEVICES                 PRE "/proc/devices"
88 #define SYSFS_USBDEV            PRE "/sys/bus/usb/devices"
89 #define DEV_DISK_BY             PRE "/dev/disk/by"
90 #define DEV_DISK_BY_ID          PRE "/dev/disk/by-id"
91 #define SYSFS_IDVENDOR          "idVendor"
92 #define SYSFS_IDPRODUCT         "idProduct"
93 #define SYSFS_BMAXPOWER         "bMaxPower"
94 #define SYSFS_MANUFACTURER      "manufacturer"
95 #define SYSFS_PRODUCT           "product"
96 #define SYSFS_FCHOST            PRE "/sys/class/fc_host"
97
98 #define MAX_FILE_LEN            512
99 #define MAX_PF_NAME             1024
100 #define MAX_NAME_LEN            128
101
102 #define IGNORE_VIRTUAL_DEVICES  FALSE
103 #define ACCEPT_VIRTUAL_DEVICES  TRUE
104
105 /* Environment variables */
106 #define ENV_TIME_FMT            "S_TIME_FORMAT"
107 #define ENV_TIME_DEFTM          "S_TIME_DEF_TIME"
108 #define ENV_COLORS              "S_COLORS"
109 #define ENV_COLORS_SGR          "S_COLORS_SGR"
110
111 #define C_NEVER                 "never"
112 #define C_ALWAYS                "always"
113
114 #define DIGITS                  "0123456789"
115
116 /*
117  ***************************************************************************
118  * Macro functions definitions.
119  ***************************************************************************
120  */
121
122 /*
123  * Macro used to define activity bitmap size.
124  * All those bitmaps have an additional bit used for global activity
125  * (eg. CPU "all" or total number of interrupts). That's why we do "(m) + 1".
126  */
127 #define BITMAP_SIZE(m)  ((((m) + 1) >> 3) + 1)
128
129 /* Allocate and init structure */
130 #define SREALLOC(S, TYPE, SIZE) do {                                                             \
131                                         TYPE *_p_ = S;                                           \
132                                         if ((SIZE) != 0) {                                       \
133                                                 if ((S = (TYPE *) realloc(S, (SIZE))) == NULL) { \
134                                                         perror("realloc");                       \
135                                                         exit(4);                                 \
136                                                 }                                                \
137                                                 /* If the ptr was null, then it's a malloc() */  \
138                                                 if (!_p_) {                                      \
139                                                         memset(S, 0, (SIZE));                    \
140                                                 }                                                \
141                                         }                                                        \
142                                         if (!S) {                                                \
143                                                 /* Should never happen */                        \
144                                                 fprintf(stderr, "srealloc\n");                   \
145                                                 exit(4);                                         \
146                                         }                                                        \
147                                 } while (0)
148
149 /*
150  * Macros used to display statistics values.
151  *
152  */
153 /* With S_VALUE macro, the interval of time (@p) is given in 1/100th of a second */
154 #define S_VALUE(m,n,p)          (((double) ((n) - (m))) / (p) * 100)
155 /* Define SP_VALUE() to normalize to % */
156 #define SP_VALUE(m,n,p)         (((double) ((n) - (m))) / (p) * 100)
157
158 /*
159  * Under very special circumstances, STDOUT may become unavailable.
160  * This is what we try to guess here.
161  */
162 #define TEST_STDOUT(_fd_)       do {                                    \
163                                         if (write(_fd_, "", 0) == -1) { \
164                                                 perror("stdout");       \
165                                                 exit(6);                \
166                                         }                               \
167                                 } while (0)
168
169 #define MINIMUM(a,b)    ((a) < (b) ? (a) : (b))
170
171 #define PANIC(m)        sysstat_panic(__FUNCTION__, m)
172
173 /* Number of ticks per second */
174 #define HZ              hz
175 extern unsigned long hz;
176
177 /* Number of bit shifts to convert pages to kB */
178 extern unsigned int kb_shift;
179
180 /*
181  * kB <-> number of pages.
182  * Page size depends on machine architecture (4 kB, 8 kB, 16 kB, 64 kB...)
183  */
184 #define KB_TO_PG(k)     ((k) >> kb_shift)
185 #define PG_TO_KB(k)     ((k) << kb_shift)
186
187 /* Type of persistent device names used in sar and iostat */
188 extern char persistent_name_type[MAX_FILE_LEN];
189
190 /*
191  ***************************************************************************
192  * Colors definitions
193  ***************************************************************************
194  */
195
196 #define C_LIGHT_RED     "\e[31;22m"
197 #define C_BOLD_RED      "\e[31;1m"
198 #define C_LIGHT_GREEN   "\e[32;22m"
199 #define C_LIGHT_YELLOW  "\e[33;22m"
200 #define C_BOLD_MAGENTA  "\e[35;1m"
201 #define C_BOLD_BLUE     "\e[34;1m"
202 #define C_LIGHT_BLUE    "\e[34;22m"
203 #define C_NORMAL        "\e[0m"
204
205 #define PERCENT_LIMIT_HIGH      75.0
206 #define PERCENT_LIMIT_LOW       50.0
207
208 #define MAX_SGR_LEN     16
209
210 #define IS_INT          0
211 #define IS_STR          1
212 #define IS_RESTART      2
213 #define IS_COMMENT      3
214 #define IS_ZERO         4
215
216 /*
217  ***************************************************************************
218  * Structures definitions
219  ***************************************************************************
220  */
221
222 /* Structure used for extended disk statistics */
223 struct ext_disk_stats {
224         double util;
225         double await;
226         double arqsz;
227 };
228
229 /*
230  ***************************************************************************
231  * Functions prototypes
232  ***************************************************************************
233  */
234 void print_version
235         (void);
236 void get_HZ
237         (void);
238 void get_kb_shift
239         (void);
240 time_t get_localtime
241         (struct tm *, int);
242 time_t get_time
243         (struct tm *, int);
244 void init_nls
245         (void);
246 int is_device
247         (char *, char *, int);
248 void sysstat_panic
249         (const char *, int);
250 int extract_wwnid
251         (char *, unsigned long long *, unsigned int *);
252 int get_wwnid_from_pretty
253         (char *, unsigned long long *, unsigned int *);
254 int check_dir
255         (char *);
256
257 #ifndef SOURCE_SADC
258 int count_bits
259         (void *, int);
260 int count_csvalues
261         (int, char **);
262 void cprintf_f
263         (int, int, int, int, ...);
264 void cprintf_in
265         (int, char *, char *, int);
266 void cprintf_pc
267         (int, int, int, int, ...);
268 void cprintf_s
269         (int, char *, char *);
270 void cprintf_u64
271         (int, int, int, ...);
272 void cprintf_x
273         (int, int, ...);
274 char *device_name
275         (char *);
276 char *get_device_name
277         (unsigned int, unsigned int, unsigned long long [],
278          unsigned int, unsigned int, unsigned int, unsigned int, char *);
279 unsigned int get_devmap_major
280         (void);
281 unsigned long long get_interval
282         (unsigned long long, unsigned long long);
283 char *get_persistent_name_from_pretty
284         (char *);
285 char *get_persistent_type_dir
286         (char *);
287 char *get_pretty_name_from_persistent
288         (char *);
289 int get_sysfs_dev_nr
290         (int);
291 int get_win_height
292         (void);
293 void init_colors
294         (void);
295 double ll_sp_value
296         (unsigned long long, unsigned long long, unsigned long long);
297 int is_iso_time_fmt
298         (void);
299 int parse_values
300         (char *, unsigned char[], int, const char *);
301 int print_gal_header
302         (struct tm *, char *, char *, char *, char *, int, int);
303 int set_report_date
304         (struct tm *, char[], int);
305 char *strtolower
306         (char *);
307 void xprintf
308         (int, const char *, ...);
309 void xprintf0
310         (int, const char *, ...);
311
312 #endif /* SOURCE_SADC undefined */
313 #endif  /* _COMMON_H */