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 // only static thread ids, VirtualThread ids are handled dynamically
 92 void JfrCheckpointThreadClosure::do_thread(Thread* t) {
 93   assert(t != NULL, "invariant");
 94   ++_count;
 95   const traceid tid = JfrThreadId::jfr_id(t);
 96   assert(tid != 0, "invariant");
 97   _writer.write_key(tid);
 98   const char* const name = JfrThreadName::name(t);
 99   assert(name != NULL, "invariant");
100   _writer.write(name);
101   _writer.write<traceid>(JfrThreadId::os_id(t));
102   if (!t->is_Java_thread()) {
103     _writer.write((const char*)NULL); // java name
104     _writer.write((traceid)0); // java thread id
105     _writer.write((traceid)0); // java thread group
106   } else {
107     _writer.write(name);
108     _writer.write(tid);
109     _writer.write(JfrThreadGroup::thread_group_id(JavaThread::cast(t), _curthread));
110   }
111   _writer.write<bool>(false); // isVirtual
112 }
113 
114 void JfrThreadConstantSet::serialize(JfrCheckpointWriter& writer) {
115   JfrCheckpointThreadClosure tc(writer);
116   JfrJavaThreadIterator javathreads(false); // include not yet live threads (_thread_new)
117   while (javathreads.has_next()) {
118     tc.do_thread(javathreads.next());
119   }
120   JfrNonJavaThreadIterator nonjavathreads;
121   while (nonjavathreads.has_next()) {
122     tc.do_thread(nonjavathreads.next());
123   }
124 }
125 
126 void JfrThreadGroupConstant::serialize(JfrCheckpointWriter& writer) {
127   JfrThreadGroup::serialize(writer);
128 }
129 
130 static const char* flag_value_origin_to_string(JVMFlagOrigin origin) {
131   switch (origin) {
132     case JVMFlagOrigin::DEFAULT: return "Default";
133     case JVMFlagOrigin::COMMAND_LINE: return "Command line";
134     case JVMFlagOrigin::ENVIRON_VAR: return "Environment variable";
135     case JVMFlagOrigin::CONFIG_FILE: return "Config file";
136     case JVMFlagOrigin::MANAGEMENT: return "Management";
137     case JVMFlagOrigin::ERGONOMIC: return "Ergonomic";
138     case JVMFlagOrigin::ATTACH_ON_DEMAND: return "Attach on demand";
139     case JVMFlagOrigin::INTERNAL: return "Internal";
140     case JVMFlagOrigin::JIMAGE_RESOURCE: return "JImage resource";
141     default: ShouldNotReachHere(); return "";
142   }
143 }
144 
145 void FlagValueOriginConstant::serialize(JfrCheckpointWriter& writer) {
146   constexpr EnumRange<JVMFlagOrigin> range{};
147   writer.write_count(static_cast<u4>(range.size()));
148 
149   for (JVMFlagOrigin origin : range) {
150     writer.write_key(static_cast<u4>(origin));
151     writer.write(flag_value_origin_to_string(origin));
152   }
153 }
154 
155 void MonitorInflateCauseConstant::serialize(JfrCheckpointWriter& writer) {
156   static const u4 nof_entries = ObjectSynchronizer::inflate_cause_nof;
157   writer.write_count(nof_entries);
158   for (u4 i = 0; i < nof_entries; ++i) {
159     writer.write_key(i);
160     writer.write(ObjectSynchronizer::inflate_cause_name((ObjectSynchronizer::InflateCause)i));
161   }
162 }
163 
164 void GCCauseConstant::serialize(JfrCheckpointWriter& writer) {
165   static const u4 nof_entries = GCCause::_last_gc_cause;
166   writer.write_count(nof_entries);
167   for (u4 i = 0; i < nof_entries; ++i) {
168     writer.write_key(i);
169     writer.write(GCCause::to_string((GCCause::Cause)i));
170   }
171 }
172 
173 void GCNameConstant::serialize(JfrCheckpointWriter& writer) {
174   static const u4 nof_entries = GCNameEndSentinel;
175   writer.write_count(nof_entries);
176   for (u4 i = 0; i < nof_entries; ++i) {
177     writer.write_key(i);
178     writer.write(GCNameHelper::to_string((GCName)i));
179   }
180 }
181 
182 void GCWhenConstant::serialize(JfrCheckpointWriter& writer) {
183   static const u4 nof_entries = GCWhen::GCWhenEndSentinel;
184   writer.write_count(nof_entries);
185   for (u4 i = 0; i < nof_entries; ++i) {
186     writer.write_key(i);
187     writer.write(GCWhen::to_string((GCWhen::Type)i));
188   }
189 }
190 
191 void GCThresholdUpdaterConstant::serialize(JfrCheckpointWriter& writer) {
192   static const u4 nof_entries = MetaspaceGCThresholdUpdater::Last;
193   writer.write_count(nof_entries);
194   for (u4 i = 0; i < nof_entries; ++i) {
195     writer.write_key(i);
196     writer.write(MetaspaceGCThresholdUpdater::to_string((MetaspaceGCThresholdUpdater::Type)i));
197   }
198 }
199 
200 void MetadataTypeConstant::serialize(JfrCheckpointWriter& writer) {
201   static const u4 nof_entries = Metaspace::MetadataTypeCount;
202   writer.write_count(nof_entries);
203   for (u4 i = 0; i < nof_entries; ++i) {
204     writer.write_key(i);
205     writer.write(Metaspace::metadata_type_name((Metaspace::MetadataType)i));
206   }
207 }
208 
209 void MetaspaceObjectTypeConstant::serialize(JfrCheckpointWriter& writer) {
210   static const u4 nof_entries = MetaspaceObj::_number_of_types;
211   writer.write_count(nof_entries);
212   for (u4 i = 0; i < nof_entries; ++i) {
213     writer.write_key(i);
214     writer.write(MetaspaceObj::type_name((MetaspaceObj::Type)i));
215   }
216 }
217 
218 static const char* reference_type_to_string(ReferenceType rt) {
219   switch (rt) {
220     case REF_NONE: return "None reference";
221     case REF_OTHER: return "Other reference";
222     case REF_SOFT: return "Soft reference";
223     case REF_WEAK: return "Weak reference";
224     case REF_FINAL: return "Final reference";
225     case REF_PHANTOM: return "Phantom reference";
226     default:
227       ShouldNotReachHere();
228     return NULL;
229   }
230 }
231 
232 void ReferenceTypeConstant::serialize(JfrCheckpointWriter& writer) {
233   static const u4 nof_entries = REF_PHANTOM + 1;
234   writer.write_count(nof_entries);
235   for (u4 i = 0; i < nof_entries; ++i) {
236     writer.write_key(i);
237     writer.write(reference_type_to_string((ReferenceType)i));
238   }
239 }
240 
241 void NarrowOopModeConstant::serialize(JfrCheckpointWriter& writer) {
242   static const u4 nof_entries = CompressedOops::HeapBasedNarrowOop + 1;
243   writer.write_count(nof_entries);
244   for (u4 i = 0; i < nof_entries; ++i) {
245     writer.write_key(i);
246     writer.write(CompressedOops::mode_to_string((CompressedOops::Mode)i));
247   }
248 }
249 
250 void CodeBlobTypeConstant::serialize(JfrCheckpointWriter& writer) {
251   static const u4 nof_entries = CodeBlobType::NumTypes;
252   writer.write_count(nof_entries);
253   for (u4 i = 0; i < nof_entries; ++i) {
254     writer.write_key(i);
255     writer.write(CodeCache::get_code_heap_name(i));
256   }
257 };
258 
259 void VMOperationTypeConstant::serialize(JfrCheckpointWriter& writer) {
260   static const u4 nof_entries = VM_Operation::VMOp_Terminating;
261   writer.write_count(nof_entries);
262   for (u4 i = 0; i < nof_entries; ++i) {
263     writer.write_key(i);
264     writer.write(VM_Operation::name(VM_Operation::VMOp_Type(i)));
265   }
266 }
267 
268 void ThreadStateConstant::serialize(JfrCheckpointWriter& writer) {
269   JfrThreadState::serialize(writer);
270 }
271 
272 void JfrThreadConstant::serialize(JfrCheckpointWriter& writer) {
273   assert(_thread != NULL, "invariant");
274   writer.write_count(1);
275   writer.write_key(JfrThreadId::jfr_id(_thread, _tid));
276   const char* const name = JfrThreadName::name(_thread, _vthread);
277   writer.write(name);
278   writer.write(JfrThreadId::os_id(_thread));
279   if (!_thread->is_Java_thread()) {
280     writer.write((const char*)NULL); // java name
281     writer.write((traceid)0);        // java thread id
282     writer.write((traceid)0);        // java thread group
283     writer.write<bool>(false);       // isVirtual
284   } else {
285     writer.write(name);
286     writer.write(JfrThreadId::id(_thread, _vthread));
287     const bool vthread = _vthread != NULL;
288     // VirtualThread threadgroup reserved id 1
289     const traceid thread_group_id = vthread ? 1 :
290       JfrThreadGroup::thread_group_id(JavaThread::cast(_thread), Thread::current());
291     writer.write(thread_group_id);
292     writer.write<bool>(vthread);    // isVirtual
293     // VirtualThread threadgroup already serialized invariant
294     if (!vthread) {
295       JfrThreadGroup::serialize(&writer, thread_group_id);
296     }
297   }
298 }
299 
300 void BytecodeConstant::serialize(JfrCheckpointWriter& writer) {
301   static const u4 nof_entries = Bytecodes::number_of_codes;
302   writer.write_count(nof_entries);
303   for (u4 i = 0; i < nof_entries; ++i) {
304     writer.write_key(i);
305     writer.write(Bytecodes::name((Bytecodes::Code)i));
306   }
307 }
308 
309 void CompilerTypeConstant::serialize(JfrCheckpointWriter& writer) {
310   static const u4 nof_entries = compiler_number_of_types;
311   writer.write_count(nof_entries);
312   for (u4 i = 0; i < nof_entries; ++i) {
313     writer.write_key(i);
314     writer.write(compilertype2name((CompilerType)i));
315   }
316 }