view src/share/vm/gc_implementation/g1/g1CodeCacheRemSet.hpp @ 20218:828056cf311f

8040792: G1: Memory usage calculation uses sizeof(this) instead of sizeof(classname) Summary: A few locations in the code use sizeof(this) which returns the size of the pointer instead of sizeof(classname) which returns the size of the sum of its members. This change fixes these errors and adds a few tests. Reviewed-by: mgerdin, brutisso
author tschatzl
date Mon, 21 Jul 2014 09:40:19 +0200
parents 82693fb204a5
children 2c6ef90f030a
line wrap: on
line source

/*
 * Copyright (c) 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_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP
#define SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP

#include "memory/allocation.hpp"
#include "memory/freeList.hpp"
#include "runtime/globals.hpp"

class CodeBlobClosure;

class G1CodeRootChunk : public CHeapObj<mtGC> {
 private:
  static const int NUM_ENTRIES = 32;
 public:
  G1CodeRootChunk*     _next;
  G1CodeRootChunk*     _prev;

  nmethod** _top;

  nmethod* _data[NUM_ENTRIES];

  nmethod** bottom() const {
    return (nmethod**) &(_data[0]);
  }

  nmethod** end() const {
    return (nmethod**) &(_data[NUM_ENTRIES]);
  }

 public:
  G1CodeRootChunk();
  ~G1CodeRootChunk() {}

  static size_t word_size() { return (size_t)(align_size_up_(sizeof(G1CodeRootChunk), HeapWordSize) / HeapWordSize); }

  // FreeList "interface" methods

  G1CodeRootChunk* next() const         { return _next; }
  G1CodeRootChunk* prev() const         { return _prev; }
  void set_next(G1CodeRootChunk* v)     { _next = v; assert(v != this, "Boom");}
  void set_prev(G1CodeRootChunk* v)     { _prev = v; assert(v != this, "Boom");}
  void clear_next()       { set_next(NULL); }
  void clear_prev()       { set_prev(NULL); }

  size_t size() const { return word_size(); }

  void link_next(G1CodeRootChunk* ptr)  { set_next(ptr); }
  void link_prev(G1CodeRootChunk* ptr)  { set_prev(ptr); }
  void link_after(G1CodeRootChunk* ptr) {
    link_next(ptr);
    if (ptr != NULL) ptr->link_prev((G1CodeRootChunk*)this);
  }

  bool is_free()                 { return true; }

  // New G1CodeRootChunk routines

  void reset();

  bool is_empty() const {
    return _top == bottom();
  }

  bool is_full() const {
    return _top == (nmethod**)end();
  }

  bool contains(nmethod* method) {
    nmethod** cur = bottom();
    while (cur != _top) {
      if (*cur == method) return true;
      cur++;
    }
    return false;
  }

  bool add(nmethod* method) {
    if (is_full()) return false;
    *_top = method;
    _top++;
    return true;
  }

  bool remove(nmethod* method) {
    nmethod** cur = bottom();
    while (cur != _top) {
      if (*cur == method) {
        memmove(cur, cur + 1, (_top - (cur + 1)) * sizeof(nmethod**));
        _top--;
        return true;
      }
      cur++;
    }
    return false;
  }

  void nmethods_do(CodeBlobClosure* blk);

  nmethod* pop() {
    if (is_empty()) {
      return NULL;
    }
    _top--;
    return *_top;
  }
};

// Manages free chunks.
class G1CodeRootChunkManager VALUE_OBJ_CLASS_SPEC {
 private:
  // Global free chunk list management
  FreeList<G1CodeRootChunk> _free_list;
  // Total number of chunks handed out
  size_t _num_chunks_handed_out;

 public:
  G1CodeRootChunkManager();

  G1CodeRootChunk* new_chunk();
  void free_chunk(G1CodeRootChunk* chunk);
  // Free all elements of the given list.
  void free_all_chunks(FreeList<G1CodeRootChunk>* list);

  void initialize();
  void purge_chunks(size_t keep_ratio);

  static size_t static_mem_size();
  size_t fl_mem_size();

#ifndef PRODUCT
  size_t num_chunks_handed_out() const;
  size_t num_free_chunks() const;
#endif
};

// Implements storage for a set of code roots.
// All methods that modify the set are not thread-safe except if otherwise noted.
class G1CodeRootSet VALUE_OBJ_CLASS_SPEC {
 private:
  // Global default free chunk manager instance.
  static G1CodeRootChunkManager _default_chunk_manager;

  G1CodeRootChunk* new_chunk() { return _manager->new_chunk(); }
  void free_chunk(G1CodeRootChunk* chunk) { _manager->free_chunk(chunk); }
  // Free all elements of the given list.
  void free_all_chunks(FreeList<G1CodeRootChunk>* list) { _manager->free_all_chunks(list); }

  // Return the chunk that contains the given nmethod, NULL otherwise.
  // Scans the list of chunks backwards, as this method is used to add new
  // entries, which are typically added in bulk for a single nmethod.
  G1CodeRootChunk* find(nmethod* method);
  void free(G1CodeRootChunk* chunk);

  size_t _length;
  FreeList<G1CodeRootChunk> _list;
  G1CodeRootChunkManager* _manager;

 public:
  // If an instance is initialized with a chunk manager of NULL, use the global
  // default one.
  G1CodeRootSet(G1CodeRootChunkManager* manager = NULL);
  ~G1CodeRootSet();

  static void purge_chunks(size_t keep_ratio);

  static size_t free_chunks_static_mem_size();
  static size_t free_chunks_mem_size();

  // Search for the code blob from the recently allocated ones to find duplicates more quickly, as this
  // method is likely to be repeatedly called with the same nmethod.
  void add(nmethod* method);

  void remove(nmethod* method);
  nmethod* pop();

  bool contains(nmethod* method);

  void clear();

  void nmethods_do(CodeBlobClosure* blk) const;

  bool is_empty() { return length() == 0; }

  // Length in elements
  size_t length() const { return _length; }

  // Static data memory size in bytes of this set.
  static size_t static_mem_size();
  // Memory size in bytes taken by this set.
  size_t mem_size();

  static void test() PRODUCT_RETURN;
};

#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP