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