xref: /6.6.0/sigar/include/sigar_util.h (revision 829a4c03)
1/*
2 * Copyright (c) 2004-2008 Hyperic, Inc.
3 * Copyright (c) 2009 SpringSource, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef SIGAR_UTIL_H
19#define SIGAR_UTIL_H
20
21/* most of this is crap for dealing with linux /proc */
22#define UITOA_BUFFER_SIZE \
23    (sizeof(int) * 3 + 1)
24
25#define SSTRLEN(s) \
26    (sizeof(s)-1)
27
28#define sigar_strtoul(ptr) \
29    strtoul(ptr, &ptr, 10)
30
31#define sigar_strtoull(ptr) \
32    strtoull(ptr, &ptr, 10)
33
34#define sigar_isspace(c) \
35    (isspace(((unsigned char)(c))))
36
37#define sigar_isdigit(c) \
38    (isdigit(((unsigned char)(c))))
39
40#define sigar_isalpha(c) \
41    (isalpha(((unsigned char)(c))))
42
43#define sigar_isupper(c) \
44    (isupper(((unsigned char)(c))))
45
46#define sigar_tolower(c) \
47    (tolower(((unsigned char)(c))))
48
49#ifdef WIN32
50#define sigar_fileno _fileno
51#define sigar_isatty _isatty
52#define sigar_write  _write
53#else
54#define sigar_fileno fileno
55#define sigar_isatty isatty
56#define sigar_write  write
57#endif
58
59#ifndef PROC_FS_ROOT
60#define PROC_FS_ROOT "/proc/"
61#endif
62
63#ifndef PROCP_FS_ROOT
64#define PROCP_FS_ROOT "/proc/"
65#endif
66
67sigar_int64_t sigar_time_now_millis(void);
68
69char *sigar_uitoa(char *buf, unsigned int n, int *len);
70
71int sigar_inet_ntoa(sigar_t *sigar,
72                    sigar_uint32_t address,
73                    char *addr_str);
74
75struct hostent *sigar_gethostbyname(const char *name,
76                                    sigar_hostent_t *data);
77
78char *sigar_skip_line(char *buffer, int buflen);
79
80char *sigar_skip_token(char *p);
81
82char *sigar_skip_multiple_token(char *p, int count);
83
84char *sigar_getword(char **line, char stop);
85
86int sigar_file2str(const char *fname, char *buffer, int buflen);
87
88int sigar_proc_file2str(char *buffer, int buflen,
89                        sigar_pid_t pid,
90                        const char *fname,
91                        int fname_len);
92
93#define SIGAR_PROC_FILE2STR(buffer, pid, fname) \
94    sigar_proc_file2str(buffer, sizeof(buffer), \
95                        pid, fname, SSTRLEN(fname))
96
97#define SIGAR_PROC_FILENAME(buffer, pid, fname) \
98    sigar_proc_filename(buffer, sizeof(buffer), \
99                        pid, fname, SSTRLEN(fname))
100
101#define SIGAR_SKIP_SPACE(ptr) \
102    while (sigar_isspace(*ptr)) ++ptr
103
104char *sigar_proc_filename(char *buffer, int buflen,
105                          sigar_pid_t pid,
106                          const char *fname, int fname_len);
107
108int sigar_proc_list_procfs_get(sigar_t *sigar,
109                               sigar_proc_list_t *proclist);
110
111int sigar_proc_fd_count(sigar_t *sigar, sigar_pid_t pid,
112                        sigar_uint64_t *total);
113
114/* linux + freebsd */
115
116int sigar_mem_calc_ram(sigar_t *sigar, sigar_mem_t *mem);
117
118int sigar_statvfs(sigar_t *sigar,
119                  const char *dirname,
120                  sigar_file_system_usage_t *fsusage);
121
122double sigar_file_system_usage_calc_used(sigar_t *sigar,
123                                         sigar_file_system_usage_t *fs);
124
125#define SIGAR_DEV_PREFIX "/dev/"
126
127#define SIGAR_NAME_IS_DEV(dev) \
128    strnEQ(dev, SIGAR_DEV_PREFIX, SSTRLEN(SIGAR_DEV_PREFIX))
129
130typedef struct {
131    char name[256];
132    int is_partition;
133    sigar_disk_usage_t disk;
134} sigar_iodev_t;
135
136sigar_iodev_t *sigar_iodev_get(sigar_t *sigar,
137                               const char *dirname);
138
139int sigar_cpu_core_count(sigar_t *sigar);
140
141/* e.g. VM guest may have 1 virtual ncpu on multicore hosts */
142#define sigar_cpu_socket_count(sigar) \
143    (sigar->ncpu < sigar->lcpu) ? sigar->ncpu : \
144    (sigar->ncpu / sigar->lcpu)
145
146int sigar_cpu_core_rollup(sigar_t *sigar);
147
148void sigar_cpu_model_adjust(sigar_t *sigar, sigar_cpu_info_t *info);
149
150int sigar_cpu_mhz_from_model(char *model);
151
152
153#if defined(__sun) || defined(__FreeBSD__)
154
155#define SIGAR_HAS_DLINFO_MODULES
156#include <dlfcn.h>
157#include <link.h>
158
159int sigar_dlinfo_modules(sigar_t *sigar, sigar_proc_modules_t *procmods);
160#endif
161
162typedef struct sigar_cache_entry_t sigar_cache_entry_t;
163
164struct sigar_cache_entry_t {
165    sigar_cache_entry_t *next;
166    sigar_uint64_t id;
167    void *value;
168};
169
170typedef struct {
171    sigar_cache_entry_t **entries;
172    unsigned int count, size;
173    void (*free_value)(void *ptr);
174} sigar_cache_t;
175
176sigar_cache_t *sigar_cache_new(int size);
177
178sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
179                                     sigar_uint64_t key);
180
181sigar_cache_entry_t *sigar_cache_find(sigar_cache_t *table,
182                                      sigar_uint64_t key);
183
184void sigar_cache_destroy(sigar_cache_t *table);
185
186#endif /* SIGAR_UTIL_H */
187