1 /*
  2  * Copyright (c) 2016, 2021, 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/javaClasses.inline.hpp"
 27 #include "code/codeBlob.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "compiler/compilerDefinitions.hpp"
 30 #include "gc/shared/gcCause.hpp"
 31 #include "gc/shared/gcName.hpp"
 32 #include "gc/shared/gcTrace.hpp"
 33 #include "gc/shared/gcWhen.hpp"
 34 #include "jfr/leakprofiler/leakProfiler.hpp"
 35 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
 36 #include "jfr/recorder/checkpoint/types/jfrType.hpp"
 37 #include "jfr/recorder/jfrRecorder.hpp"
 38 #include "jfr/recorder/checkpoint/types/jfrThreadGroup.hpp"
 39 #include "jfr/recorder/checkpoint/types/jfrThreadState.hpp"
 40 #include "jfr/support/jfrThreadLocal.hpp"
 41 #include "jfr/writers/jfrJavaEventWriter.hpp"
 42 #include "jfr/utilities/jfrThreadIterator.hpp"
 43 #include "memory/iterator.hpp"
 44 #include "memory/metaspace.hpp"
 45 #include "memory/metaspaceUtils.hpp"
 46 #include "memory/referenceType.hpp"
 47 #include "memory/universe.hpp"
 48 #include "oops/compressedOops.hpp"
 49 #include "runtime/flags/jvmFlag.hpp"
 50 #include "runtime/mutexLocker.hpp"
 51 #include "runtime/osThread.hpp"
 52 #include "runtime/safepoint.hpp"
 53 #include "runtime/synchronizer.hpp"
 54 #include "runtime/thread.inline.hpp"
 55 #include "runtime/vmOperations.hpp"
 56 
 57 #ifdef COMPILER2
 58 #include "opto/compile.hpp"
 59 #include "opto/node.hpp"
 60 #endif
 61 
 62 // Requires a ResourceMark for get_thread_name/as_utf8
 63 class JfrCheckpointThreadClosure : public ThreadClosure {
 64  private:
 65   JfrCheckpointWriter& _writer;
 66   JfrCheckpointContext _ctx;
 67   const int64_t _count_position;
 68   Thread* const _curthread;
 69   u4 _count;
 70 
 71  public:
 72   JfrCheckpointThreadClosure(JfrCheckpointWriter& writer) : _writer(writer),
 73                                                             _ctx(writer.context()),
 74                                                             _count_position(writer.reserve(sizeof(u4))),
 75                                                             _curthread(Thread::current()),
 76                                                             _count(0) {
 77   }
 78 
 79   ~JfrCheckpointThreadClosure() {
 80     if (_count == 0) {
 81       // restore
 82       _writer.set_context(_ctx);
 83       return;
 84     }
 85     _writer.write_count(_count, _count_position);
 86   }
 87 
 88   void do_thread(Thread* t);
 89 };
 90 
 91 void JfrCheckpointThreadClosure::do_thread(Thread* t) {
 92   assert(t != NULL, "invariant");
 93   ++_count;
 94   _writer.write_key(JfrThreadId::jfr_id(t));
 95   const char* const name = JfrThreadName::name(t);
 96   assert(name != NULL, "invariant");
 97   _writer.write(name);
 98   _writer.write<traceid>(JfrThreadId::os_id(t));
 99   if (t->is_Java_thread()) {
100     _writer.write(name);
101     _writer.write(JfrThreadId::id(t));
102     _writer.write(JfrThreadGroup::thread_group_id(JavaThread::cast(t), _curthread));
103     return;
104   }
105   _writer.write((const char*)NULL); // java name
106   _writer.write((traceid)0); // java thread id
107   _writer.write((traceid)0); // java thread group
108 }
109 
110 void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) {
111   JfrCheckpointThreadClosure tc(writer);
112   JfrJavaThreadIterator javathreads(false); // include not yet live threads (_thread_new)
113   while (javathreads.has_next()) {
114     tc.do_thread(javathreads.next());
115   }
116   JfrNonJavaThreadIterator nonjavathreads;
117   while (nonjavathreads.has_next()) {
118     tc.do_thread(nonjavathreads.next());
119   }
120 }
121 
122 void JfrThreadGroupConstant::serialize(JfrCheckpointWriter& writer) {
123   JfrThreadGroup::serialize(writer);
124 }
125 
126 static const char* flag_value_origin_to_string(JVMFlagOrigin origin) {
127   switch (origin) {
128     case JVMFlagOrigin::DEFAULT: return "Default";
129     case JVMFlagOrigin::COMMAND_LINE: return "Command line";
130     case JVMFlagOrigin::ENVIRON_VAR: return "Environment variable";
131     case JVMFlagOrigin::CONFIG_FILE: return "Config file";
132     case JVMFlagOrigin::MANAGEMENT: return "Management";
133     case JVMFlagOrigin::ERGONOMIC: return "Ergonomic";
134     case JVMFlagOrigin::ATTACH_ON_DEMAND: return "Attach on demand";
135     case JVMFlagOrigin::INTERNAL: return "Internal";
136     case JVMFlagOrigin::JIMAGE_RESOURCE: return "JImage resource";
137     default: ShouldNotReachHere(); return "";
138   }
139 }
140 
141 void FlagValueOriginConstant::serialize(JfrCheckpointWriter& writer) {
142   constexpr EnumRange<JVMFlagOrigin> range{};
143   writer.write_count(static_cast<u4>(range.size()));
144 
145   for (JVMFlagOrigin origin : range) {
146     writer.write_key(static_cast<u4>(origin));
147     writer.write(flag_value_origin_to_string(origin));
148   }
149 }
150 
151 void MonitorInflateCauseConstant::serialize(JfrCheckpointWriter& writer) {
152   static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof;
153   writer.write_count(nof_entries);
154   for (u4 i = 0; i < nof_entries; ++i) {
155     writer.write_key(i);
156     writer.write(ObjectSynchronizer::inflate_cause_name((ObjectSynchronizer::InflateCause)i));
157   }
158 }
159 
160 void GCCauseConstant::serialize(JfrCheckpointWriter& writer) {
161   static const u4 nof_entries = GCCause::_last_gc_cause;
162   writer.write_count(nof_entries);
163   for (u4 i = 0; i < nof_entries; ++i) {
164     writer.write_key(i);
165     writer.write(GCCause::to_string((GCCause::Cause)i));
166   }
167 }
168 
169 void GCNameConstant::serialize(JfrCheckpointWriter& writer) {
170   static const u4 nof_entries = GCNameEndSentinel;
171   writer.write_count(nof_entries);
172   for (u4 i = 0; i < nof_entries; ++i) {
173     writer.write_key(i);
174     writer.write(GCNameHelper::to_string((GCName)i));
175   }
176 }
177 
178 void GCWhenConstant::serialize(JfrCheckpointWriter& writer) {
179   static const u4 nof_entries = GCWhen::GCWhenEndSentinel;
180   writer.write_count(nof_entries);
181   for (u4 i = 0; i < nof_entries; ++i) {
182     writer.write_key(i);
183     writer.write(GCWhen::to_string((GCWhen::Type)i));
184   }
185 }
186 
187 void GCThresholdUpdaterConstant::serialize(JfrCheckpointWriter& writer) {
188   static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last;
189   writer.write_count(nof_entries);
190   for (u4 i = 0; i < nof_entries; ++i) {
191     writer.write_key(i);
192     writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i));
193   }
194 }
195 
196 void MetadataTypeConstant::serialize(JfrCheckpointWriter& writer) {
197   static const u4 nof_entries = Metaspace::MetadataTypeCount;
198   writer.write_count(nof_entries);
199   for (u4 i = 0; i < nof_entries; ++i) {
200     writer.write_key(i);
201     writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i));
202   }
203 }
204 
205 void MetaspaceObjectTypeConstant::serialize(JfrCheckpointWriter& writer) {
206   static const u4 nof_entries = MetaspaceObj::_number_of_types;
207   writer.write_count(nof_entries);
208   for (u4 i = 0; i < nof_entries; ++i) {
209     writer.write_key(i);
210     writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i));
211   }
212 }
213 
214 static const char* reference_type_to_string(ReferenceType rt) {
215   switch (rt) {
216     case REF_NONE: return "None reference";
217     case REF_OTHER: return "Other reference";
218     case REF_SOFT: return "Soft reference";
219     case REF_WEAK: return "Weak reference";
220     case REF_FINAL: return "Final reference";
221     case REF_PHANTOM: return "Phantom reference";
222     default:
223       ShouldNotReachHere();
224     return NULL;
225   }
226 }
227 
228 void ReferenceTypeConstant::serialize(JfrCheckpointWriter& writer) {
229   static const u4 nof_entries = REF_PHANTOM + 1;
230   writer.write_count(nof_entries);
231   for (u4 i = 0; i < nof_entries; ++i) {
232     writer.write_key(i);
233     writer.write(reference_type_to_string((ReferenceType)i));
234   }
235 }
236 
237 void NarrowOopModeConstant::serialize(JfrCheckpointWriter& writer) {
238   static const u4 nof_entries = CompressedOops::HeapBasedNarrowOop + 1;
239   writer.write_count(nof_entries);
240   for (u4 i = 0; i < nof_entries; ++i) {
241     writer.write_key(i);
242     writer.write(CompressedOops::mode_to_string((CompressedOops::Mode)i));
243   }
244 }
245 
246 void CodeBlobTypeConstant::serialize(JfrCheckpointWriter& writer) {
247   static const u4 nof_entries = CodeBlobType::NumTypes;
248   writer.write_count(nof_entries);
249   for (u4 i = 0; i < nof_entries; ++i) {
250     writer.write_key(i);
251     writer.write(CodeCache::get_code_heap_name(i));
252   }
253 };
254 
255 void VMOperationTypeConstant::serialize(JfrCheckpointWriter& writer) {
256   static const u4 nof_entries = VM_Operation::VMOp_Terminating;
257   writer.write_count(nof_entries);
258   for (u4 i = 0; i < nof_entries; ++i) {
259     writer.write_key(i);
260     writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i)));
261   }
262 }
263 
264 void ThreadStateConstant::serialize(JfrCheckpointWriter& writer) {
265   JfrThreadState::serialize(writer);
266 }
267 
268 void JfrThreadConstant::serialize(JfrCheckpointWriter& writer) {
269   assert(_thread != NULL, "invariant");
270   assert(_thread == Thread::current(), "invariant");
271   writer.write_count(1);
272   writer.write_key(JfrThreadId::jfr_id(_thread));
273   const char* const name = JfrThreadName::name(_thread);
274   writer.write(name);
275   writer.write(JfrThreadId::os_id(_thread));
276   if (_thread->is_Java_thread()) {
277     writer.write(name);
278     writer.write(JfrThreadId::id(_thread));
279     JavaThread* const jt = JavaThread::cast(_thread);
280     const traceid thread_group_id = JfrThreadGroup::thread_group_id(jt, jt);
281     writer.write(thread_group_id);
282     JfrThreadGroup::serialize(&writer, thread_group_id);
283     return;
284   }
285   writer.write((const char*)NULL); // java name
286   writer.write((traceid)0); // java thread id
287   writer.write((traceid)0); // java thread group
288 }
289 
290 void BytecodeConstant::serialize(JfrCheckpointWriter& writer) {
291   static const u4 nof_entries = Bytecodes::number_of_codes;
292   writer.write_count(nof_entries);
293   for (u4 i = 0; i < nof_entries; ++i) {
294     writer.write_key(i);
295     writer.write(Bytecodes::name((Bytecodes::Code)i));
296   }
297 }
298 
299 void CompilerTypeConstant::serialize(JfrCheckpointWriter& writer) {
300   static const u4 nof_entries = compiler_number_of_types;
301   writer.write_count(nof_entries);
302   for (u4 i = 0; i < nof_entries; ++i) {
303     writer.write_key(i);
304     writer.write(compilertype2name((CompilerType)i));
305   }
306 }