VirtualBox

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

Last change on this file since 98020 was 98020, checked in by vboxsync, 2 years ago

Main/PerformanceSolaris.cpp: 4 warnings, two involving what seems like 32-bit code not properly updated to 64-bit.

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