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