VirtualBox

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

Last change on this file since 78395 was 76592, checked in by vboxsync, 6 years ago

Main: Don't use Logging.h.

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