hotspot/test/native/runtime/test_os.cpp
author coleenp
Wed, 28 Jun 2017 16:14:20 -0400
changeset 46587 6c97f34cb194
parent 42056 9296564b8c8b
permissions -rw-r--r--
8182554: Code for os::random() assumes long is 32 bits Summary: And make updating the _rand_seed thread safe. Reviewed-by: stuefe, kbarrett, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     1
/*
46587
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     4
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     8
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    14
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    18
 *
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    21
 * questions.
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    22
 */
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    23
39394
044a0eb96215 8159364: Gtest unit tests does not support PCH
ehelin
parents: 38255
diff changeset
    24
#include "precompiled.hpp"
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    25
#include "runtime/os.hpp"
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    26
#include "unittest.hpp"
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    27
42056
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    28
static size_t small_page_size() {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    29
  return os::vm_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    30
}
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    31
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    32
static size_t large_page_size() {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    33
  const size_t large_page_size_example = 4 * M;
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    34
  return os::page_size_for_region_aligned(large_page_size_example, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    35
}
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    36
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    37
TEST_VM(os, page_size_for_region) {
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    38
  size_t large_page_example = 4 * M;
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    39
  size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    40
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    41
  size_t small_page = os::vm_page_size();
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    42
  if (large_page > small_page) {
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    43
    size_t num_small_in_large = large_page / small_page;
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    44
    size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    45
    ASSERT_EQ(page, small_page) << "Did not get a small page";
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    46
  }
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    47
}
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
    48
42056
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    49
TEST_VM(os, page_size_for_region_aligned) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    50
  if (UseLargePages) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    51
    const size_t small_page = small_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    52
    const size_t large_page = large_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    53
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    54
    if (large_page > small_page) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    55
      size_t num_small_pages_in_large = large_page / small_page;
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    56
      size_t page = os::page_size_for_region_aligned(large_page, num_small_pages_in_large);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    57
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    58
      ASSERT_EQ(page, small_page);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    59
    }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    60
  }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    61
}
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    62
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    63
TEST_VM(os, page_size_for_region_alignment) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    64
  if (UseLargePages) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    65
    const size_t small_page = small_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    66
    const size_t large_page = large_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    67
    if (large_page > small_page) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    68
      const size_t unaligned_region = large_page + 17;
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    69
      size_t page = os::page_size_for_region_aligned(unaligned_region, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    70
      ASSERT_EQ(page, small_page);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    71
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    72
      const size_t num_pages = 5;
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    73
      const size_t aligned_region = large_page * num_pages;
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    74
      page = os::page_size_for_region_aligned(aligned_region, num_pages);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    75
      ASSERT_EQ(page, large_page);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    76
    }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    77
  }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    78
}
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    79
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    80
TEST_VM(os, page_size_for_region_unaligned) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    81
  if (UseLargePages) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    82
    // Given exact page size, should return that page size.
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    83
    for (size_t i = 0; os::_page_sizes[i] != 0; i++) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    84
      size_t expected = os::_page_sizes[i];
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    85
      size_t actual = os::page_size_for_region_unaligned(expected, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    86
      ASSERT_EQ(expected, actual);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    87
    }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    88
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    89
    // Given slightly larger size than a page size, return the page size.
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    90
    for (size_t i = 0; os::_page_sizes[i] != 0; i++) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    91
      size_t expected = os::_page_sizes[i];
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    92
      size_t actual = os::page_size_for_region_unaligned(expected + 17, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    93
      ASSERT_EQ(expected, actual);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    94
    }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    95
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    96
    // Given a slightly smaller size than a page size,
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    97
    // return the next smaller page size.
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    98
    if (os::_page_sizes[1] > os::_page_sizes[0]) {
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
    99
      size_t expected = os::_page_sizes[0];
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   100
      size_t actual = os::page_size_for_region_unaligned(os::_page_sizes[1] - 17, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   101
      ASSERT_EQ(actual, expected);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   102
    }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   103
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   104
    // Return small page size for values less than a small page.
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   105
    size_t small_page = small_page_size();
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   106
    size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   107
    ASSERT_EQ(small_page, actual);
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   108
  }
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   109
}
9296564b8c8b 8157455: Convert TestOS_test to GTest
jwilhelm
parents: 41705
diff changeset
   110
46587
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   111
TEST(os, test_random) {
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   112
  const double m = 2147483647;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   113
  double mean = 0.0, variance = 0.0, t;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   114
  const int reps = 10000;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   115
  unsigned int seed = 1;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   116
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   117
  // tty->print_cr("seed %ld for %ld repeats...", seed, reps);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   118
  os::init_random(seed);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   119
  int num;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   120
  for (int k = 0; k < reps; k++) {
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   121
    num = os::random();
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   122
    double u = (double)num / m;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   123
    ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!";
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   124
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   125
    // calculate mean and variance of the random sequence
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   126
    mean += u;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   127
    variance += (u*u);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   128
  }
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   129
  mean /= reps;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   130
  variance /= (reps - 1);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   131
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   132
  ASSERT_EQ(num, 1043618065) << "bad seed";
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   133
  // tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   134
  int intmean = mean*100;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   135
  ASSERT_EQ(intmean, 50);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   136
  // tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   137
  int intvariance = variance*100;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   138
  ASSERT_EQ(intvariance, 33);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   139
  const double eps = 0.0001;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   140
  t = fabsd(mean - 0.5018);
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   141
  ASSERT_LT(t, eps) << "bad mean";
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   142
  t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   143
  ASSERT_LT(t, eps) << "bad variance";
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   144
}
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   145
6c97f34cb194 8182554: Code for os::random() assumes long is 32 bits
coleenp
parents: 42056
diff changeset
   146
38255
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   147
#ifdef ASSERT
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   148
TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") {
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   149
  size_t region_size = 16 * os::vm_page_size();
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   150
  os::page_size_for_region_aligned(region_size, 0); // should assert
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   151
}
5784bccf53b0 8148244: Finalize and integrate GTest implementation
iignatyev
parents:
diff changeset
   152
#endif