summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-server/solaris/PerformanceSolaris.cpp
blob: 169c51ca5068d22b896f7c53a94996a75ab30fdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/* $Id: PerformanceSolaris.cpp $ */
/** @file
 * VBox Solaris-specific Performance Classes implementation.
 */

/*
 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#define LOG_GROUP LOG_GROUP_MAIN_PERFORMANCECOLLECTOR
#undef _FILE_OFFSET_BITS
#include <procfs.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <kstat.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/statvfs.h>

#include <iprt/ctype.h>
#include <iprt/err.h>
#include <iprt/string.h>
#include <iprt/alloc.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/system.h>

#include "LoggingNew.h"
#include "Performance.h"

#include <dlfcn.h>

#include <libzfs.h>
#include <libnvpair.h>

#include <map>

namespace pm {

    typedef libzfs_handle_t *(*PFNZFSINIT)(void);
    typedef void (*PFNZFSFINI)(libzfs_handle_t *);
    typedef zfs_handle_t *(*PFNZFSOPEN)(libzfs_handle_t *, const char *, int);
    typedef void (*PFNZFSCLOSE)(zfs_handle_t *);
    typedef uint64_t (*PFNZFSPROPGETINT)(zfs_handle_t *, zfs_prop_t);
    typedef zpool_handle_t *(*PFNZPOOLOPEN)(libzfs_handle_t *, const char *);
    typedef void (*PFNZPOOLCLOSE)(zpool_handle_t *);
    typedef nvlist_t *(*PFNZPOOLGETCONFIG)(zpool_handle_t *, nvlist_t **);
    typedef char *(*PFNZPOOLVDEVNAME)(libzfs_handle_t *, zpool_handle_t *, nvlist_t *, boolean_t);

    typedef std::map<RTCString,RTCString> FsMap;

class CollectorSolaris : public CollectorHAL
{
public:
    CollectorSolaris();
    virtual ~CollectorSolaris();
    virtual int getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available);
    virtual int getHostFilesystemUsage(const char *name, ULONG *total, ULONG *used, ULONG *available);
    virtual int getHostDiskSize(const char *name, uint64_t *size);
    virtual int getProcessMemoryUsage(RTPROCESS process, ULONG *used);

    virtual int getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle);
    virtual int getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx);
    virtual int getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms);
    virtual int getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total);

    virtual int getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad);
private:
    static uint32_t getInstance(const char *pszIfaceName, char *pszDevName);
    uint64_t getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName);
    void updateFilesystemMap(void);
    RTCString physToInstName(const char *pcszPhysName);
    RTCString pathToInstName(const char *pcszDevPathName);
    uint64_t wrapCorrection(uint32_t cur, uint64_t prev, const char *name);
    uint64_t wrapDetection(uint64_t cur, uint64_t prev, const char *name);

    kstat_ctl_t      *mKC;
    kstat_t          *mSysPages;
    kstat_t          *mZFSCache;

    void             *mZfsSo;
    libzfs_handle_t  *mZfsLib;
    PFNZFSINIT        mZfsInit;
    PFNZFSFINI        mZfsFini;
    PFNZFSOPEN        mZfsOpen;
    PFNZFSCLOSE       mZfsClose;
    PFNZFSPROPGETINT  mZfsPropGetInt;
    PFNZPOOLOPEN      mZpoolOpen;
    PFNZPOOLCLOSE     mZpoolClose;
    PFNZPOOLGETCONFIG mZpoolGetConfig;
    PFNZPOOLVDEVNAME  mZpoolVdevName;

    FsMap             mFsMap;
    uint32_t          mCpus;
    ULONG             totalRAM;
};

CollectorHAL *createHAL()
{
    return new CollectorSolaris();
}

// Collector HAL for Solaris


CollectorSolaris::CollectorSolaris()
    : mKC(0),
      mSysPages(0),
      mZFSCache(0),
      mZfsLib(0),
      mCpus(0)
{
    if ((mKC = kstat_open()) == 0)
    {
        Log(("kstat_open() -> %d\n", errno));
        return;
    }

    if ((mSysPages = kstat_lookup(mKC, (char *)"unix", 0, (char *)"system_pages")) == 0)
    {
        Log(("kstat_lookup(system_pages) -> %d\n", errno));
        return;
    }

    if ((mZFSCache = kstat_lookup(mKC, (char *)"zfs", 0, (char *)"arcstats")) == 0)
    {
        Log(("kstat_lookup(system_pages) -> %d\n", errno));
    }

    /* Try to load libzfs dynamically, it may be missing. */
    mZfsSo = dlopen("libzfs.so", RTLD_LAZY);
    if (mZfsSo)
    {
        mZfsInit        =        (PFNZFSINIT)(uintptr_t)dlsym(mZfsSo, "libzfs_init");
        mZfsFini        =        (PFNZFSFINI)(uintptr_t)dlsym(mZfsSo, "libzfs_fini");
        mZfsOpen        =        (PFNZFSOPEN)(uintptr_t)dlsym(mZfsSo, "zfs_open");
        mZfsClose       =       (PFNZFSCLOSE)(uintptr_t)dlsym(mZfsSo, "zfs_close");
        mZfsPropGetInt  =  (PFNZFSPROPGETINT)(uintptr_t)dlsym(mZfsSo, "zfs_prop_get_int");
        mZpoolOpen      =      (PFNZPOOLOPEN)(uintptr_t)dlsym(mZfsSo, "zpool_open");
        mZpoolClose     =     (PFNZPOOLCLOSE)(uintptr_t)dlsym(mZfsSo, "zpool_close");
        mZpoolGetConfig = (PFNZPOOLGETCONFIG)(uintptr_t)dlsym(mZfsSo, "zpool_get_config");
        mZpoolVdevName  =  (PFNZPOOLVDEVNAME)(uintptr_t)dlsym(mZfsSo, "zpool_vdev_name");

        if (   mZfsInit
            && mZfsOpen
            && mZfsClose
            && mZfsPropGetInt
            && mZpoolOpen
            && mZpoolClose
            && mZpoolGetConfig
            && mZpoolVdevName)
            mZfsLib = mZfsInit();
        else
            LogRel(("Incompatible libzfs? libzfs_init=%p zfs_open=%p zfs_close=%p zfs_prop_get_int=%p\n",
                    mZfsInit, mZfsOpen, mZfsClose, mZfsPropGetInt));
    }

    updateFilesystemMap();
    /* Notice that mCpus member will be initialized by HostCpuLoadRaw::init() */

    uint64_t cb;
    int vrc = RTSystemQueryTotalRam(&cb);
    if (RT_FAILURE(vrc))
        totalRAM = 0;
    else
        totalRAM = (ULONG)(cb / 1024);
}

