-
Notifications
You must be signed in to change notification settings - Fork 105
/
jvmtiTagMap.cpp
3408 lines (2919 loc) · 114 KB
/
jvmtiTagMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "classfile/javaClasses.inline.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/codeCache.hpp"
#include "jvmtifiles/jvmtiEnv.hpp"
#include "logging/log.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/access.inline.hpp"
#include "oops/arrayOop.inline.hpp"
#include "oops/constantPool.inline.hpp"
#include "oops/instanceMirrorKlass.hpp"
#include "oops/objArrayKlass.hpp"
#include "oops/objArrayOop.inline.hpp"
#include "oops/oop.inline.hpp"
#include "oops/typeArrayOop.inline.hpp"
#include "prims/jvmtiEventController.hpp"
#include "prims/jvmtiEventController.inline.hpp"
#include "prims/jvmtiExport.hpp"
#include "prims/jvmtiImpl.hpp"
#include "prims/jvmtiTagMap.hpp"
#include "runtime/biasedLocking.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/jniHandles.inline.hpp"
#include "runtime/mutex.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/reflectionUtils.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vmThread.hpp"
#include "runtime/vm_operations.hpp"
#include "utilities/macros.hpp"
#if INCLUDE_ZGC
#include "gc/z/zGlobals.hpp"
#endif
// JvmtiTagHashmapEntry
//
// Each entry encapsulates a reference to the tagged object
// and the tag value. In addition an entry includes a next pointer which
// is used to chain entries together.
class JvmtiTagHashmapEntry : public CHeapObj<mtInternal> {
private:
friend class JvmtiTagMap;
oop _object; // tagged object
jlong _tag; // the tag
JvmtiTagHashmapEntry* _next; // next on the list
inline void init(oop object, jlong tag) {
_object = object;
_tag = tag;
_next = NULL;
}
// constructor
JvmtiTagHashmapEntry(oop object, jlong tag) { init(object, tag); }
public:
// accessor methods
inline oop* object_addr() { return &_object; }
inline oop object() { return NativeAccess<ON_PHANTOM_OOP_REF>::oop_load(object_addr()); }
// Peek at the object without keeping it alive. The returned object must be
// kept alive using a normal access if it leaks out of a thread transition from VM.
inline oop object_peek() {
return NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE>::oop_load(object_addr());
}
inline jlong tag() const { return _tag; }
inline void set_tag(jlong tag) {
assert(tag != 0, "can't be zero");
_tag = tag;
}
inline bool equals(oop object) {
return object == object_peek();
}
inline JvmtiTagHashmapEntry* next() const { return _next; }
inline void set_next(JvmtiTagHashmapEntry* next) { _next = next; }
};
// JvmtiTagHashmap
//
// A hashmap is essentially a table of pointers to entries. Entries
// are hashed to a location, or position in the table, and then
// chained from that location. The "key" for hashing is address of
// the object, or oop. The "value" is the tag value.
//
// A hashmap maintains a count of the number entries in the hashmap
// and resizes if the number of entries exceeds a given threshold.
// The threshold is specified as a percentage of the size - for
// example a threshold of 0.75 will trigger the hashmap to resize
// if the number of entries is >75% of table size.
//
// A hashmap provides functions for adding, removing, and finding
// entries. It also provides a function to iterate over all entries
// in the hashmap.
class JvmtiTagHashmap : public CHeapObj<mtInternal> {
private:
friend class JvmtiTagMap;
enum {
small_trace_threshold = 10000, // threshold for tracing
medium_trace_threshold = 100000,
large_trace_threshold = 1000000,
initial_trace_threshold = small_trace_threshold
};
static int _sizes[]; // array of possible hashmap sizes
int _size; // actual size of the table
int _size_index; // index into size table
int _entry_count; // number of entries in the hashmap
float _load_factor; // load factor as a % of the size
int _resize_threshold; // computed threshold to trigger resizing.
bool _resizing_enabled; // indicates if hashmap can resize
int _trace_threshold; // threshold for trace messages
JvmtiTagHashmapEntry** _table; // the table of entries.
// private accessors
int resize_threshold() const { return _resize_threshold; }
int trace_threshold() const { return _trace_threshold; }
// initialize the hashmap
void init(int size_index=0, float load_factor=4.0f) {
int initial_size = _sizes[size_index];
_size_index = size_index;
_size = initial_size;
_entry_count = 0;
_trace_threshold = initial_trace_threshold;
_load_factor = load_factor;
_resize_threshold = (int)(_load_factor * _size);
_resizing_enabled = true;
size_t s = initial_size * sizeof(JvmtiTagHashmapEntry*);
_table = (JvmtiTagHashmapEntry**)os::malloc(s, mtInternal);
if (_table == NULL) {
vm_exit_out_of_memory(s, OOM_MALLOC_ERROR,
"unable to allocate initial hashtable for jvmti object tags");
}
for (int i=0; i<initial_size; i++) {
_table[i] = NULL;
}
}
// hash a given key (oop) with the specified size
static unsigned int hash(oop key, int size) {
ZGC_ONLY(assert(ZAddressMetadataShift >= sizeof(unsigned int) * BitsPerByte, "cast removes the metadata bits");)
// shift right to get better distribution (as these bits will be zero
// with aligned addresses)
unsigned int addr = (unsigned int)(cast_from_oop<intptr_t>(key));
#ifdef _LP64
return (addr >> 3) % size;
#else
return (addr >> 2) % size;
#endif
}
// hash a given key (oop)
unsigned int hash(oop key) {
return hash(key, _size);
}
// resize the hashmap - allocates a large table and re-hashes
// all entries into the new table.
void resize() {
int new_size_index = _size_index+1;
int new_size = _sizes[new_size_index];
if (new_size < 0) {
// hashmap already at maximum capacity
return;
}
// allocate new table
size_t s = new_size * sizeof(JvmtiTagHashmapEntry*);
JvmtiTagHashmapEntry** new_table = (JvmtiTagHashmapEntry**)os::malloc(s, mtInternal);
if (new_table == NULL) {
warning("unable to allocate larger hashtable for jvmti object tags");
set_resizing_enabled(false);
return;
}
// initialize new table
int i;
for (i=0; i<new_size; i++) {
new_table[i] = NULL;
}
// rehash all entries into the new table
for (i=0; i<_size; i++) {
JvmtiTagHashmapEntry* entry = _table[i];
while (entry != NULL) {
JvmtiTagHashmapEntry* next = entry->next();
oop key = entry->object_peek();
assert(key != NULL, "jni weak reference cleared!!");
unsigned int h = hash(key, new_size);
JvmtiTagHashmapEntry* anchor = new_table[h];
if (anchor == NULL) {
new_table[h] = entry;
entry->set_next(NULL);
} else {
entry->set_next(anchor);
new_table[h] = entry;
}
entry = next;
}
}
// free old table and update settings.
os::free((void*)_table);
_table = new_table;
_size_index = new_size_index;
_size = new_size;
// compute new resize threshold
_resize_threshold = (int)(_load_factor * _size);
}
// internal remove function - remove an entry at a given position in the
// table.
inline void remove(JvmtiTagHashmapEntry* prev, int pos, JvmtiTagHashmapEntry* entry) {
assert(pos >= 0 && pos < _size, "out of range");
if (prev == NULL) {
_table[pos] = entry->next();
} else {
prev->set_next(entry->next());
}
assert(_entry_count > 0, "checking");
_entry_count--;
}
// resizing switch
bool is_resizing_enabled() const { return _resizing_enabled; }
void set_resizing_enabled(bool enable) { _resizing_enabled = enable; }
// debugging
void print_memory_usage();
void compute_next_trace_threshold();
public:
// create a JvmtiTagHashmap of a preferred size and optionally a load factor.
// The preferred size is rounded down to an actual size.
JvmtiTagHashmap(int size, float load_factor=0.0f) {
int i=0;
while (_sizes[i] < size) {
if (_sizes[i] < 0) {
assert(i > 0, "sanity check");
i--;
break;
}
i++;
}
// if a load factor is specified then use it, otherwise use default
if (load_factor > 0.01f) {
init(i, load_factor);
} else {
init(i);
}
}
// create a JvmtiTagHashmap with default settings
JvmtiTagHashmap() {
init();
}
// release table when JvmtiTagHashmap destroyed
~JvmtiTagHashmap() {
if (_table != NULL) {
os::free((void*)_table);
_table = NULL;
}
}
// accessors
int size() const { return _size; }
JvmtiTagHashmapEntry** table() const { return _table; }
int entry_count() const { return _entry_count; }
// find an entry in the hashmap, returns NULL if not found.
inline JvmtiTagHashmapEntry* find(oop key) {
unsigned int h = hash(key);
JvmtiTagHashmapEntry* entry = _table[h];
while (entry != NULL) {
if (entry->equals(key)) {
return entry;
}
entry = entry->next();
}
return NULL;
}
// add a new entry to hashmap
inline void add(oop key, JvmtiTagHashmapEntry* entry) {
assert(key != NULL, "checking");
assert(find(key) == NULL, "duplicate detected");
unsigned int h = hash(key);
JvmtiTagHashmapEntry* anchor = _table[h];
if (anchor == NULL) {
_table[h] = entry;
entry->set_next(NULL);
} else {
entry->set_next(anchor);
_table[h] = entry;
}
_entry_count++;
if (log_is_enabled(Debug, jvmti, objecttagging) && entry_count() >= trace_threshold()) {
print_memory_usage();
compute_next_trace_threshold();
}
// if the number of entries exceed the threshold then resize
if (entry_count() > resize_threshold() && is_resizing_enabled()) {
resize();
}
}
// remove an entry with the given key.
inline JvmtiTagHashmapEntry* remove(oop key) {
unsigned int h = hash(key);
JvmtiTagHashmapEntry* entry = _table[h];
JvmtiTagHashmapEntry* prev = NULL;
while (entry != NULL) {
if (entry->equals(key)) {
break;
}
prev = entry;
entry = entry->next();
}
if (entry != NULL) {
remove(prev, h, entry);
}
return entry;
}
// iterate over all entries in the hashmap
void entry_iterate(JvmtiTagHashmapEntryClosure* closure);
};
// possible hashmap sizes - odd primes that roughly double in size.
// To avoid excessive resizing the odd primes from 4801-76831 and
// 76831-307261 have been removed. The list must be terminated by -1.
int JvmtiTagHashmap::_sizes[] = { 4801, 76831, 307261, 614563, 1228891,
2457733, 4915219, 9830479, 19660831, 39321619, 78643219, -1 };
// A supporting class for iterating over all entries in Hashmap
class JvmtiTagHashmapEntryClosure {
public:
virtual void do_entry(JvmtiTagHashmapEntry* entry) = 0;
};
// iterate over all entries in the hashmap
void JvmtiTagHashmap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) {
for (int i=0; i<_size; i++) {
JvmtiTagHashmapEntry* entry = _table[i];
JvmtiTagHashmapEntry* prev = NULL;
while (entry != NULL) {
// obtain the next entry before invoking do_entry - this is
// necessary because do_entry may remove the entry from the
// hashmap.
JvmtiTagHashmapEntry* next = entry->next();
closure->do_entry(entry);
entry = next;
}
}
}
// debugging
void JvmtiTagHashmap::print_memory_usage() {
intptr_t p = (intptr_t)this;
tty->print("[JvmtiTagHashmap @ " INTPTR_FORMAT, p);
// table + entries in KB
int hashmap_usage = (size()*sizeof(JvmtiTagHashmapEntry*) +
entry_count()*sizeof(JvmtiTagHashmapEntry))/K;
int weak_globals_usage = (int)(JNIHandles::weak_global_handle_memory_usage()/K);
tty->print_cr(", %d entries (%d KB) <JNI weak globals: %d KB>]",
entry_count(), hashmap_usage, weak_globals_usage);
}
// compute threshold for the next trace message
void JvmtiTagHashmap::compute_next_trace_threshold() {
_trace_threshold = entry_count();
if (trace_threshold() < medium_trace_threshold) {
_trace_threshold += small_trace_threshold;
} else {
if (trace_threshold() < large_trace_threshold) {
_trace_threshold += medium_trace_threshold;
} else {
_trace_threshold += large_trace_threshold;
}
}
}
// create a JvmtiTagMap
JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) :
_env(env),
_lock(Mutex::nonleaf+2, "JvmtiTagMap._lock", false),
_free_entries(NULL),
_free_entries_count(0)
{
assert(JvmtiThreadState_lock->is_locked(), "sanity check");
assert(((JvmtiEnvBase *)env)->tag_map() == NULL, "tag map already exists for environment");
_hashmap = new JvmtiTagHashmap();
// finally add us to the environment
((JvmtiEnvBase *)env)->release_set_tag_map(this);
}
// destroy a JvmtiTagMap
JvmtiTagMap::~JvmtiTagMap() {
// no lock acquired as we assume the enclosing environment is
// also being destroryed.
((JvmtiEnvBase *)_env)->set_tag_map(NULL);
JvmtiTagHashmapEntry** table = _hashmap->table();
for (int j = 0; j < _hashmap->size(); j++) {
JvmtiTagHashmapEntry* entry = table[j];
while (entry != NULL) {
JvmtiTagHashmapEntry* next = entry->next();
delete entry;
entry = next;
}
}
// finally destroy the hashmap
delete _hashmap;
_hashmap = NULL;
// remove any entries on the free list
JvmtiTagHashmapEntry* entry = _free_entries;
while (entry != NULL) {
JvmtiTagHashmapEntry* next = entry->next();
delete entry;
entry = next;
}
_free_entries = NULL;
}
// create a hashmap entry
// - if there's an entry on the (per-environment) free list then this
// is returned. Otherwise an new entry is allocated.
JvmtiTagHashmapEntry* JvmtiTagMap::create_entry(oop ref, jlong tag) {
assert(Thread::current()->is_VM_thread() || is_locked(), "checking");
JvmtiTagHashmapEntry* entry;
if (_free_entries == NULL) {
entry = new JvmtiTagHashmapEntry(ref, tag);
} else {
assert(_free_entries_count > 0, "mismatched _free_entries_count");
_free_entries_count--;
entry = _free_entries;
_free_entries = entry->next();
entry->init(ref, tag);
}
return entry;
}
// destroy an entry by returning it to the free list
void JvmtiTagMap::destroy_entry(JvmtiTagHashmapEntry* entry) {
assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking");
// limit the size of the free list
if (_free_entries_count >= max_free_entries) {
delete entry;
} else {
entry->set_next(_free_entries);
_free_entries = entry;
_free_entries_count++;
}
}
// returns the tag map for the given environments. If the tag map
// doesn't exist then it is created.
JvmtiTagMap* JvmtiTagMap::tag_map_for(JvmtiEnv* env) {
JvmtiTagMap* tag_map = ((JvmtiEnvBase*)env)->tag_map_acquire();
if (tag_map == NULL) {
MutexLocker mu(JvmtiThreadState_lock);
tag_map = ((JvmtiEnvBase*)env)->tag_map();
if (tag_map == NULL) {
tag_map = new JvmtiTagMap(env);
}
} else {
CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
}
return tag_map;
}
// iterate over all entries in the tag map.
void JvmtiTagMap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) {
hashmap()->entry_iterate(closure);
}
// returns true if the hashmaps are empty
bool JvmtiTagMap::is_empty() {
assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking");
return hashmap()->entry_count() == 0;
}
// Return the tag value for an object, or 0 if the object is
// not tagged
//
static inline jlong tag_for(JvmtiTagMap* tag_map, oop o) {
JvmtiTagHashmapEntry* entry = tag_map->hashmap()->find(o);
if (entry == NULL) {
return 0;
} else {
return entry->tag();
}
}
// A CallbackWrapper is a support class for querying and tagging an object
// around a callback to a profiler. The constructor does pre-callback
// work to get the tag value, klass tag value, ... and the destructor
// does the post-callback work of tagging or untagging the object.
//
// {
// CallbackWrapper wrapper(tag_map, o);
//
// (*callback)(wrapper.klass_tag(), wrapper.obj_size(), wrapper.obj_tag_p(), ...)
//
// } // wrapper goes out of scope here which results in the destructor
// checking to see if the object has been tagged, untagged, or the
// tag value has changed.
//
class CallbackWrapper : public StackObj {
private:
JvmtiTagMap* _tag_map;
JvmtiTagHashmap* _hashmap;
JvmtiTagHashmapEntry* _entry;
oop _o;
jlong _obj_size;
jlong _obj_tag;
jlong _klass_tag;
protected:
JvmtiTagMap* tag_map() const { return _tag_map; }
// invoked post-callback to tag, untag, or update the tag of an object
void inline post_callback_tag_update(oop o, JvmtiTagHashmap* hashmap,
JvmtiTagHashmapEntry* entry, jlong obj_tag);
public:
CallbackWrapper(JvmtiTagMap* tag_map, oop o) {
assert(Thread::current()->is_VM_thread() || tag_map->is_locked(),
"MT unsafe or must be VM thread");
// object to tag
_o = o;
// object size
_obj_size = (jlong)_o->size() * wordSize;
// record the context
_tag_map = tag_map;
_hashmap = tag_map->hashmap();
_entry = _hashmap->find(_o);
// get object tag
_obj_tag = (_entry == NULL) ? 0 : _entry->tag();
// get the class and the class's tag value
assert(SystemDictionary::Class_klass()->is_mirror_instance_klass(), "Is not?");
_klass_tag = tag_for(tag_map, _o->klass()->java_mirror());
}
~CallbackWrapper() {
post_callback_tag_update(_o, _hashmap, _entry, _obj_tag);
}
inline jlong* obj_tag_p() { return &_obj_tag; }
inline jlong obj_size() const { return _obj_size; }
inline jlong obj_tag() const { return _obj_tag; }
inline jlong klass_tag() const { return _klass_tag; }
};
// callback post-callback to tag, untag, or update the tag of an object
void inline CallbackWrapper::post_callback_tag_update(oop o,
JvmtiTagHashmap* hashmap,
JvmtiTagHashmapEntry* entry,
jlong obj_tag) {
if (entry == NULL) {
if (obj_tag != 0) {
// callback has tagged the object
assert(Thread::current()->is_VM_thread(), "must be VMThread");
entry = tag_map()->create_entry(o, obj_tag);
hashmap->add(o, entry);
}
} else {
// object was previously tagged - the callback may have untagged
// the object or changed the tag value
if (obj_tag == 0) {
JvmtiTagHashmapEntry* entry_removed = hashmap->remove(o);
assert(entry_removed == entry, "checking");
tag_map()->destroy_entry(entry);
} else {
if (obj_tag != entry->tag()) {
entry->set_tag(obj_tag);
}
}
}
}
// An extended CallbackWrapper used when reporting an object reference
// to the agent.
//
// {
// TwoOopCallbackWrapper wrapper(tag_map, referrer, o);
//
// (*callback)(wrapper.klass_tag(),
// wrapper.obj_size(),
// wrapper.obj_tag_p()
// wrapper.referrer_tag_p(), ...)
//
// } // wrapper goes out of scope here which results in the destructor
// checking to see if the referrer object has been tagged, untagged,
// or the tag value has changed.
//
class TwoOopCallbackWrapper : public CallbackWrapper {
private:
bool _is_reference_to_self;
JvmtiTagHashmap* _referrer_hashmap;
JvmtiTagHashmapEntry* _referrer_entry;
oop _referrer;
jlong _referrer_obj_tag;
jlong _referrer_klass_tag;
jlong* _referrer_tag_p;
bool is_reference_to_self() const { return _is_reference_to_self; }
public:
TwoOopCallbackWrapper(JvmtiTagMap* tag_map, oop referrer, oop o) :
CallbackWrapper(tag_map, o)
{
// self reference needs to be handled in a special way
_is_reference_to_self = (referrer == o);
if (_is_reference_to_self) {
_referrer_klass_tag = klass_tag();
_referrer_tag_p = obj_tag_p();
} else {
_referrer = referrer;
// record the context
_referrer_hashmap = tag_map->hashmap();
_referrer_entry = _referrer_hashmap->find(_referrer);
// get object tag
_referrer_obj_tag = (_referrer_entry == NULL) ? 0 : _referrer_entry->tag();
_referrer_tag_p = &_referrer_obj_tag;
// get referrer class tag.
_referrer_klass_tag = tag_for(tag_map, _referrer->klass()->java_mirror());
}
}
~TwoOopCallbackWrapper() {
if (!is_reference_to_self()){
post_callback_tag_update(_referrer,
_referrer_hashmap,
_referrer_entry,
_referrer_obj_tag);
}
}
// address of referrer tag
// (for a self reference this will return the same thing as obj_tag_p())
inline jlong* referrer_tag_p() { return _referrer_tag_p; }
// referrer's class tag
inline jlong referrer_klass_tag() { return _referrer_klass_tag; }
};
// tag an object
//
// This function is performance critical. If many threads attempt to tag objects
// around the same time then it's possible that the Mutex associated with the
// tag map will be a hot lock.
void JvmtiTagMap::set_tag(jobject object, jlong tag) {
MutexLocker ml(lock());
// resolve the object
oop o = JNIHandles::resolve_non_null(object);
// see if the object is already tagged
JvmtiTagHashmap* hashmap = _hashmap;
JvmtiTagHashmapEntry* entry = hashmap->find(o);
// if the object is not already tagged then we tag it
if (entry == NULL) {
if (tag != 0) {
entry = create_entry(o, tag);
hashmap->add(o, entry);
} else {
// no-op
}
} else {
// if the object is already tagged then we either update
// the tag (if a new tag value has been provided)
// or remove the object if the new tag value is 0.
if (tag == 0) {
hashmap->remove(o);
destroy_entry(entry);
} else {
entry->set_tag(tag);
}
}
}
// get the tag for an object
jlong JvmtiTagMap::get_tag(jobject object) {
MutexLocker ml(lock());
// resolve the object
oop o = JNIHandles::resolve_non_null(object);
return tag_for(this, o);
}
// Helper class used to describe the static or instance fields of a class.
// For each field it holds the field index (as defined by the JVMTI specification),
// the field type, and the offset.
class ClassFieldDescriptor: public CHeapObj<mtInternal> {
private:
int _field_index;
int _field_offset;
char _field_type;
public:
ClassFieldDescriptor(int index, char type, int offset) :
_field_index(index), _field_type(type), _field_offset(offset) {
}
int field_index() const { return _field_index; }
char field_type() const { return _field_type; }
int field_offset() const { return _field_offset; }
};
class ClassFieldMap: public CHeapObj<mtInternal> {
private:
enum {
initial_field_count = 5
};
// list of field descriptors
GrowableArray<ClassFieldDescriptor*>* _fields;
// constructor
ClassFieldMap();
// add a field
void add(int index, char type, int offset);
// returns the field count for the given class
static int compute_field_count(InstanceKlass* ik);
public:
~ClassFieldMap();
// access
int field_count() { return _fields->length(); }
ClassFieldDescriptor* field_at(int i) { return _fields->at(i); }
// functions to create maps of static or instance fields
static ClassFieldMap* create_map_of_static_fields(Klass* k);
static ClassFieldMap* create_map_of_instance_fields(oop obj);
};
ClassFieldMap::ClassFieldMap() {
_fields = new (ResourceObj::C_HEAP, mtInternal)
GrowableArray<ClassFieldDescriptor*>(initial_field_count, true);
}
ClassFieldMap::~ClassFieldMap() {
for (int i=0; i<_fields->length(); i++) {
delete _fields->at(i);
}
delete _fields;
}
void ClassFieldMap::add(int index, char type, int offset) {
ClassFieldDescriptor* field = new ClassFieldDescriptor(index, type, offset);
_fields->append(field);
}
// Returns a heap allocated ClassFieldMap to describe the static fields
// of the given class.
//
ClassFieldMap* ClassFieldMap::create_map_of_static_fields(Klass* k) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(k);
// create the field map
ClassFieldMap* field_map = new ClassFieldMap();
FilteredFieldStream f(ik, false, false);
int max_field_index = f.field_count()-1;
int index = 0;
for (FilteredFieldStream fld(ik, true, true); !fld.eos(); fld.next(), index++) {
// ignore instance fields
if (!fld.access_flags().is_static()) {
continue;
}
field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset());
}
return field_map;
}
// Returns a heap allocated ClassFieldMap to describe the instance fields
// of the given class. All instance fields are included (this means public
// and private fields declared in superclasses and superinterfaces too).
//
ClassFieldMap* ClassFieldMap::create_map_of_instance_fields(oop obj) {
HandleMark hm;
InstanceKlass* ik = InstanceKlass::cast(obj->klass());
// create the field map
ClassFieldMap* field_map = new ClassFieldMap();
FilteredFieldStream f(ik, false, false);
int max_field_index = f.field_count()-1;
int index = 0;
for (FilteredFieldStream fld(ik, false, false); !fld.eos(); fld.next(), index++) {
// ignore static fields
if (fld.access_flags().is_static()) {
continue;
}
field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset());
}
return field_map;
}
// Helper class used to cache a ClassFileMap for the instance fields of
// a cache. A JvmtiCachedClassFieldMap can be cached by an InstanceKlass during
// heap iteration and avoid creating a field map for each object in the heap
// (only need to create the map when the first instance of a class is encountered).
//
class JvmtiCachedClassFieldMap : public CHeapObj<mtInternal> {
private:
enum {
initial_class_count = 200
};
ClassFieldMap* _field_map;
ClassFieldMap* field_map() const { return _field_map; }
JvmtiCachedClassFieldMap(ClassFieldMap* field_map);
~JvmtiCachedClassFieldMap();
static GrowableArray<InstanceKlass*>* _class_list;
static void add_to_class_list(InstanceKlass* ik);
public:
// returns the field map for a given object (returning map cached
// by InstanceKlass if possible
static ClassFieldMap* get_map_of_instance_fields(oop obj);
// removes the field map from all instanceKlasses - should be
// called before VM operation completes
static void clear_cache();
// returns the number of ClassFieldMap cached by instanceKlasses
static int cached_field_map_count();
};
GrowableArray<InstanceKlass*>* JvmtiCachedClassFieldMap::_class_list;
JvmtiCachedClassFieldMap::JvmtiCachedClassFieldMap(ClassFieldMap* field_map) {
_field_map = field_map;
}
JvmtiCachedClassFieldMap::~JvmtiCachedClassFieldMap() {
if (_field_map != NULL) {
delete _field_map;
}
}
// Marker class to ensure that the class file map cache is only used in a defined
// scope.
class ClassFieldMapCacheMark : public StackObj {
private:
static bool _is_active;
public:
ClassFieldMapCacheMark() {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
assert(JvmtiCachedClassFieldMap::cached_field_map_count() == 0, "cache not empty");
assert(!_is_active, "ClassFieldMapCacheMark cannot be nested");
_is_active = true;
}
~ClassFieldMapCacheMark() {
JvmtiCachedClassFieldMap::clear_cache();
_is_active = false;
}
static bool is_active() { return _is_active; }
};
bool ClassFieldMapCacheMark::_is_active;
// record that the given InstanceKlass is caching a field map
void JvmtiCachedClassFieldMap::add_to_class_list(InstanceKlass* ik) {
if (_class_list == NULL) {
_class_list = new (ResourceObj::C_HEAP, mtInternal)
GrowableArray<InstanceKlass*>(initial_class_count, true);
}
_class_list->push(ik);
}
// returns the instance field map for the given object
// (returns field map cached by the InstanceKlass if possible)
ClassFieldMap* JvmtiCachedClassFieldMap::get_map_of_instance_fields(oop obj) {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
assert(ClassFieldMapCacheMark::is_active(), "ClassFieldMapCacheMark not active");
Klass* k = obj->klass();
InstanceKlass* ik = InstanceKlass::cast(k);
// return cached map if possible
JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
if (cached_map != NULL) {
assert(cached_map->field_map() != NULL, "missing field list");
return cached_map->field_map();
} else {
ClassFieldMap* field_map = ClassFieldMap::create_map_of_instance_fields(obj);
cached_map = new JvmtiCachedClassFieldMap(field_map);
ik->set_jvmti_cached_class_field_map(cached_map);
add_to_class_list(ik);
return field_map;
}
}
// remove the fields maps cached from all instanceKlasses
void JvmtiCachedClassFieldMap::clear_cache() {
assert(Thread::current()->is_VM_thread(), "must be VMThread");
if (_class_list != NULL) {
for (int i = 0; i < _class_list->length(); i++) {
InstanceKlass* ik = _class_list->at(i);
JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
assert(cached_map != NULL, "should not be NULL");
ik->set_jvmti_cached_class_field_map(NULL);
delete cached_map; // deletes the encapsulated field map
}
delete _class_list;
_class_list = NULL;
}
}