jdk

Форк
0
/
resolvedMethodTable.cpp 
407 строк · 12.4 Кб
1
/*
2
 * Copyright (c) 2017, 2024, 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
#include "precompiled.hpp"
26
#include "classfile/classLoaderData.hpp"
27
#include "classfile/javaClasses.hpp"
28
#include "gc/shared/oopStorage.inline.hpp"
29
#include "gc/shared/oopStorageSet.hpp"
30
#include "logging/log.hpp"
31
#include "memory/allocation.hpp"
32
#include "memory/resourceArea.hpp"
33
#include "memory/universe.hpp"
34
#include "oops/access.inline.hpp"
35
#include "oops/method.hpp"
36
#include "oops/oop.inline.hpp"
37
#include "oops/weakHandle.inline.hpp"
38
#include "prims/resolvedMethodTable.hpp"
39
#include "runtime/atomic.hpp"
40
#include "runtime/handles.inline.hpp"
41
#include "runtime/interfaceSupport.inline.hpp"
42
#include "runtime/mutexLocker.hpp"
43
#include "runtime/safepointVerifiers.hpp"
44
#include "runtime/timerTrace.hpp"
45
#include "utilities/concurrentHashTable.inline.hpp"
46
#include "utilities/concurrentHashTableTasks.inline.hpp"
47
#include "utilities/macros.hpp"
48

49
// 2^24 is max size
50
static const size_t END_SIZE = 24;
51
// If a chain gets to 32 something might be wrong
52
static const size_t GROW_HINT = 32;
53

54
static const size_t ResolvedMethodTableSizeLog = 10;
55

56
static unsigned int method_hash(const Method* method) {
57
  unsigned int hash = method->method_holder()->class_loader_data()->identity_hash();
58
  hash = (hash * 31) ^ method->klass_name()->identity_hash();
59
  hash = (hash * 31) ^ method->name()->identity_hash();
60
  hash = (hash * 31) ^ method->signature()->identity_hash();
61
  return hash;
62
}
63

64
typedef ConcurrentHashTable<ResolvedMethodTableConfig,
65
                            mtClass> ResolvedMethodTableHash;
66

67
class ResolvedMethodTableConfig : public AllStatic {
68
 private:
69
 public:
70
  typedef WeakHandle Value;
71

72
  static uintx get_hash(Value const& value, bool* is_dead) {
73
    oop val_oop = value.peek();
74
    if (val_oop == nullptr) {
75
      *is_dead = true;
76
      return 0;
77
    }
78
    *is_dead = false;
79
    Method* method = java_lang_invoke_ResolvedMethodName::vmtarget(val_oop);
80
    return method_hash(method);
81
  }
82

83
  // We use default allocation/deallocation but counted
84
  static void* allocate_node(void* context, size_t size, Value const& value) {
85
    ResolvedMethodTable::item_added();
86
    return AllocateHeap(size, mtClass);
87
  }
88
  static void free_node(void* context, void* memory, Value& value) {
89
    value.release(ResolvedMethodTable::_oop_storage);
90
    FreeHeap(memory);
91
    ResolvedMethodTable::item_removed();
92
  }
93
};
94

95
static ResolvedMethodTableHash* _local_table           = nullptr;
96
static size_t                   _current_size          = (size_t)1 << ResolvedMethodTableSizeLog;
97

98
volatile bool            ResolvedMethodTable::_has_work              = false;
99
OopStorage*              ResolvedMethodTable::_oop_storage;
100

101
volatile size_t          _items_count           = 0;
102

103
void ResolvedMethodTable::create_table() {
104
  _local_table  = new ResolvedMethodTableHash(ResolvedMethodTableSizeLog, END_SIZE, GROW_HINT);
105
  log_trace(membername, table)("Start size: " SIZE_FORMAT " (" SIZE_FORMAT ")",
106
                               _current_size, ResolvedMethodTableSizeLog);
107
  _oop_storage = OopStorageSet::create_weak("ResolvedMethodTable Weak", mtClass);
108
  _oop_storage->register_num_dead_callback(&gc_notification);
109
}
110

111
size_t ResolvedMethodTable::table_size() {
112
  return (size_t)1 << _local_table->get_size_log2(Thread::current());
113
}
114

115
class ResolvedMethodTableLookup : StackObj {
116
 private:
117
  Thread*       _thread;
118
  uintx         _hash;
119
  const Method* _method;
120
  Handle        _found;
121

122
 public:
123
  ResolvedMethodTableLookup(Thread* thread, uintx hash, const Method* key)
124
    : _thread(thread), _hash(hash), _method(key) {
125
  }
126
  uintx get_hash() const {
127
    return _hash;
128
  }
129
  bool equals(WeakHandle* value) {
130
    oop val_oop = value->peek();
131
    if (val_oop == nullptr) {
132
      return false;
133
    }
134
    bool equals = _method == java_lang_invoke_ResolvedMethodName::vmtarget(val_oop);
135
    if (!equals) {
136
      return false;
137
    }
138
    // Need to resolve weak handle and Handleize through possible safepoint.
139
    _found = Handle(_thread, value->resolve());
140
    return true;
141
  }
142
  bool is_dead(WeakHandle* value) {
143
    oop val_oop = value->peek();
144
    return val_oop == nullptr;
145
  }
146
};
147

148

149
class ResolvedMethodGet : public StackObj {
150
  Thread*       _thread;
151
  const Method* _method;
152
  Handle        _return;
153
public:
154
  ResolvedMethodGet(Thread* thread, const Method* method) : _thread(thread), _method(method) {}
155
  void operator()(WeakHandle* val) {
156
    oop result = val->resolve();
157
    assert(result != nullptr, "Result should be reachable");
158
    _return = Handle(_thread, result);
159
    log_get();
160
  }
161
  oop get_res_oop() {
162
    return _return();
163
  }
164
  void log_get() {
165
    LogTarget(Trace, membername, table) log;
166
    if (log.is_enabled()) {
167
      ResourceMark rm;
168
      log.print("ResolvedMethod entry found for %s",
169
                _method->name_and_sig_as_C_string());
170
    }
171
  }
172
};
173

174
oop ResolvedMethodTable::find_method(const Method* method) {
175
  Thread* thread = Thread::current();
176

177
  ResolvedMethodTableLookup lookup(thread, method_hash(method), method);
178
  ResolvedMethodGet rmg(thread, method);
179
  _local_table->get(thread, lookup, rmg);
180

181
  return rmg.get_res_oop();
182
}
183

184
static void log_insert(const Method* method) {
185
  LogTarget(Debug, membername, table) log;
186
  if (log.is_enabled()) {
187
    ResourceMark rm;
188
    log.print("ResolvedMethod entry added for %s",
189
              method->name_and_sig_as_C_string());
190
  }
191
}
192

193
oop ResolvedMethodTable::add_method(const Method* method, Handle rmethod_name) {
194
  Thread* thread = Thread::current();
195

196
  ResolvedMethodTableLookup lookup(thread, method_hash(method), method);
197
  ResolvedMethodGet rmg(thread, method);
198

199
  while (true) {
200
    if (_local_table->get(thread, lookup, rmg)) {
201
      return rmg.get_res_oop();
202
    }
203
    WeakHandle wh(_oop_storage, rmethod_name);
204
    // The hash table takes ownership of the WeakHandle, even if it's not inserted.
205
    if (_local_table->insert(thread, lookup, wh)) {
206
      log_insert(method);
207
      return wh.resolve();
208
    }
209
  }
210
}
211

212
void ResolvedMethodTable::item_added() {
213
  Atomic::inc(&_items_count);
214
}
215

216
void ResolvedMethodTable::item_removed() {
217
  Atomic::dec(&_items_count);
218
  log_trace(membername, table) ("ResolvedMethod entry removed");
219
}
220

221
double ResolvedMethodTable::get_load_factor() {
222
  return double(_items_count)/double(_current_size);
223
}
224

225
double ResolvedMethodTable::get_dead_factor(size_t num_dead) {
226
  return double(num_dead)/double(_current_size);
227
}
228

229
static const double PREF_AVG_LIST_LEN = 2.0;
230
// If we have as many dead items as 50% of the number of bucket
231
static const double CLEAN_DEAD_HIGH_WATER_MARK = 0.5;
232

233
void ResolvedMethodTable::gc_notification(size_t num_dead) {
234
  log_trace(membername, table)("Uncleaned items:" SIZE_FORMAT, num_dead);
235

236
  if (has_work()) {
237
    return;
238
  }
239

240
  double load_factor = get_load_factor();
241
  double dead_factor = get_dead_factor(num_dead);
242
  // We should clean/resize if we have more dead than alive,
243
  // more items than preferred load factor or
244
  // more dead items than water mark.
245
  if ((dead_factor > load_factor) ||
246
      (load_factor > PREF_AVG_LIST_LEN) ||
247
      (dead_factor > CLEAN_DEAD_HIGH_WATER_MARK)) {
248
    log_debug(membername, table)("Concurrent work triggered, live factor: %g dead factor: %g",
249
                                 load_factor, dead_factor);
250
    trigger_concurrent_work();
251
  }
252
}
253

254
void ResolvedMethodTable::trigger_concurrent_work() {
255
  MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
256
  Atomic::store(&_has_work, true);
257
  Service_lock->notify_all();
258
}
259

260
bool ResolvedMethodTable::has_work() {
261
  return Atomic::load_acquire(&_has_work);
262
}
263

264
void ResolvedMethodTable::do_concurrent_work(JavaThread* jt) {
265
  double load_factor = get_load_factor();
266
  log_debug(membername, table)("Concurrent work, live factor: %g", load_factor);
267
  // We prefer growing, since that also removes dead items
268
  if (load_factor > PREF_AVG_LIST_LEN && !_local_table->is_max_size_reached()) {
269
    grow(jt);
270
  } else {
271
    clean_dead_entries(jt);
272
  }
273
  Atomic::release_store(&_has_work, false);
274
}
275

276
void ResolvedMethodTable::grow(JavaThread* jt) {
277
  ResolvedMethodTableHash::GrowTask gt(_local_table);
278
  if (!gt.prepare(jt)) {
279
    return;
280
  }
281
  log_trace(membername, table)("Started to grow");
282
  {
283
    TraceTime timer("Grow", TRACETIME_LOG(Debug, membername, table, perf));
284
    while (gt.do_task(jt)) {
285
      gt.pause(jt);
286
      {
287
        ThreadBlockInVM tbivm(jt);
288
      }
289
      gt.cont(jt);
290
    }
291
  }
292
  gt.done(jt);
293
  _current_size = table_size();
294
  log_info(membername, table)("Grown to size:" SIZE_FORMAT, _current_size);
295
}
296

297
struct ResolvedMethodTableDoDelete : StackObj {
298
  void operator()(WeakHandle* val) {
299
    /* do nothing */
