view hotspot/src/share/vm/memory/padded.hpp @ 23452:d7dca4e6b95d

8035815: Cache-align and pad the from card cache Summary: The from card cache is a very frequently accessed data structure. It is essentially a 2d array of per-region values, one row of values for every GC thread. Pad and align the data structure to avoid false sharing. Reviewed-by: stefank
author tschatzl
date Mon, 17 Mar 2014 10:12:47 +0100
parents 0a3b3f115402
children 947a3d680f3e
line wrap: on
line source
/*
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
 * 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.
 *
 * 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.
 *
 */

#ifndef SHARE_VM_MEMORY_PADDED_HPP
#define SHARE_VM_MEMORY_PADDED_HPP

#include "memory/allocation.hpp"
#include "utilities/globalDefinitions.hpp"

// Bytes needed to pad type to avoid cache-line sharing; alignment should be the
// expected cache line size (a power of two).  The first addend avoids sharing
// when the start address is not a multiple of alignment; the second maintains
// alignment of starting addresses that happen to be a multiple.
#define PADDING_SIZE(type, alignment)                           \
  ((alignment) + align_size_up_(sizeof(type), alignment))

// Templates to create a subclass padded to avoid cache line sharing.  These are
// effective only when applied to derived-most (leaf) classes.

// When no args are passed to the base ctor.
template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
class Padded : public T {
 private:
  char _pad_buf_[PADDING_SIZE(T, alignment)];
};

// When either 0 or 1 args may be passed to the base ctor.
template <class T, typename Arg1T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
class Padded01 : public T {
 public:
  Padded01(): T() { }
  Padded01(Arg1T arg1): T(arg1) { }
 private:
  char _pad_buf_[PADDING_SIZE(T, alignment)];
};

// Super class of PaddedEnd when pad_size != 0.
template <class T, size_t pad_size>
class PaddedEndImpl : public T {
 private:
  char _pad_buf[pad_size];
};

// Super class of PaddedEnd when pad_size == 0.
template <class T>
class PaddedEndImpl<T, /*pad_size*/ 0> : public T {
  // No padding.
};

#define PADDED_END_SIZE(type, alignment) (align_size_up_(sizeof(type), alignment) - sizeof(type))

// More memory conservative implementation of Padded. The subclass adds the
// minimal amount of padding needed to make the size of the objects be aligned.
// This will help reducing false sharing,
// if the start address is a multiple of alignment.
template <class T, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
class PaddedEnd : public PaddedEndImpl<T, PADDED_END_SIZE(T, alignment)> {
  // C++ don't allow zero-length arrays. The padding is put in a
  // super class that is specialized for the pad_size == 0 case.
};

// Helper class to create an array of PaddedEnd<T> objects. All elements will
// start at a multiple of alignment and the size will be aligned to alignment.
template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
class PaddedArray {
 public:
  // Creates an aligned padded array.
  // The memory can't be deleted since the raw memory chunk is not returned.
  static PaddedEnd<T>* create_unfreeable(uint length);
};

// Helper class to create an array of references to arrays of primitive types
// Both the array of references and the data arrays are aligned to the given
// alignment. The allocated memory is zero-filled.
template <class T, MEMFLAGS flags, size_t alignment = DEFAULT_CACHE_LINE_SIZE>
class Padded2DArray {
 public:
  // Creates an aligned padded 2D array.
  // The memory cannot be deleted since the raw memory chunk is not returned.
  static T** create_unfreeable(uint rows, uint columns, size_t* allocation_size = NULL);
};

#endif // SHARE_VM_MEMORY_PADDED_HPP