Skip to content

Commit 6c1eed1

Browse files
committed
Trivial prototype of JFR context
1 parent 1e25540 commit 6c1eed1

35 files changed

+1044
-41
lines changed

make/src/classes/build/tools/jfr/GenerateJfrFiles.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static class TypeElement {
204204
boolean isEvent;
205205
boolean isRelation;
206206
boolean supportStruct = false;
207+
boolean withContext = false;
207208
String commitState;
208209
public boolean primitive;
209210

@@ -227,6 +228,7 @@ public void persist(DataOutputStream pos) throws IOException {
227228
pos.writeLong(id);
228229
pos.writeBoolean(isEvent);
229230
pos.writeBoolean(isRelation);
231+
pos.writeBoolean(withContext);
230232
}
231233
}
232234

@@ -524,6 +526,7 @@ public void startElement(String uri, String localName, String qName, Attributes
524526
currentType.commitState = getString(attributes, "commitState");
525527
currentType.isEvent = "Event".equals(qName);
526528
currentType.isRelation = "Relation".equals(qName);
529+
currentType.withContext = getBoolean(attributes, "withContext", false);
527530
break;
528531
case "Field":
529532
currentField = new FieldElement(metadata);
@@ -655,7 +658,8 @@ private static void printJfrEventControlHpp(Metadata metadata, File outputFile)
655658
out.write(" u1 stacktrace;");
656659
out.write(" u1 enabled;");
657660
out.write(" u1 large;");
658-
out.write(" u1 pad[5]; // Because GCC on linux ia32 at least tries to pack this.");
661+
out.write(" u1 context;");
662+
out.write(" u1 pad[4]; // Because GCC on linux ia32 at least tries to pack this.");
659663
out.write("};");
660664
out.write("");
661665
out.write("union JfrNativeSettings {");
@@ -860,6 +864,7 @@ private static void printEvent(Printer out, TypeElement event, boolean empty) {
860864
out.write(" static const bool isInstant = " + !event.startTime + ";");
861865
out.write(" static const bool hasCutoff = " + event.cutoff + ";");
862866
out.write(" static const bool hasThrottle = " + event.throttle + ";");
867+
out.write(" static const bool hasContext = " + event.withContext + ";");
863868
out.write(" static const bool isRequestable = " + !event.period.isEmpty() + ";");
864869
out.write(" static const JfrEventId eventId = Jfr" + event.name + "Event;");
865870
out.write("");

src/hotspot/share/jfr/jni/jfrJniMethod.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "jfr/instrumentation/jfrEventClassTransformer.hpp"
4444
#include "jfr/instrumentation/jfrJvmtiAgent.hpp"
4545
#include "jfr/leakprofiler/leakProfiler.hpp"
46+
#include "jfr/support/jfrContext.hpp"
4647
#include "jfr/support/jfrJdkJfrEvent.hpp"
4748
#include "jfr/support/jfrKlassUnloading.hpp"
4849
#include "jfr/utilities/jfrJavaLog.hpp"
@@ -397,3 +398,17 @@ JVM_END
397398
JVM_ENTRY_NO_ENV(void, jfr_emit_data_loss(JNIEnv* env, jclass jvm, jlong bytes))
398399
EventDataLoss::commit(bytes, min_jlong);
399400
JVM_END
401+
402+
NO_TRANSITION(void, jfr_set_used_context_size(JNIEnv* env, jclass jvm, jint size))
403+
JfrContext::set_used_context_size(size);
404+
NO_TRANSITION_END
405+
406+
NO_TRANSITION(jboolean, jfr_is_context_enabled(JNIEnv* env, jclass jvmf))
407+
return JfrOptionSet::is_context_enabled();
408+
NO_TRANSITION_END
409+
410+
NO_TRANSITION(jobject, jfr_get_thread_context_buffer(JNIEnv* env, jclass jvm))
411+
uint64_t* data;
412+
uint8_t size = JfrContext::get_context(&data);
413+
return env->NewDirectByteBuffer((void*)data, (jlong) size * 8);
414+
NO_TRANSITION_END

src/hotspot/share/jfr/jni/jfrJniMethod.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ jlong JNICALL jfr_host_total_memory(JNIEnv* env, jclass jvm);
159159

160160
void JNICALL jfr_emit_data_loss(JNIEnv* env, jclass jvm, jlong bytes);
161161

162+
void JNICALL jfr_set_used_context_size(JNIEnv* env, jclass jvm, jint size);
163+
jboolean JNICALL jfr_is_context_enabled(JNIEnv* env, jclass jvm);
164+
jobject JNICALL jfr_get_thread_context_buffer(JNIEnv* env, jclass jvm);
165+
162166
#ifdef __cplusplus
163167
}
164168
#endif

src/hotspot/share/jfr/jni/jfrJniMethodRegistration.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ JfrJniMethodRegistration::JfrJniMethodRegistration(JNIEnv* env) {
9797
(char*)"isInstrumented", (char*)"(Ljava/lang/Class;)Z", (void*) jfr_is_class_instrumented,
9898
(char*)"isContainerized", (char*)"()Z", (void*) jfr_is_containerized,
9999
(char*)"hostTotalMemory", (char*)"()J", (void*) jfr_host_total_memory,
100-
(char*)"emitDataLoss", (char*)"(J)V", (void*)jfr_emit_data_loss
100+
(char*)"emitDataLoss", (char*)"(J)V", (void*)jfr_emit_data_loss,
101+
(char*)"setUsedContextSize", (char*)"(I)V", (void*)jfr_set_used_context_size,
102+
(char*)"isContextEnabled", (char*)"()Z", (void*)jfr_is_context_enabled,
103+
(char*)"getThreadContextBuffer0", (char*)"()Ljava/lang/Object;", (void*)jfr_get_thread_context_buffer
101104
};
102105

103106
const size_t method_array_length = sizeof(method) / sizeof(JNINativeMethod);

src/hotspot/share/jfr/metadata/metadata.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@
8989
<Field type="Thread" name="thread" label="Java Thread" />
9090
</Event>
9191

92-
<Event name="ThreadPark" category="Java Application" label="Java Thread Park" thread="true" stackTrace="true">
92+
<Event name="ThreadPark" category="Java Application" label="Java Thread Park" thread="true" stackTrace="true" withContext="true">
9393
<Field type="Class" name="parkedClass" label="Class Parked On" />
9494
<Field type="long" contentType="nanos" name="timeout" label="Park Timeout" />
9595
<Field type="long" contentType="epochmillis" name="until" label="Park Until" />
9696
<Field type="ulong" contentType="address" name="address" label="Address of Object Parked" relation="JavaMonitorAddress" />
9797
</Event>
9898

99-
<Event name="JavaMonitorEnter" category="Java Application" label="Java Monitor Blocked" thread="true" stackTrace="true">
99+
<Event name="JavaMonitorEnter" category="Java Application" label="Java Monitor Blocked" thread="true" stackTrace="true" withContext="true">
100100
<Field type="Class" name="monitorClass" label="Monitor Class" />
101101
<Field type="Thread" name="previousOwner" label="Previous Monitor Owner" />
102102
<Field type="ulong" contentType="address" name="address" label="Monitor Address" relation="JavaMonitorAddress" />
103103
</Event>
104104

105-
<Event name="JavaMonitorWait" category="Java Application" label="Java Monitor Wait" description="Waiting on a Java monitor" thread="true" stackTrace="true">
105+
<Event name="JavaMonitorWait" category="Java Application" label="Java Monitor Wait" description="Waiting on a Java monitor" thread="true" stackTrace="true" withContext="true">
106106
<Field type="Class" name="monitorClass" label="Monitor Class" description="Class of object waited on" />
107107
<Field type="Thread" name="notifier" label="Notifier Thread" description="Notifying Thread" />
108108
<Field type="long" contentType="millis" name="timeout" label="Timeout" description="Maximum wait time" />
109109
<Field type="boolean" name="timedOut" label="Timed Out" description="Wait has been timed out" />
110110
<Field type="ulong" contentType="address" name="address" label="Monitor Address" description="Address of object waited on" relation="JavaMonitorAddress" />
111111
</Event>
112112

113-
<Event name="JavaMonitorInflate" category="Java Application" label="Java Monitor Inflated" thread="true" stackTrace="true">
113+
<Event name="JavaMonitorInflate" category="Java Application" label="Java Monitor Inflated" thread="true" stackTrace="true" withContext="true">
114114
<Field type="Class" name="monitorClass" label="Monitor Class" />
115115
<Field type="ulong" contentType="address" name="address" label="Monitor Address" relation="JavaMonitorAddress" />
116116
<Field type="InflateCause" name="cause" label="Monitor Inflation Cause" description="Cause of inflation" />
@@ -727,7 +727,7 @@
727727
<Field type="ulong" contentType="bytes" name="allocationSize" label="Allocation Size" />
728728
</Event>
729729

730-
<Event name="ObjectAllocationSample" category="Java Application" label="Object Allocation Sample" thread="true" stackTrace="true" startTime="false" throttle="true">
730+
<Event name="ObjectAllocationSample" category="Java Application" label="Object Allocation Sample" thread="true" stackTrace="true" startTime="false" throttle="true" withContext="true">
731731
<Field type="Class" name="objectClass" label="Object Class" description="Class of allocated object" />
732732
<Field type="long" contentType="bytes" name="weight" label="Sample Weight"
733733
description="The relative weight of the sample. Aggregating the weights for a large number of samples, for a particular class, thread or stack trace, gives a statistically accurate representation of the allocation pressure" />
@@ -915,7 +915,7 @@
915915
</Event>
916916

917917
<Event name="ExecutionSample" category="Java Virtual Machine, Profiling" label="Method Profiling Sample" description="Snapshot of a threads state"
918-
period="everyChunk">
918+
period="everyChunk" withContext="true">
919919
<Field type="Thread" name="sampledThread" label="Thread" />
920920
<Field type="StackTrace" name="stackTrace" label="Stack Trace" />
921921
<Field type="ThreadState" name="state" label="Thread State" />

src/hotspot/share/jfr/metadata/metadata.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<xs:attribute name="period" type="periodType" use="optional" />
7373
<xs:attribute name="cutoff" type="xs:boolean" use="optional" />
7474
<xs:attribute name="throttle" type="xs:boolean" use="optional" />
75+
<xs:attribute name="withContext" type="xs:boolean" use="optional" />
7576
</xs:complexType>
7677
</xs:element>
7778
<xs:element maxOccurs="unbounded" name="Type">

src/hotspot/share/jfr/recorder/service/jfrEvent.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "jfr/recorder/jfrEventSetting.inline.hpp"
2929
#include "jfr/recorder/service/jfrEventThrottler.hpp"
3030
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
31+
#include "jfr/support/jfrContext.hpp"
3132
#include "jfr/utilities/jfrTime.hpp"
3233
#include "jfr/utilities/jfrTypes.hpp"
3334
#include "jfr/writers/jfrNativeEventWriter.hpp"
@@ -243,6 +244,13 @@ class JfrEvent {
243244
if (T::hasStackTrace) {
244245
writer.write(sid);
245246
}
247+
if (T::hasContext) {
248+
uint64_t* data;
249+
uint8_t ctx_len = JfrContext::get_context(&data);
250+
for (uint8_t i = 0; i < ctx_len; i++) {
251+
writer.write(data[i]);
252+
}
253+
}
246254
// Payload.
247255
static_cast<T*>(this)->writeData(writer);
248256
return writer.end_event_write(large_size) > 0;

src/hotspot/share/jfr/recorder/service/jfrOptionSet.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class JfrOptionSet : public AllStatic {
4747
static u4 _stack_depth;
4848
static jboolean _retransform;
4949
static jboolean _sample_protection;
50+
static jboolean _context;
5051

5152
static bool initialize(JavaThread* thread);
5253
static bool configure(TRAPS);
@@ -74,6 +75,12 @@ class JfrOptionSet : public AllStatic {
7475
static bool allow_event_retransforms();
7576
static bool sample_protection();
7677
DEBUG_ONLY(static void set_sample_protection(jboolean protection);)
78+
static void enable_context() {
79+
_context = true;
80+
}
81+
static bool is_context_enabled() {
82+
return true; // _context;
83+
}
7784

7885
static bool parse_flight_recorder_option(const JavaVMOption** option, char* delimiter);
7986
static bool parse_start_flight_recording_option(const JavaVMOption** option, char* delimiter);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "precompiled.hpp"
2+
#include "jfrContext.hpp"
3+
#include "jfr/support/jfrThreadLocal.hpp"
4+
#include "runtime/thread.hpp"
5+
6+
#ifndef MIN
7+
#define MIN(a,b) ((a) < (b) ? (a) : (b))
8+
#endif
9+
10+
uint8_t JfrContext::_size = 0;
11+
12+
JfrThreadLocal* getThreadLocal() {
13+
Thread* thrd = Thread::current_or_null_safe();
14+
return thrd != nullptr ? thrd->jfr_thread_local() : nullptr;
15+
}
16+
17+
void JfrContext::set_used_context_size(uint8_t size) {
18+
assert(size <= 8, "max context size is 8");
19+
_size = size;
20+
}
21+
22+
uint64_t JfrContext::get_and_set_context(uint8_t idx, uint64_t value) {
23+
JfrThreadLocal* jfrTLocal = getThreadLocal();
24+
if (jfrTLocal != nullptr) {
25+
uint64_t old_value = 0;
26+
jfrTLocal->get_context(&old_value, 1, idx);
27+
jfrTLocal->set_context(&value, 1, idx);
28+
return old_value;
29+
}
30+
return 0;
31+
}
32+
33+
uint8_t JfrContext::get_all_context(uint64_t* data, uint8_t length) {
34+
JfrThreadLocal* jfrTLocal = getThreadLocal();
35+
if (jfrTLocal != nullptr) {
36+
return jfrTLocal->get_context(data, MIN(_size, length), 0);
37+
}
38+
39+
return 0;
40+
}
41+
42+
uint8_t JfrContext::get_context(uint64_t** data) {
43+
void* buffer = get_thread_context_buffer();
44+
if (buffer) {
45+
*data = (uint64_t*) buffer;
46+
return _size;
47+
}
48+
return 0;
49+
}
50+
51+
uint8_t JfrContext::set_all_context(uint64_t* data, uint8_t length) {
52+
JfrThreadLocal* jfrTLocal = getThreadLocal();
53+
if (jfrTLocal != nullptr) {
54+
jfrTLocal->set_context(data, MIN(_size, length), 0);
55+
return length;
56+
}
57+
58+
return 0;
59+
}
60+
61+
void* JfrContext::get_thread_context_buffer() {
62+
JfrThreadLocal* jfrTLocal = getThreadLocal();
63+
if (jfrTLocal != nullptr) {
64+
return (void*)jfrTLocal->get_context_buffer();
65+
}
66+
return nullptr;
67+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SHARE_JFR_SUPPORT_CONTEXT_HPP
2+
#define SHARE_JFR_SUPPORT_CONTEXT_HPP
3+
4+
#include "memory/allStatic.hpp"
5+
#include "utilities/globalDefinitions.hpp"
6+
7+
class JfrContext : AllStatic {
8+
private:
9+
static uint8_t _size;
10+
public:
11+
static void set_used_context_size(uint8_t size);
12+
static inline uint8_t get_used_context_size() { return _size; }
13+
static uint64_t get_and_set_context(uint8_t idx, uint64_t value);
14+
static uint8_t get_all_context(uint64_t* data, uint8_t length);
15+
static uint8_t set_all_context(uint64_t* data, uint8_t length);
16+
static uint8_t get_context(uint64_t** data);
17+
static void* get_thread_context_buffer();
18+
};
19+
20+
#endif // SHARE_JFR_SUPPORT_CONTEXT_HPP

0 commit comments

Comments
 (0)