300
  }
301
};
302

303
struct ResolvedMethodTableDeleteCheck : StackObj {
304
  long _count;
305
  long _item;
306
  ResolvedMethodTableDeleteCheck() : _count(0), _item(0) {}
307
  bool operator()(WeakHandle* val) {
308
    ++_item;
309
    oop tmp = val->peek();
310
    if (tmp == nullptr) {
311
      ++_count;
312
      return true;
313
    } else {
314
      return false;
315
    }
316
  }
317
};
318

319
void ResolvedMethodTable::clean_dead_entries(JavaThread* jt) {
320
  ResolvedMethodTableHash::BulkDeleteTask bdt(_local_table);
321
  if (!bdt.prepare(jt)) {
322
    return;
323
  }
324
  ResolvedMethodTableDeleteCheck stdc;
325
  ResolvedMethodTableDoDelete stdd;
326
  {
327
    TraceTime timer("Clean", TRACETIME_LOG(Debug, membername, table, perf));
328
    while(bdt.do_task(jt, stdc, stdd)) {
329
      bdt.pause(jt);
330
      {
331
        ThreadBlockInVM tbivm(jt);
332
      }
333
      bdt.cont(jt);
334
    }
335
    bdt.done(jt);
336
  }
337
  log_info(membername, table)("Cleaned %ld of %ld", stdc._count, stdc._item);
338
}
339

