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