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 "logging/log.hpp"
  27 #include "logging/logStream.hpp"
  28 #include "memory/universe.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "runtime/vm_version.hpp"
  32 
  33 const char* Abstract_VM_Version::_s_vm_release = Abstract_VM_Version::vm_release();
  34 const char* Abstract_VM_Version::_s_internal_vm_info_string = Abstract_VM_Version::internal_vm_info_string();
  35 
  36 uint64_t Abstract_VM_Version::_features = 0;
  37 const char* Abstract_VM_Version::_features_string = "";
  38 
  39 bool Abstract_VM_Version::_supports_cx8 = false;
  40 bool Abstract_VM_Version::_supports_atomic_getset4 = false;
  41 bool Abstract_VM_Version::_supports_atomic_getset8 = false;
  42 bool Abstract_VM_Version::_supports_atomic_getadd4 = false;
  43 bool Abstract_VM_Version::_supports_atomic_getadd8 = false;
  44 unsigned int Abstract_VM_Version::_logical_processors_per_package = 1U;
  45 unsigned int Abstract_VM_Version::_L1_data_cache_line_size = 0;
  46 
  47 VirtualizationType Abstract_VM_Version::_detected_virtualization = NoDetectedVirtualization;
  48 
  49 #ifndef HOTSPOT_VERSION_STRING
  50   #error HOTSPOT_VERSION_STRING must be defined
  51 #endif
  52 
  53 #ifndef VERSION_FEATURE
  54   #error VERSION_FEATURE must be defined
  55 #endif
  56 #ifndef VERSION_INTERIM
  57   #error VERSION_INTERIM must be defined
  58 #endif
  59 #ifndef VERSION_UPDATE
  60   #error VERSION_UPDATE must be defined
  61 #endif
  62 #ifndef VERSION_PATCH
  63   #error VERSION_PATCH must be defined
  64 #endif
  65 #ifndef VERSION_BUILD
  66   #error VERSION_BUILD must be defined
  67 #endif
  68 
  69 #ifndef VERSION_STRING
  70   #error VERSION_STRING must be defined
  71 #endif
  72 
  73 #ifndef DEBUG_LEVEL
  74   #error DEBUG_LEVEL must be defined
  75 #endif
  76 
  77 #define VM_RELEASE HOTSPOT_VERSION_STRING
  78 
  79 // HOTSPOT_VERSION_STRING equals the JDK VERSION_STRING (unless overridden
  80 // in a standalone build).
  81 int Abstract_VM_Version::_vm_major_version = VERSION_FEATURE;
  82 int Abstract_VM_Version::_vm_minor_version = VERSION_INTERIM;
  83 int Abstract_VM_Version::_vm_security_version = VERSION_UPDATE;
  84 int Abstract_VM_Version::_vm_patch_version = VERSION_PATCH;
  85 int Abstract_VM_Version::_vm_build_number = VERSION_BUILD;
  86 unsigned int Abstract_VM_Version::_parallel_worker_threads = 0;
  87 bool Abstract_VM_Version::_parallel_worker_threads_initialized = false;
  88 
  89 #if defined(_LP64)
  90   #define VMLP "64-Bit "
  91 #else
  92   #define VMLP ""
  93 #endif
  94 
  95 #ifndef VMTYPE
  96   #ifdef TIERED
  97     #define VMTYPE "Server"
  98   #else // TIERED
  99   #ifdef ZERO
 100     #define VMTYPE "Zero"
 101   #else // ZERO
 102      #define VMTYPE COMPILER1_PRESENT("Client")   \
 103                     COMPILER2_PRESENT("Server")
 104   #endif // ZERO
 105   #endif // TIERED
 106 #endif
 107 
 108 #ifndef HOTSPOT_VM_DISTRO
 109   #error HOTSPOT_VM_DISTRO must be defined
 110 #endif
 111 #define VMNAME HOTSPOT_VM_DISTRO " " VMLP VMTYPE " VM"
 112 
 113 const char* Abstract_VM_Version::vm_name() {
 114   return VMNAME;
 115 }
 116 
 117 
 118 const char* Abstract_VM_Version::vm_vendor() {
 119 #ifdef VENDOR
 120   return VENDOR;
 121 #else
 122   return "Oracle Corporation";
 123 #endif
 124 }
 125 
 126 
 127 const char* Abstract_VM_Version::vm_info_string() {
 128   switch (Arguments::mode()) {
 129     case Arguments::_int:
 130       return UseSharedSpaces ? "interpreted mode, sharing" : "interpreted mode";
 131     case Arguments::_mixed:
 132       if (UseSharedSpaces) {
 133         if (UseAOT) {
 134           return "mixed mode, aot, sharing";
 135 #ifdef TIERED
 136         } else if(is_client_compilation_mode_vm()) {
 137           return "mixed mode, emulated-client, sharing";
 138 #endif
 139         } else {
 140           return "mixed mode, sharing";
 141          }
 142       } else {
 143         if (UseAOT) {
 144           return "mixed mode, aot";
 145 #ifdef TIERED
 146         } else if(is_client_compilation_mode_vm()) {
 147           return "mixed mode, emulated-client";
 148 #endif
 149         } else {
 150           return "mixed mode";
 151         }
 152       }
 153     case Arguments::_comp:
 154 #ifdef TIERED
 155       if (is_client_compilation_mode_vm()) {
 156          return UseSharedSpaces ? "compiled mode, emulated-client, sharing" : "compiled mode, emulated-client";
 157       }
 158 #endif
 159       return UseSharedSpaces ? "compiled mode, sharing"    : "compiled mode";
 160   };
 161   ShouldNotReachHere();
 162   return "";
 163 }
 164 
 165 // NOTE: do *not* use stringStream. this function is called by
 166 //       fatal error handler. if the crash is in native thread,
 167 //       stringStream cannot get resource allocated and will SEGV.
 168 const char* Abstract_VM_Version::vm_release() {
 169   return VM_RELEASE;
 170 }
 171 
 172 // NOTE: do *not* use stringStream. this function is called by
 173 //       fatal error handlers. if the crash is in native thread,
 174 //       stringStream cannot get resource allocated and will SEGV.
 175 const char* Abstract_VM_Version::jre_release_version() {
 176   return VERSION_STRING;
 177 }
 178 
 179 #define OS       LINUX_ONLY("linux")             \
 180                  WINDOWS_ONLY("windows")         \
 181                  SOLARIS_ONLY("solaris")         \
 182                  AIX_ONLY("aix")                 \
 183                  BSD_ONLY("bsd")
 184 
 185 #ifndef CPU
 186 #ifdef ZERO
 187 #define CPU      ZERO_LIBARCH
 188 #elif defined(PPC64)
 189 #if defined(VM_LITTLE_ENDIAN)
 190 #define CPU      "ppc64le"
 191 #else
 192 #define CPU      "ppc64"
 193 #endif // PPC64
 194 #else
 195 #define CPU      AARCH64_ONLY("aarch64")         \
 196                  AMD64_ONLY("amd64")             \
 197                  IA32_ONLY("x86")                \
 198                  IA64_ONLY("ia64")               \
 199                  S390_ONLY("s390")               \
 200                  SPARC_ONLY("sparc")
 201 #endif // !ZERO
 202 #endif // !CPU
 203 
 204 const char *Abstract_VM_Version::vm_platform_string() {
 205   return OS "-" CPU;
 206 }
 207 
 208 const char* Abstract_VM_Version::internal_vm_info_string() {
 209   #ifndef HOTSPOT_BUILD_USER
 210     #define HOTSPOT_BUILD_USER unknown
 211   #endif
 212 
 213   #ifndef HOTSPOT_BUILD_COMPILER
 214     #ifdef _MSC_VER
 215       #if _MSC_VER == 1600
 216         #define HOTSPOT_BUILD_COMPILER "MS VC++ 10.0 (VS2010)"
 217       #elif _MSC_VER == 1700
 218         #define HOTSPOT_BUILD_COMPILER "MS VC++ 11.0 (VS2012)"
 219       #elif _MSC_VER == 1800
 220         #define HOTSPOT_BUILD_COMPILER "MS VC++ 12.0 (VS2013)"
 221       #elif _MSC_VER == 1900
 222         #define HOTSPOT_BUILD_COMPILER "MS VC++ 14.0 (VS2015)"
 223       #elif _MSC_VER == 1911
 224         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.3 (VS2017)"
 225       #elif _MSC_VER == 1912
 226         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.5 (VS2017)"
 227       #elif _MSC_VER == 1913
 228         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.6 (VS2017)"
 229       #elif _MSC_VER == 1914
 230         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.7 (VS2017)"
 231       #elif _MSC_VER == 1915
 232         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.8 (VS2017)"
 233       #elif _MSC_VER == 1916
 234         #define HOTSPOT_BUILD_COMPILER "MS VC++ 15.9 (VS2017)"
 235       #elif _MSC_VER == 1920
 236         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.0 (VS2019)"
 237       #elif _MSC_VER == 1921
 238         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.1 (VS2019)"
 239       #elif _MSC_VER == 1922
 240         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.2 (VS2019)"
 241       #elif _MSC_VER == 1923
 242         #define HOTSPOT_BUILD_COMPILER "MS VC++ 16.3 (VS2019)"
 243       #else
 244         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
 245       #endif
 246     #elif defined(__SUNPRO_CC)
 247       #if   __SUNPRO_CC == 0x420
 248         #define HOTSPOT_BUILD_COMPILER "Workshop 4.2"
 249       #elif __SUNPRO_CC == 0x500
 250         #define HOTSPOT_BUILD_COMPILER "Workshop 5.0 compat=" XSTR(__SUNPRO_CC_COMPAT)
 251       #elif __SUNPRO_CC == 0x520
 252         #define HOTSPOT_BUILD_COMPILER "Workshop 5.2 compat=" XSTR(__SUNPRO_CC_COMPAT)
 253       #elif __SUNPRO_CC == 0x580
 254         #define HOTSPOT_BUILD_COMPILER "Workshop 5.8"
 255       #elif __SUNPRO_CC == 0x590
 256         #define HOTSPOT_BUILD_COMPILER "Workshop 5.9"
 257       #elif __SUNPRO_CC == 0x5100
 258         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u1"
 259       #elif __SUNPRO_CC == 0x5120
 260         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u3"
 261       #elif __SUNPRO_CC == 0x5130
 262         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u4"
 263       #else
 264         #define HOTSPOT_BUILD_COMPILER "unknown Workshop:" XSTR(__SUNPRO_CC)
 265       #endif
 266     #elif defined(__GNUC__)
 267         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
 268     #elif defined(__IBMCPP__)
 269         #define HOTSPOT_BUILD_COMPILER "xlC " XSTR(__IBMCPP__)
 270 
 271     #else
 272       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
 273     #endif
 274   #endif
 275 
 276   #ifndef FLOAT_ARCH
 277     #if defined(__SOFTFP__)
 278       #define FLOAT_ARCH_STR "-sflt"
 279     #else
 280       #define FLOAT_ARCH_STR ""
 281     #endif
 282   #else
 283     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
 284   #endif
 285 
 286   #define INTERNAL_VERSION_SUFFIX VM_RELEASE ")" \
 287          " for " OS "-" CPU FLOAT_ARCH_STR \
 288          " JRE (" VERSION_STRING "), built on " __DATE__ " " __TIME__ \
 289          " by " XSTR(HOTSPOT_BUILD_USER) " with " HOTSPOT_BUILD_COMPILER
 290 
 291   return strcmp(DEBUG_LEVEL, "release") == 0
 292       ? VMNAME " (" INTERNAL_VERSION_SUFFIX
 293       : VMNAME " (" DEBUG_LEVEL " " INTERNAL_VERSION_SUFFIX;
 294 }
 295 
 296 const char *Abstract_VM_Version::vm_build_user() {
 297   return HOTSPOT_BUILD_USER;
 298 }
 299 
 300 const char *Abstract_VM_Version::jdk_debug_level() {
 301   return DEBUG_LEVEL;
 302 }
 303 
 304 const char *Abstract_VM_Version::printable_jdk_debug_level() {
 305   // Debug level is not printed for "release" builds
 306   return strcmp(DEBUG_LEVEL, "release") == 0 ? "" : DEBUG_LEVEL " ";
 307 }
 308 
 309 unsigned int Abstract_VM_Version::jvm_version() {
 310   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
 311          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
 312          ((Abstract_VM_Version::vm_security_version() & 0xFF) << 8) |
 313          (Abstract_VM_Version::vm_build_number() & 0xFF);
 314 }
 315 
 316 void VM_Version_init() {
 317   VM_Version::initialize();
 318 
 319   if (log_is_enabled(Info, os, cpu)) {
 320     char buf[1024];
 321     ResourceMark rm;
 322     LogStream ls(Log(os, cpu)::info());
 323     os::print_cpu_info(&ls, buf, sizeof(buf));
 324   }
 325 }
 326 
 327 bool Abstract_VM_Version::print_matching_lines_from_file(const char* filename, outputStream* st, const char* keywords_to_match[]) {
 328   char line[500];
 329   FILE* fp = fopen(filename, "r");
 330   if (fp == NULL) {
 331     return false;
 332   }
 333 
 334   st->print_cr("Virtualization information:");
 335   while (fgets(line, sizeof(line), fp) != NULL) {
 336     int i = 0;
 337     while (keywords_to_match[i] != NULL) {
 338       if (strncmp(line, keywords_to_match[i], strlen(keywords_to_match[i])) == 0) {
 339         st->print("%s", line);
 340         break;
 341       }
 342       i++;
 343     }
 344   }
 345   fclose(fp);
 346   return true;
 347 }
 348 
 349 
 350 
 351 unsigned int Abstract_VM_Version::nof_parallel_worker_threads(
 352                                                       unsigned int num,
 353                                                       unsigned int den,
 354                                                       unsigned int switch_pt) {
 355   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 356     assert(ParallelGCThreads == 0, "Default ParallelGCThreads is not 0");
 357     unsigned int threads;
 358     // For very large machines, there are diminishing returns
 359     // for large numbers of worker threads.  Instead of
 360     // hogging the whole system, use a fraction of the workers for every
 361     // processor after the first 8.  For example, on a 72 cpu machine
 362     // and a chosen fraction of 5/8
 363     // use 8 + (72 - 8) * (5/8) == 48 worker threads.
 364     unsigned int ncpus = (unsigned int) os::initial_active_processor_count();
 365     threads = (ncpus <= switch_pt) ?
 366              ncpus :
 367              (switch_pt + ((ncpus - switch_pt) * num) / den);
 368 #ifndef _LP64
 369     // On 32-bit binaries the virtual address space available to the JVM
 370     // is usually limited to 2-3 GB (depends on the platform).
 371     // Do not use up address space with too many threads (stacks and per-thread
 372     // data). Note that x86 apps running on Win64 have 2 stacks per thread.
 373     // GC may more generally scale down threads by max heap size (etc), but the
 374     // consequences of over-provisioning threads are higher on 32-bit JVMS,
 375     // so add hard limit here:
 376     threads = MIN2(threads, (2*switch_pt));
 377 #endif
 378     return threads;
 379   } else {
 380     return ParallelGCThreads;
 381   }
 382 }
 383 
 384 unsigned int Abstract_VM_Version::calc_parallel_worker_threads() {
 385   return nof_parallel_worker_threads(5, 8, 8);
 386 }
 387 
 388 
 389 // Does not set the _initialized flag since it is
 390 // a global flag.
 391 unsigned int Abstract_VM_Version::parallel_worker_threads() {
 392   if (!_parallel_worker_threads_initialized) {
 393     if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 394       _parallel_worker_threads = VM_Version::calc_parallel_worker_threads();
 395     } else {
 396       _parallel_worker_threads = ParallelGCThreads;
 397     }
 398     _parallel_worker_threads_initialized = true;
 399   }
 400   return _parallel_worker_threads;
 401 }