CollectorSolaris::~CollectorSolaris()
{
    if (mKC)
        kstat_close(mKC);
    /* Not calling libzfs_fini() causes file descriptor leaks (#6788). */
    if (mZfsFini && mZfsLib)
        mZfsFini(mZfsLib);
    if (mZfsSo)
        dlclose(mZfsSo);
}

int CollectorSolaris::getRawHostCpuLoad(uint64_t *user, uint64_t *kernel, uint64_t *idle)
{
    kstat_t *ksp;
    uint64_t tmpUser, tmpKernel, tmpIdle;
    int cpus;
    cpu_stat_t cpu_stats;

    if (mKC == 0)
        return VERR_INTERNAL_ERROR;

    tmpUser = tmpKernel = tmpIdle = cpus = 0;
    for (ksp = mKC->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
        if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
            if (kstat_read(mKC, ksp, &cpu_stats) == -1)
            {
                Log(("kstat_read() -> %d\n", errno));
                return VERR_INTERNAL_ERROR;
            }
            ++cpus;
            tmpUser   += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
            tmpKernel += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
            tmpIdle   += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
        }
    }

    if (cpus == 0)
    {
        Log(("no cpu stats found!\n"));
        return VERR_INTERNAL_ERROR;
    }
    else
        mCpus = cpus;

    if (user)   *user   = tmpUser;
    if (kernel) *kernel = tmpKernel;
    if (idle)   *idle   = tmpIdle;

    return VINF_SUCCESS;
}

