VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/solaris/PerformanceSolaris.cpp@ 47470

Last change on this file since 47470 was 47428, checked in by vboxsync, 11 years ago

Addressed some warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.4 KB
Line 
1/* $Id: PerformanceSolaris.cpp 47428 2013-07-26 16:19:03Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Solaris-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008-2013 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 <iprt/system.h>
39
40#include "Logging.h"
41#include "Performance.h"
42
43#include <dlfcn.h>
44
45#include <libzfs.h>
46#include <libnvpair.h>
47
48#include <map>
49
50namespace pm {
51
52 typedef libzfs_handle_t *(*PFNZFSINIT)(void);
53 typedef void (*PFNZFSFINI)(libzfs_handle_t *);
54 typedef zfs_handle_t *(*PFNZFSOPEN)(libzfs_handle_t *, const char *, int);
55 typedef void (*PFNZFSCLOSE)(zfs_handle_t *);
56 typedef uint64_t (*PFNZFSPROPGETINT)(zfs_handle_t *, zfs_prop_t);
57 typedef zpool_handle_t *(*PFNZPOOLOPEN)(libzfs_handle_t *, const char *);
58 typedef void (*PFNZPOOLCLOSE)(zpool_handle_t *);
59 typedef nvlist_t *(*PFNZPOOLGETCONFIG)(zpool_handle_t *, nvlist_t **);
60 typedef char *(*PFNZPOOLVDEVNAME)(libzfs_handle_t *, zpool_handle_t *, nvlist_t *, boolean_t);
61
62 typedef std::map<RTCString,RTCString> FsMap;
63
64class CollectorSolaris : public CollectorHAL
65{
66public:
67 CollectorSolaris();
68 virtual ~CollectorSolaris();
69 virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
70 virtual int getHostFilesystemUsage(const char *name, ULONG *total, ULONG *used, ULONG *available);
71 virtual int getHostDiskSize(const char *name, uint64_t *size);
72 virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);
73
74 virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
75 virtual int getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx);
76 virtual int getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms);
77 virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);
78
79 virtual int getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad);
80private:
81 static uint32_t getInstance(const char *pszIfaceName, char *pszDevName);
82 uint64_t getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName);
83 void updateFilesystemMap(void);
84 RTCString physToInstName(const char *pcszPhysName);
85 RTCString pathToInstName(const char *pcszDevPathName);
86 uint64_t wrapCorrection(uint32_t cur, uint64_t prev, const char *name);
87 uint64_t wrapDetection(uint64_t cur, uint64_t prev, const char *name);
88
89 kstat_ctl_t *mKC;
90 kstat_t *mSysPages;
91 kstat_t *mZFSCache;
92
93 void *mZfsSo;
94 libzfs_handle_t *mZfsLib;
95 PFNZFSINIT mZfsInit;
96 PFNZFSFINI mZfsFini;
97 PFNZFSOPEN mZfsOpen;
98 PFNZFSCLOSE mZfsClose;
99 PFNZFSPROPGETINT mZfsPropGetInt;
100 PFNZPOOLOPEN mZpoolOpen;
101 PFNZPOOLCLOSE mZpoolClose;
102 PFNZPOOLGETCONFIG mZpoolGetConfig;
103 PFNZPOOLVDEVNAME mZpoolVdevName;
104
105 FsMap mFsMap;
106 uint32_t mCpus;
107 ULONG totalRAM;
108};
109
110CollectorHAL *createHAL()
111{
112 return new CollectorSolaris();
113}
114
115// Collector HAL for Solaris
116
117
118CollectorSolaris::CollectorSolaris()
119 : mKC(0),
120 mSysPages(0),
121 mZFSCache(0),
122 mZfsLib(0),
123 mCpus(0)
124{
125 if ((mKC = kstat_open()) == 0)
126 {
127 Log(("kstat_open() -> %d\n", errno));
128 return;
129 }
130
131 if ((mSysPages = kstat_lookup(mKC, (char *)"unix", 0, (char *)"system_pages")) == 0)
132 {
133 Log(("kstat_lookup(system_pages) -> %d\n", errno));
134 return;
135 }
136
137 if ((mZFSCache = kstat_lookup(mKC, (char *)"zfs", 0, (char *)"arcstats")) == 0)
138 {
139 Log(("kstat_lookup(system_pages) -> %d\n", errno));
140 }
141
142 /* Try to load libzfs dynamically, it may be missing. */
143 mZfsSo = dlopen("libzfs.so", RTLD_LAZY);
144 if (mZfsSo)
145 {
146 mZfsInit = (PFNZFSINIT)(uintptr_t)dlsym(mZfsSo, "libzfs_init");
147 mZfsFini = (PFNZFSFINI)(uintptr_t)dlsym(mZfsSo, "libzfs_fini");
148 mZfsOpen = (PFNZFSOPEN)(uintptr_t)dlsym(mZfsSo, "zfs_open");
149 mZfsClose = (PFNZFSCLOSE)(uintptr_t)dlsym(mZfsSo, "zfs_close");
150 mZfsPropGetInt = (PFNZFSPROPGETINT)(uintptr_t)dlsym(mZfsSo, "zfs_prop_get_int");
151 mZpoolOpen = (PFNZPOOLOPEN)(uintptr_t)dlsym(mZfsSo, "zpool_open");
152 mZpoolClose = (PFNZPOOLCLOSE)(uintptr_t)dlsym(mZfsSo, "zpool_close");
153 mZpoolGetConfig = (PFNZPOOLGETCONFIG)(uintptr_t)dlsym(mZfsSo, "zpool_get_config");
154 mZpoolVdevName = (PFNZPOOLVDEVNAME)(uintptr_t)dlsym(mZfsSo, "zpool_vdev_name");
155
156 if ( mZfsInit
157 && mZfsOpen
158 && mZfsClose
159 && mZfsPropGetInt
160 && mZpoolOpen
161 && mZpoolClose
162 && mZpoolGetConfig
163 && mZpoolVdevName)
164 mZfsLib = mZfsInit();
165 else
166 LogRel(("Incompatible libzfs? libzfs_init=%p zfs_open=%p zfs_close=%p zfs_prop_get_int=%p\n",
167 mZfsInit, mZfsOpen, mZfsClose, mZfsPropGetInt));
168 }
169
170 updateFilesystemMap();
171 /* Notice that mCpus member will be initialized by HostCpuLoadRaw::init() */
172
173 uint64_t cb;
174 int rc = RTSystemQueryTotalRam(&cb);
175 if (RT_FAILURE(rc))
176 totalRAM = 0;
177 else
178 totalRAM = (ULONG)(cb / 1024);
179}
180
181CollectorSolaris::~CollectorSolaris()
182{
183 if (mKC)
184 kstat_close(mKC);
185 /* Not calling libzfs_fini() causes file descriptor leaks (#6788). */
186 if (mZfsFini && mZfsLib)
187 mZfsFini(mZfsLib);
188 if (mZfsSo)
189 dlclose(mZfsSo);
190}
191
192int CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
193{
194 int rc = VINF_SUCCESS;
195 kstat_t *ksp;
196 uint64_t tmpUser, tmpKernel, tmpIdle;
197 int cpus;
198 cpu_stat_t cpu_stats;
199
200 if (mKC == 0)
201 return VERR_INTERNAL_ERROR;
202
203 tmpUser = tmpKernel = tmpIdle = cpus = 0;
204 for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
205 if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
206 if (kstat_read(mKC, ksp, &cpu_stats) == -1)
207 {
208 Log(("kstat_read() -> %d\n", errno));
209 return VERR_INTERNAL_ERROR;
210 }
211 ++cpus;
212 tmpUser += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
213 tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
214 tmpIdle += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
215 }
216 }
217
218 if (cpus == 0)
219 {
220 Log(("no cpu stats found!\n"));
221 return VERR_INTERNAL_ERROR;
222 }
223 else
224 mCpus = cpus;
225
226 if (user) *user = tmpUser;
227 if (kernel) *kernel = tmpKernel;
228 if (idle) *idle = tmpIdle;
229
230 return rc;
231}
232
233int CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
234{
235 int rc = VINF_SUCCESS;
236 char *pszName;
237 prusage_t prusage;
238
239 RTStrAPrintf(&pszName, "/proc/%d/usage", process);
240 Log(("Opening %s...\n", pszName));
241 int h = open(pszName, O_RDONLY);
242 RTStrFree(pszName);
243
244 if (h != -1)
245 {
246 if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
247 {
248 //Assert((pid_t)process == pstatus.pr_pid);
249 //Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
250 /*
251 * The CPU time spent must be adjusted by the number of cores for compatibility with
252 * other platforms (see @bugref{6345}).
253 */
254 Assert(mCpus);
255 if (mCpus)
256 {
257 *user = ((uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec) / mCpus;
258 *kernel = ((uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec) / mCpus;
259 }
260 else
261 *user = *kernel = 0;
262 *total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
263 //Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
264 }
265 else
266 {
267 Log(("read() -> %d\n", errno));
268 rc = VERR_FILE_IO_ERROR;
269 }
270 close(h);
271 }
272 else
273 {
274 Log(("open() -> %d\n", errno));
275 rc = VERR_ACCESS_DENIED;
276 }
277
278 return rc;
279}
280
281int CollectorSolaris::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
282{
283 AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
284 uint64_t cb;
285 int rc = RTSystemQueryAvailableRam(&cb);
286 if (RT_SUCCESS(rc))
287 {
288 *total = totalRAM;
289 *available = cb / 1024;
290 *used = *total - *available;
291 }
292 return rc;
293}
294
295int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
296{
297 int rc = VINF_SUCCESS;
298 char *pszName = NULL;
299 psinfo_t psinfo;
300
301 RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
302 Log(("Opening %s...\n", pszName));
303 int h = open(pszName, O_RDONLY);
304 RTStrFree(pszName);
305
306 if (h != -1)
307 {
308 if (read(h, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
309 {
310 Assert((pid_t)process == psinfo.pr_pid);
311 *used = psinfo.pr_rssize;
312 }
313 else
314 {
315 Log(("read() -> %d\n", errno));
316 rc = VERR_FILE_IO_ERROR;
317 }
318 close(h);
319 }
320 else
321 {
322 Log(("open() -> %d\n", errno));
323 rc = VERR_ACCESS_DENIED;
324 }
325
326 return rc;
327}
328
329uint32_t CollectorSolaris::getInstance(const char *pszIfaceName, char *pszDevName)
330{
331 /*
332 * Get the instance number from the interface name, then clip it off.
333 */
334 int cbInstance = 0;
335 int cbIface = strlen(pszIfaceName);
336 const char *pszEnd = pszIfaceName + cbIface - 1;
337 for (int i = 0; i < cbIface - 1; i++)
338 {
339 if (!RT_C_IS_DIGIT(*pszEnd))
340 break;
341 cbInstance++;
342 pszEnd--;
343 }
344
345 uint32_t uInstance = RTStrToUInt32(pszEnd + 1);
346 strncpy(pszDevName, pszIfaceName, cbIface - cbInstance);
347 pszDevName[cbIface - cbInstance] = '\0';
348 return uInstance;
349}
350
351uint64_t CollectorSolaris::wrapCorrection(uint32_t cur, uint64_t prev, const char *name)
352{
353 NOREF(name);
354 uint64_t corrected = (prev & 0xffffffff00000000) + cur;
355 if (cur < (prev & 0xffffffff))
356 {
357 /* wrap has occurred */
358 corrected += 0x100000000;
359 LogFlowThisFunc(("Corrected wrap on %s (%u < %u), returned %llu.\n",
360 name, cur, (uint32_t)prev, corrected));
361 }
362 return corrected;
363}
364
365uint64_t CollectorSolaris::wrapDetection(uint64_t cur, uint64_t prev, const char *name)
366{
367 static bool fNotSeen = true;
368
369 if (fNotSeen && cur < prev)
370 {
371 fNotSeen = false;
372 LogRel(("Detected wrap on %s (%llu < %llu).\n", name, cur, prev));
373 }
374 return cur;
375}
376
377/*
378 * WARNING! This function expects the previous values of rx and tx counter to
379 * be passed in as well as returnes new values in the same parameters. This is
380 * needed to provide a workaround for 32-bit counter wrapping.
381 */
382int CollectorSolaris::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
383{
384 static bool g_fNotReported = true;
385 AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
386 LogFlowThisFunc(("m=%s i=%d n=%s\n", "link", -1, name));
387 kstat_t *ksAdapter = kstat_lookup(mKC, "link", -1, (char *)name);
388 if (ksAdapter == 0)
389 {
390 char szModule[KSTAT_STRLEN];
391 uint32_t uInstance = getInstance(name, szModule);
392 LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, "phys"));
393 ksAdapter = kstat_lookup(mKC, szModule, uInstance, "phys");
394 if (ksAdapter == 0)
395 {
396 LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, name));
397 ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)name);
398 if (ksAdapter == 0)
399 {
400 LogRel(("Failed to get network statistics for %s\n", name));
401 return VERR_INTERNAL_ERROR;
402 }
403 }
404 }
405 if (kstat_read(mKC, ksAdapter, 0) == -1)
406 {
407 LogRel(("kstat_read(adapter) -> %d\n", errno));
408 return VERR_INTERNAL_ERROR;
409 }
410 kstat_named_t *kn;
411 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes64")) == 0)
412 {
413 if (g_fNotReported)
414 {
415 g_fNotReported = false;
416 LogRel(("Failed to locate rbytes64, falling back to 32-bit counters...\n"));
417 }
418 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes")) == 0)
419 {
420 LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
421 return VERR_INTERNAL_ERROR;
422 }
423 *rx = wrapCorrection(kn->value.ul, *rx, "rbytes");
424 }
425 else
426 *rx = wrapDetection(kn->value.ull, *rx, "rbytes64");
427 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes64")) == 0)
428 {
429 if (g_fNotReported)
430 {
431 g_fNotReported = false;
432 LogRel(("Failed to locate obytes64, falling back to 32-bit counters...\n"));
433 }
434 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes")) == 0)
435 {
436 LogRel(("kstat_data_lookup(obytes) -> %d\n", errno));
437 return VERR_INTERNAL_ERROR;
438 }
439 *tx = wrapCorrection(kn->value.ul, *tx, "obytes");
440 }
441 else
442 *tx = wrapDetection(kn->value.ull, *tx, "obytes64");
443 return VINF_SUCCESS;
444}
445
446int CollectorSolaris::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
447{
448 int rc = VINF_SUCCESS;
449 AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
450 LogFlowThisFunc(("n=%s\n", name));
451 kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, (char *)name);
452 if (ksDisk != 0)
453 {
454 if (kstat_read(mKC, ksDisk, 0) == -1)
455 {
456 LogRel(("kstat_read(%s) -> %d\n", name, errno));
457 rc = VERR_INTERNAL_ERROR;
458 }
459 else
460 {
461 kstat_io_t *ksIo = KSTAT_IO_PTR(ksDisk);
462 /*
463 * We do not care for wrap possibility here, although we may
464 * reconsider in about 300 years (9223372036854775807 ns).
465 */
466 *disk_ms = ksIo->rtime / 1000000;
467 *total_ms = ksDisk->ks_snaptime / 1000000;
468 }
469 }
470 else
471 {
472 LogRel(("kstat_lookup(%s) -> %d\n", name, errno));
473 rc = VERR_INTERNAL_ERROR;
474 }
475
476 return rc;
477}
478
479uint64_t CollectorSolaris::getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName)
480{
481 if (strcmp(szFsType, "zfs"))
482 return cbTotal;
483 FsMap::iterator it = mFsMap.find(szFsName);
484 if (it == mFsMap.end())
485 return cbTotal;
486
487 char *pszDataset = strdup(it->second.c_str());
488 char *pszEnd = pszDataset + strlen(pszDataset);
489 uint64_t uAvail = 0;
490 while (pszEnd)
491 {
492 zfs_handle_t *hDataset;
493
494 *pszEnd = 0;
495 hDataset = mZfsOpen(mZfsLib, pszDataset, ZFS_TYPE_DATASET);
496 if (!hDataset)
497 break;
498
499 if (uAvail == 0)
500 {
501 uAvail = mZfsPropGetInt(hDataset, ZFS_PROP_REFQUOTA);
502 if (uAvail == 0)
503 uAvail = UINT64_MAX;
504 }
505
506 uint64_t uQuota = mZfsPropGetInt(hDataset, ZFS_PROP_QUOTA);
507 if (uQuota && uAvail > uQuota)
508 uAvail = uQuota;
509
510 pszEnd = strrchr(pszDataset, '/');
511 if (!pszEnd)
512 {
513 uint64_t uPoolSize = mZfsPropGetInt(hDataset, ZFS_PROP_USED) +
514 mZfsPropGetInt(hDataset, ZFS_PROP_AVAILABLE);
515 if (uAvail > uPoolSize)
516 uAvail = uPoolSize;
517 }
518 mZfsClose(hDataset);
519 }
520 free(pszDataset);
521
522 return uAvail ? uAvail : cbTotal;
523}
524
525int CollectorSolaris::getHostFilesystemUsage(const char *path, ULONG *total, ULONG *used, ULONG *available)
526{
527 struct statvfs64 stats;
528 const unsigned _MB = 1024 * 1024;
529
530 if (statvfs64(path, &stats) == -1)
531 {
532 LogRel(("Failed to collect %s filesystem usage: errno=%d.\n", path, errno));
533 return VERR_ACCESS_DENIED;
534 }
535 uint64_t cbBlock = stats.f_frsize ? stats.f_frsize : stats.f_bsize;
536 *total = (ULONG)(getZfsTotal(cbBlock * stats.f_blocks, stats.f_basetype, path) / _MB);
537 LogFlowThisFunc(("f_blocks=%llu.\n", stats.f_blocks));
538 *used = (ULONG)(cbBlock * (stats.f_blocks - stats.f_bfree) / _MB);
539 *available = (ULONG)(cbBlock * stats.f_bavail / _MB);
540
541 return VINF_SUCCESS;
542}
543
544int CollectorSolaris::getHostDiskSize(const char *name, uint64_t *size)
545{
546 int rc = VINF_SUCCESS;
547 AssertReturn(strlen(name) + 5 < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
548 LogFlowThisFunc(("n=%s\n", name));
549 char szName[KSTAT_STRLEN];
550 strcpy(szName, name);
551 strcat(szName, ",err");
552 kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, szName);
553 if (ksDisk != 0)
554 {
555 if (kstat_read(mKC, ksDisk, 0) == -1)
556 {
557 LogRel(("kstat_read(%s) -> %d\n", name, errno));
558 rc = VERR_INTERNAL_ERROR;
559 }
560 else
561 {
562 kstat_named_t *kn;
563 if ((kn = (kstat_named_t *)kstat_data_lookup(ksDisk, (char *)"Size")) == 0)
564 {
565 LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
566 return VERR_INTERNAL_ERROR;
567 }
568 *size = kn->value.ull;
569 }
570 }
571 else
572 {
573 LogRel(("kstat_lookup(%s) -> %d\n", szName, errno));
574 rc = VERR_INTERNAL_ERROR;
575 }
576
577
578 return rc;
579}
580
581RTCString CollectorSolaris::physToInstName(const char *pcszPhysName)
582{
583 FILE *fp = fopen("/etc/path_to_inst", "r");
584 if (!fp)
585 return RTCString();
586
587 RTCString strInstName;
588 size_t cbName = strlen(pcszPhysName);
589 char szBuf[RTPATH_MAX];
590 while (fgets(szBuf, sizeof(szBuf), fp))
591 {
592 if (szBuf[0] == '"' && strncmp(szBuf + 1, pcszPhysName, cbName) == 0)
593 {
594 char *pszDriver, *pszInstance;
595 pszDriver = strrchr(szBuf, '"');
596 if (pszDriver)
597 {
598 *pszDriver = '\0';
599 pszDriver = strrchr(szBuf, '"');
600 if (pszDriver)
601 {
602 *pszDriver++ = '\0';
603 pszInstance = strrchr(szBuf, ' ');
604 if (pszInstance)
605 {
606 *pszInstance = '\0';
607 pszInstance = strrchr(szBuf, ' ');
608 if (pszInstance)
609 {
610 *pszInstance++ = '\0';
611 strInstName = pszDriver;
612 strInstName += pszInstance;
613 break;
614 }
615 }
616 }
617 }
618 }
619 }
620 fclose(fp);
621
622 return strInstName;
623}
624
625RTCString CollectorSolaris::pathToInstName(const char *pcszDevPathName)
626{
627 char szLink[RTPATH_MAX];
628 if (readlink(pcszDevPathName, szLink, sizeof(szLink)) != -1)
629 {
630 char *pszStart, *pszEnd;
631 pszStart = strstr(szLink, "/devices/");
632 pszEnd = strrchr(szLink, ':');
633 if (pszStart && pszEnd)
634 {
635 pszStart += 8; // Skip "/devices"
636 *pszEnd = '\0'; // Trim partition
637 return physToInstName(pszStart);
638 }
639 }
640
641 return RTCString(pcszDevPathName);
642}
643
644int CollectorSolaris::getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad)
645{
646 FsMap::iterator it = mFsMap.find(name);
647 if (it == mFsMap.end())
648 return VERR_INVALID_PARAMETER;
649
650 RTCString strName = it->second.substr(0, it->second.find("/"));
651 if (mZpoolOpen && mZpoolClose && mZpoolGetConfig && !strName.isEmpty())
652 {
653 zpool_handle_t *zh = mZpoolOpen(mZfsLib, strName.c_str());
654 if (zh)
655 {
656 unsigned int cChildren = 0;
657 nvlist_t **nvChildren = NULL;
658 nvlist_t *nvRoot = NULL;
659 nvlist_t *nvConfig = mZpoolGetConfig(zh, NULL);
660 if ( !nvlist_lookup_nvlist(nvConfig, ZPOOL_CONFIG_VDEV_TREE, &nvRoot)
661 && !nvlist_lookup_nvlist_array(nvRoot, ZPOOL_CONFIG_CHILDREN, &nvChildren, &cChildren))
662 {
663 for (unsigned int i = 0; i < cChildren; ++i)
664 {
665 uint64_t fHole = 0;
666 uint64_t fLog = 0;
667
668 nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_HOLE, &fHole);
669 nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_LOG, &fLog);
670
671 if (!fHole && !fLog)
672 {
673 char *pszChildName = mZpoolVdevName(mZfsLib, zh, nvChildren[i], _B_FALSE);
674 Assert(pszChildName);
675 RTCString strDevPath("/dev/dsk/");
676 strDevPath += pszChildName;
677 char szLink[RTPATH_MAX];
678 if (readlink(strDevPath.c_str(), szLink, sizeof(szLink)) != -1)
679 {
680 char *pszStart, *pszEnd;
681 pszStart = strstr(szLink, "/devices/");
682 pszEnd = strrchr(szLink, ':');
683 if (pszStart && pszEnd)
684 {
685 pszStart += 8; // Skip "/devices"
686 *pszEnd = '\0'; // Trim partition
687 listUsage.push_back(physToInstName(pszStart));
688 }
689 }
690 free(pszChildName);
691 }
692 }
693 }
694 mZpoolClose(zh);
695 }
696 }
697 else
698 listUsage.push_back(pathToInstName(it->second.c_str()));
699 listLoad = listUsage;
700 return VINF_SUCCESS;
701}
702
703void CollectorSolaris::updateFilesystemMap(void)
704{
705 FILE *fp = fopen("/etc/mnttab", "r");
706 if (fp)
707 {
708 struct mnttab Entry;
709 int rc = 0;
710 resetmnttab(fp);
711 while ((rc = getmntent(fp, &Entry)) == 0)
712 mFsMap[Entry.mnt_mountp] = Entry.mnt_special;
713 fclose(fp);
714 if (rc != -1)
715 LogRel(("Error while reading mnttab: %d\n", rc));
716 }
717}
718
719}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette