1 | /* $Id: PerformanceSolaris.cpp 43831 2012-11-07 13:11:51Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VBox Solaris-specific Performance Classes implementation.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.215389.xyz. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #undef _FILE_OFFSET_BITS
|
---|
21 | #include <procfs.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <errno.h>
|
---|
24 | #include <fcntl.h>
|
---|
25 | #include <kstat.h>
|
---|
26 | #include <unistd.h>
|
---|
27 | #include <sys/sysinfo.h>
|
---|
28 | #include <sys/time.h>
|
---|
29 | #include <sys/types.h>
|
---|
30 | #include <sys/statvfs.h>
|
---|
31 |
|
---|
32 | #include <iprt/ctype.h>
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/path.h>
|
---|
38 | #include <VBox/log.h>
|
---|
39 | #include "Performance.h"
|
---|
40 |
|
---|
41 | #include <dlfcn.h>
|
---|
42 |
|
---|
43 | #include <libzfs.h>
|
---|
44 | #include <libnvpair.h>
|
---|
45 |
|
---|
46 | #include <map>
|
---|
47 |
|
---|
48 | namespace pm {
|
---|
49 |
|
---|
50 | typedef libzfs_handle_t *(*PFNZFSINIT)(void);
|
---|
51 | typedef zfs_handle_t *(*PFNZFSOPEN)(libzfs_handle_t *, const char *, int);
|
---|
52 | typedef void (*PFNZFSCLOSE)(zfs_handle_t *);
|
---|
53 | typedef uint64_t (*PFNZFSPROPGETINT)(zfs_handle_t *, zfs_prop_t);
|
---|
54 | typedef zpool_handle_t *(*PFNZPOOLOPEN)(libzfs_handle_t *, const char *);
|
---|
55 | typedef void (*PFNZPOOLCLOSE)(zpool_handle_t *);
|
---|
56 | typedef nvlist_t *(*PFNZPOOLGETCONFIG)(zpool_handle_t *, nvlist_t **);
|
---|
57 | typedef char *(*PFNZPOOLVDEVNAME)(libzfs_handle_t *, zpool_handle_t *, nvlist_t *, boolean_t);
|
---|
58 |
|
---|
59 | typedef std::map<RTCString,RTCString> FsMap;
|
---|
60 |
|
---|
61 | class CollectorSolaris : public CollectorHAL
|
---|
62 | {
|
---|
63 | public:
|
---|
64 | CollectorSolaris();
|
---|
65 | virtual ~CollectorSolaris();
|
---|
66 | virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
|
---|
67 | virtual int getHostFilesystemUsage(const char *name, ULONG *total, ULONG *used, ULONG *available);
|
---|
68 | virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
|
---|
69 |
|
---|
70 | virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
|
---|
71 | virtual int getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx);
|
---|
72 | virtual int getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms);
|
---|
73 | virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
|
---|
74 |
|
---|
75 | virtual int getDiskListByFs(const char *name, DiskList& list);
|
---|
76 | private:
|
---|
77 | static uint32_t getInstance(const char *pszIfaceName, char *pszDevName);
|
---|
78 | uint64_t getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName);
|
---|
79 | void updateFilesystemMap(void);
|
---|
80 | RTCString physToInstName(const char *pcszPhysName);
|
---|
81 |
|
---|
82 | kstat_ctl_t *mKC;
|
---|
83 | kstat_t *mSysPages;
|
---|
84 | kstat_t *mZFSCache;
|
---|
85 |
|
---|
86 | void *mZfsSo;
|
---|
87 | libzfs_handle_t *mZfsLib;
|
---|
88 | PFNZFSINIT mZfsInit;
|
---|
89 | PFNZFSOPEN mZfsOpen;
|
---|
90 | PFNZFSCLOSE mZfsClose;
|
---|
91 | PFNZFSPROPGETINT mZfsPropGetInt;
|
---|
92 | PFNZPOOLOPEN mZpoolOpen;
|
---|
93 | PFNZPOOLCLOSE mZpoolClose;
|
---|
94 | PFNZPOOLGETCONFIG mZpoolGetConfig;
|
---|
95 | PFNZPOOLVDEVNAME mZpoolVdevName;
|
---|
96 |
|
---|
97 | FsMap mFsMap;
|
---|
98 | };
|
---|
99 |
|
---|
100 | CollectorHAL *createHAL()
|
---|
101 | {
|
---|
102 | return new CollectorSolaris();
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Collector HAL for Solaris
|
---|
106 |
|
---|
107 |
|
---|
108 | CollectorSolaris::CollectorSolaris()
|
---|
109 | : mKC(0),
|
---|
110 | mSysPages(0),
|
---|
111 | mZFSCache(0),
|
---|
112 | mZfsLib(0)
|
---|
113 | {
|
---|
114 | if ((mKC = kstat_open()) == 0)
|
---|
115 | {
|
---|
116 | Log(("kstat_open() -> %d\n", errno));
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if ((mSysPages = kstat_lookup(mKC, (char *)"unix", 0, (char *)"system_pages")) == 0)
|
---|
121 | {
|
---|
122 | Log(("kstat_lookup(system_pages) -> %d\n", errno));
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if ((mZFSCache = kstat_lookup(mKC, (char *)"zfs", 0, (char *)"arcstats")) == 0)
|
---|
127 | {
|
---|
128 | Log(("kstat_lookup(system_pages) -> %d\n", errno));
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Try to load libzfs dynamically, it may be missing. */
|
---|
132 | mZfsSo = dlopen("libzfs.so", RTLD_LAZY);
|
---|
133 | if (mZfsSo)
|
---|
134 | {
|
---|
135 | mZfsInit = (PFNZFSINIT)dlsym(mZfsSo, "libzfs_init");
|
---|
136 | mZfsOpen = (PFNZFSOPEN)dlsym(mZfsSo, "zfs_open");
|
---|
137 | mZfsClose = (PFNZFSCLOSE)dlsym(mZfsSo, "zfs_close");
|
---|
138 | mZfsPropGetInt = (PFNZFSPROPGETINT)dlsym(mZfsSo, "zfs_prop_get_int");
|
---|
139 | mZpoolOpen = (PFNZPOOLOPEN)dlsym(mZfsSo, "zpool_open");
|
---|
140 | mZpoolClose = (PFNZPOOLCLOSE)dlsym(mZfsSo, "zpool_close");
|
---|
141 | mZpoolGetConfig = (PFNZPOOLGETCONFIG)dlsym(mZfsSo, "zpool_get_config");
|
---|
142 | mZpoolVdevName = (PFNZPOOLVDEVNAME)dlsym(mZfsSo, "zpool_vdev_name");
|
---|
143 |
|
---|
144 | if (mZfsInit && mZfsOpen && mZfsClose && mZfsPropGetInt
|
---|
145 | && mZpoolOpen && mZpoolClose && mZpoolGetConfig && mZpoolVdevName)
|
---|
146 | mZfsLib = mZfsInit();
|
---|
147 | else
|
---|
148 | LogRel(("Incompatible libzfs? libzfs_init=%p zfs_open=%p zfs_close=%p zfs_prop_get_int=%p\n",
|
---|
149 | mZfsInit, mZfsOpen, mZfsClose, mZfsPropGetInt));
|
---|
150 | }
|
---|
151 |
|
---|
152 | updateFilesystemMap();
|
---|
153 | }
|
---|
154 |
|
---|
155 | CollectorSolaris::~CollectorSolaris()
|
---|
156 | {
|
---|
157 | if (mKC)
|
---|
158 | kstat_close(mKC);
|
---|
159 | if (mZfsSo)
|
---|
160 | dlclose(mZfsSo);
|
---|
161 | }
|
---|
162 |
|
---|
163 | int CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
|
---|
164 | {
|
---|
165 | int rc = VINF_SUCCESS;
|
---|
166 | kstat_t *ksp;
|
---|
167 | uint64_t tmpUser, tmpKernel, tmpIdle;
|
---|
168 | int cpus;
|
---|
169 | cpu_stat_t cpu_stats;
|
---|
170 |
|
---|
171 | if (mKC == 0)
|
---|
172 | return VERR_INTERNAL_ERROR;
|
---|
173 |
|
---|
174 | tmpUser = tmpKernel = tmpIdle = cpus = 0;
|
---|
175 | for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
|
---|
176 | if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
|
---|
177 | if (kstat_read(mKC, ksp, &cpu_stats) == -1)
|
---|
178 | {
|
---|
179 | Log(("kstat_read() -> %d\n", errno));
|
---|
180 | return VERR_INTERNAL_ERROR;
|
---|
181 | }
|
---|
182 | ++cpus;
|
---|
183 | tmpUser += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
|
---|
184 | tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
|
---|
185 | tmpIdle += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (cpus == 0)
|
---|
190 | {
|
---|
191 | Log(("no cpu stats found!\n"));
|
---|
192 | return VERR_INTERNAL_ERROR;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (user) *user = tmpUser;
|
---|
196 | if (kernel) *kernel = tmpKernel;
|
---|
197 | if (idle) *idle = tmpIdle;
|
---|
198 |
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | int CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
|
---|
203 | {
|
---|
204 | int rc = VINF_SUCCESS;
|
---|
205 | char *pszName;
|
---|
206 | prusage_t prusage;
|
---|
207 |
|
---|
208 | RTStrAPrintf(&pszName, "/proc/%d/usage", process);
|
---|
209 | Log(("Opening %s...\n", pszName));
|
---|
210 | int h = open(pszName, O_RDONLY);
|
---|
211 | RTStrFree(pszName);
|
---|
212 |
|
---|
213 | if (h != -1)
|
---|
214 | {
|
---|
215 | if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
|
---|
216 | {
|
---|
217 | //Assert((pid_t)process == pstatus.pr_pid);
|
---|
218 | //Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
|
---|
219 | *user = (uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec;
|
---|
220 | *kernel = (uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec;
|
---|
221 | *total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
|
---|
222 | //Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | Log(("read() -> %d\n", errno));
|
---|
227 | rc = VERR_FILE_IO_ERROR;
|
---|
228 | }
|
---|
229 | close(h);
|
---|
230 | }
|
---|
231 | else
|
---|
232 | {
|
---|
233 | Log(("open() -> %d\n", errno));
|
---|
234 | rc = VERR_ACCESS_DENIED;
|
---|
235 | }
|
---|
236 |
|
---|
237 | return rc;
|
---|
238 | }
|
---|
239 |
|
---|
240 | int CollectorSolaris::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
|
---|
241 | {
|
---|
242 | int rc = VINF_SUCCESS;
|
---|
243 |
|
---|
244 | kstat_named_t *kn;
|
---|
245 |
|
---|
246 | if (mKC == 0 || mSysPages == 0)
|
---|
247 | return VERR_INTERNAL_ERROR;
|
---|
248 |
|
---|
249 | if (kstat_read(mKC, mSysPages, 0) == -1)
|
---|
250 | {
|
---|
251 | Log(("kstat_read(sys_pages) -> %d\n", errno));
|
---|
252 | return VERR_INTERNAL_ERROR;
|
---|
253 | }
|
---|
254 | if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, (char *)"freemem")) == 0)
|
---|
255 | {
|
---|
256 | Log(("kstat_data_lookup(freemem) -> %d\n", errno));
|
---|
257 | return VERR_INTERNAL_ERROR;
|
---|
258 | }
|
---|
259 | *available = kn->value.ul * (PAGE_SIZE/1024);
|
---|
260 |
|
---|
261 | if (kstat_read(mKC, mZFSCache, 0) != -1)
|
---|
262 | {
|
---|
263 | if (mZFSCache)
|
---|
264 | {
|
---|
265 | if ((kn = (kstat_named_t *)kstat_data_lookup(mZFSCache, (char *)"size")))
|
---|
266 | {
|
---|
267 | ulong_t ulSize = kn->value.ul;
|
---|
268 |
|
---|
269 | if ((kn = (kstat_named_t *)kstat_data_lookup(mZFSCache, (char *)"c_min")))
|
---|
270 | {
|
---|
271 | /*
|
---|
272 | * Account for ZFS minimum arc cache size limit.
|
---|
273 | * "c_min" is the target minimum size of the ZFS cache, and not the hard limit. It's possible
|
---|
274 | * for "size" to shrink below "c_min" (e.g: during boot & high memory consumption).
|
---|
275 | */
|
---|
276 | ulong_t ulMin = kn->value.ul;
|
---|
277 | *available += ulSize > ulMin ? (ulSize - ulMin) / 1024 : 0;
|
---|
278 | }
|
---|
279 | else
|
---|
280 | Log(("kstat_data_lookup(c_min) ->%d\n", errno));
|
---|
281 | }
|
---|
282 | else
|
---|
283 | Log(("kstat_data_lookup(size) -> %d\n", errno));
|
---|
284 | }
|
---|
285 | else
|
---|
286 | Log(("mZFSCache missing.\n"));
|
---|
287 | }
|
---|
288 |
|
---|
289 | if ((kn = (kstat_named_t *)kstat_data_lookup(mSysPages, (char *)"physmem")) == 0)
|
---|
290 | {
|
---|
291 | Log(("kstat_data_lookup(physmem) -> %d\n", errno));
|
---|
292 | return VERR_INTERNAL_ERROR;
|
---|
293 | }
|
---|
294 | *total = kn->value.ul * (PAGE_SIZE/1024);
|
---|
295 | *used = *total - *available;
|
---|
296 |
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 | int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
|
---|
301 | {
|
---|
302 | int rc = VINF_SUCCESS;
|
---|
303 | char *pszName = NULL;
|
---|
304 | psinfo_t psinfo;
|
---|
305 |
|
---|
306 | RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
|
---|
307 | Log(("Opening %s...\n", pszName));
|
---|
308 | int h = open(pszName, O_RDONLY);
|
---|
309 | RTStrFree(pszName);
|
---|
310 |
|
---|
311 | if (h != -1)
|
---|
312 | {
|
---|
313 | if (read(h, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
|
---|
314 | {
|
---|
315 | Assert((pid_t)process == psinfo.pr_pid);
|
---|
316 | *used = psinfo.pr_rssize;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | Log(("read() -> %d\n", errno));
|
---|
321 | rc = VERR_FILE_IO_ERROR;
|
---|
322 | }
|
---|
323 | close(h);
|
---|
324 | }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | Log(("open() -> %d\n", errno));
|
---|
328 | rc = VERR_ACCESS_DENIED;
|
---|
329 | }
|
---|
330 |
|
---|
331 | return rc;
|
---|
332 | }
|
---|
333 |
|
---|
334 | uint32_t CollectorSolaris::getInstance(const char *pszIfaceName, char *pszDevName)
|
---|
335 | {
|
---|
336 | /*
|
---|
337 | * Get the instance number from the interface name, then clip it off.
|
---|
338 | */
|
---|
339 | int cbInstance = 0;
|
---|
340 | int cbIface = strlen(pszIfaceName);
|
---|
341 | const char *pszEnd = pszIfaceName + cbIface - 1;
|
---|
342 | for (int i = 0; i < cbIface - 1; i++)
|
---|
343 | {
|
---|
344 | if (!RT_C_IS_DIGIT(*pszEnd))
|
---|
345 | break;
|
---|
346 | cbInstance++;
|
---|
347 | pszEnd--;
|
---|
348 | }
|
---|
349 |
|
---|
350 | uint32_t uInstance = RTStrToUInt32(pszEnd + 1);
|
---|
351 | strncpy(pszDevName, pszIfaceName, cbIface - cbInstance);
|
---|
352 | pszDevName[cbIface - cbInstance] = '\0';
|
---|
353 | return uInstance;
|
---|
354 | }
|
---|
355 |
|
---|
356 | int CollectorSolaris::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
|
---|
357 | {
|
---|
358 | AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
|
---|
359 | LogFlowThisFunc(("m=%s i=%d n=%s\n", "link", -1, name));
|
---|
360 | kstat_t *ksAdapter = kstat_lookup(mKC, "link", -1, (char *)name);
|
---|
361 | if (ksAdapter == 0)
|
---|
362 | {
|
---|
363 | char szModule[KSTAT_STRLEN];
|
---|
364 | uint32_t uInstance = getInstance(name, szModule);
|
---|
365 | LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, "phys"));
|
---|
366 | ksAdapter = kstat_lookup(mKC, szModule, uInstance, "phys");
|
---|
367 | if (ksAdapter == 0)
|
---|
368 | {
|
---|
369 | LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, name));
|
---|
370 | ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)name);
|
---|
371 | if (ksAdapter == 0)
|
---|
372 | {
|
---|
373 | LogRel(("Failed to get network statistics for %s\n", name));
|
---|
374 | return VERR_INTERNAL_ERROR;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | if (kstat_read(mKC, ksAdapter, 0) == -1)
|
---|
379 | {
|
---|
380 | LogRel(("kstat_read(adapter) -> %d\n", errno));
|
---|
381 | return VERR_INTERNAL_ERROR;
|
---|
382 | }
|
---|
383 | kstat_named_t *kn;
|
---|
384 | if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes")) == 0)
|
---|
385 | {
|
---|
386 | LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
|
---|
387 | return VERR_INTERNAL_ERROR;
|
---|
388 | }
|
---|
389 | *rx = kn->value.ul;
|
---|
390 | if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes")) == 0)
|
---|
391 | {
|
---|
392 | LogRel(("kstat_data_lookup(obytes) -> %d\n", errno));
|
---|
393 | return VERR_INTERNAL_ERROR;
|
---|
394 | }
|
---|
395 | *tx = kn->value.ul;
|
---|
396 | return VINF_SUCCESS;
|
---|
397 | }
|
---|
398 |
|
---|
399 | int CollectorSolaris::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
|
---|
400 | {
|
---|
401 | int rc = VINF_SUCCESS;
|
---|
402 | AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
|
---|
403 | LogFlowThisFunc(("n=%s\n", name));
|
---|
404 | kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, (char *)name);
|
---|
405 | if (ksDisk != 0)
|
---|
406 | {
|
---|
407 | if (kstat_read(mKC, ksDisk, 0) == -1)
|
---|
408 | {
|
---|
409 | LogRel(("kstat_read(%s) -> %d\n", name, errno));
|
---|
410 | rc = VERR_INTERNAL_ERROR;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | kstat_io_t *ksIo = KSTAT_IO_PTR(ksDisk);
|
---|
415 | /*
|
---|
416 | * We do not care for wrap possibility here, although we may
|
---|
417 | * reconsider in about 300 years (9223372036854775807 ns).
|
---|
418 | */
|
---|
419 | *disk_ms = ksIo->rtime / 1000000;
|
---|
420 | *total_ms = ksDisk->ks_snaptime / 1000000;
|
---|
421 | }
|
---|
422 | }
|
---|
423 | else
|
---|
424 | {
|
---|
425 | LogRel(("kstat_lookup(%s) -> %d\n", name, errno));
|
---|
426 | rc = VERR_INTERNAL_ERROR;
|
---|
427 | }
|
---|
428 |
|
---|
429 | return rc;
|
---|
430 | }
|
---|
431 |
|
---|
432 | uint64_t CollectorSolaris::getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName)
|
---|
433 | {
|
---|
434 | if (strcmp(szFsType, "zfs"))
|
---|
435 | return cbTotal;
|
---|
436 | FsMap::iterator it = mFsMap.find(szFsName);
|
---|
437 | if (it == mFsMap.end())
|
---|
438 | return cbTotal;
|
---|
439 |
|
---|
440 | char *pszDataset = strdup(it->second.c_str());
|
---|
441 | char *pszEnd = pszDataset + strlen(pszDataset);
|
---|
442 | uint64_t uAvail = 0;
|
---|
443 | while (pszEnd)
|
---|
444 | {
|
---|
445 | zfs_handle_t *hDataset;
|
---|
446 |
|
---|
447 | *pszEnd = 0;
|
---|
448 | hDataset = mZfsOpen(mZfsLib, pszDataset, ZFS_TYPE_DATASET);
|
---|
449 | if (!hDataset)
|
---|
450 | break;
|
---|
451 |
|
---|
452 | if (uAvail == 0)
|
---|
453 | {
|
---|
454 | uAvail = mZfsPropGetInt(hDataset, ZFS_PROP_REFQUOTA);
|
---|
455 | if (uAvail == 0)
|
---|
456 | uAvail = UINT64_MAX;
|
---|
457 | }
|
---|
458 |
|
---|
459 | uint64_t uQuota = mZfsPropGetInt(hDataset, ZFS_PROP_QUOTA);
|
---|
460 | if (uQuota && uAvail > uQuota)
|
---|
461 | uAvail = uQuota;
|
---|
462 |
|
---|
463 | pszEnd = strrchr(pszDataset, '/');
|
---|
464 | if (!pszEnd)
|
---|
465 | {
|
---|
466 | uint64_t uPoolSize = mZfsPropGetInt(hDataset, ZFS_PROP_USED) +
|
---|
467 | mZfsPropGetInt(hDataset, ZFS_PROP_AVAILABLE);
|
---|
468 | if (uAvail > uPoolSize)
|
---|
469 | uAvail = uPoolSize;
|
---|
470 | }
|
---|
471 | mZfsClose(hDataset);
|
---|
472 | }
|
---|
473 | free(pszDataset);
|
---|
474 |
|
---|
475 | return uAvail ? uAvail : cbTotal;
|
---|
476 | }
|
---|
477 |
|
---|
478 | int CollectorSolaris::getHostFilesystemUsage(const char *path, ULONG *total, ULONG *used, ULONG *available)
|
---|
479 | {
|
---|
480 | struct statvfs64 stats;
|
---|
481 | const unsigned _MB = 1024 * 1024;
|
---|
482 |
|
---|
483 | if (statvfs64(path, &stats) == -1)
|
---|
484 | {
|
---|
485 | LogRel(("Failed to collect %s filesystem usage: errno=%d.\n", path, errno));
|
---|
486 | return VERR_ACCESS_DENIED;
|
---|
487 | }
|
---|
488 | uint64_t cbBlock = stats.f_frsize ? stats.f_frsize : stats.f_bsize;
|
---|
489 | *total = (ULONG)(getZfsTotal(cbBlock * stats.f_blocks, stats.f_basetype, path) / _MB);
|
---|
490 | LogRel(("f_blocks=%llu.\n", stats.f_blocks));
|
---|
491 | *used = (ULONG)(cbBlock * (stats.f_blocks - stats.f_bfree) / _MB);
|
---|
492 | *available = (ULONG)(cbBlock * stats.f_bavail / _MB);
|
---|
493 |
|
---|
494 | return VINF_SUCCESS;
|
---|
495 | }
|
---|
496 |
|
---|
497 | RTCString CollectorSolaris::physToInstName(const char *pcszPhysName)
|
---|
498 | {
|
---|
499 | FILE *fp = fopen("/etc/path_to_inst", "r");
|
---|
500 | if (!fp)
|
---|
501 | return RTCString();
|
---|
502 |
|
---|
503 | RTCString strInstName;
|
---|
504 | size_t cbName = strlen(pcszPhysName);
|
---|
505 | char szBuf[RTPATH_MAX];
|
---|
506 | while (fgets(szBuf, sizeof(szBuf), fp))
|
---|
507 | {
|
---|
508 | if (szBuf[0] == '"' && strncmp(szBuf + 1, pcszPhysName, cbName) == 0)
|
---|
509 | {
|
---|
510 | char *pszDriver, *pszInstance;
|
---|
511 | pszDriver = strrchr(szBuf, '"');
|
---|
512 | if (pszDriver)
|
---|
513 | {
|
---|
514 | *pszDriver = '\0';
|
---|
515 | pszDriver = strrchr(szBuf, '"');
|
---|
516 | if (pszDriver)
|
---|
517 | {
|
---|
518 | *pszDriver++ = '\0';
|
---|
519 | pszInstance = strrchr(szBuf, ' ');
|
---|
520 | if (pszInstance)
|
---|
521 | {
|
---|
522 | *pszInstance = '\0';
|
---|
523 | pszInstance = strrchr(szBuf, ' ');
|
---|
524 | if (pszInstance)
|
---|
525 | {
|
---|
526 | *pszInstance++ = '\0';
|
---|
527 | strInstName = pszDriver;
|
---|
528 | strInstName += pszInstance;
|
---|
529 | break;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 | fclose(fp);
|
---|
537 |
|
---|
538 | return strInstName;
|
---|
539 | }
|
---|
540 |
|
---|
541 | int CollectorSolaris::getDiskListByFs(const char *name, DiskList& list)
|
---|
542 | {
|
---|
543 | FsMap::iterator it = mFsMap.find(name);
|
---|
544 | if (it == mFsMap.end())
|
---|
545 | return VERR_INVALID_PARAMETER;
|
---|
546 |
|
---|
547 | RTCString strName = it->second.substr(0, it->second.find("/"));
|
---|
548 | if (mZpoolOpen && mZpoolClose && mZpoolGetConfig)
|
---|
549 | {
|
---|
550 | zpool_handle_t *zh = mZpoolOpen(mZfsLib, strName.c_str());
|
---|
551 | if (zh)
|
---|
552 | {
|
---|
553 | unsigned int cChildren;
|
---|
554 | nvlist_t **nvChildren, *nvRoot, *nvConfig = mZpoolGetConfig(zh, NULL);
|
---|
555 | Assert(!nvlist_lookup_nvlist(nvConfig, ZPOOL_CONFIG_VDEV_TREE, &nvRoot));
|
---|
556 | if (!nvlist_lookup_nvlist_array(nvRoot, ZPOOL_CONFIG_CHILDREN, &nvChildren, &cChildren))
|
---|
557 | {
|
---|
558 | for (unsigned int i = 0; i < cChildren; ++i)
|
---|
559 | {
|
---|
560 | uint64_t fHole = 0;
|
---|
561 | uint64_t fLog = 0;
|
---|
562 |
|
---|
563 | nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_HOLE, &fHole);
|
---|
564 | nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_LOG, &fLog);
|
---|
565 |
|
---|
566 | if (!fHole && !fLog)
|
---|
567 | {
|
---|
568 | char *pszChildName = mZpoolVdevName(mZfsLib, zh, nvChildren[i], _B_FALSE);
|
---|
569 | Assert(pszChildName);
|
---|
570 | RTCString strDevPath("/dev/dsk/");
|
---|
571 | strDevPath += pszChildName;
|
---|
572 | char szLink[RTPATH_MAX];
|
---|
573 | if (readlink(strDevPath.c_str(), szLink, sizeof(szLink)) != -1)
|
---|
574 | {
|
---|
575 | char *pszStart, *pszEnd;
|
---|
576 | pszStart = strstr(szLink, "/devices/");
|
---|
577 | pszEnd = strrchr(szLink, ':');
|
---|
578 | if (pszStart && pszEnd)
|
---|
579 | {
|
---|
580 | pszStart += 8; // Skip "/devices"
|
---|
581 | *pszEnd = '\0'; // Trim partition
|
---|
582 | list.push_back(physToInstName(pszStart));
|
---|
583 | }
|
---|
584 | }
|
---|
585 | free(pszChildName);
|
---|
586 | }
|
---|
587 | }
|
---|
588 | }
|
---|
589 | mZpoolClose(zh);
|
---|
590 | }
|
---|
591 | }
|
---|
592 | else
|
---|
593 | list.push_back(RTCString(name));
|
---|
594 | return VINF_SUCCESS;
|
---|
595 | }
|
---|
596 |
|
---|
597 | void CollectorSolaris::updateFilesystemMap(void)
|
---|
598 | {
|
---|
599 | FILE *fp = fopen("/etc/mnttab", "r");
|
---|
600 | if (fp)
|
---|
601 | {
|
---|
602 | struct mnttab Entry;
|
---|
603 | int rc = 0;
|
---|
604 | resetmnttab(fp);
|
---|
605 | while ((rc = getmntent(fp, &Entry)) == 0)
|
---|
606 | mFsMap[Entry.mnt_mountp] = Entry.mnt_special;
|
---|
607 | fclose(fp);
|
---|
608 | if (rc != -1)
|
---|
609 | LogRel(("Error while reading mnttab: %d\n", rc));
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | }
|
---|