hotspot/src/share/vm/memory/threadLocalAllocBuffer.inline.hpp
author acorn
Thu, 05 Mar 2009 22:07:29 -0500
changeset 2140 07437c6a4cd4
parent 1 489c9b5090e2
child 4636 90e004691873
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
inline HeapWord* ThreadLocalAllocBuffer::allocate(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
  invariants();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
  HeapWord* obj = top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
  if (pointer_delta(end(), obj) >= size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
    // successful thread-local allocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
    DEBUG_ONLY(Copy::fill_to_words(obj, size, badHeapWordVal));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
    // This addition is safe because we know that top is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    // at least size below end, so the add can't wrap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    set_top(obj + size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    invariants();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    return obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
inline size_t ThreadLocalAllocBuffer::compute_size(size_t obj_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  const size_t aligned_obj_size = align_object_size(obj_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // Compute the size for the new TLAB.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // The "last" tlab may be smaller to reduce fragmentation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  // unsafe_max_tlab_alloc is just a hint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  const size_t available_size = Universe::heap()->unsafe_max_tlab_alloc(myThread()) /
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
                                                  HeapWordSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  size_t new_tlab_size = MIN2(available_size, desired_size() + aligned_obj_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  // Make sure there's enough room for object and filler int[].
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  const size_t obj_plus_filler_size = aligned_obj_size + alignment_reserve();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  if (new_tlab_size < obj_plus_filler_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    // If there isn't enough room for the allocation, return failure.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    if (PrintTLAB && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      gclog_or_tty->print_cr("ThreadLocalAllocBuffer::compute_size(" SIZE_FORMAT ")"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
                    " returns failure",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
                    obj_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  if (PrintTLAB && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    gclog_or_tty->print_cr("ThreadLocalAllocBuffer::compute_size(" SIZE_FORMAT ")"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
                  " returns " SIZE_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
                  obj_size, new_tlab_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  return new_tlab_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
void ThreadLocalAllocBuffer::record_slow_allocation(size_t obj_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Raise size required to bypass TLAB next time. Why? Else there's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // a risk that a thread that repeatedly allocates objects of one
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // size will get stuck on this slow path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  set_refill_waste_limit(refill_waste_limit() + refill_waste_limit_increment());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  _slow_allocations++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  if (PrintTLAB && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    Thread* thrd = myThread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    gclog_or_tty->print("TLAB: %s thread: "INTPTR_FORMAT" [id: %2d]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                        " obj: "SIZE_FORMAT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                        " free: "SIZE_FORMAT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                        " waste: "SIZE_FORMAT"\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                        "slow", thrd, thrd->osthread()->thread_id(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                        obj_size, free(), refill_waste_limit());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
}