340
#if INCLUDE_JVMTI
341
class AdjustMethodEntries : public StackObj {
342
  bool* _trace_name_printed;
343
public:
344
  AdjustMethodEntries(bool* trace_name_printed) : _trace_name_printed(trace_name_printed) {};
345
  bool operator()(WeakHandle* entry) {
346
    oop mem_name = entry->peek();
347
    if (mem_name == nullptr) {
348
      // Removed
349
      return true;
350
    }
351

352
    Method* old_method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(mem_name);
353

354
    if (old_method->is_old()) {
355

356
      Method* new_method = (old_method->is_deleted()) ?
357
                            Universe::throw_no_such_method_error() :
358
                            old_method->get_new_method();
359
      java_lang_invoke_ResolvedMethodName::set_vmtarget(mem_name, new_method);
360

361
      ResourceMark rm;
362
      if (!(*_trace_name_printed)) {
363
        log_info(redefine, class, update)("adjust: name=%s", old_method->method_holder()->external_name());
364
         *_trace_name_printed = true;
365
      }
366
      log_debug(redefine, class, update, constantpool)
367
        ("ResolvedMethod method update: %s(%s)",
368
         new_method->name()->as_C_string(), new_method->signature()->as_C_string());
369
    }
370

371
    return true;
372
  }
373
};
374

375
// It is called at safepoint only for RedefineClasses
376
void ResolvedMethodTable::adjust_method_entries(bool * trace_name_printed) {
377
  assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
378
  // For each entry in RMT, change to new method
379
  AdjustMethodEntries adjust(trace_name_printed);
380
  _local_table->do_safepoint_scan(adjust);
381
}
382
#endif // INCLUDE_JVMTI
383

384
// Verification
385
class VerifyResolvedMethod : StackObj {
386
 public:
387
  bool operator()(WeakHandle* val) {
388
    oop obj = val->peek();
389
    if (obj != nullptr) {
390
      Method* method = (Method*)java_lang_invoke_ResolvedMethodName::vmtarget(obj);
391
      guarantee(method->is_method(), "Must be");
392
      guarantee(!method->is_old(), "Must be");
393
    }
394
    return true;
395
  };
396
};
397

398
size_t ResolvedMethodTable::items_count() {
399
  return _items_count;
400
}
401

402
void ResolvedMethodTable::verify() {
403
  VerifyResolvedMethod vcs;
404
  if (!_local_table->try_scan(Thread::current(), vcs)) {
405
    log_info(membername, table)("verify unavailable at this moment");
406
  }
407
}
408

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.