int CollectorSolaris::getRawProcessCpuLoad(RTPROCESS process, uint64_t *user, uint64_t *kernel, uint64_t *total)
{
    int vrc = VINF_SUCCESS;
    char *pszName;
    prusage_t prusage;

    RTStrAPrintf(&pszName, "/proc/%d/usage", process);
    Log(("Opening %s...\n", pszName));
    int h = open(pszName, O_RDONLY);
    RTStrFree(pszName);

    if (h != -1)
    {
        if (read(h, &prusage, sizeof(prusage)) == sizeof(prusage))
        {
            //Assert((pid_t)process == pstatus.pr_pid);
            //Log(("user=%u kernel=%u total=%u\n", prusage.pr_utime.tv_sec, prusage.pr_stime.tv_sec, prusage.pr_tstamp.tv_sec));
            /*
             * The CPU time spent must be adjusted by the number of cores for compatibility with
             * other platforms (see @bugref{6345}).
             */
            Assert(mCpus);
            if (mCpus)
            {
                *user = ((uint64_t)prusage.pr_utime.tv_sec * 1000000000 + prusage.pr_utime.tv_nsec) / mCpus;
                *kernel = ((uint64_t)prusage.pr_stime.tv_sec * 1000000000 + prusage.pr_stime.tv_nsec) / mCpus;
            }
            else
                *user = *kernel = 0;
            *total = (uint64_t)prusage.pr_tstamp.tv_sec * 1000000000 + prusage.pr_tstamp.tv_nsec;
            //Log(("user=%llu kernel=%llu total=%llu\n", *user, *kernel, *total));
        }
        else
        {
            Log(("read() -> %d\n", errno));
            vrc = VERR_FILE_IO_ERROR;
        }
        close(h);
    }
    else
    {
        Log(("open() -> %d\n", errno));
        vrc = VERR_ACCESS_DENIED;
    }

    return vrc;
}

int CollectorSolaris::getHostMemoryUsage(ULONG *total, ULONG *used, ULONG *available)
{
    AssertReturn(totalRAM, VERR_INTERNAL_ERROR);
    uint64_t cb;
    int vrc = RTSystemQueryAvailableRam(&cb);
    if (RT_SUCCESS(vrc))
    {
        *total = totalRAM;
        *available = (ULONG)RT_MIN(cb / 1024, ~(ULONG)0);
        *used = *total - *available;
    }
    return vrc;
}

int CollectorSolaris::getProcessMemoryUsage(RTPROCESS process, ULONG *used)
{
    int vrc = VINF_SUCCESS;
    char *pszName = NULL;
    psinfo_t psinfo;

    RTStrAPrintf(&pszName, "/proc/%d/psinfo", process);
    Log(("Opening %s...\n", pszName));
    int h = open(pszName, O_RDONLY);
    RTStrFree(pszName);

    if (h != -1)
    {
        /* psinfo_t keeps growing, so only read what we need to maximize
         * cross-version compatibility. The structures are compatible. */
        ssize_t cb = RT_UOFFSETOF(psinfo_t, pr_rssize) + RT_SIZEOFMEMB(psinfo_t, pr_rssize);
        AssertCompile(RTASSERT_OFFSET_OF(psinfo_t, pr_rssize) > RTASSERT_OFFSET_OF(psinfo_t, pr_pid));
        if (read(h, &psinfo, cb) == cb)
        {
            Assert((pid_t)process == psinfo.pr_pid);
            *used = (ULONG)RT_MIN(psinfo.pr_rssize, ~(ULONG)0);
        }
        else
        {
            Log(("read() -> %d\n", errno));
            vrc = VERR_FILE_IO_ERROR;
        }
        close(h);
    }
    else
    {
        Log(("open() -> %d\n", errno));
        vrc = VERR_ACCESS_DENIED;
    }

    return vrc;
}

uint32_t CollectorSolaris::getInstance(const char *pszIfaceName, char *pszDevName)
{
    /*
     * Get the instance number from the interface name, then clip it off.
     */
    int cbInstance = 0;
    size_t cbIface = strlen(pszIfaceName);
    const char *pszEnd = pszIfaceName + cbIface - 1;
    for (size_t i = 0; i < cbIface - 1; i++)
    {
        if (!RT_C_IS_DIGIT(*pszEnd))
            break;
        cbInstance++;
        pszEnd--;
    }

    uint32_t uInstance = RTStrToUInt32(pszEnd + 1);
    strncpy(pszDevName, pszIfaceName, cbIface - cbInstance);
    pszDevName[cbIface - cbInstance] = '\0';
    return uInstance;
}

uint64_t CollectorSolaris::wrapCorrection(uint32_t cur, uint64_t prev, const char *name)
{
    NOREF(name);
    uint64_t corrected = (prev & 0xffffffff00000000) + cur;
    if (cur < (prev & 0xffffffff))
    {
        /* wrap has occurred */
        corrected += 0x100000000;
        LogFlowThisFunc(("Corrected wrap on %s (%u < %u), returned %llu.\n",
                         name, cur, (uint32_t)prev, corrected));
    }
    return corrected;
}

