rev 12348 : 8166132: Convert TestGlobalDefinitions_test to GTest

   1 /*
   2  * Copyright (c) 1997, 2016, 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 "runtime/os.hpp"
  27 #include "utilities/globalDefinitions.hpp"
  28 
  29 // Basic error support
  30 
  31 // Info for oops within a java object.  Defaults are zero so
  32 // things will break badly if incorrectly initialized.
  33 int heapOopSize        = 0;
  34 int LogBytesPerHeapOop = 0;
  35 int LogBitsPerHeapOop  = 0;
  36 int BytesPerHeapOop    = 0;
  37 int BitsPerHeapOop     = 0;
  38 
  39 // Object alignment, in units of HeapWords.
  40 // Defaults are -1 so things will break badly if incorrectly initialized.
  41 int MinObjAlignment            = -1;
  42 int MinObjAlignmentInBytes     = -1;
  43 int MinObjAlignmentInBytesMask = 0;
  44 
  45 int LogMinObjAlignment         = -1;
  46 int LogMinObjAlignmentInBytes  = -1;
  47 
  48 // Oop encoding heap max
  49 uint64_t OopEncodingHeapMax = 0;
  50 
  51 void basic_fatal(const char* msg) {
  52   fatal("%s", msg);
  53 }
  54 
  55 // Something to help porters sleep at night
  56 
  57 void basic_types_init() {
  58 #ifdef ASSERT
  59 #ifdef _LP64
  60   assert(min_intx ==  (intx)CONST64(0x8000000000000000), "correct constant");
  61   assert(max_intx ==  CONST64(0x7FFFFFFFFFFFFFFF), "correct constant");
  62   assert(max_uintx == CONST64(0xFFFFFFFFFFFFFFFF), "correct constant");
  63   assert( 8 == sizeof( intx),      "wrong size for basic type");
  64   assert( 8 == sizeof( jobject),   "wrong size for basic type");
  65 #else
  66   assert(min_intx ==  (intx)0x80000000,  "correct constant");
  67   assert(max_intx ==  0x7FFFFFFF,  "correct constant");
  68   assert(max_uintx == 0xFFFFFFFF,  "correct constant");
  69   assert( 4 == sizeof( intx),      "wrong size for basic type");
  70   assert( 4 == sizeof( jobject),   "wrong size for basic type");
  71 #endif
  72   assert( (~max_juint) == 0,  "max_juint has all its bits");
  73   assert( (~max_uintx) == 0,  "max_uintx has all its bits");
  74   assert( (~max_julong) == 0, "max_julong has all its bits");
  75   assert( 1 == sizeof( jbyte),     "wrong size for basic type");
  76   assert( 2 == sizeof( jchar),     "wrong size for basic type");
  77   assert( 2 == sizeof( jshort),    "wrong size for basic type");
  78   assert( 4 == sizeof( juint),     "wrong size for basic type");
  79   assert( 4 == sizeof( jint),      "wrong size for basic type");
  80   assert( 1 == sizeof( jboolean),  "wrong size for basic type");
  81   assert( 8 == sizeof( jlong),     "wrong size for basic type");
  82   assert( 4 == sizeof( jfloat),    "wrong size for basic type");
  83   assert( 8 == sizeof( jdouble),   "wrong size for basic type");
  84   assert( 1 == sizeof( u1),        "wrong size for basic type");
  85   assert( 2 == sizeof( u2),        "wrong size for basic type");
  86   assert( 4 == sizeof( u4),        "wrong size for basic type");
  87   assert(wordSize == BytesPerWord, "should be the same since they're used interchangeably");
  88   assert(wordSize == HeapWordSize, "should be the same since they're also used interchangeably");
  89 
  90   int num_type_chars = 0;
  91   for (int i = 0; i < 99; i++) {
  92     if (type2char((BasicType)i) != 0) {
  93       assert(char2type(type2char((BasicType)i)) == i, "proper inverses");
  94       num_type_chars++;
  95     }
  96   }
  97   assert(num_type_chars == 11, "must have tested the right number of mappings");
  98   assert(char2type(0) == T_ILLEGAL, "correct illegality");
  99 
 100   {
 101     for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) {
 102       BasicType vt = (BasicType)i;
 103       BasicType ft = type2field[vt];
 104       switch (vt) {
 105       // the following types might plausibly show up in memory layouts:
 106       case T_BOOLEAN:
 107       case T_BYTE:
 108       case T_CHAR:
 109       case T_SHORT:
 110       case T_INT:
 111       case T_FLOAT:
 112       case T_DOUBLE:
 113       case T_LONG:
 114       case T_OBJECT:
 115       case T_ADDRESS:     // random raw pointer
 116       case T_METADATA:    // metadata pointer
 117       case T_NARROWOOP:   // compressed pointer
 118       case T_NARROWKLASS: // compressed klass pointer
 119       case T_CONFLICT:    // might as well support a bottom type
 120       case T_VOID:        // padding or other unaddressed word
 121         // layout type must map to itself
 122         assert(vt == ft, "");
 123         break;
 124       default:
 125         // non-layout type must map to a (different) layout type
 126         assert(vt != ft, "");
 127         assert(ft == type2field[ft], "");
 128       }
 129       // every type must map to same-sized layout type:
 130       assert(type2size[vt] == type2size[ft], "");
 131     }
 132   }
 133   // These are assumed, e.g., when filling HeapWords with juints.
 134   assert(is_power_of_2(sizeof(juint)), "juint must be power of 2");
 135   assert(is_power_of_2(HeapWordSize), "HeapWordSize must be power of 2");
 136   assert((size_t)HeapWordSize >= sizeof(juint),
 137          "HeapWord should be at least as large as juint");
 138   assert(sizeof(NULL) == sizeof(char*), "NULL must be same size as pointer");
 139 #endif
 140 
 141   if( JavaPriority1_To_OSPriority != -1 )
 142     os::java_to_os_priority[1] = JavaPriority1_To_OSPriority;
 143   if( JavaPriority2_To_OSPriority != -1 )
 144     os::java_to_os_priority[2] = JavaPriority2_To_OSPriority;
 145   if( JavaPriority3_To_OSPriority != -1 )
 146     os::java_to_os_priority[3] = JavaPriority3_To_OSPriority;
 147   if( JavaPriority4_To_OSPriority != -1 )
 148     os::java_to_os_priority[4] = JavaPriority4_To_OSPriority;
 149   if( JavaPriority5_To_OSPriority != -1 )
 150     os::java_to_os_priority[5] = JavaPriority5_To_OSPriority;
 151   if( JavaPriority6_To_OSPriority != -1 )
 152     os::java_to_os_priority[6] = JavaPriority6_To_OSPriority;
 153   if( JavaPriority7_To_OSPriority != -1 )
 154     os::java_to_os_priority[7] = JavaPriority7_To_OSPriority;
 155   if( JavaPriority8_To_OSPriority != -1 )
 156     os::java_to_os_priority[8] = JavaPriority8_To_OSPriority;
 157   if( JavaPriority9_To_OSPriority != -1 )
 158     os::java_to_os_priority[9] = JavaPriority9_To_OSPriority;
 159   if(JavaPriority10_To_OSPriority != -1 )
 160     os::java_to_os_priority[10] = JavaPriority10_To_OSPriority;
 161 
 162   // Set the size of basic types here (after argument parsing but before
 163   // stub generation).
 164   if (UseCompressedOops) {
 165     // Size info for oops within java objects is fixed
 166     heapOopSize        = jintSize;
 167     LogBytesPerHeapOop = LogBytesPerInt;
 168     LogBitsPerHeapOop  = LogBitsPerInt;
 169     BytesPerHeapOop    = BytesPerInt;
 170     BitsPerHeapOop     = BitsPerInt;
 171   } else {
 172     heapOopSize        = oopSize;
 173     LogBytesPerHeapOop = LogBytesPerWord;
 174     LogBitsPerHeapOop  = LogBitsPerWord;
 175     BytesPerHeapOop    = BytesPerWord;
 176     BitsPerHeapOop     = BitsPerWord;
 177   }
 178   _type2aelembytes[T_OBJECT] = heapOopSize;
 179   _type2aelembytes[T_ARRAY]  = heapOopSize;
 180 }
 181 
 182 
 183 // Map BasicType to signature character
 184 char type2char_tab[T_CONFLICT+1]={ 0, 0, 0, 0, 'Z', 'C', 'F', 'D', 'B', 'S', 'I', 'J', 'L', '[', 'V', 0, 0, 0, 0, 0};
 185 
 186 // Map BasicType to Java type name
 187 const char* type2name_tab[T_CONFLICT+1] = {
 188   NULL, NULL, NULL, NULL,
 189   "boolean",
 190   "char",
 191   "float",
 192   "double",
 193   "byte",
 194   "short",
 195   "int",
 196   "long",
 197   "object",
 198   "array",
 199   "void",
 200   "*address*",
 201   "*narrowoop*",
 202   "*metadata*",
 203   "*narrowklass*",
 204   "*conflict*"
 205 };
 206 
 207 
 208 BasicType name2type(const char* name) {
 209   for (int i = T_BOOLEAN; i <= T_VOID; i++) {
 210     BasicType t = (BasicType)i;
 211     if (type2name_tab[t] != NULL && 0 == strcmp(type2name_tab[t], name))
 212       return t;
 213   }
 214   return T_ILLEGAL;
 215 }
 216 
 217 // Map BasicType to size in words
 218 int type2size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 0, 1, 1, 1, 1, -1};
 219 
 220 BasicType type2field[T_CONFLICT+1] = {
 221   (BasicType)0,            // 0,
 222   (BasicType)0,            // 1,
 223   (BasicType)0,            // 2,
 224   (BasicType)0,            // 3,
 225   T_BOOLEAN,               // T_BOOLEAN  =  4,
 226   T_CHAR,                  // T_CHAR     =  5,
 227   T_FLOAT,                 // T_FLOAT    =  6,
 228   T_DOUBLE,                // T_DOUBLE   =  7,
 229   T_BYTE,                  // T_BYTE     =  8,
 230   T_SHORT,                 // T_SHORT    =  9,
 231   T_INT,                   // T_INT      = 10,
 232   T_LONG,                  // T_LONG     = 11,
 233   T_OBJECT,                // T_OBJECT   = 12,
 234   T_OBJECT,                // T_ARRAY    = 13,
 235   T_VOID,                  // T_VOID     = 14,
 236   T_ADDRESS,               // T_ADDRESS  = 15,
 237   T_NARROWOOP,             // T_NARROWOOP= 16,
 238   T_METADATA,              // T_METADATA = 17,
 239   T_NARROWKLASS,           // T_NARROWKLASS = 18,
 240   T_CONFLICT               // T_CONFLICT = 19,
 241 };
 242 
 243 
 244 BasicType type2wfield[T_CONFLICT+1] = {
 245   (BasicType)0,            // 0,
 246   (BasicType)0,            // 1,
 247   (BasicType)0,            // 2,
 248   (BasicType)0,            // 3,
 249   T_INT,     // T_BOOLEAN  =  4,
 250   T_INT,     // T_CHAR     =  5,
 251   T_FLOAT,   // T_FLOAT    =  6,
 252   T_DOUBLE,  // T_DOUBLE   =  7,
 253   T_INT,     // T_BYTE     =  8,
 254   T_INT,     // T_SHORT    =  9,
 255   T_INT,     // T_INT      = 10,
 256   T_LONG,    // T_LONG     = 11,
 257   T_OBJECT,  // T_OBJECT   = 12,
 258   T_OBJECT,  // T_ARRAY    = 13,
 259   T_VOID,    // T_VOID     = 14,
 260   T_ADDRESS, // T_ADDRESS  = 15,
 261   T_NARROWOOP, // T_NARROWOOP  = 16,
 262   T_METADATA,  // T_METADATA   = 17,
 263   T_NARROWKLASS, // T_NARROWKLASS  = 18,
 264   T_CONFLICT // T_CONFLICT = 19,
 265 };
 266 
 267 
 268 int _type2aelembytes[T_CONFLICT+1] = {
 269   0,                         // 0
 270   0,                         // 1
 271   0,                         // 2
 272   0,                         // 3
 273   T_BOOLEAN_aelem_bytes,     // T_BOOLEAN  =  4,
 274   T_CHAR_aelem_bytes,        // T_CHAR     =  5,
 275   T_FLOAT_aelem_bytes,       // T_FLOAT    =  6,
 276   T_DOUBLE_aelem_bytes,      // T_DOUBLE   =  7,
 277   T_BYTE_aelem_bytes,        // T_BYTE     =  8,
 278   T_SHORT_aelem_bytes,       // T_SHORT    =  9,
 279   T_INT_aelem_bytes,         // T_INT      = 10,
 280   T_LONG_aelem_bytes,        // T_LONG     = 11,
 281   T_OBJECT_aelem_bytes,      // T_OBJECT   = 12,
 282   T_ARRAY_aelem_bytes,       // T_ARRAY    = 13,
 283   0,                         // T_VOID     = 14,
 284   T_OBJECT_aelem_bytes,      // T_ADDRESS  = 15,
 285   T_NARROWOOP_aelem_bytes,   // T_NARROWOOP= 16,
 286   T_OBJECT_aelem_bytes,      // T_METADATA = 17,
 287   T_NARROWKLASS_aelem_bytes, // T_NARROWKLASS= 18,
 288   0                          // T_CONFLICT = 19,
 289 };
 290 
 291 #ifdef ASSERT
 292 int type2aelembytes(BasicType t, bool allow_address) {
 293   assert(allow_address || t != T_ADDRESS, " ");
 294   return _type2aelembytes[t];
 295 }
 296 #endif
 297 
 298 // Support for 64-bit integer arithmetic
 299 
 300 // The following code is mostly taken from JVM typedefs_md.h and system_md.c
 301 
 302 static const jlong high_bit   = (jlong)1 << (jlong)63;
 303 static const jlong other_bits = ~high_bit;
 304 
 305 jlong float2long(jfloat f) {
 306   jlong tmp = (jlong) f;
 307   if (tmp != high_bit) {
 308     return tmp;
 309   } else {
 310     if (g_isnan((jdouble)f)) {
 311       return 0;
 312     }
 313     if (f < 0) {
 314       return high_bit;
 315     } else {
 316       return other_bits;
 317     }
 318   }
 319 }
 320 
 321 
 322 jlong double2long(jdouble f) {
 323   jlong tmp = (jlong) f;
 324   if (tmp != high_bit) {
 325     return tmp;
 326   } else {
 327     if (g_isnan(f)) {
 328       return 0;
 329     }
 330     if (f < 0) {
 331       return high_bit;
 332     } else {
 333       return other_bits;
 334     }
 335   }
 336 }
 337 
 338 // least common multiple
 339 size_t lcm(size_t a, size_t b) {
 340     size_t cur, div, next;
 341 
 342     cur = MAX2(a, b);
 343     div = MIN2(a, b);
 344 
 345     assert(div != 0, "lcm requires positive arguments");
 346 
 347 
 348     while ((next = cur % div) != 0) {
 349         cur = div; div = next;
 350     }
 351 
 352 
 353     julong result = julong(a) * b / div;
 354     assert(result <= (size_t)max_uintx, "Integer overflow in lcm");
 355 
 356     return size_t(result);
 357 }
 358 
 359 
 360 // Test that nth_bit macro and friends behave as
 361 // expected, even with low-precedence operators.
 362 
 363 STATIC_ASSERT(nth_bit(3)   == 0x8);
 364 STATIC_ASSERT(nth_bit(1|2) == 0x8);
 365 
 366 STATIC_ASSERT(right_n_bits(3)   == 0x7);
 367 STATIC_ASSERT(right_n_bits(1|2) == 0x7);
 368 
 369 STATIC_ASSERT(left_n_bits(3)   == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
 370 STATIC_ASSERT(left_n_bits(1|2) == (intptr_t) LP64_ONLY(0xE000000000000000) NOT_LP64(0xE0000000));
 371 
 372 
 373 #ifndef PRODUCT
 374 // For unit testing only
 375 class TestGlobalDefinitions {
 376 private:
 377 
 378   static void test_clamp_address_in_page() {
 379     intptr_t page_sizes[] = { os::vm_page_size(), 4096, 8192, 65536, 2*1024*1024 };
 380     const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);
 381 
 382     for (int i = 0; i < num_page_sizes; i++) {
 383       intptr_t page_size = page_sizes[i];
 384 
 385       address a_page = (address)(10*page_size);
 386 
 387       // Check that address within page is returned as is
 388       assert(clamp_address_in_page(a_page, a_page, page_size) == a_page, "incorrect");
 389       assert(clamp_address_in_page(a_page + 128, a_page, page_size) == a_page + 128, "incorrect");
 390       assert(clamp_address_in_page(a_page + page_size - 1, a_page, page_size) == a_page + page_size - 1, "incorrect");
 391 
 392       // Check that address above page returns start of next page
 393       assert(clamp_address_in_page(a_page + page_size, a_page, page_size) == a_page + page_size, "incorrect");
 394       assert(clamp_address_in_page(a_page + page_size + 1, a_page, page_size) == a_page + page_size, "incorrect");
 395       assert(clamp_address_in_page(a_page + page_size*5 + 1, a_page, page_size) == a_page + page_size, "incorrect");
 396 
 397       // Check that address below page returns start of page
 398       assert(clamp_address_in_page(a_page - 1, a_page, page_size) == a_page, "incorrect");
 399       assert(clamp_address_in_page(a_page - 2*page_size - 1, a_page, page_size) == a_page, "incorrect");
 400       assert(clamp_address_in_page(a_page - 5*page_size - 1, a_page, page_size) == a_page, "incorrect");
 401     }
 402   }
 403 
 404   static void test_exact_unit_for_byte_size() {
 405     assert(strcmp(exact_unit_for_byte_size(0),     "B") == 0, "incorrect");
 406     assert(strcmp(exact_unit_for_byte_size(1),     "B") == 0, "incorrect");
 407     assert(strcmp(exact_unit_for_byte_size(K - 1), "B") == 0, "incorrect");
 408     assert(strcmp(exact_unit_for_byte_size(K),     "K") == 0, "incorrect");
 409     assert(strcmp(exact_unit_for_byte_size(K + 1), "B") == 0, "incorrect");
 410     assert(strcmp(exact_unit_for_byte_size(M - 1), "B") == 0, "incorrect");
 411     assert(strcmp(exact_unit_for_byte_size(M),     "M") == 0, "incorrect");
 412     assert(strcmp(exact_unit_for_byte_size(M + 1), "B") == 0, "incorrect");
 413     assert(strcmp(exact_unit_for_byte_size(M + K), "K") == 0, "incorrect");
 414 #ifdef LP64
 415     assert(strcmp(exact_unit_for_byte_size(G - 1),     "B") == 0, "incorrect");
 416     assert(strcmp(exact_unit_for_byte_size(G),         "G") == 0, "incorrect");
 417     assert(strcmp(exact_unit_for_byte_size(G + 1),     "B") == 0, "incorrect");
 418     assert(strcmp(exact_unit_for_byte_size(G + K),     "K") == 0, "incorrect");
 419     assert(strcmp(exact_unit_for_byte_size(G + M),     "M") == 0, "incorrect");
 420     assert(strcmp(exact_unit_for_byte_size(G + M + K), "K") == 0, "incorrect");
 421 #endif
 422   }
 423 
 424   static void test_byte_size_in_exact_unit() {
 425     assert(byte_size_in_exact_unit(0)     == 0,     "incorrect");
 426     assert(byte_size_in_exact_unit(1)     == 1,     "incorrect");
 427     assert(byte_size_in_exact_unit(K - 1) == K - 1, "incorrect");
 428     assert(byte_size_in_exact_unit(K)     == 1,     "incorrect");
 429     assert(byte_size_in_exact_unit(K + 1) == K + 1, "incorrect");
 430     assert(byte_size_in_exact_unit(M - 1) == M - 1, "incorrect");
 431     assert(byte_size_in_exact_unit(M)     == 1,     "incorrect");
 432     assert(byte_size_in_exact_unit(M + 1) == M + 1, "incorrect");
 433     assert(byte_size_in_exact_unit(M + K) == K + 1, "incorrect");
 434 #ifdef LP64
 435     assert(byte_size_in_exact_unit(G - 1)     == G - 1,     "incorrect");
 436     assert(byte_size_in_exact_unit(G)         == 1,         "incorrect");
 437     assert(byte_size_in_exact_unit(G + 1)     == G + 1,     "incorrect");
 438     assert(byte_size_in_exact_unit(G + K)     == M + 1,     "incorrect");
 439     assert(byte_size_in_exact_unit(G + M)     == K + 1,     "incorrect");
 440     assert(byte_size_in_exact_unit(G + M + K) == M + K + 1, "incorrect");
 441 #endif
 442   }
 443 
 444   static void test_exact_units() {
 445     test_exact_unit_for_byte_size();
 446     test_byte_size_in_exact_unit();
 447   }
 448 
 449 public:
 450   static void test() {
 451     test_clamp_address_in_page();
 452     test_exact_units();
 453   }
 454 };
 455 
 456 void TestGlobalDefinitions_test() {
 457   TestGlobalDefinitions::test();
 458 }
 459 
 460 #endif // PRODUCT
--- EOF ---