21 * questions. |
21 * questions. |
22 * |
22 * |
23 */ |
23 */ |
24 |
24 |
25 #include "precompiled.hpp" |
25 #include "precompiled.hpp" |
26 #include "jfr/leakprofiler/emitEventOperation.hpp" |
|
27 #include "jfr/leakprofiler/leakProfiler.hpp" |
26 #include "jfr/leakprofiler/leakProfiler.hpp" |
28 #include "jfr/leakprofiler/startOperation.hpp" |
27 #include "jfr/leakprofiler/startOperation.hpp" |
29 #include "jfr/leakprofiler/stopOperation.hpp" |
28 #include "jfr/leakprofiler/stopOperation.hpp" |
|
29 #include "jfr/leakprofiler/checkpoint/eventEmitter.hpp" |
30 #include "jfr/leakprofiler/sampling/objectSampler.hpp" |
30 #include "jfr/leakprofiler/sampling/objectSampler.hpp" |
31 #include "jfr/recorder/service/jfrOptionSet.hpp" |
31 #include "jfr/recorder/service/jfrOptionSet.hpp" |
32 #include "memory/iterator.hpp" |
32 #include "memory/iterator.hpp" |
33 #include "oops/oop.hpp" |
|
34 #include "runtime/atomic.hpp" |
|
35 #include "runtime/orderAccess.hpp" |
|
36 #include "runtime/thread.inline.hpp" |
33 #include "runtime/thread.inline.hpp" |
37 #include "runtime/vmThread.hpp" |
34 #include "runtime/vmThread.hpp" |
38 #include "utilities/ostream.hpp" |
|
39 |
35 |
40 // Only to be updated during safepoint |
36 bool LeakProfiler::is_running() { |
41 ObjectSampler* LeakProfiler::_object_sampler = NULL; |
37 return ObjectSampler::is_created(); |
|
38 } |
42 |
39 |
43 static volatile jbyte suspended = 0; |
40 bool LeakProfiler::start(int sample_count) { |
44 bool LeakProfiler::start(jint sample_count) { |
41 if (is_running()) { |
45 if (_object_sampler != NULL) { |
|
46 // already started |
|
47 return true; |
42 return true; |
48 } |
43 } |
|
44 |
49 // Allows user to disable leak profiler on command line by setting queue size to zero. |
45 // Allows user to disable leak profiler on command line by setting queue size to zero. |
50 if (sample_count > 0) { |
46 if (sample_count == 0) { |
51 StartOperation op(sample_count); |
47 return false; |
52 VMThread::execute(&op); |
|
53 return _object_sampler != NULL; |
|
54 } |
48 } |
55 return false; |
49 |
|
50 assert(!is_running(), "invariant"); |
|
51 assert(sample_count > 0, "invariant"); |
|
52 |
|
53 // schedule the safepoint operation for installing the object sampler |
|
54 StartOperation op(sample_count); |
|
55 VMThread::execute(&op); |
|
56 |
|
57 if (!is_running()) { |
|
58 if (LogJFR && Verbose) tty->print_cr("Object sampling could not be started because the sampler could not be allocated"); |
|
59 return false; |
|
60 } |
|
61 assert(is_running(), "invariant"); |
|
62 if (LogJFR && Verbose) tty->print_cr("Object sampling started"); |
|
63 return true; |
56 } |
64 } |
57 |
65 |
58 bool LeakProfiler::stop() { |
66 bool LeakProfiler::stop() { |
59 if (_object_sampler == NULL) { |
67 if (!is_running()) { |
60 // already stopped/not started |
68 return false; |
61 return true; |
|
62 } |
69 } |
|
70 |
|
71 // schedule the safepoint operation for uninstalling and destroying the object sampler |
63 StopOperation op; |
72 StopOperation op; |
64 VMThread::execute(&op); |
73 VMThread::execute(&op); |
65 return _object_sampler == NULL; |
74 |
|
75 assert(!is_running(), "invariant"); |
|
76 if (LogJFR && Verbose) tty->print_cr("Object sampling stopped"); |
|
77 return true; |
66 } |
78 } |
67 |
79 |
68 void LeakProfiler::emit_events(jlong cutoff_ticks, bool emit_all) { |
80 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all) { |
69 if (!is_running()) { |
81 if (!is_running()) { |
70 return; |
82 return; |
71 } |
83 } |
72 EmitEventOperation op(cutoff_ticks, emit_all); |
84 // exclusive access to object sampler instance |
73 VMThread::execute(&op); |
85 ObjectSampler* const sampler = ObjectSampler::acquire(); |
|
86 assert(sampler != NULL, "invariant"); |
|
87 EventEmitter::emit(sampler, cutoff_ticks, emit_all); |
|
88 ObjectSampler::release(); |
74 } |
89 } |
75 |
90 |
76 void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
91 void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
77 assert(SafepointSynchronize::is_at_safepoint(), |
92 assert(SafepointSynchronize::is_at_safepoint(), |
78 "Leak Profiler::oops_do(...) may only be called during safepoint"); |
93 "Leak Profiler::oops_do(...) may only be called during safepoint"); |
79 |
94 if (is_running()) { |
80 if (_object_sampler != NULL) { |
95 ObjectSampler::oops_do(is_alive, f); |
81 _object_sampler->oops_do(is_alive, f); |
|
82 } |
96 } |
83 } |
97 } |
84 |
98 |
85 void LeakProfiler::sample(HeapWord* object, |
99 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) { |
86 size_t size, |
|
87 JavaThread* thread) { |
|
88 assert(is_running(), "invariant"); |
100 assert(is_running(), "invariant"); |
89 assert(thread != NULL, "invariant"); |
101 assert(thread != NULL, "invariant"); |
90 assert(thread->thread_state() == _thread_in_vm, "invariant"); |
102 assert(thread->thread_state() == _thread_in_vm, "invariant"); |
91 |
103 |
92 // exclude compiler threads and code sweeper thread |
104 // exclude compiler threads and code sweeper thread |
93 if (thread->is_hidden_from_external_view()) { |
105 if (thread->is_hidden_from_external_view()) { |
94 return; |
106 return; |
95 } |
107 } |
96 |
108 |
97 _object_sampler->add(object, size, thread); |
109 ObjectSampler::sample(object, size, thread); |
98 } |
110 } |
99 |
|
100 ObjectSampler* LeakProfiler::object_sampler() { |
|
101 assert(is_suspended() || SafepointSynchronize::is_at_safepoint(), |
|
102 "Leak Profiler::object_sampler() may only be called during safepoint"); |
|
103 return _object_sampler; |
|
104 } |
|
105 |
|
106 void LeakProfiler::set_object_sampler(ObjectSampler* object_sampler) { |
|
107 assert(SafepointSynchronize::is_at_safepoint(), |
|
108 "Leak Profiler::set_object_sampler() may only be called during safepoint"); |
|
109 _object_sampler = object_sampler; |
|
110 } |
|
111 |
|
112 bool LeakProfiler::is_running() { |
|
113 return _object_sampler != NULL && !suspended; |
|
114 } |
|
115 |
|
116 bool LeakProfiler::is_suspended() { |
|
117 return _object_sampler != NULL && suspended; |
|
118 } |
|
119 |
|
120 void LeakProfiler::resume() { |
|
121 assert(is_suspended(), "invariant"); |
|
122 OrderAccess::storestore(); |
|
123 Atomic::store((jbyte)0, &suspended); |
|
124 assert(is_running(), "invariant"); |
|
125 } |
|
126 |
|
127 void LeakProfiler::suspend() { |
|
128 assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
|
129 assert(_object_sampler != NULL, "invariant"); |
|
130 assert(!is_suspended(), "invariant"); |
|
131 suspended = (jbyte)1; // safepoint visible |
|
132 } |
|