uint64_t CollectorSolaris::wrapDetection(uint64_t cur, uint64_t prev, const char *name)
{
    if (cur < prev)
        LogRelMax(2, ("Detected wrap on %s (%llu < %llu).\n", name, cur, prev));
    return cur;
}

/*
 * WARNING! This function expects the previous values of rx and tx counter to
 * be passed in as well as returnes new values in the same parameters. This is
 * needed to provide a workaround for 32-bit counter wrapping.
 */
int CollectorSolaris::getRawHostNetworkLoad(const char *name, uint64_t *rx, uint64_t *tx)
{
#if ARCH_BITS == 32
    static bool g_fNotReported = true;
#endif
    AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
    LogFlowThisFunc(("m=%s i=%d n=%s\n", "link", -1, name));
    kstat_t *ksAdapter = kstat_lookup(mKC, (char *)"link", -1, (char *)name);
    if (ksAdapter == 0)
    {
        char szModule[KSTAT_STRLEN];
        uint32_t uInstance = getInstance(name, szModule);
        LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, "phys"));
        ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)"phys");
        if (ksAdapter == 0)
        {
            LogFlowThisFunc(("m=%s i=%u n=%s\n", szModule, uInstance, name));
            ksAdapter = kstat_lookup(mKC, szModule, uInstance, (char *)name);
            if (ksAdapter == 0)
            {
                static uint32_t s_tsLogRelLast;
                uint32_t tsNow = RTTimeProgramSecTS();
                if (   tsNow < RT_SEC_1HOUR
                    || (tsNow - s_tsLogRelLast >= 60))
                {
                    s_tsLogRelLast = tsNow;
                    LogRel(("Failed to get network statistics for %s. Max one msg/min.\n", name));
                }
                return VERR_INTERNAL_ERROR;
            }
        }
    }
    if (kstat_read(mKC, ksAdapter, 0) == -1)
    {
        LogRel(("kstat_read(adapter) -> %d\n", errno));
        return VERR_INTERNAL_ERROR;
    }
    kstat_named_t *kn;
    if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes64")) == NULL)
    {
        if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"rbytes")) == NULL)
        {
            LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
            return VERR_INTERNAL_ERROR;
        }
#if ARCH_BITS == 32
        if (g_fNotReported)
        {
            g_fNotReported = false;
            LogRel(("Failed to locate rbytes64, falling back to 32-bit counters...\n"));
        }
        *rx = wrapCorrection(kn->value.ul, *rx, "rbytes");
#else
        AssertCompile(sizeof(kn->value.ul) == sizeof(uint64_t));
        *rx = wrapDetection(kn->value.ul, *rx, "rbytes");
#endif
    }
    else
        *rx = wrapDetection(kn->value.ull, *rx, "rbytes64");
    if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes64")) == NULL)
    {
        if ((kn = (kstat_named_t *)kstat_data_lookup(ksAdapter, (char *)"obytes")) == NULL)
        {
            LogRel(("kstat_data_lookup(obytes) -> %d\n", errno));
            return VERR_INTERNAL_ERROR;
        }
#if ARCH_BITS == 32
        if (g_fNotReported)
        {
            g_fNotReported = false;
            LogRel(("Failed to locate obytes64, falling back to 32-bit counters...\n"));
        }
        *tx = wrapCorrection(kn->value.ul, *tx, "obytes");
#else
        AssertCompile(sizeof(kn->value.ul) == sizeof(uint64_t));
        *tx = wrapDetection(kn->value.ul, *tx, "obytes");
#endif
    }
    else
        *tx = wrapDetection(kn->value.ull, *tx, "obytes64");
    return VINF_SUCCESS;
}

int CollectorSolaris::getRawHostDiskLoad(const char *name, uint64_t *disk_ms, uint64_t *total_ms)
{
    int vrc = VINF_SUCCESS;
    AssertReturn(strlen(name) < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
    LogFlowThisFunc(("n=%s\n", name));
    kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, (char *)name);
    if (ksDisk != 0)
    {
        if (kstat_read(mKC, ksDisk, 0) == -1)
        {
            LogRel(("kstat_read(%s) -> %d\n", name, errno));
            vrc = VERR_INTERNAL_ERROR;
        }
        else
        {
            kstat_io_t *ksIo = KSTAT_IO_PTR(ksDisk);
            /*
             * We do not care for wrap possibility here, although we may
             * reconsider in about 300 years (9223372036854775807 ns).
             */
            *disk_ms = ksIo->rtime / 1000000;
            *total_ms = ksDisk->ks_snaptime / 1000000;
        }
    }
    else
    {
        LogRel(("kstat_lookup(%s) -> %d\n", name, errno));
        vrc = VERR_INTERNAL_ERROR;
    }

    return vrc;
}

