blob: 29d483094bdcc98db9dfd9044bda08d0690ff4b2 [file] [log] [blame]
Andreas Gampecc13b222016-10-10 19:09:09 -07001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include "object_tagging.h"
33
34#include "art_jvmti.h"
35#include "base/logging.h"
36#include "events-inl.h"
37#include "gc/allocation_listener.h"
38#include "instrumentation.h"
39#include "jni_env_ext-inl.h"
40#include "mirror/class.h"
41#include "mirror/object.h"
42#include "runtime.h"
43#include "ScopedLocalRef.h"
44
45namespace openjdkjvmti {
46
47void ObjectTagTable::Add(art::mirror::Object* obj, jlong tag) {
48 art::Thread* self = art::Thread::Current();
49 art::MutexLock mu(self, allow_disallow_lock_);
50 Wait(self);
51
52 if (first_free_ == tagged_objects_.size()) {
53 tagged_objects_.push_back(Entry(art::GcRoot<art::mirror::Object>(obj), tag));
54 first_free_++;
55 } else {
56 DCHECK_LT(first_free_, tagged_objects_.size());
57 DCHECK(tagged_objects_[first_free_].first.IsNull());
58 tagged_objects_[first_free_] = Entry(art::GcRoot<art::mirror::Object>(obj), tag);
59 // TODO: scan for free elements.
60 first_free_ = tagged_objects_.size();
61 }
62}
63
64bool ObjectTagTable::Remove(art::mirror::Object* obj, jlong* tag) {
65 art::Thread* self = art::Thread::Current();
66 art::MutexLock mu(self, allow_disallow_lock_);
67 Wait(self);
68
69 for (auto it = tagged_objects_.begin(); it != tagged_objects_.end(); ++it) {
70 if (it->first.Read(nullptr) == obj) {
71 if (tag != nullptr) {
72 *tag = it->second;
73 }
74 it->first = art::GcRoot<art::mirror::Object>(nullptr);
75
76 size_t index = it - tagged_objects_.begin();
77 if (index < first_free_) {
78 first_free_ = index;
79 }
80
81 // TODO: compaction.
82
83 return true;
84 }
85 }
86
87 return false;
88}
89
Andreas Gampee54d9922016-10-11 19:55:37 -070090bool ObjectTagTable::Set(art::mirror::Object* obj, jlong new_tag) {
91 art::Thread* self = art::Thread::Current();
92 art::MutexLock mu(self, allow_disallow_lock_);
93 Wait(self);
94
95 for (auto& pair : tagged_objects_) {
96 if (pair.first.Read(nullptr) == obj) {
97 pair.second = new_tag;
98 return true;
99 }
100 }
101
102 // TODO refactor with Add.
103 if (first_free_ == tagged_objects_.size()) {
104 tagged_objects_.push_back(Entry(art::GcRoot<art::mirror::Object>(obj), new_tag));
105 first_free_++;
106 } else {
107 DCHECK_LT(first_free_, tagged_objects_.size());
108 DCHECK(tagged_objects_[first_free_].first.IsNull());
109 tagged_objects_[first_free_] = Entry(art::GcRoot<art::mirror::Object>(obj), new_tag);
110 // TODO: scan for free elements.
111 first_free_ = tagged_objects_.size();
112 }
113
114 return false;
115}
116
Andreas Gampecc13b222016-10-10 19:09:09 -0700117void ObjectTagTable::Sweep(art::IsMarkedVisitor* visitor) {
118 if (event_handler_->IsEventEnabledAnywhere(JVMTI_EVENT_OBJECT_FREE)) {
119 SweepImpl<true>(visitor);
120 } else {
121 SweepImpl<false>(visitor);
122 }
123}
124
125template <bool kHandleNull>
126void ObjectTagTable::SweepImpl(art::IsMarkedVisitor* visitor) {
127 art::Thread* self = art::Thread::Current();
128 art::MutexLock mu(self, allow_disallow_lock_);
129
130 for (auto it = tagged_objects_.begin(); it != tagged_objects_.end(); ++it) {
131 if (!it->first.IsNull()) {
132 art::mirror::Object* original_obj = it->first.Read<art::kWithoutReadBarrier>();
133 art::mirror::Object* target_obj = visitor->IsMarked(original_obj);
134 if (original_obj != target_obj) {
135 it->first = art::GcRoot<art::mirror::Object>(target_obj);
136
137 if (kHandleNull && target_obj == nullptr) {
138 HandleNullSweep(it->second);
139 }
140 }
141 } else {
142 size_t index = it - tagged_objects_.begin();
143 if (index < first_free_) {
144 first_free_ = index;
145 }
146 }
147 }
148}
149
150void ObjectTagTable::HandleNullSweep(jlong tag) {
151 event_handler_->DispatchEvent(nullptr, JVMTI_EVENT_OBJECT_FREE, tag);
152}
153
154} // namespace openjdkjvmti