psVirtualspace.cpp 12.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37
#include "precompiled.hpp"
#include "gc_implementation/parallelScavenge/psVirtualspace.hpp"
#include "runtime/os.hpp"
#include "runtime/virtualspace.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "os_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "os_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "os_windows.inline.hpp"
#endif
38 39 40
#ifdef TARGET_OS_FAMILY_aix
# include "os_aix.inline.hpp"
#endif
N
never 已提交
41 42 43
#ifdef TARGET_OS_FAMILY_bsd
# include "os_bsd.inline.hpp"
#endif
D
duke 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

// PSVirtualSpace

PSVirtualSpace::PSVirtualSpace(ReservedSpace rs, size_t alignment) :
  _alignment(alignment)
{
  set_reserved(rs);
  set_committed(reserved_low_addr(), reserved_low_addr());
  DEBUG_ONLY(verify());
}

PSVirtualSpace::PSVirtualSpace(ReservedSpace rs) :
  _alignment(os::vm_page_size())
{
  set_reserved(rs);
  set_committed(reserved_low_addr(), reserved_low_addr());
  DEBUG_ONLY(verify());
}

// Deprecated.
PSVirtualSpace::PSVirtualSpace(): _alignment(os::vm_page_size()) {
}

// Deprecated.
bool PSVirtualSpace::initialize(ReservedSpace rs,
                                size_t commit_size) {
  set_reserved(rs);
  set_committed(reserved_low_addr(), reserved_low_addr());

  // Commit to initial size.
  assert(commit_size <= rs.size(), "commit_size too big");
  bool result = commit_size > 0 ? expand_by(commit_size) : true;
  DEBUG_ONLY(verify());
  return result;
}

PSVirtualSpace::~PSVirtualSpace() {
  release();
}

bool PSVirtualSpace::contains(void* p) const {
  char* const cp = (char*)p;
  return cp >= committed_low_addr() && cp < committed_high_addr();
}

void PSVirtualSpace::release() {
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));
91 92
  // This may not release memory it didn't reserve.
  // Use rs.release() to release the underlying memory instead.
D
duke 已提交
93 94 95 96 97
  _reserved_low_addr = _reserved_high_addr = NULL;
  _committed_low_addr = _committed_high_addr = NULL;
  _special = false;
}

98
bool PSVirtualSpace::expand_by(size_t bytes) {
D
duke 已提交
99 100 101 102 103 104 105 106
  assert(is_aligned(bytes), "arg not aligned");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));

  if (uncommitted_size() < bytes) {
    return false;
  }

  char* const base_addr = committed_high_addr();
107 108
  bool result = special() ||
         os::commit_memory(base_addr, bytes, alignment(), !ExecMem);
D
duke 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  if (result) {
    _committed_high_addr += bytes;
  }

  return result;
}

bool PSVirtualSpace::shrink_by(size_t bytes) {
  assert(is_aligned(bytes), "arg not aligned");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));

  if (committed_size() < bytes) {
    return false;
  }

  char* const base_addr = committed_high_addr() - bytes;
  bool result = special() || os::uncommit_memory(base_addr, bytes);
  if (result) {
    _committed_high_addr -= bytes;
  }

  return result;
}

size_t
PSVirtualSpace::expand_into(PSVirtualSpace* other_space, size_t bytes) {
  assert(is_aligned(bytes), "arg not aligned");
  assert(grows_up(), "this space must grow up");
  assert(other_space->grows_down(), "other space must grow down");
  assert(reserved_high_addr() == other_space->reserved_low_addr(),
         "spaces not contiguous");
  assert(special() == other_space->special(), "one space is special, the other is not");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));
  DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space));

  size_t bytes_needed = bytes;

  // First use the uncommitted region in this space.
  size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed);
  if (tmp_bytes > 0) {
    if (expand_by(tmp_bytes)) {
      bytes_needed -= tmp_bytes;
    } else {
      return 0;
    }
  }

  // Next take from the uncommitted region in the other space, and commit it.
  tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed);
  if (tmp_bytes > 0) {
    char* const commit_base = committed_high_addr();
    if (other_space->special() ||
161
        os::commit_memory(commit_base, tmp_bytes, alignment(), !ExecMem)) {
D
duke 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
      // Reduce the reserved region in the other space.
      other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes,
                                other_space->reserved_high_addr(),
                                other_space->special());

      // Grow both reserved and committed in this space.
      _reserved_high_addr += tmp_bytes;
      _committed_high_addr += tmp_bytes;
      bytes_needed -= tmp_bytes;
    } else {
      return bytes - bytes_needed;
    }
  }

  // Finally take from the already committed region in the other space.
  tmp_bytes = bytes_needed;
  if (tmp_bytes > 0) {
    // Reduce both committed and reserved in the other space.
    other_space->set_committed(other_space->committed_low_addr() + tmp_bytes,
                               other_space->committed_high_addr());
    other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes,
                              other_space->reserved_high_addr(),
                              other_space->special());

    // Grow both reserved and committed in this space.
    _reserved_high_addr += tmp_bytes;
    _committed_high_addr += tmp_bytes;
  }

  return bytes;
}

#ifndef PRODUCT
bool PSVirtualSpace::is_aligned(size_t value, size_t align) {
  const size_t tmp_value = value + align - 1;
  const size_t mask = ~(align - 1);
  return (tmp_value & mask) == value;
}

bool PSVirtualSpace::is_aligned(size_t value) const {
  return is_aligned(value, alignment());
}

bool PSVirtualSpace::is_aligned(char* value) const {
  return is_aligned((size_t)value);
}

