apetushkov@9858: /* apetushkov@9858: * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #include "precompiled.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/types/jfrTypeSetUtils.hpp" apetushkov@9858: #include "oops/instanceKlass.hpp" apetushkov@9858: #include "oops/oop.inline.hpp" apetushkov@9858: #include "oops/symbol.hpp" apetushkov@9858: apetushkov@9858: JfrSymbolId::JfrSymbolId() : _symbol_id_counter(0), _sym_table(new SymbolTable(this)), _cstring_table(new CStringTable(this)) { apetushkov@9858: assert(_sym_table != NULL, "invariant"); apetushkov@9858: assert(_cstring_table != NULL, "invariant"); apetushkov@9858: initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrSymbolId::initialize() { apetushkov@9858: clear(); apetushkov@9858: assert(_symbol_id_counter == 0, "invariant"); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrSymbolId::clear() { apetushkov@9858: assert(_sym_table != NULL, "invariant"); apetushkov@9858: if (_sym_table->has_entries()) { apetushkov@9858: _sym_table->clear_entries(); apetushkov@9858: } apetushkov@9858: assert(!_sym_table->has_entries(), "invariant"); apetushkov@9858: apetushkov@9858: assert(_cstring_table != NULL, "invariant"); apetushkov@9858: if (_cstring_table->has_entries()) { apetushkov@9858: _cstring_table->clear_entries(); apetushkov@9858: } apetushkov@9858: assert(!_cstring_table->has_entries(), "invariant"); apetushkov@9858: _symbol_id_counter = 0; apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrSymbolId::~JfrSymbolId() { apetushkov@9858: delete _sym_table; apetushkov@9858: delete _cstring_table; apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrSymbolId::mark_anonymous_klass_name(const Klass* k) { apetushkov@9858: assert(k != NULL, "invariant"); apetushkov@9858: assert(k->oop_is_instance(), "invariant"); apetushkov@9858: assert(is_anonymous_klass(k), "invariant"); apetushkov@9858: apetushkov@9858: uintptr_t anonymous_symbol_hash_code = 0; apetushkov@9858: const char* const anonymous_symbol = apetushkov@9858: create_anonymous_klass_symbol((const InstanceKlass*)k, anonymous_symbol_hash_code); apetushkov@9858: apetushkov@9858: if (anonymous_symbol == NULL) { apetushkov@9858: return 0; apetushkov@9858: } apetushkov@9858: apetushkov@9858: assert(anonymous_symbol_hash_code != 0, "invariant"); apetushkov@9858: traceid symbol_id = mark(anonymous_symbol, anonymous_symbol_hash_code); apetushkov@9858: assert(mark(anonymous_symbol, anonymous_symbol_hash_code) == symbol_id, "invariant"); apetushkov@9858: return symbol_id; apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::SymbolEntry* JfrSymbolId::map_symbol(const Symbol* symbol) const { apetushkov@9858: return _sym_table->lookup_only(symbol, (uintptr_t)const_cast(symbol)->identity_hash()); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::SymbolEntry* JfrSymbolId::map_symbol(uintptr_t hash) const { apetushkov@9858: return _sym_table->lookup_only(NULL, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::CStringEntry* JfrSymbolId::map_cstring(uintptr_t hash) const { apetushkov@9858: return _cstring_table->lookup_only(NULL, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrSymbolId::assign_id(SymbolEntry* entry) { apetushkov@9858: assert(entry != NULL, "invariant"); apetushkov@9858: assert(entry->id() == 0, "invariant"); apetushkov@9858: entry->set_id(++_symbol_id_counter); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrSymbolId::equals(const Symbol* query, uintptr_t hash, const SymbolEntry* entry) { apetushkov@9858: // query might be NULL apetushkov@9858: assert(entry != NULL, "invariant"); apetushkov@9858: assert(entry->hash() == hash, "invariant"); apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrSymbolId::assign_id(CStringEntry* entry) { apetushkov@9858: assert(entry != NULL, "invariant"); apetushkov@9858: assert(entry->id() == 0, "invariant"); apetushkov@9858: entry->set_id(++_symbol_id_counter); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrSymbolId::equals(const char* query, uintptr_t hash, const CStringEntry* entry) { apetushkov@9858: // query might be NULL apetushkov@9858: assert(entry != NULL, "invariant"); apetushkov@9858: assert(entry->hash() == hash, "invariant"); apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrSymbolId::mark(const Klass* k) { apetushkov@9858: assert(k != NULL, "invariant"); apetushkov@9858: traceid symbol_id = 0; apetushkov@9858: if (is_anonymous_klass(k)) { apetushkov@9858: symbol_id = mark_anonymous_klass_name(k); apetushkov@9858: } apetushkov@9858: if (0 == symbol_id) { apetushkov@9858: const Symbol* const sym = k->name(); apetushkov@9858: if (sym != NULL) { apetushkov@9858: symbol_id = mark(sym); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: assert(symbol_id > 0, "a symbol handler must mark the symbol for writing"); apetushkov@9858: return symbol_id; apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrSymbolId::mark(const Symbol* symbol) { apetushkov@9858: assert(symbol != NULL, "invariant"); apetushkov@9858: return mark(symbol, (uintptr_t)const_cast(symbol)->identity_hash()); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrSymbolId::mark(const Symbol* data, uintptr_t hash) { apetushkov@9858: assert(data != NULL, "invariant"); apetushkov@9858: assert(_sym_table != NULL, "invariant"); apetushkov@9858: return _sym_table->id(data, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrSymbolId::mark(const char* str, uintptr_t hash) { apetushkov@9858: assert(str != NULL, "invariant"); apetushkov@9858: return _cstring_table->id(str, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrSymbolId::is_anonymous_klass(const Klass* k) { apetushkov@9858: assert(k != NULL, "invariant"); apetushkov@9858: return k->oop_is_instance() && ((const InstanceKlass*)k)->is_anonymous(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: /* apetushkov@9858: * jsr292 anonymous classes symbol is the external name + apetushkov@9858: * the identity_hashcode slash appended: apetushkov@9858: * java.lang.invoke.LambdaForm$BMH/22626602 apetushkov@9858: * apetushkov@9858: * caller needs ResourceMark apetushkov@9858: */ apetushkov@9858: apetushkov@9858: uintptr_t JfrSymbolId::anonymous_klass_name_hash_code(const InstanceKlass* ik) { apetushkov@9858: assert(ik != NULL, "invariant"); apetushkov@9858: assert(ik->is_anonymous(), "invariant"); apetushkov@9858: const oop mirror = ik->java_mirror(); apetushkov@9858: assert(mirror != NULL, "invariant"); apetushkov@9858: return (uintptr_t)mirror->identity_hash(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const char* JfrSymbolId::create_anonymous_klass_symbol(const InstanceKlass* ik, uintptr_t& hashcode) { apetushkov@9858: assert(ik != NULL, "invariant"); apetushkov@9858: assert(ik->is_anonymous(), "invariant"); apetushkov@9858: assert(0 == hashcode, "invariant"); apetushkov@9858: char* anonymous_symbol = NULL; apetushkov@9858: const oop mirror = ik->java_mirror(); apetushkov@9858: assert(mirror != NULL, "invariant"); apetushkov@9858: char hash_buf[40]; apetushkov@9858: hashcode = anonymous_klass_name_hash_code(ik); apetushkov@9858: sprintf(hash_buf, "/" UINTX_FORMAT, hashcode); apetushkov@9858: const size_t hash_len = strlen(hash_buf); apetushkov@9858: const size_t result_len = ik->name()->utf8_length(); apetushkov@9858: anonymous_symbol = NEW_RESOURCE_ARRAY(char, result_len + hash_len + 1); apetushkov@9858: ik->name()->as_klass_external_name(anonymous_symbol, (int)result_len + 1); apetushkov@9858: assert(strlen(anonymous_symbol) == result_len, "invariant"); apetushkov@9858: strcpy(anonymous_symbol + result_len, hash_buf); apetushkov@9858: assert(strlen(anonymous_symbol) == result_len + hash_len, "invariant"); apetushkov@9858: return anonymous_symbol; apetushkov@9858: } apetushkov@9858: apetushkov@9858: uintptr_t JfrSymbolId::regular_klass_name_hash_code(const Klass* k) { apetushkov@9858: assert(k != NULL, "invariant"); apetushkov@9858: const Symbol* const sym = k->name(); apetushkov@9858: assert(sym != NULL, "invariant"); apetushkov@9858: return (uintptr_t)const_cast(sym)->identity_hash(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrArtifactSet::JfrArtifactSet(bool class_unload) : _symbol_id(new JfrSymbolId()), apetushkov@9858: _klass_list(NULL), apetushkov@9858: _class_unload(class_unload) { apetushkov@9858: initialize(class_unload); apetushkov@9858: assert(_klass_list != NULL, "invariant"); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static const size_t initial_class_list_size = 200; apetushkov@9858: void JfrArtifactSet::initialize(bool class_unload) { apetushkov@9858: assert(_symbol_id != NULL, "invariant"); apetushkov@9858: _symbol_id->initialize(); apetushkov@9858: assert(!_symbol_id->has_entries(), "invariant"); apetushkov@9858: _symbol_id->mark(BOOTSTRAP_LOADER_NAME, 0); // pre-load "bootstrap" apetushkov@9858: _class_unload = class_unload; apetushkov@9858: // resource allocation apetushkov@9858: _klass_list = new GrowableArray(initial_class_list_size, false, mtTracing); apetushkov@9858: } apetushkov@9858: apetushkov@9858: JfrArtifactSet::~JfrArtifactSet() { apetushkov@9858: clear(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrArtifactSet::clear() { apetushkov@9858: _symbol_id->clear(); apetushkov@9858: // _klass_list will be cleared by a ResourceMark apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrArtifactSet::mark_anonymous_klass_name(const Klass* klass) { apetushkov@9858: return _symbol_id->mark_anonymous_klass_name(klass); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrArtifactSet::mark(const Symbol* sym, uintptr_t hash) { apetushkov@9858: return _symbol_id->mark(sym, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrArtifactSet::mark(const Klass* klass) { apetushkov@9858: return _symbol_id->mark(klass); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrArtifactSet::mark(const Symbol* symbol) { apetushkov@9858: return _symbol_id->mark(symbol); apetushkov@9858: } apetushkov@9858: apetushkov@9858: traceid JfrArtifactSet::mark(const char* const str, uintptr_t hash) { apetushkov@9858: return _symbol_id->mark(str, hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::SymbolEntry* JfrArtifactSet::map_symbol(const Symbol* symbol) const { apetushkov@9858: return _symbol_id->map_symbol(symbol); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::SymbolEntry* JfrArtifactSet::map_symbol(uintptr_t hash) const { apetushkov@9858: return _symbol_id->map_symbol(hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: const JfrSymbolId::CStringEntry* JfrArtifactSet::map_cstring(uintptr_t hash) const { apetushkov@9858: return _symbol_id->map_cstring(hash); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrArtifactSet::has_klass_entries() const { apetushkov@9858: return _klass_list->is_nonempty(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: int JfrArtifactSet::entries() const { apetushkov@9858: return _klass_list->length(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrArtifactSet::register_klass(const Klass* k) { apetushkov@9858: assert(k != NULL, "invariant"); apetushkov@9858: assert(_klass_list != NULL, "invariant"); apetushkov@9858: assert(_klass_list->find(k) == -1, "invariant"); apetushkov@9858: _klass_list->append(k); apetushkov@9858: }