1 /*
   2  * Copyright (c) 1998, 2015, 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 "code/codeCacheExtensions.hpp"
  27 #include "memory/universe.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/vm_version.hpp"
  31 
  32 const char* Abstract_VM_Version::_s_vm_release = Abstract_VM_Version::vm_release();
  33 const char* Abstract_VM_Version::_s_internal_vm_info_string = Abstract_VM_Version::internal_vm_info_string();
  34 bool Abstract_VM_Version::_supports_cx8 = false;
  35 bool Abstract_VM_Version::_supports_atomic_getset4 = false;
  36 bool Abstract_VM_Version::_supports_atomic_getset8 = false;
  37 bool Abstract_VM_Version::_supports_atomic_getadd4 = false;
  38 bool Abstract_VM_Version::_supports_atomic_getadd8 = false;
  39 unsigned int Abstract_VM_Version::_logical_processors_per_package = 1U;
  40 unsigned int Abstract_VM_Version::_L1_data_cache_line_size = 0;
  41 int Abstract_VM_Version::_reserve_for_allocation_prefetch = 0;
  42 
  43 #ifndef HOTSPOT_RELEASE_VERSION
  44   #error HOTSPOT_RELEASE_VERSION must be defined
  45 #endif
  46 
  47 #ifndef JDK_MAJOR_VERSION
  48   #error JDK_MAJOR_VERSION must be defined
  49 #endif
  50 #ifndef JDK_MINOR_VERSION
  51   #error JDK_MINOR_VERSION must be defined
  52 #endif
  53 #ifndef JDK_MICRO_VERSION
  54   #error JDK_MICRO_VERSION must be defined
  55 #endif
  56 #ifndef JDK_BUILD_NUMBER
  57   #error JDK_BUILD_NUMBER must be defined
  58 #endif
  59 
  60 #ifndef JRE_RELEASE_VERSION
  61   #error JRE_RELEASE_VERSION must be defined
  62 #endif
  63 
  64 // NOTE: Builds within Visual Studio do not define the build target in
  65 //       HOTSPOT_RELEASE_VERSION, so it must be done here
  66 #if defined(VISUAL_STUDIO_BUILD) && !defined(PRODUCT)
  67   #ifndef HOTSPOT_BUILD_TARGET
  68     #error HOTSPOT_BUILD_TARGET must be defined
  69   #endif
  70   #define VM_RELEASE HOTSPOT_RELEASE_VERSION "-" HOTSPOT_BUILD_TARGET
  71 #else
  72   #define VM_RELEASE HOTSPOT_RELEASE_VERSION
  73 #endif
  74 
  75 // HOTSPOT_RELEASE_VERSION follows the JDK release version naming convention
  76 // <major_ver>.<minor_ver>.<micro_ver>[-<identifier>][-<debug_target>][-b<nn>]
  77 int Abstract_VM_Version::_vm_major_version = 0;
  78 int Abstract_VM_Version::_vm_minor_version = 0;
  79 int Abstract_VM_Version::_vm_micro_version = 0;
  80 int Abstract_VM_Version::_vm_build_number = 0;
  81 bool Abstract_VM_Version::_initialized = false;
  82 unsigned int Abstract_VM_Version::_parallel_worker_threads = 0;
  83 bool Abstract_VM_Version::_parallel_worker_threads_initialized = false;
  84 
  85 #ifdef ASSERT
  86 static void assert_digits(const char * s, const char * message) {
  87   for (int i = 0; s[i] != '\0'; i++) {
  88     assert(isdigit(s[i]), "%s", message);
  89   }
  90 }
  91 #endif
  92 
  93 static void set_version_field(int * version_field, const char * version_str,
  94                               const char * const assert_msg) {
  95   if (version_str != NULL && *version_str != '\0') {
  96     DEBUG_ONLY(assert_digits(version_str, assert_msg));
  97     *version_field = atoi(version_str);
  98   }
  99 }
 100 
 101 void Abstract_VM_Version::initialize() {
 102   if (_initialized) {
 103     return;
 104   }
 105 
 106   set_version_field(&_vm_major_version, JDK_MAJOR_VERSION, "bad major version");
 107   set_version_field(&_vm_minor_version, JDK_MINOR_VERSION, "bad minor version");
 108   set_version_field(&_vm_micro_version, JDK_MICRO_VERSION, "bad micro version");
 109   int offset = (JDK_BUILD_NUMBER != NULL && JDK_BUILD_NUMBER[0] == 'b') ? 1 : 0;
 110   set_version_field(&_vm_build_number, &JDK_BUILD_NUMBER[offset],
 111                     "bad build number");
 112 
 113   _initialized = true;
 114 }
 115 
 116 #if defined(_LP64)
 117   #define VMLP "64-Bit "
 118 #else
 119   #define VMLP ""
 120 #endif
 121 
 122 #ifndef VMTYPE
 123   #ifdef TIERED
 124     #define VMTYPE "Server"
 125   #else // TIERED
 126   #ifdef ZERO
 127   #ifdef SHARK
 128     #define VMTYPE "Shark"
 129   #else // SHARK
 130     #define VMTYPE "Zero"
 131   #endif // SHARK
 132   #else // ZERO
 133      #define VMTYPE COMPILER1_PRESENT("Client")   \
 134                     COMPILER2_PRESENT("Server")
 135   #endif // ZERO
 136   #endif // TIERED
 137 #endif
 138 
 139 #ifndef HOTSPOT_VM_DISTRO
 140   #error HOTSPOT_VM_DISTRO must be defined
 141 #endif
 142 #define VMNAME HOTSPOT_VM_DISTRO " " VMLP EMBEDDED_ONLY("Embedded ") VMTYPE " VM"
 143 
 144 const char* Abstract_VM_Version::vm_name() {
 145   return VMNAME;
 146 }
 147 
 148 
 149 const char* Abstract_VM_Version::vm_vendor() {
 150 #ifdef VENDOR
 151   return XSTR(VENDOR);
 152 #else
 153   return "Oracle Corporation";
 154 #endif
 155 }
 156 
 157 const char* Abstract_VM_Version::vm_info_string() {
 158   if (CodeCacheExtensions::use_pregenerated_interpreter()) {
 159     return "interpreted mode, pregenerated";
 160   }
 161   switch (Arguments::mode()) {
 162     case Arguments::_int:
 163       return UseSharedSpaces ? "interpreted mode, sharing" : "interpreted mode";
 164     case Arguments::_mixed:
 165       return UseSharedSpaces ? "mixed mode, sharing"       :  "mixed mode";
 166     case Arguments::_comp:
 167       return UseSharedSpaces ? "compiled mode, sharing"    : "compiled mode";
 168   };
 169   ShouldNotReachHere();
 170   return "";
 171 }
 172 
 173 // NOTE: do *not* use stringStream. this function is called by
 174 //       fatal error handler. if the crash is in native thread,
 175 //       stringStream cannot get resource allocated and will SEGV.
 176 const char* Abstract_VM_Version::vm_release() {
 177   return VM_RELEASE;
 178 }
 179 
 180 // NOTE: do *not* use stringStream. this function is called by
 181 //       fatal error handlers. if the crash is in native thread,
 182 //       stringStream cannot get resource allocated and will SEGV.
 183 const char* Abstract_VM_Version::jre_release_version() {
 184   return JRE_RELEASE_VERSION;
 185 }
 186 
 187 #define OS       LINUX_ONLY("linux")             \
 188                  WINDOWS_ONLY("windows")         \
 189                  SOLARIS_ONLY("solaris")         \
 190                  AIX_ONLY("aix")                 \
 191                  BSD_ONLY("bsd")
 192 
 193 #ifndef CPU
 194 #ifdef ZERO
 195 #define CPU      ZERO_LIBARCH
 196 #elif defined(PPC64)
 197 #if defined(VM_LITTLE_ENDIAN)
 198 #define CPU      "ppc64le"
 199 #else
 200 #define CPU      "ppc64"
 201 #endif
 202 #else
 203 #define CPU      IA32_ONLY("x86")                \
 204                  IA64_ONLY("ia64")               \
 205                  AMD64_ONLY("amd64")             \
 206                  AARCH64_ONLY("aarch64")         \
 207                  SPARC_ONLY("sparc")
 208 #endif //
 209 #endif
 210 
 211 const char *Abstract_VM_Version::vm_platform_string() {
 212   return OS "-" CPU;
 213 }
 214 
 215 const char* Abstract_VM_Version::internal_vm_info_string() {
 216   #ifndef HOTSPOT_BUILD_USER
 217     #define HOTSPOT_BUILD_USER unknown
 218   #endif
 219 
 220   #ifndef HOTSPOT_BUILD_COMPILER
 221     #ifdef _MSC_VER
 222       #if _MSC_VER == 1600
 223         #define HOTSPOT_BUILD_COMPILER "MS VC++ 10.0 (VS2010)"
 224       #elif _MSC_VER == 1700
 225         #define HOTSPOT_BUILD_COMPILER "MS VC++ 11.0 (VS2012)"
 226       #elif _MSC_VER == 1800
 227         #define HOTSPOT_BUILD_COMPILER "MS VC++ 12.0 (VS2013)"
 228       #else
 229         #define HOTSPOT_BUILD_COMPILER "unknown MS VC++:" XSTR(_MSC_VER)
 230       #endif
 231     #elif defined(__SUNPRO_CC)
 232       #if   __SUNPRO_CC == 0x420
 233         #define HOTSPOT_BUILD_COMPILER "Workshop 4.2"
 234       #elif __SUNPRO_CC == 0x500
 235         #define HOTSPOT_BUILD_COMPILER "Workshop 5.0 compat=" XSTR(__SUNPRO_CC_COMPAT)
 236       #elif __SUNPRO_CC == 0x520
 237         #define HOTSPOT_BUILD_COMPILER "Workshop 5.2 compat=" XSTR(__SUNPRO_CC_COMPAT)
 238       #elif __SUNPRO_CC == 0x580
 239         #define HOTSPOT_BUILD_COMPILER "Workshop 5.8"
 240       #elif __SUNPRO_CC == 0x590
 241         #define HOTSPOT_BUILD_COMPILER "Workshop 5.9"
 242       #elif __SUNPRO_CC == 0x5100
 243         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u1"
 244       #elif __SUNPRO_CC == 0x5120
 245         #define HOTSPOT_BUILD_COMPILER "Sun Studio 12u3"
 246       #else
 247         #define HOTSPOT_BUILD_COMPILER "unknown Workshop:" XSTR(__SUNPRO_CC)
 248       #endif
 249     #elif defined(__GNUC__)
 250         #define HOTSPOT_BUILD_COMPILER "gcc " __VERSION__
 251     #elif defined(__IBMCPP__)
 252         #define HOTSPOT_BUILD_COMPILER "xlC " XSTR(__IBMCPP__)
 253 
 254     #else
 255       #define HOTSPOT_BUILD_COMPILER "unknown compiler"
 256     #endif
 257   #endif
 258 
 259   #ifndef FLOAT_ARCH
 260     #if defined(__SOFTFP__)
 261       #define FLOAT_ARCH_STR "-sflt"
 262     #else
 263       #define FLOAT_ARCH_STR ""
 264     #endif
 265   #else
 266     #define FLOAT_ARCH_STR XSTR(FLOAT_ARCH)
 267   #endif
 268 
 269   return VMNAME " (" VM_RELEASE ") for " OS "-" CPU FLOAT_ARCH_STR
 270          " JRE (" JRE_RELEASE_VERSION "), built on " __DATE__ " " __TIME__
 271          " by " XSTR(HOTSPOT_BUILD_USER) " with " HOTSPOT_BUILD_COMPILER;
 272 }
 273 
 274 const char *Abstract_VM_Version::vm_build_user() {
 275   return HOTSPOT_BUILD_USER;
 276 }
 277 
 278 unsigned int Abstract_VM_Version::jvm_version() {
 279   return ((Abstract_VM_Version::vm_major_version() & 0xFF) << 24) |
 280          ((Abstract_VM_Version::vm_minor_version() & 0xFF) << 16) |
 281          ((Abstract_VM_Version::vm_micro_version() & 0xFF) << 8) |
 282          (Abstract_VM_Version::vm_build_number() & 0xFF);
 283 }
 284 
 285 
 286 void VM_Version_init() {
 287   VM_Version::initialize();
 288 
 289 #ifndef PRODUCT
 290   if (PrintMiscellaneous && Verbose) {
 291     char buf[512];
 292     os::print_cpu_info(tty, buf, sizeof(buf));
 293   }
 294 #endif
 295 }
 296 
 297 unsigned int Abstract_VM_Version::nof_parallel_worker_threads(
 298                                                       unsigned int num,
 299                                                       unsigned int den,
 300                                                       unsigned int switch_pt) {
 301   if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 302     assert(ParallelGCThreads == 0, "Default ParallelGCThreads is not 0");
 303     // For very large machines, there are diminishing returns
 304     // for large numbers of worker threads.  Instead of
 305     // hogging the whole system, use a fraction of the workers for every
 306     // processor after the first 8.  For example, on a 72 cpu machine
 307     // and a chosen fraction of 5/8
 308     // use 8 + (72 - 8) * (5/8) == 48 worker threads.
 309     unsigned int ncpus = (unsigned int) os::active_processor_count();
 310     return (ncpus <= switch_pt) ?
 311            ncpus :
 312           (switch_pt + ((ncpus - switch_pt) * num) / den);
 313   } else {
 314     return ParallelGCThreads;
 315   }
 316 }
 317 
 318 unsigned int Abstract_VM_Version::calc_parallel_worker_threads() {
 319   return nof_parallel_worker_threads(5, 8, 8);
 320 }
 321 
 322 
 323 // Does not set the _initialized flag since it is
 324 // a global flag.
 325 unsigned int Abstract_VM_Version::parallel_worker_threads() {
 326   if (!_parallel_worker_threads_initialized) {
 327     if (FLAG_IS_DEFAULT(ParallelGCThreads)) {
 328       _parallel_worker_threads = VM_Version::calc_parallel_worker_threads();
 329     } else {
 330       _parallel_worker_threads = ParallelGCThreads;
 331     }
 332     _parallel_worker_threads_initialized = true;
 333   }
 334   return _parallel_worker_threads;
 335 }