1 /*
   2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "compiler/compilerDefinitions.hpp"
  27 #include "runtime/arguments.hpp"
  28 #include "runtime/vm_version.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 const char* Abstract_VM_Version::_s_vm_release = Abstract_VM_Version::vm_release();
  32 const char* Abstract_VM_Version::_s_internal_vm_info_string = Abstract_VM_Version::internal_vm_info_string();
  33 
  34 uint64_t Abstract_VM_Version::_features = 0;
  35 const char* Abstract_VM_Version::_features_string = "";
  36 
  37 bool Abstract_VM_Version::_supports_cx8 = false;
  38 bool Abstract_VM_Version::_supports_atomic_getset4 = false;
  39 bool Abstract_VM_Version::_supports_atomic_getset8 = false;
  40 bool Abstract_VM_Version::_supports_atomic_getadd4 = false;
  41 bool Abstract_VM_Version::_supports_atomic_getadd8 = false;
  42 unsigned int Abstract_VM_Version::_logical_processors_per_package = 1U;
  43 unsigned int Abstract_VM_Version::_L1_data_cache_line_size = 0;
  44 unsigned int Abstract_VM_Version::_data_cache_line_flush_size = 0;
  45 
  46 VirtualizationType Abstract_VM_Version::_detected_virtualization = NoDetectedVirtualization;
  47 
  48 #ifndef HOTSPOT_VERSION_STRING
  49   #error HOTSPOT_VERSION_STRING must be defined
  50 #endif
  51 
  52 #ifndef VERSION_FEATURE
  53   #error VERSION_FEATURE must be defined
  54 #endif
  55 #ifndef VERSION_INTERIM
  56   #error VERSION_INTERIM must be defined
  57 #endif
  58 #ifndef VERSION_UPDATE
  59   #error VERSION_UPDATE must be defined
  60 #endif
  61 #ifndef VERSION_PATCH
  62   #error VERSION_PATCH must be defined
  63 #endif
  64 #ifndef VERSION_BUILD
  65   #error VERSION_BUILD must be defined
  66 #endif
  67 
  68 #ifndef VERSION_STRING
  69   #error VERSION_STRING must be defined
  70 #endif
  71 
  72 #ifndef DEBUG_LEVEL
  73   #error DEBUG_LEVEL must be defined
  74 #endif
  75 
  76 #define VM_RELEASE HOTSPOT_VERSION_STRING
  77 
  78 // HOTSPOT_VERSION_STRING equals the JDK VERSION_STRING (unless overridden
  79 // in a standalone build).
  80 int Abstract_VM_Version::_vm_major_version = VERSION_FEATURE;
  81 int Abstract_VM_Version::_vm_minor_version = VERSION_INTERIM;
  82 int Abstract_VM_Version::_vm_security_version = VERSION_UPDATE;
  83 int Abstract_VM_Version::_vm_patch_version = VERSION_PATCH;
  84 int Abstract_VM_Version::_vm_build_number = VERSION_BUILD;
  85 unsigned int Abstract_VM_Version::_parallel_worker_threads = 0;
  86 bool Abstract_VM_Version::_parallel_worker_threads_initialized = false;
  87 
  88 #if defined(_LP64)
  89   #define VMLP "64-Bit "
  90 #else
  91   #define VMLP ""
  92 #endif
  93 
  94 #ifndef VMTYPE
  95   #ifdef TIERED
  96     #define VMTYPE "Server"
  97   #else // TIERED
  98   #ifdef ZERO
  99     #define VMTYPE "Zero"
 100   #else // ZERO
 101      #define VMTYPE COMPILER1_PRESENT("Client")   \
 102                     COMPILER2_PRESENT("Server")
 103   #endif // ZERO
 104   #endif // TIERED
 105 #endif
 106 
 107 #ifndef HOTSPOT_VM_DISTRO
 108   #error HOTSPOT_VM_DISTRO must be defined
 109 #endif
 110 #define VMNAME HOTSPOT_VM_DISTRO " " VMLP VMTYPE " VM"
 111 
 112 const char* Abstract_VM_Version::vm_name() {
 113   return VMNAME;
 114 }
 115 
 116 
 117 const char* Abstract_VM_Version::vm_vendor() {
 118 #ifdef VENDOR
 119   return VENDOR;
 120 #else
 121   return "Oracle Corporation";
 122 #endif
 123 }
 124 
 125 
 126 const char* Abstract_VM_Version::vm_info_string() {
 127   switch (Arguments::mode()) {
 128     case Arguments::_int:
 129       return UseSharedSpaces ? "interpreted mode, sharing" : "interpreted mode";
 130     case Arguments::_mixed:
 131       if (UseSharedSpaces) {
 132         if (UseAOT) {
 133           return "mixed mode, aot, sharing";
 134 #ifdef TIERED
 135         } else if(is_client_compilation_mode_vm()) {
 136           return "mixed mode, emulated-client, sharing";
 137 #endif
 138         } else {
 139           return "mixed mode, sharing";
 140          }
 141       } else {
 142         if (UseAOT) {
 143           return "mixed mode, aot";
 144 #ifdef TIERED
 145         } else if(is_client_compilation_mode_vm()) {
 146           return "mixed mode, emulated-client";
 147 #endif
 148         } else {
 149           return "mixed mode";
 150         }
 151       }
 152     case Arguments::_comp:
 153 #ifdef TIERED
 154       if (is_client_compilation_mode_vm()) {
 155          return UseSharedSpaces ? "compiled mode, emulated-client, sharing" : "compiled mode, emulated-client";
 156       }
 157 #endif
 158       return UseSharedSpaces ? "compiled mode, sharing"    : "compiled mode";
 159   };
 160   ShouldNotReachHere();
 161   return "";
 162 }
 163 
 164 // NOTE: do *not* use stringStream. this function is called by
 165 //       fatal error handler. if the crash is in native thread,
 166 //       stringStream cannot get resource allocated and will SEGV.
 167 const char* Abstract_VM_Version::vm_release() {
 168   return VM_RELEASE;
 169 }
 170 
 171 // NOTE: do *not* use stringStream. this function is called by
 172 //       fatal error handlers. if the crash is in native thread,
 173 //       stringStream cannot get resource allocated and will SEGV.
 174 const char* Abstract_VM_Version::jre_release_version() {
 175   return VERSION_STRING;
 176 }
 177 
 178 #define OS       LINUX_ONLY("linux")             \
 179                  WINDOWS_ONLY("windows")         \
 180                  SOLARIS_ONLY("solaris")         \
 181                  AIX_ONLY("aix")                 \
 182                  BSD_ONLY("bsd")
 183 
 184 #ifndef CPU
 185 #ifdef ZERO
 186 #define CPU      ZERO_LIBARCH
 187 #elif defined(PPC64)
 188 #if defined(VM_LITTLE_ENDIAN)
 189 #define CPU      "ppc64le"
 190 #else
 191 #define CPU      "ppc64"
 192 #endif // PPC64
 193 #else
 194 #define CPU      AARCH64_ONLY("aarch64")         \
 195                  AMD64_ONLY("amd64")             \
 196                  IA32_ONLY("x86")                \
 197                  IA64_ONLY("ia64")               \
 198                  S390_ONLY("s390")               \
 199                  SPARC_ONLY("sparc")
 200 #endif // !ZERO
 201 #endif // !CPU
 202 
 203 const char *Abstract_VM_Version::vm_platform_string() {
 204   return OS "-" CPU;
 205 }
 206 
 207 const char* Abstract_VM_Version::internal_vm_info_string() {
 208   #ifndef HOTSPOT_BUILD_USER
 209     #define HOTSPOT_BUILD_USER unknown
 210   #endif
 211 
 212   #ifndef HOTSPOT_BUILD_COMPILER
 213     #ifdef _MSC_VER
 214       #if _MSC_VER == 1600
 215         #define HOTSPOT_BUILD_COMPILER "MS VC++ 10.0 (VS2010)"
 216       #elif _MSC_VER == 1700
 217         #define HOTSPOT_BUILD_COMPILER "MS VC++ 11.0 (VS2012)"
 218       #elif _MSC_VER == 1800
 219         #define HOTSPOT_BUILD_COMPILER "MS VC++ 12.0 (VS2013)"
 220       #elif _MSC_VER == 1900
 221         #define HOTSPOT_BUILD_COMPILER "MS VC++ 14.0 (VS2015)"
 222       #elif _MSC_VER == 1911
 223         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.3 (VS2017)"
 224       #elif _MSC_VER == 1912
 225         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.5 (VS2017)"
 226       #elif _MSC_VER == 1913
 227         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.6 (VS2017)"
 228       #elif _MSC_VER == 1914
 229         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.7 (VS2017)"
 230       #elif _MSC_VER == 1915
 231         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.8 (VS2017)"
 232       #else
 233         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
 234       #endif
 235     #elif defined(__SUNPRO_CC)
 236       #if __SUNPRO_CC == 0x580
 237         #define HOTSPOT_BUILD_COMPILER "Workshop 5.8"
 238       #elif __SUNPRO_CC == 0x590
 239         #define HOTSPOT_BUILD_COMPILER "Workshop 5.9"
 240       #elif __SUNPRO_CC == 0x5100
 241         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u1"
 242       #elif __SUNPRO_CC == 0x5120
 243         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u3"
 244       #elif __SUNPRO_CC == 0x5130
 245         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u4"
 246       #else
 247         #define HOTSPOT_BUILD_COMPILER "unknown Workshop:" XSTR(__SUNPRO_CC)
 248       #endif
 249     #elif defined(__clang_version__)
 250         #define HOTSPOT_BUILD_COMPILER "clang " __VERSION__
 251     #elif defined(__GNUC__)
 252         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
 253     #else
 254       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
 255     #endif
 256   #endif
 257 
 258   #ifndef FLOAT_ARCH
 259     #if defined(__SOFTFP__)
 260       #define FLOAT_ARCH_STR "-sflt"
 261     #else
 262       #define FLOAT_ARCH_STR ""
 263     #endif
 264   #else
 265     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
 266   #endif
 267 
 268   #define INTERNAL_VERSION_SUFFIX VM_RELEASE ")" \
 269          " for " OS "-" CPU FLOAT_ARCH_STR \
 270          " JRE (" VERSION_STRING "), built on " __DATE__ " " __TIME__ \
 271          " by " XSTR(HOTSPOT_BUILD_USER) " with " HOTSPOT_BUILD_COMPILER
 272 
 273   return strcmp(DEBUG_LEVEL, "release") == 0
 274       ? VMNAME " (" INTERNAL_VERSION_SUFFIX
 275       : VMNAME " (" DEBUG_LEVEL " " INTERNAL_VERSION_SUFFIX;
 276 }
 277 
 278 const char *Abstract_VM_Version::vm_build_user() {
 279   return HOTSPOT_BUILD_USER;
 280 }
 281 
 282 const char *Abstract_VM_Version::jdk_debug_level() {
 283   return DEBUG_LEVEL;
 284 }
 285 
 286 const char *Abstract_VM_Version::printable_jdk_debug_level() {
 287   // Debug level is not printed for "release" builds
 288   return strcmp(DEBUG_LEVEL, "release") == 0 ? "" : DEBUG_LEVEL " ";
 289 }
 290 
 291 unsigned int Abstract_VM_Version::jvm_version() {
 292   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
 293          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
 294          ((Abstract_VM_Version::vm_security_version() & 0xFF) << 8) |
 295          (Abstract_VM_Version::vm_build_number() & 0xFF);
 296 }
 297 
 298 bool Abstract_VM_Version::print_matching_lines_from_file(const char* filename, outputStream* st, const char* keywords_to_match[]) {
 299   char line[500];
 300   FILE* fp = fopen(filename, "r");
 301   if (fp == NULL) {
 302     return false;
 303   }
 304 
 305   st->print_cr("Virtualization information:");
 306   while (fgets(line, sizeof(line), fp) != NULL) {
 307     int i = 0;
 308     while (keywords_to_match[i] != NULL) {
 309       if (strncmp(line, keywords_to_match[i], strlen(keywords_to_match[i])) == 0) {
 310         st->print("%s", line);
 311         break;
 312       }
 313       i++;
 314     }
 315   }
 316   fclose(fp);
 317   return true;
 318 }
 319 
 320 unsigned int Abstract_VM_Version::nof_parallel_worker_threads(
 321                                                       unsigned int num,
 322                                                       unsigned int den,
 323                                                       unsigned int switch_pt) {
 324   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 325     assert(ParallelGCThreads == 0, "Default ParallelGCThreads is not 0");
 326     unsigned int threads;
 327     // For very large machines, there are diminishing returns
 328     // for large numbers of worker threads.  Instead of
 329     // hogging the whole system, use a fraction of the workers for every
 330     // processor after the first 8.  For example, on a 72 cpu machine
 331     // and a chosen fraction of 5/8
 332     // use 8 + (72 - 8) * (5/8) == 48 worker threads.
 333     unsigned int ncpus = (unsigned int) os::initial_active_processor_count();
 334     threads = (ncpus <= switch_pt) ?
 335              ncpus :
 336              (switch_pt + ((ncpus - switch_pt) * num) / den);
 337 #ifndef _LP64
 338     // On 32-bit binaries the virtual address space available to the JVM
 339     // is usually limited to 2-3 GB (depends on the platform).
 340     // Do not use up address space with too many threads (stacks and per-thread
 341     // data). Note that x86 apps running on Win64 have 2 stacks per thread.
 342     // GC may more generally scale down threads by max heap size (etc), but the
 343     // consequences of over-provisioning threads are higher on 32-bit JVMS,
 344     // so add hard limit here:
 345     threads = MIN2(threads, (2*switch_pt));
 346 #endif
 347     return threads;
 348   } else {
 349     return ParallelGCThreads;
 350   }
 351 }
 352 
 353 unsigned int Abstract_VM_Version::calc_parallel_worker_threads() {
 354   return nof_parallel_worker_threads(5, 8, 8);
 355 }
 356 
 357 
 358 // Does not set the _initialized flag since it is
 359 // a global flag.
 360 unsigned int Abstract_VM_Version::parallel_worker_threads() {
 361   if (!_parallel_worker_threads_initialized) {
 362     if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 363       _parallel_worker_threads = VM_Version::calc_parallel_worker_threads();
 364     } else {
 365       _parallel_worker_threads = ParallelGCThreads;
 366     }
 367     _parallel_worker_threads_initialized = true;
 368   }
 369   return _parallel_worker_threads;
 370 }