VirtualBox

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

Last change on this file since 97698 was 96407, checked in by vboxsync, 3 years ago

scm copyright and license note update

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