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