]> granicus.if.org Git - sysstat/blob - common.h
6b58aa5887e71d03c66258e63ecd9d474859a300
[sysstat] / common.h
1 /*
2  * sysstat: System performance tools for Linux
3  * (C) 1999-2016 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
16 #ifdef HAVE_SYS_SYSMACROS_H
17 /* Needed on some non-glibc environments */
18 #include <sys/sysmacros.h>
19 #endif
20
21 #include "rd_stats.h"
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 /* Timestamp buffer length */
37 #define TIMESTAMP_LEN   64
38
39 /* Number of seconds per day */
40 #define SEC_PER_DAY     3600 * 24
41
42 /* Maximum number of CPUs */
43 #if defined(__CPU_SETSIZE) && __CPU_SETSIZE > 8192
44 #define NR_CPUS         __CPU_SETSIZE
45 #else
46 #define NR_CPUS         8192
47 #endif
48
49 /* Maximum number of interrupts */
50 #define NR_IRQS         1024
51
52 /* Size of /proc/interrupts line, CPU data excluded */
53 #define INTERRUPTS_LINE 128
54
55 /* Keywords */
56 #define K_ISO   "ISO"
57 #define K_ALL   "ALL"
58 #define K_UTC   "UTC"
59 #define K_JSON  "JSON"
60
61 /* Files */
62 #define STAT                    "/proc/stat"
63 #define UPTIME                  "/proc/uptime"
64 #define DISKSTATS               "/proc/diskstats"
65 #define INTERRUPTS              "/proc/interrupts"
66 #define MEMINFO                 "/proc/meminfo"
67 #define SYSFS_BLOCK             "/sys/block"
68 #define SYSFS_DEV_BLOCK         "/sys/dev/block"
69 #define SYSFS_DEVCPU            "/sys/devices/system/cpu"
70 #define SYSFS_TIME_IN_STATE     "cpufreq/stats/time_in_state"
71 #define S_STAT                  "stat"
72 #define DEVMAP_DIR              "/dev/mapper"
73 #define DEVICES                 "/proc/devices"
74 #define SYSFS_USBDEV            "/sys/bus/usb/devices"
75 #define DEV_DISK_BY             "/dev/disk/by"
76 #define SYSFS_IDVENDOR          "idVendor"
77 #define SYSFS_IDPRODUCT         "idProduct"
78 #define SYSFS_BMAXPOWER         "bMaxPower"
79 #define SYSFS_MANUFACTURER      "manufacturer"
80 #define SYSFS_PRODUCT           "product"
81 #define SYSFS_FCHOST            "/sys/class/fc_host"
82
83 #define MAX_FILE_LEN            256
84 #define MAX_PF_NAME             1024
85 #define MAX_NAME_LEN            128
86
87 #define IGNORE_VIRTUAL_DEVICES  FALSE
88 #define ACCEPT_VIRTUAL_DEVICES  TRUE
89
90 /* Environment variables */
91 #define ENV_TIME_FMT            "S_TIME_FORMAT"
92 #define ENV_TIME_DEFTM          "S_TIME_DEF_TIME"
93 #define ENV_COLORS              "S_COLORS"
94 #define ENV_COLORS_SGR          "S_COLORS_SGR"
95
96 #define C_NEVER                 "never"
97 #define C_ALWAYS                "always"
98
99 #define DIGITS                  "0123456789"
100
101 /*
102  ***************************************************************************
103  * Macro functions definitions.
104  ***************************************************************************
105  */
106
107 /* Allocate and init structure */
108 #define SREALLOC(S, TYPE, SIZE) do {                                                             \
109                                         TYPE *_p_;                                               \
110                                         _p_ = S;                                                 \
111                                         if (SIZE) {                                              \
112                                                 if ((S = (TYPE *) realloc(S, (SIZE))) == NULL) { \
113                                                         perror("realloc");                       \
114                                                         exit(4);                                 \
115                                                 }                                                \
116                                                 /* If the ptr was null, then it's a malloc() */  \
117                                                 if (!_p_) {                                      \
118                                                         memset(S, 0, (SIZE));                    \
119                                                 }                                                \
120                                         }                                                        \
121                                         if (!S) {                                                \
122                                                 /* Should never happen */                        \
123                                                 fprintf(stderr, "srealloc\n");                   \
124                                                 exit(4);                                         \
125                                         }                                                        \
126                                 } while (0)
127
128 /*
129  * Macros used to display statistics values.
130  *
131  * NB: Define SP_VALUE() to normalize to %;
132  * HZ is 1024 on IA64 and % should be normalized to 100.
133  * SP_VALUE_100() will not output value bigger than 100; this is needed for some
134  * corner cases, should be used with care.
135  */
136 #define S_VALUE(m,n,p)          (((double) ((n) - (m))) / (p) * HZ)
137 #define SP_VALUE(m,n,p)         (((double) ((n) - (m))) / (p) * 100)
138 #define SP_VALUE_100(m,n,p)     MINIMUM((((double) ((n) - (m))) / (p) * 100), 100.0)
139
140 /*
141  * Under very special circumstances, STDOUT may become unavailable.
142  * This is what we try to guess here.
143  */
144 #define TEST_STDOUT(_fd_)       do {                                    \
145                                         if (write(_fd_, "", 0) == -1) { \
146                                                 perror("stdout");       \
147                                                 exit(6);                \
148                                         }                               \
149                                 } while (0)
150
151 #define MINIMUM(a,b)    ((a) < (b) ? (a) : (b))
152
153 #define PANIC(m)        sysstat_panic(__FUNCTION__, m)
154
155 /* Number of ticks per second */
156 #define HZ              hz
157 extern unsigned int hz;
158
159 /* Number of bit shifts to convert pages to kB */
160 extern unsigned int kb_shift;
161
162 /*
163  * kB <-> number of pages.
164  * Page size depends on machine architecture (4 kB, 8 kB, 16 kB, 64 kB...)
165  */
166 #define KB_TO_PG(k)     ((k) >> kb_shift)
167 #define PG_TO_KB(k)     ((k) << kb_shift)
168
169 /* Type of persistent device names used in sar and iostat */
170 extern char persistent_name_type[MAX_FILE_LEN];
171
172 /*
173  ***************************************************************************
174  * Colors definitions
175  ***************************************************************************
176  */
177
178 #define C_LIGHT_RED     "\e[31;22m"
179 #define C_BOLD_RED      "\e[31;1m"
180 #define C_LIGHT_GREEN   "\e[32;22m"
181 #define C_LIGHT_YELLOW  "\e[33;22m"
182 #define C_BOLD_YELLOW   "\e[33;1m"
183 #define C_BOLD_BLUE     "\e[34;1m"
184 #define C_LIGHT_CYAN    "\e[36;22m"
185 #define C_NORMAL        "\e[0m"
186
187 #define PERCENT_LIMIT_HIGH      75.0
188 #define PERCENT_LIMIT_LOW       50.0
189
190 #define MAX_SGR_LEN     16
191
192 #define IS_INT          0
193 #define IS_STR          1
194 #define IS_RESTART      2
195 #define IS_COMMENT      3
196 #define IS_ZERO         4
197
198 /*
199  ***************************************************************************
200  * Structures definitions
201  ***************************************************************************
202  */
203
204 /* Structure used for extended disk statistics */
205 struct ext_disk_stats {
206         double util;
207         double await;
208         double svctm;
209         double arqsz;
210 };
211
212 /*
213  ***************************************************************************
214  * Functions prototypes
215  ***************************************************************************
216  */
217
218 void compute_ext_disk_stats
219         (struct stats_disk *, struct stats_disk *, unsigned long long,
220          struct ext_disk_stats *);
221 int count_bits
222         (void *, int);
223 int count_csvalues
224         (int, char **);
225 void cprintf_f
226         (int, int, int, ...);
227 void cprintf_in
228         (int, char *, char *, int);
229 void cprintf_pc
230         (int, int, int, ...);
231 void cprintf_s
232         (int, char *, char *);
233 void cprintf_u64
234         (int, int, ...);
235 void cprintf_x
236         (int, int, ...);
237 char *device_name
238         (char *);
239 void get_HZ
240         (void);
241 unsigned int get_devmap_major
242         (void);
243 unsigned long long get_interval
244         (unsigned long long, unsigned long long);
245 void get_kb_shift
246         (void);
247 time_t get_localtime
248         (struct tm *, int);
249 time_t get_time
250         (struct tm *, int);
251 unsigned long long get_per_cpu_interval
252         (struct stats_cpu *, struct stats_cpu *);
253 char *get_persistent_name_from_pretty
254         (char *);
255 char *get_persistent_type_dir
256         (char *);
257 char *get_pretty_name_from_persistent
258         (char *);
259 int get_sysfs_dev_nr
260         (int);
261 int get_win_height
262         (void);
263 void init_colors
264         (void);
265 void init_nls
266         (void);
267 int is_device
268         (char *, int);
269 double ll_sp_value
270         (unsigned long long, unsigned long long, unsigned long long);
271 int is_iso_time_fmt
272         (void);
273 int print_gal_header
274         (struct tm *, char *, char *, char *, char *, int, int);
275 void print_version
276         (void);
277 char *strtolower
278         (char *);
279 void sysstat_panic
280         (const char *, int);
281 void xprintf
282         (int, const char *, ...);
283 void xprintf0
284         (int, const char *, ...);
285
286 #endif  /* _COMMON_H */