uint64_t CollectorSolaris::getZfsTotal(uint64_t cbTotal, const char *szFsType, const char *szFsName)
{
    if (strcmp(szFsType, "zfs"))
        return cbTotal;
    FsMap::iterator it = mFsMap.find(szFsName);
    if (it == mFsMap.end())
        return cbTotal;

    char *pszDataset = strdup(it->second.c_str());
    char *pszEnd = pszDataset + strlen(pszDataset);
    uint64_t uAvail = 0;
    while (pszEnd)
    {
        zfs_handle_t *hDataset;

        *pszEnd = 0;
        hDataset = mZfsOpen(mZfsLib, pszDataset, ZFS_TYPE_DATASET);
        if (!hDataset)
            break;

        if (uAvail == 0)
        {
            uAvail = mZfsPropGetInt(hDataset, ZFS_PROP_REFQUOTA);
            if (uAvail == 0)
                uAvail = UINT64_MAX;
        }

        uint64_t uQuota = mZfsPropGetInt(hDataset, ZFS_PROP_QUOTA);
        if (uQuota && uAvail > uQuota)
            uAvail = uQuota;

        pszEnd = strrchr(pszDataset, '/');
        if (!pszEnd)
        {
            uint64_t uPoolSize = mZfsPropGetInt(hDataset, ZFS_PROP_USED) +
                                 mZfsPropGetInt(hDataset, ZFS_PROP_AVAILABLE);
            if (uAvail > uPoolSize)
                uAvail = uPoolSize;
        }
        mZfsClose(hDataset);
    }
    free(pszDataset);

    return uAvail ? uAvail : cbTotal;
}

int CollectorSolaris::getHostFilesystemUsage(const char *path, ULONG *total, ULONG *used, ULONG *available)
{
    struct statvfs64 stats;

    if (statvfs64(path, &stats) == -1)
    {
        LogRel(("Failed to collect %s filesystem usage: errno=%d.\n", path, errno));
        return VERR_ACCESS_DENIED;
    }
    uint64_t cbBlock = stats.f_frsize ? stats.f_frsize : stats.f_bsize;
    *total = (ULONG)(getZfsTotal(cbBlock * stats.f_blocks, stats.f_basetype, path) / _1M);
    LogFlowThisFunc(("f_blocks=%llu.\n", stats.f_blocks));
    *used  = (ULONG)(cbBlock * (stats.f_blocks - stats.f_bfree) / _1M);
    *available = (ULONG)(cbBlock * stats.f_bavail / _1M);

    return VINF_SUCCESS;
}

int CollectorSolaris::getHostDiskSize(const char *name, uint64_t *size)
{
    int vrc = VINF_SUCCESS;
    AssertReturn(strlen(name) + 5 < KSTAT_STRLEN, VERR_INVALID_PARAMETER);
    LogFlowThisFunc(("n=%s\n", name));
    char szName[KSTAT_STRLEN];
    strcpy(szName, name);
    strcat(szName, ",err");
    kstat_t *ksDisk = kstat_lookup(mKC, NULL, -1, szName);
    if (ksDisk != 0)
    {
        if (kstat_read(mKC, ksDisk, 0) == -1)
        {
            LogRel(("kstat_read(%s) -> %d\n", name, errno));
            vrc = VERR_INTERNAL_ERROR;
        }
        else
        {
            kstat_named_t *kn;
            if ((kn = (kstat_named_t *)kstat_data_lookup(ksDisk, (char *)"Size")) == 0)
            {
                LogRel(("kstat_data_lookup(rbytes) -> %d, name=%s\n", errno, name));
                return VERR_INTERNAL_ERROR;
            }
            *size = kn->value.ull;
        }
    }
    else
    {
        LogRel(("kstat_lookup(%s) -> %d\n", szName, errno));
        vrc = VERR_INTERNAL_ERROR;
    }


    return vrc;
}

