1 /*
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2019 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27 #include "runtime/os.hpp"
28 #include "utilities/virtualizationSupport.hpp"
29
30 static void *dlHandle = NULL;
31
32 static GuestLib_StatGet_t GuestLib_StatGet = NULL;
33 static GuestLib_StatFree_t GuestLib_StatFree = NULL;
34
35 static bool has_host_information = false;
36 static bool has_resource_information = false;
37
38 // host + resource information; avoid the session and other special info vectors
39 static char host_information[300];
40 static char extended_resource_info_at_startup[600];
41
initialize()42 void VirtualizationSupport::initialize() {
43 if (!ExtensiveErrorReports) return;
44
45 // open vmguestlib and bind SDK functions
46 char ebuf[1024];
47 dlHandle = os::dll_load("vmGuestLib", ebuf, sizeof ebuf);
48
49 #ifdef LINUX
50 if (dlHandle == NULL) {
51 // the open-vm-tools have a different guest lib name
52 // on some distros e.g. SLES12 the open-vm-tools are the default,
53 // so use the different libname as a fallback
54 dlHandle = os::dll_load("/usr/lib64/libguestlib.so.0", ebuf, sizeof ebuf);
55 }
56 #endif
57 if (dlHandle == NULL) {
58 return;
59 }
60
61 GuestLib_StatGet = CAST_TO_FN_PTR(GuestLib_StatGet_t, os::dll_lookup(dlHandle, "VMGuestLib_StatGet"));
62 GuestLib_StatFree = CAST_TO_FN_PTR(GuestLib_StatFree_t, os::dll_lookup(dlHandle, "VMGuestLib_StatFree"));
63
64 if (GuestLib_StatGet != NULL && GuestLib_StatFree != NULL) {
65 char* result_info = NULL;
66 size_t result_size = 0;
67 VMGuestLibError sg_error = GuestLib_StatGet("text", "resources", &result_info, &result_size);
68 if (sg_error == VMGUESTLIB_ERROR_SUCCESS) {
69 has_resource_information = true;
70 os::snprintf(extended_resource_info_at_startup, sizeof(extended_resource_info_at_startup), "%s", result_info);
71 GuestLib_StatFree(result_info, result_size);
72 }
73 sg_error = GuestLib_StatGet("text", "host", &result_info, &result_size);
74 if (sg_error == VMGUESTLIB_ERROR_SUCCESS) {
75 has_host_information = true;
76 os::snprintf(host_information, sizeof(host_information), "%s", result_info);
77 GuestLib_StatFree(result_info, result_size);
78 }
79 }
80 }
81
print_virtualization_info(outputStream * st)82 void VirtualizationSupport::print_virtualization_info(outputStream* st) {
83 if (has_host_information) {
84 st->print_cr("vSphere host information:");
85 st->print_cr("%s", host_information);
86 }
87 // resource info at startup
88 if (has_resource_information) {
89 st->print_cr("vSphere resource information collected at VM startup:");
90 st->print_cr("%s", extended_resource_info_at_startup);
91 }
92 // current resource info
93 if (GuestLib_StatGet != NULL && GuestLib_StatFree != NULL) {
94 char* result_info = NULL;
95 size_t result_size = 0;
96 VMGuestLibError sg_error = GuestLib_StatGet("text", "resources", &result_info, &result_size);
97 if (sg_error == VMGUESTLIB_ERROR_SUCCESS) {
98 st->print_cr("vSphere resource information available now:");
99 st->print_cr("%s", result_info);
100 GuestLib_StatFree(result_info, result_size);
101 }
102 }
103 }
104
105