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 | |
42 | void VirtualizationSupport::initialize() { |
43 | // open vmguestlib and bind SDK functions |
44 | char ebuf[1024]; |
45 | dlHandle = os::dll_load("vmGuestLib" , ebuf, sizeof ebuf); |
46 | |
47 | #ifdef LINUX |
48 | if (dlHandle == NULL) { |
49 | // the open-vm-tools have a different guest lib name |
50 | // on some distros e.g. SLES12 the open-vm-tools are the default, |
51 | // so use the different libname as a fallback |
52 | dlHandle = os::dll_load("/usr/lib64/libguestlib.so.0" , ebuf, sizeof ebuf); |
53 | } |
54 | #endif |
55 | if (dlHandle == NULL) { |
56 | return; |
57 | } |
58 | |
59 | GuestLib_StatGet = CAST_TO_FN_PTR(GuestLib_StatGet_t, os::dll_lookup(dlHandle, "VMGuestLib_StatGet" )); |
60 | GuestLib_StatFree = CAST_TO_FN_PTR(GuestLib_StatFree_t, os::dll_lookup(dlHandle, "VMGuestLib_StatFree" )); |
61 | |
62 | if (GuestLib_StatGet != NULL && GuestLib_StatFree != NULL) { |
63 | char* result_info = NULL; |
64 | size_t result_size = 0; |
65 | VMGuestLibError sg_error = GuestLib_StatGet("text" , "resources" , &result_info, &result_size); |
66 | if (sg_error == VMGUESTLIB_ERROR_SUCCESS) { |
67 | has_resource_information = true; |
68 | os::snprintf(extended_resource_info_at_startup, sizeof(extended_resource_info_at_startup), "%s" , result_info); |
69 | GuestLib_StatFree(result_info, result_size); |
70 | } |
71 | sg_error = GuestLib_StatGet("text" , "host" , &result_info, &result_size); |
72 | if (sg_error == VMGUESTLIB_ERROR_SUCCESS) { |
73 | has_host_information = true; |
74 | os::snprintf(host_information, sizeof(host_information), "%s" , result_info); |
75 | GuestLib_StatFree(result_info, result_size); |
76 | } |
77 | } |
78 | } |
79 | |
80 | void VirtualizationSupport::print_virtualization_info(outputStream* st) { |
81 | if (has_host_information) { |
82 | st->print_cr("vSphere host information:" ); |
83 | st->print_cr("%s" , host_information); |
84 | } |
85 | // resource info at startup |
86 | if (has_resource_information) { |
87 | st->print_cr("vSphere resource information collected at VM startup:" ); |
88 | st->print_cr("%s" , extended_resource_info_at_startup); |
89 | } |
90 | // current resource info |
91 | if (GuestLib_StatGet != NULL && GuestLib_StatFree != NULL) { |
92 | char* result_info = NULL; |
93 | size_t result_size = 0; |
94 | VMGuestLibError sg_error = GuestLib_StatGet("text" , "resources" , &result_info, &result_size); |
95 | if (sg_error == VMGUESTLIB_ERROR_SUCCESS) { |
96 | st->print_cr("vSphere resource information available now:" ); |
97 | st->print_cr("%s" , result_info); |
98 | GuestLib_StatFree(result_info, result_size); |
99 | } |
100 | } |
101 | } |
102 | |
103 | |