RTCString CollectorSolaris::physToInstName(const char *pcszPhysName)
{
    FILE *fp = fopen("/etc/path_to_inst", "r");
    if (!fp)
        return RTCString();

    RTCString strInstName;
    size_t cbName = strlen(pcszPhysName);
    char szBuf[RTPATH_MAX];
    while (fgets(szBuf, sizeof(szBuf), fp))
    {
        if (szBuf[0] == '"' && strncmp(szBuf + 1, pcszPhysName, cbName) == 0)
        {
            char *pszDriver, *pszInstance;
            pszDriver = strrchr(szBuf, '"');
            if (pszDriver)
            {
                *pszDriver = '\0';
                pszDriver = strrchr(szBuf, '"');
                if (pszDriver)
                {
                    *pszDriver++ = '\0';
                    pszInstance = strrchr(szBuf, ' ');
                    if (pszInstance)
                    {
                        *pszInstance = '\0';
                        pszInstance = strrchr(szBuf, ' ');
                        if (pszInstance)
                        {
                            *pszInstance++ = '\0';
                            strInstName = pszDriver;
                            strInstName += pszInstance;
                            break;
                        }
                    }
                }
            }
        }
    }
    fclose(fp);

    return strInstName;
}

RTCString CollectorSolaris::pathToInstName(const char *pcszDevPathName)
{
    char szLink[RTPATH_MAX];
    if (readlink(pcszDevPathName, szLink, sizeof(szLink)) != -1)
    {
        char *pszStart, *pszEnd;
        pszStart = strstr(szLink, "/devices/");
        pszEnd = strrchr(szLink, ':');
        if (pszStart && pszEnd)
        {
            pszStart += 8; // Skip "/devices"
            *pszEnd = '\0'; // Trim partition
            return physToInstName(pszStart);
        }
    }

    return RTCString(pcszDevPathName);
}

int CollectorSolaris::getDiskListByFs(const char *name, DiskList& listUsage, DiskList& listLoad)
{
    FsMap::iterator it = mFsMap.find(name);
    if (it == mFsMap.end())
        return VERR_INVALID_PARAMETER;

    RTCString strName = it->second.substr(0, it->second.find("/"));
    if (mZpoolOpen && mZpoolClose && mZpoolGetConfig && !strName.isEmpty())
    {
        zpool_handle_t *zh = mZpoolOpen(mZfsLib, strName.c_str());
        if (zh)
        {
            unsigned int cChildren = 0;
            nvlist_t **nvChildren  = NULL;
            nvlist_t *nvRoot       = NULL;
            nvlist_t *nvConfig     = mZpoolGetConfig(zh, NULL);
            if (   !nvlist_lookup_nvlist(nvConfig, ZPOOL_CONFIG_VDEV_TREE, &nvRoot)
                && !nvlist_lookup_nvlist_array(nvRoot, ZPOOL_CONFIG_CHILDREN, &nvChildren, &cChildren))
            {
                for (unsigned int i = 0; i < cChildren; ++i)
                {
                    uint64_t fHole = 0;
                    uint64_t fLog  = 0;

                    nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_HOLE, &fHole);
                    nvlist_lookup_uint64(nvChildren[i], ZPOOL_CONFIG_IS_LOG,  &fLog);

                    if (!fHole && !fLog)
                    {
                        char *pszChildName = mZpoolVdevName(mZfsLib, zh, nvChildren[i], _B_FALSE);
                        Assert(pszChildName);
                        RTCString strDevPath("/dev/dsk/");
                        strDevPath += pszChildName;
                        char szLink[RTPATH_MAX];
                        if (readlink(strDevPath.c_str(), szLink, sizeof(szLink)) != -1)
                        {
                            char *pszStart, *pszEnd;
                            pszStart = strstr(szLink, "/devices/");
                            pszEnd = strrchr(szLink, ':');
                            if (pszStart && pszEnd)
                            {
                                pszStart += 8; // Skip "/devices"
                                *pszEnd = '\0'; // Trim partition
                                listUsage.push_back(physToInstName(pszStart));
                            }
                        }
                        free(pszChildName);
                    }
                }
            }
            mZpoolClose(zh);
        }
    }
    else
        listUsage.push_back(pathToInstName(it->second.c_str()));
    listLoad = listUsage;
    return VINF_SUCCESS;
}

void CollectorSolaris::updateFilesystemMap(void)
{
    FILE *fp = fopen("/etc/mnttab", "r");
    if (fp)
    {
        struct mnttab Entry;
        int iRc = 0;
        resetmnttab(fp);
        while ((iRc = getmntent(fp, &Entry)) == 0)
            mFsMap[Entry.mnt_mountp] = Entry.mnt_special;
        fclose(fp);
        if (iRc != -1)
            LogRel(("Error while reading mnttab: %d\n", iRc));
    }
}

}