void PSVirtualSpace::verify() const {
  assert(is_aligned(alignment(), os::vm_page_size()), "bad alignment");
  assert(is_aligned(reserved_low_addr()), "bad reserved_low_addr");
  assert(is_aligned(reserved_high_addr()), "bad reserved_high_addr");
  assert(is_aligned(committed_low_addr()), "bad committed_low_addr");
  assert(is_aligned(committed_high_addr()), "bad committed_high_addr");

  // Reserved region must be non-empty or both addrs must be 0.
  assert(reserved_low_addr() < reserved_high_addr() ||
         reserved_low_addr() == NULL && reserved_high_addr() == NULL,
         "bad reserved addrs");
  assert(committed_low_addr() <= committed_high_addr(), "bad committed addrs");

  if (grows_up()) {
    assert(reserved_low_addr() == committed_low_addr(), "bad low addrs");
    assert(reserved_high_addr() >= committed_high_addr(), "bad high addrs");
  } else {
    assert(reserved_high_addr() == committed_high_addr(), "bad high addrs");
    assert(reserved_low_addr() <= committed_low_addr(), "bad low addrs");
  }
}

void PSVirtualSpace::print() const {
  gclog_or_tty->print_cr("virtual space [" PTR_FORMAT "]:  alignment="
                         SIZE_FORMAT "K grows %s%s",
                         this, alignment() / K, grows_up() ? "up" : "down",
                         special() ? " (pinned in memory)" : "");
  gclog_or_tty->print_cr("    reserved=" SIZE_FORMAT "K"
                         " [" PTR_FORMAT "," PTR_FORMAT "]"
                         " committed=" SIZE_FORMAT "K"
                         " [" PTR_FORMAT "," PTR_FORMAT "]",
                         reserved_size() / K,
                         reserved_low_addr(), reserved_high_addr(),
                         committed_size() / K,
                         committed_low_addr(), committed_high_addr());
}
#endif // #ifndef PRODUCT

void PSVirtualSpace::print_space_boundaries_on(outputStream* st) const {
  st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ")",
               low_boundary(), high(), high_boundary());
}

PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs,
                                                 size_t alignment) :
  PSVirtualSpace(alignment)
{
  set_reserved(rs);
  set_committed(reserved_high_addr(), reserved_high_addr());
  DEBUG_ONLY(verify());
}

PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs) {
  set_reserved(rs);
  set_committed(reserved_high_addr(), reserved_high_addr());
  DEBUG_ONLY(verify());
}

267
bool PSVirtualSpaceHighToLow::expand_by(size_t bytes) {
D
duke 已提交
268 269 270 271 272 273 274 275
  assert(is_aligned(bytes), "arg not aligned");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));

  if (uncommitted_size() < bytes) {
    return false;
  }

  char* const base_addr = committed_low_addr() - bytes;
276 277
  bool result = special() ||
         os::commit_memory(base_addr, bytes, alignment(), !ExecMem);
D
duke 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  if (result) {
    _committed_low_addr -= bytes;
  }

  return result;
}

bool PSVirtualSpaceHighToLow::shrink_by(size_t bytes) {
  assert(is_aligned(bytes), "arg not aligned");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));

  if (committed_size() < bytes) {
    return false;
  }

  char* const base_addr = committed_low_addr();
  bool result = special() || os::uncommit_memory(base_addr, bytes);
  if (result) {
    _committed_low_addr += bytes;
  }

  return result;
}

size_t PSVirtualSpaceHighToLow::expand_into(PSVirtualSpace* other_space,
                                            size_t bytes) {
  assert(is_aligned(bytes), "arg not aligned");
  assert(grows_down(), "this space must grow down");
  assert(other_space->grows_up(), "other space must grow up");
  assert(reserved_low_addr() == other_space->reserved_high_addr(),
         "spaces not contiguous");
  assert(special() == other_space->special(), "one space is special in memory, the other is not");
  DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this));
  DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space));

  size_t bytes_needed = bytes;

  // First use the uncommitted region in this space.
  size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed);
  if (tmp_bytes > 0) {
    if (expand_by(tmp_bytes)) {
      bytes_needed -= tmp_bytes;
    } else {
      return 0;
    }
  }

  // Next take from the uncommitted region in the other space, and commit it.
  tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed);
  if (tmp_bytes > 0) {
    char* const commit_base = committed_low_addr() - tmp_bytes;
    if (other_space->special() ||
330
        os::commit_memory(commit_base, tmp_bytes, alignment(), !ExecMem)) {
D
duke 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
      // Reduce the reserved region in the other space.
      other_space->set_reserved(other_space->reserved_low_addr(),
                                other_space->reserved_high_addr() - tmp_bytes,
                                other_space->special());

      // Grow both reserved and committed in this space.
      _reserved_low_addr -= tmp_bytes;
      _committed_low_addr -= tmp_bytes;
      bytes_needed -= tmp_bytes;
    } else {
      return bytes - bytes_needed;
    }
  }

  // Finally take from the already committed region in the other space.
  tmp_bytes = bytes_needed;
  if (tmp_bytes > 0) {
    // Reduce both committed and reserved in the other space.
    other_space->set_committed(other_space->committed_low_addr(),
                               other_space->committed_high_addr() - tmp_bytes);
    other_space->set_reserved(other_space->reserved_low_addr(),
                              other_space->reserved_high_addr() - tmp_bytes,
                              other_space->special());

    // Grow both reserved and committed in this space.
    _reserved_low_addr -= tmp_bytes;
    _committed_low_addr -= tmp_bytes;
  }

  return bytes;
}

void
PSVirtualSpaceHighToLow::print_space_boundaries_on(outputStream* st) const {
  st->print_cr(" (" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT "]",
               high_boundary(), low(), low_boundary());
}