1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_PRIMS_JVMTITAGMAP_HPP
  26 #define SHARE_VM_PRIMS_JVMTITAGMAP_HPP
  27 
  28 #ifndef KERNEL
  29 #include "gc_interface/collectedHeap.hpp"
  30 #include "jvmtifiles/jvmti.h"
  31 #include "jvmtifiles/jvmtiEnv.hpp"
  32 #include "memory/allocation.hpp"
  33 #include "memory/genCollectedHeap.hpp"
  34 #include "memory/universe.hpp"
  35 #endif
  36 
  37 // JvmtiTagMap
  38 
  39 #ifndef _JAVA_JVMTI_TAG_MAP_H_
  40 #define _JAVA_JVMTI_TAG_MAP_H_
  41 
  42 // forward references
  43 class JvmtiTagHashmap;
  44 class JvmtiTagHashmapEntry;
  45 class JvmtiTagHashmapEntryClosure;
  46 
  47 class JvmtiTagMap :  public CHeapObj {
  48  private:
  49 
  50   enum{
  51     n_hashmaps = 2,                                 // encapsulates 2 hashmaps
  52     max_free_entries = 4096                         // maximum number of free entries per env
  53   };
  54 
  55   // memory region for young generation
  56   static MemRegion _young_gen;
  57   static void get_young_generation();
  58 
  59   JvmtiEnv*             _env;                       // the jvmti environment
  60   Mutex                 _lock;                      // lock for this tag map
  61   JvmtiTagHashmap*      _hashmap[n_hashmaps];       // the hashmaps
  62 
  63   JvmtiTagHashmapEntry* _free_entries;              // free list for this environment
  64   int _free_entries_count;                          // number of entries on the free list
  65 
  66   // create a tag map
  67   JvmtiTagMap(JvmtiEnv* env);
  68 
  69   // accessors
  70   inline Mutex* lock()                      { return &_lock; }
  71   inline JvmtiEnv* env() const              { return _env; }
  72 
  73   // rehash tags maps for generation start to end
  74   void rehash(int start, int end);
  75 
  76   // indicates if the object is in the young generation
  77   static bool is_in_young(oop o);
  78 
  79   // iterate over all entries in this tag map
  80   void entry_iterate(JvmtiTagHashmapEntryClosure* closure);
  81 
  82  public:
  83 
  84   // indicates if this tag map is locked
  85   bool is_locked()                          { return lock()->is_locked(); }
  86 
  87   // return the appropriate hashmap for a given object
  88   JvmtiTagHashmap* hashmap_for(oop o);
  89 
  90   // create/destroy entries
  91   JvmtiTagHashmapEntry* create_entry(jweak ref, jlong tag);
  92   void destroy_entry(JvmtiTagHashmapEntry* entry);
  93 
  94   // returns true if the hashmaps are empty
  95   bool is_empty();
  96 
  97   // return tag for the given environment
  98   static JvmtiTagMap* tag_map_for(JvmtiEnv* env);
  99 
 100   // destroy tag map
 101   ~JvmtiTagMap();
 102 
 103   // set/get tag
 104   void set_tag(jobject obj, jlong tag);
 105   jlong get_tag(jobject obj);
 106 
 107   // deprecated heap iteration functions
 108   void iterate_over_heap(jvmtiHeapObjectFilter object_filter,
 109                          KlassHandle klass,
 110                          jvmtiHeapObjectCallback heap_object_callback,
 111                          const void* user_data);
 112 
 113   void iterate_over_reachable_objects(jvmtiHeapRootCallback heap_root_callback,
 114                                       jvmtiStackReferenceCallback stack_ref_callback,
 115                                       jvmtiObjectReferenceCallback object_ref_callback,
 116                                       const void* user_data);
 117 
 118   void iterate_over_objects_reachable_from_object(jobject object,
 119                                                   jvmtiObjectReferenceCallback object_reference_callback,
 120                                                   const void* user_data);
 121 
 122 
 123   // advanced (JVMTI 1.1) heap iteration functions
 124   void iterate_through_heap(jint heap_filter,
 125                             KlassHandle klass,
 126                             const jvmtiHeapCallbacks* callbacks,
 127                             const void* user_data);
 128 
 129   void follow_references(jint heap_filter,
 130                          KlassHandle klass,
 131                          jobject initial_object,
 132                          const jvmtiHeapCallbacks* callbacks,
 133                          const void* user_data);
 134 
 135   // get tagged objects
 136   jvmtiError get_objects_with_tags(const jlong* tags, jint count,
 137                                    jint* count_ptr, jobject** object_result_ptr,
 138                                    jlong** tag_result_ptr);
 139 
 140   // call post-GC to rehash the tag maps.
 141   static void gc_epilogue(bool full);
 142 
 143   // call after referencing processing has completed (CMS)
 144   static void cms_ref_processing_epilogue();
 145 };
 146 
 147 #endif   /* _JAVA_JVMTI_TAG_MAP_H_ */
 148 
 149 #endif // SHARE_VM_PRIMS_JVMTITAGMAP_HPP