codeCache.hpp revision 1827:1e9a9d2e6509
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
1458705Scharnier *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes *
231541Srgrimes */
241541Srgrimes
251541Srgrimes// The CodeCache implements the code cache for various pieces of generated
261541Srgrimes// code, e.g., compiled java methods, runtime stubs, transition frames, etc.
271541Srgrimes// The entries in the CodeCache are all CodeBlob's.
281541Srgrimes
291541Srgrimes// Implementation:
301541Srgrimes//   - Each CodeBlob occupies one chunk of memory.
311541Srgrimes//   - Like the offset table in oldspace the zone has at table for
321541Srgrimes//     locating a method given a addess of an instruction.
331541Srgrimes
3450477Speterclass OopClosure;
351541Srgrimesclass DepChange;
361541Srgrimes
371541Srgrimesclass CodeCache : AllStatic {
381541Srgrimes  friend class VMStructs;
391541Srgrimes private:
401541Srgrimes  // CodeHeap is malloc()'ed at startup and never deleted during shutdown,
4134924Sbde  // so that the generated assembly code is always there when it's needed.
4212662Sdg  // This may cause memory leak, but is necessary, for now. See 4423824,
4312662Sdg  // 4422213 or 4436291 for details.
441541Srgrimes  static CodeHeap * _heap;
4540794Speter  static int _number_of_blobs;
4612726Sbde  static int _number_of_adapters;
4712662Sdg  static int _number_of_nmethods;
4822521Sdyson  static int _number_of_nmethods_with_dependencies;
4912662Sdg  static bool _needs_cache_clean;
5012662Sdg  static nmethod* _scavenge_root_nmethods;  // linked via nm->scavenge_root_link()
5112662Sdg  static nmethod* _saved_nmethods;          // linked via nm->saved_nmethod_look()
521541Srgrimes
531541Srgrimes  static void verify_if_often() PRODUCT_RETURN;
5412623Sphk
5512623Sphk  static void mark_scavenge_root_nmethods() PRODUCT_RETURN;
5612623Sphk  static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN;
579759Sbde
581541Srgrimes public:
5912820Sphk
601541Srgrimes  // Initialization
611541Srgrimes  static void initialize();
621541Srgrimes
631541Srgrimes  // Allocation/administration
641541Srgrimes  static CodeBlob* allocate(int size);              // allocates a new CodeBlob
6512820Sphk  static void commit(CodeBlob* cb);                 // called when the allocated CodeBlob has been filled
661541Srgrimes  static int alignment_unit();                      // guaranteed alignment of all CodeBlobs
671541Srgrimes  static int alignment_offset();                    // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
681541Srgrimes  static void free(CodeBlob* cb);                   // frees a CodeBlob
691541Srgrimes  static void flush();                              // flushes all CodeBlobs
701541Srgrimes  static bool contains(void *p);                    // returns whether p is included
711541Srgrimes  static void blobs_do(void f(CodeBlob* cb));       // iterates over all CodeBlobs
721541Srgrimes  static void blobs_do(CodeBlobClosure* f);         // iterates over all CodeBlobs
731541Srgrimes  static void nmethods_do(void f(nmethod* nm));     // iterates over all nmethods
741541Srgrimes
759507Sdg  // Lookup
7612286Sphk  static CodeBlob* find_blob(void* start);
771541Srgrimes  static nmethod*  find_nmethod(void* start);
781541Srgrimes
791541Srgrimes  // Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know
801541Srgrimes  // what you are doing)
8169947Sjake  static CodeBlob* find_blob_unsafe(void* start) {
8214531Shsu    CodeBlob* result = (CodeBlob*)_heap->find_start(start);
831541Srgrimes    // this assert is too strong because the heap code will return the
841541Srgrimes    // heapblock containing start. That block can often be larger than
851541Srgrimes    // the codeBlob itself. If you look up an address that is within
861541Srgrimes    // the heapblock but not in the codeBlob you will assert.
8758634Scharnier    //
881541Srgrimes    // Most things will not lookup such bad addresses. However
8965904Sjhb    // AsyncGetCallTrace can see intermediate frames and get that kind
9065904Sjhb    // of invalid address and so can a developer using hsfind.
9165904Sjhb    //
921541Srgrimes    // The more correct answer is to return NULL if blob_contains() returns
931541Srgrimes    // false.
941541Srgrimes    // assert(result == NULL || result->blob_contains((address)start), "found wrong CodeBlob");
951541Srgrimes
9669947Sjake    if (result != NULL && !result->blob_contains((address)start)) {
971541Srgrimes      result = NULL;
981541Srgrimes    }
995455Sdg    return result;
1001541Srgrimes  }
1011541Srgrimes
1029507Sdg  // Iteration
1039507Sdg  static CodeBlob* first();
1049507Sdg  static CodeBlob* next (CodeBlob* cb);
1059507Sdg  static CodeBlob* alive(CodeBlob *cb);
10634961Sphk  static nmethod* alive_nmethod(CodeBlob *cb);
1079507Sdg  static nmethod* first_nmethod();
1089507Sdg  static nmethod* next_nmethod (CodeBlob* cb);
1099507Sdg  static int       nof_blobs()                 { return _number_of_blobs; }
1109507Sdg  static int       nof_adapters()              { return _number_of_adapters; }
1119507Sdg  static int       nof_nmethods()              { return _number_of_nmethods; }
11262622Sjhb
11312286Sphk  // GC support
11462622Sjhb  static void gc_epilogue();
11512286Sphk  static void gc_prologue();
11662622Sjhb  // If "unloading_occurred" is true, then unloads (i.e., breaks root links
11712286Sphk  // to) any unmarked codeBlobs in the cache.  Sets "marked_for_unloading"
11862622Sjhb  // to "true" iff some code got unloaded.
11912286Sphk  static void do_unloading(BoolObjectClosure* is_alive,
12062622Sjhb                           OopClosure* keep_alive,
12112286Sphk                           bool unloading_occurred);
12262622Sjhb  static void oops_do(OopClosure* f) {
12312286Sphk    CodeBlobToOopClosure oopc(f, /*do_marking=*/ false);
12462622Sjhb    blobs_do(&oopc);
12512286Sphk  }
12662622Sjhb  static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN;
12751337Sdillon  static void scavenge_root_nmethods_do(CodeBlobClosure* f);
12812286Sphk
12946381Sbillf  static nmethod* scavenge_root_nmethods()          { return _scavenge_root_nmethods; }
13046381Sbillf  static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; }
1311541Srgrimes  static void add_scavenge_root_nmethod(nmethod* nm);
13212286Sphk  static void drop_scavenge_root_nmethod(nmethod* nm);
13362573Sphk  static void prune_scavenge_root_nmethods();
1341541Srgrimes
13512286Sphk  // Printing/debugging
13612286Sphk  static void print()   PRODUCT_RETURN;          // prints summary
13712286Sphk  static void print_internals();
13812286Sphk  static void verify();                          // verifies the code cache
13912286Sphk  static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
1401541Srgrimes  static void print_bounds(outputStream* st);    // Prints a summary of the bounds of the code cache
1411541Srgrimes
14212286Sphk  // The full limits of the codeCache
1431541Srgrimes  static address  low_bound()                    { return (address) _heap->low_boundary(); }
1441541Srgrimes  static address  high_bound()                   { return (address) _heap->high_boundary(); }
1451541Srgrimes
1461541Srgrimes  // Profiling
14715809Sdyson  static address first_address();                // first address used for CodeBlobs
1485455Sdg  static address last_address();                 // last  address used for CodeBlobs
14915809Sdyson  static size_t  capacity()                      { return _heap->capacity(); }
15038517Sdfr  static size_t  max_capacity()                  { return _heap->max_capacity(); }
1511541Srgrimes  static size_t  unallocated_capacity()          { return _heap->unallocated_capacity(); }
1521541Srgrimes  static bool    needs_flushing()                { return unallocated_capacity() < CodeCacheFlushingMinimumFreeSpace; }
1531541Srgrimes
15469947Sjake  static bool needs_cache_clean()                { return _needs_cache_clean; }
15514531Shsu  static void set_needs_cache_clean(bool v)      { _needs_cache_clean = v;    }
1561541Srgrimes  static void clear_inline_caches();             // clear all inline caches
1571541Srgrimes
1581541Srgrimes  static nmethod* find_and_remove_saved_code(methodOop m);
1591541Srgrimes  static void remove_saved_code(nmethod* nm);
1601541Srgrimes  static void speculatively_disconnect(nmethod* nm);
1611541Srgrimes
16265557Sjasone  // Deoptimization
1631541Srgrimes  static int  mark_for_deoptimization(DepChange& changes);
1641541Srgrimes#ifdef HOTSWAP
1651541Srgrimes  static int  mark_for_evol_deoptimization(instanceKlassHandle dependee);
1661541Srgrimes#endif // HOTSWAP
1671541Srgrimes
1681541Srgrimes  static void mark_all_nmethods_for_deoptimization();
1691541Srgrimes  static int  mark_for_deoptimization(methodOop dependee);
1701541Srgrimes  static void make_marked_nmethods_zombies();
1711541Srgrimes  static void make_marked_nmethods_not_entrant();
1721541Srgrimes
1731541Srgrimes    // tells how many nmethods have dependencies
1741541Srgrimes  static int number_of_nmethods_with_dependencies();
1751541Srgrimes};
17665557Sjasone