VirtualBox

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

Last change on this file since 76190 was 73097, checked in by vboxsync, 6 years ago

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: PerformanceSolaris.cpp 73097 2018-07-12 21:06:33Z vboxsync $ */
2
3/** @file
4 *
5 * VBox Solaris-specific Performance Classes implementation.
6 */
7
8/*
9 * Copyright (C) 2008-2017 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 /* psinfo_t keeps growing, so only read what we need to maximize
309 * cross-version compatibility. The structures are compatible. */
310 ssize_t cb = RT_UOFFSETOF(psinfo_t, pr_rssize) + RT_SIZEOFMEMB(psinfo_t, pr_rssize);
311 AssertCompile(RTASSERT_OFFSET_OF(psinfo_t, pr_rssize) > RTASSERT_OFFSET_OF(psinfo_t, pr_pid));
312 if (read(h, &psinfo, cb) == cb)
313 {
314 Assert((pid_t)process == psinfo.pr_pid);
315 *used = psinfo.pr_rssize;
316 }
317 else
318 {
319 Log(("read() -> %d\n", errno));
320 rc = VERR_FILE_IO_ERROR;
321 }
322 close(h);
323 }
324 else
325 {
326 Log(("open() -> %d\n", errno));
327 rc = VERR_ACCESS_DENIED;
328 }
329
330 return rc;
331}
332
333uint32_t CollectorSolaris::getInstance(const char *pszIfaceName, char *pszDevName)
334{
335 /*
336 * Get the instance number from the interface name, then clip it off.
337 */
338 int cbInstance = 0;
339 int cbIface = strlen(pszIfaceName);
340 const char *pszEnd = pszIfaceName + cbIface - 1;
341 for (int i = 0; i < cbIface - 1; i++)
342 {
343 if (!RT_C_IS_DIGIT(*pszEnd))
344 break;
345 cbInstance++;
346 pszEnd--;
347 }
348
349 uint32_t uInstance = RTStrToUInt32(pszEnd + 1);
350 strncpy(pszDevName, pszIfaceName, cbIface - cbInstance);
351 pszDevName[cbIface - cbInstance] = '\0';
352 return uInstance;
353}
354
355uint64_t CollectorSolaris::wrapCorrection(uint32_t cur, uint64_t prev, const char *name)
356{
357 NOREF(name);
358 uint64_t corrected = (prev & 0xffffffff00000000) + cur;
359 if (cur < (prev & 0xffffffff))
360 {
361 /* wrap has occurred */
362 corrected += 0x100000000;
363 LogFlowThisFunc(("Corrected wrap on %s (%u < %u), returned %llu.\n",
364 name, cur, (uint32_t)prev, corrected));
365 }
366 return corrected;
367}
368
369uint64_t CollectorSolaris::wrapDetection(uint64_t cur, uint64_t prev, const char *name)
370{
371 static bool fNotSeen = true;
372
373 if (fNotSeen && cur < prev)
374 {
375 fNotSeen = false;
376 LogRel(("Detected wrap on %s (%llu < %llu).\n", name, cur, prev));
377 }
378 return cur;
379}
380
381/*
382 * WARNING! This function expects the previous values of rx and tx counter to
383 * be passed in as well as returnes new values in the same parameters. This is
384 * needed to provide a workaround for 32-bit counter wrapping.
385 */
386int CollectorSolaris::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
387{
388 static bool g_fNotReported = true;
389 AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
390 LogFlowThisFunc(("m=%s i=%d n=%s\n", "link", -1, name));
391 kstat_t *ksAdapter = kstat_lookup(mKC, (char *)"link", -1, (char *)name);
392 if (ksAdapter == 0)
393 {
394 char szModule[KSTAT_STRLEN];
395 uint32_t uInstance = getInstance(name, szModule);
396 LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, "phys"));
397 ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)"phys");
398 if (ksAdapter == 0)
399 {
400 LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, name));
401 ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)name);
402 if (ksAdapter == 0)
403 {
404 static uint32_t s_tsLogRelLast;
405 uint32_t tsNow = RTTimeProgramSecTS();
406 if ( tsNow < RT_SEC_1HOUR
407 || (tsNow - s_tsLogRelLast >= 60))
408 {
409 s_tsLogRelLast = tsNow;
410 LogRel(("Failed to get network statistics for %s. Max one msg/min.\n", name));
411 }
412 return VERR_INTERNAL_ERROR;
413 }
414 }
415 }
416 if (kstat_read(mKC, ksAdapter, 0) == -1)
417 {
418 LogRel(("kstat_read(adapter) -> %d\n", errno));
419 return VERR_INTERNAL_ERROR;
420 }
421 kstat_named_t *kn;
422 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes64")) == 0)
423 {
424 if (g_fNotReported)
425 {
426 g_fNotReported = false;
427 LogRel(("Failed to locate rbytes64, falling back to 32-bit counters...\n"));
428 }
429 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes")) == 0)
430 {
431 LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
432 return VERR_INTERNAL_ERROR;
433 }
434 *rx = wrapCorrection(kn->value.ul, *rx, "rbytes");
435 }
436 else
437 *rx = wrapDetection(kn->value.ull, *rx, "rbytes64");
438 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes64")) == 0)
439 {
440 if (g_fNotReported)
441 {
442 g_fNotReported = false;
443 LogRel(("Failed to locate obytes64, falling back to 32-bit counters...\n"));
444 }
445 if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes")) == 0)
446 {
447 LogRel(("kstat_data_lookup(obytes) -> %d\n", errno));
448 return VERR_INTERNAL_ERROR;
449 }
450 *tx = wrapCorrection(kn->value.ul, *tx, "obytes");
451 }
452 else
453 *tx = wrapDetection(kn->value.ull, *tx, "obytes64");
454 return VINF_SUCCESS;
455}
456
457int CollectorSolaris::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
458{
459 int rc = VINF_SUCCESS;
460 AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
461 LogFlowThisFunc(("n=%s\n", name));
462 kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, (char *)name);
463 if (ksDisk != 0)
464 {
465 if (kstat_read(mKC, ksDisk, 0) == -1)
466 {
467 LogRel(("kstat_read(%s) -> %d\n", name, errno));
468 rc = VERR_INTERNAL_ERROR;
469 }
470 else
471 {
472 kstat_io_t *ksIo = KSTAT_IO_PTR(ksDisk);
473 /*
474 * We do not care for wrap possibility here, although we may
475 * reconsider in about 300 years (9223372036854775807 ns).
476 */
477 *disk_ms = ksIo->rtime / 1000000;
478 *total_ms = ksDisk->ks_snaptime / 1000000;
479 }
480 }
481 else
482 {
483 LogRel(("kstat_lookup(%s) -> %d\n", name, errno));
484 rc = VERR_INTERNAL_ERROR;
485 }
486
487 return rc;
488}
489
490uint64_t CollectorSolaris::getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName)
491{
492 if (strcmp(szFsType, "zfs"))
493 return cbTotal;
494 FsMap::iterator it = mFsMap.find(szFsName);
495 if (it == mFsMap.end())
496 return cbTotal;
497
498 char *pszDataset = strdup(it->second.c_str());
499 char *pszEnd = pszDataset + strlen(pszDataset);
500 uint64_t uAvail = 0;
501 while (pszEnd)
502 {
503 zfs_handle_t *hDataset;
504
505 *pszEnd = 0;
506 hDataset = mZfsOpen(mZfsLib, pszDataset, ZFS_TYPE_DATASET);
507 if (!hDataset)
508 break;
509
510 if (uAvail == 0)
511 {
512 uAvail = mZfsPropGetInt(hDataset, ZFS_PROP_REFQUOTA);
513 if (uAvail == 0)
514 uAvail = UINT64_MAX;
515 }
516
517 uint64_t uQuota = mZfsPropGetInt(hDataset, ZFS_PROP_QUOTA);
518 if (uQuota && uAvail > uQuota)
519 uAvail = uQuota;
520
521 pszEnd = strrchr(pszDataset, '/');
522 if (!pszEnd)
523 {
524 uint64_t uPoolSize = mZfsPropGetInt(hDataset, ZFS_PROP_USED) +
525 mZfsPropGetInt(hDataset, ZFS_PROP_AVAILABLE);
526 if (uAvail > uPoolSize)
527 uAvail = uPoolSize;
528 }
529 mZfsClose(hDataset);
530 }
531 free(pszDataset);
532
533 return uAvail ? uAvail : cbTotal;
534}
535
536int CollectorSolaris::getHostFilesystemUsage(const char *path, ULONG *total, ULONG *used, ULONG *available)
537{
538 struct statvfs64 stats;
539
540 if (statvfs64(path, &stats) == -1)
541 {
542 LogRel(("Failed to collect %s filesystem usage: errno=%d.\n", path, errno));
543 return VERR_ACCESS_DENIED;
544 }
545 uint64_t cbBlock = stats.f_frsize ? stats.f_frsize : stats.f_bsize;
546 *total = (ULONG)(getZfsTotal(cbBlock * stats.f_blocks, stats.f_basetype, path) / _1M);
547 LogFlowThisFunc(("f_blocks=%llu.\n", stats.f_blocks));
548 *used = (ULONG)(cbBlock * (stats.f_blocks - stats.f_bfree) / _1M);
549 *available = (ULONG)(cbBlock * stats.f_bavail / _1M);
550
551 return VINF_SUCCESS;
552}
553
554int CollectorSolaris::getHostDiskSize(const char *name, uint64_t *size)
555{
556 int rc = VINF_SUCCESS;
557 AssertReturn(strlen(name) + 5 < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
558 LogFlowThisFunc(("n=%s\n", name));
559 char szName[KSTAT_STRLEN];
560 strcpy(szName, name);
561 strcat(szName, ",err");
562 kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, szName);
563 if (ksDisk != 0)
564 {
565 if (kstat_read(mKC, ksDisk, 0) == -1)
566 {
567 LogRel(("kstat_read(%s) -> %d\n", name, errno));
568 rc = VERR_INTERNAL_ERROR;
569 }
570 else
571 {
572 kstat_named_t *kn;
573 if ((kn = (kstat_named_t *)kstat_data_lookup(ksDisk, (char *)"Size")) == 0)
574 {
575 LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
576 return VERR_INTERNAL_ERROR;
577 }
578 *size = kn->value.ull;
579 }
580 }
581 else
582 {
583 LogRel(("kstat_lookup(%s) -> %d\n", szName, errno));
584 rc = VERR_INTERNAL_ERROR;
585 }
586
587
588 return rc;
589}
590
591RTCString CollectorSolaris::physToInstName(const char *pcszPhysName)
592{
593 FILE *fp = fopen("/etc/path_to_inst", "r");
594 if (!fp)
595 return RTCString();
596
597 RTCString strInstName;
598 size_t cbName = strlen(pcszPhysName);
599 char szBuf[RTPATH_MAX];
600 while (fgets(szBuf, sizeof(szBuf), fp))
601 {
602 if (szBuf[0] == '"' && strncmp(szBuf + 1, pcszPhysName, cbName) == 0)
603 {
604 char *pszDriver, *pszInstance;
605 pszDriver = strrchr(szBuf, '"');
606 if (pszDriver)
607 {
608 *pszDriver = '\0';
609 pszDriver = strrchr(szBuf, '"');
610 if (pszDriver)
611 {
612 *pszDriver++ = '\0';
613 pszInstance = strrchr(szBuf, ' ');
614 if (pszInstance)
615 {
616 *pszInstance = '\0';
617 pszInstance = strrchr(szBuf, ' ');
618 if (pszInstance)
619 {
620 *pszInstance++ = '\0';
621 strInstName = pszDriver;
622 strInstName += pszInstance;
623 break;
624 }
625 }
626 }
627 }
628 }
629 }
630 fclose(fp);
631
632 return strInstName;
633}
634
635RTCString CollectorSolaris::pathToInstName(const char *pcszDevPathName)
636{
637 char szLink[RTPATH_MAX];
638 if (readlink(pcszDevPathName, szLink, sizeof(szLink)) != -1)
639 {
640 char *pszStart, *pszEnd;
641 pszStart = strstr(szLink, "/devices/");
642 pszEnd = strrchr(szLink, ':');
643 if (pszStart && pszEnd)
644 {
645 pszStart += 8; // Skip "/devices"
646 *pszEnd = '\0'; // Trim partition
647 return physToInstName(pszStart);
648 }
649 }
650
651 return RTCString(pcszDevPathName);
652}
653
654int CollectorSolaris::getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad)
655{
656 FsMap::iterator it = mFsMap.find(name);
657 if (it == mFsMap.end())
658 return VERR_INVALID_PARAMETER;
659
660 RTCString strName = it->second.substr(0, it->second.find("/"));
661 if (mZpoolOpen && mZpoolClose && mZpoolGetConfig && !strName.isEmpty())
662 {
663 zpool_handle_t *zh = mZpoolOpen(mZfsLib, strName.c_str());
664 if (zh)
665 {
666 unsigned int cChildren = 0;
667 nvlist_t **nvChildren = NULL;
668 nvlist_t *nvRoot = NULL;
669 nvlist_t *nvConfig = mZpoolGetConfig(zh, NULL);
670 if ( !nvlist_lookup_nvlist(nvConfig, ZPOOL_CONFIG_VDEV_TREE, &nvRoot)
671 && !nvlist_lookup_nvlist_array(nvRoot, ZPOOL_CONFIG_CHILDREN, &nvChildren, &cChildren))
672 {
673 for (unsigned int i = 0; i < cChildren; ++i)
674 {
675 uint64_t fHole = 0;
676 uint64_t fLog = 0;
677
678 nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_HOLE, &fHole);
679 nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_LOG, &fLog);
680
681 if (!fHole && !fLog)
682 {
683 char *pszChildName = mZpoolVdevName(mZfsLib, zh, nvChildren[i], _B_FALSE);
684 Assert(pszChildName);
685 RTCString strDevPath("/dev/dsk/");
686 strDevPath += pszChildName;
687 char szLink[RTPATH_MAX];
688 if (readlink(strDevPath.c_str(), szLink, sizeof(szLink)) != -1)
689 {
690 char *pszStart, *pszEnd;
691 pszStart = strstr(szLink, "/devices/");
692 pszEnd = strrchr(szLink, ':');
693 if (pszStart && pszEnd)
694 {
695 pszStart += 8; // Skip "/devices"
696 *pszEnd = '\0'; // Trim partition
697 listUsage.push_back(physToInstName(pszStart));
698 }
699 }
700 free(pszChildName);
701 }
702 }
703 }
704 mZpoolClose(zh);
705 }
706 }
707 else
708 listUsage.push_back(pathToInstName(it->second.c_str()));
709 listLoad = listUsage;
710 return VINF_SUCCESS;
711}
712
713void CollectorSolaris::updateFilesystemMap(void)
714{
715 FILE *fp = fopen("/etc/mnttab", "r");
716 if (fp)
717 {
718 struct mnttab Entry;
719 int rc = 0;
720 resetmnttab(fp);
721 while ((rc = getmntent(fp, &Entry)) == 0)
722 mFsMap[Entry.mnt_mountp] = Entry.mnt_special;
723 fclose(fp);
724 if (rc != -1)
725 LogRel(("Error while reading mnttab: %d\n", rc));
726 }
727}
728
729}
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