jdk.jfr.EventFactory Java Examples

The following examples show how to use jdk.jfr.EventFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestDynamicAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void testEventFactoryExample() throws IOException {
     List<ValueDescriptor> fields = new ArrayList<>();
     List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
     fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
     List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
     fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

     String[] category = { "Example", "Getting Started" };
     List<AnnotationElement> eventAnnotations = new ArrayList<>();
     eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
     eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
     eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
     eventAnnotations.add(new AnnotationElement(Category.class, category));

     EventFactory f = EventFactory.create(eventAnnotations, fields);

     Event event = f.newEvent();
     event.set(0, "hello, world!");
     event.set(1, 4711);
     event.commit();
}
 
Example #2
Source File: TestDynamicAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void testEventFactoryExample() throws IOException {
     List<ValueDescriptor> fields = new ArrayList<>();
     List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
     fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
     List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
     fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

     String[] category = { "Example", "Getting Started" };
     List<AnnotationElement> eventAnnotations = new ArrayList<>();
     eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
     eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
     eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
     eventAnnotations.add(new AnnotationElement(Category.class, category));

     EventFactory f = EventFactory.create(eventAnnotations, fields);

     Event event = f.newEvent();
     event.set(0, "hello, world!");
     event.set(1, 4711);
     event.commit();
}
 
Example #3
Source File: TestDynamicAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void testEventFactoryExample() throws IOException {
     List<ValueDescriptor> fields = new ArrayList<>();
     List<AnnotationElement> messageAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Message"));
     fields.add(new ValueDescriptor(String.class, "message", messageAnnotations));
     List<AnnotationElement> numberAnnotations = Collections.singletonList(new AnnotationElement(Label.class, "Number"));
     fields.add(new ValueDescriptor(int.class, "number", numberAnnotations));

     String[] category = { "Example", "Getting Started" };
     List<AnnotationElement> eventAnnotations = new ArrayList<>();
     eventAnnotations.add(new AnnotationElement(Name.class, "com.example.HelloWorld"));
     eventAnnotations.add(new AnnotationElement(Label.class, "Hello World"));
     eventAnnotations.add(new AnnotationElement(Description.class, "Helps programmer getting started"));
     eventAnnotations.add(new AnnotationElement(Category.class, category));

     EventFactory f = EventFactory.create(eventAnnotations, fields);

     Event event = f.newEvent();
     event.set(0, "hello, world!");
     event.set(1, 4711);
     event.commit();
}
 
Example #4
Source File: TestEventFactoryRegistration.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Create an unregistered event
    List<AnnotationElement> annotations = new ArrayList<>();
    annotations.add(new AnnotationElement(Registered.class, false));
    EventFactory factory = EventFactory.create(annotations, Collections.emptyList());

    try {
        factory.getEventType();
        Asserts.fail("Should not be able to get event type from an unregistered event");
    } catch(IllegalStateException ise) {
        // OK as expected
    }

    // Now, register the event
    factory.register();
    EventType eventType = factory.getEventType();
    verifyRegistered(factory.getEventType());


    // Now, unregister the event
    factory.unregister();

    verifyUnregistered(eventType);

    // Create a registered event
    factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    verifyRegistered(eventType);

}
 
Example #5
Source File: TestEventFactory.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(EVENT_TYPE_SHOULD_COMMIT.getName()).withoutStackTrace();
    r.enable(EVENT_TYPE_SHOULD_NOT_COMMIT.getName()).withoutStackTrace();

    // Commit before start, should not be included
    ef1 = EventFactory.create(EVENT_TYPE_SHOULD_NOT_COMMIT.getAnnotations(), EVENT_TYPE_SHOULD_NOT_COMMIT.getFields());

    Event event1 = ef1.newEvent();

    setEventValues(event1, ef1, EVENT_TYPE_SHOULD_NOT_COMMIT);
    event1.commit();

    r.start();
    // Commit after start, should be included
    ef2 = EventFactory.create(EVENT_TYPE_SHOULD_COMMIT.getAnnotations(),  EVENT_TYPE_SHOULD_COMMIT.getFields());

    Event event2 = ef2.newEvent();
    setEventValues(event2, ef2, EVENT_TYPE_SHOULD_COMMIT);
    event2.commit();

    r.stop();

    RecordingFile es = Events.copyTo(r);
    EventType e1 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_NOT_COMMIT.getName());
    assertEquals(e1, ef1.getEventType());

    EventType e2 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_COMMIT.getName());
    assertEquals(e2, ef2.getEventType());

    verifyEvent(es);
}
 
Example #6
Source File: TestDynamicAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void testArray() throws Exception {
    List<AnnotationElement> annotations = new ArrayList<>();
    Map<String, Object> values = new HashMap<>();
    values.put("stringArray", new String[] {"zero", "one"});
    values.put("intArray", new int[] {0, 1});
    values.put("longArray", new long[] {0L, 1L});
    values.put("floatArray", new float[] {0.0f, 1.0f});
    values.put("doubleArray", new double[] {0.0, 1.0});
    values.put("booleanArray", new boolean[] {false, true});
    values.put("shortArray", new short[] {(short)0, (short)1});
    values.put("byteArray", new byte[] {(byte)0, (byte)1});
    values.put("charArray", new char[] {'0','1'});

    annotations.add(new AnnotationElement(Array.class, values));
    EventFactory f = EventFactory.create(annotations, Collections.emptyList());
    Array a = f.getEventType().getAnnotation(Array.class);
    if (a == null) {
        throw new Exception("Missing array annotation");
    }
    verifyArrayAnnotation(a);
    System.out.println("Event metadata is correct");
    try (Recording r = new Recording()) {
        r.start();
        Event e = f.newEvent();
        e.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        RecordedEvent re = events.get(0);
        Array arrayAnnotation = re.getEventType().getAnnotation(Array.class);
        if (arrayAnnotation== null) {
            throw new Exception("Missing array annotation");
        }
        verifyArrayAnnotation(arrayAnnotation);
        System.out.println("Persisted event metadata is correct");
    }
}
 
Example #7
Source File: TestDynamicAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void testECID() throws Exception {
    List<ValueDescriptor> fields = new ArrayList<>();

    List<AnnotationElement> fieldAnnotations = new ArrayList<>();
    fieldAnnotations.add(new AnnotationElement(ECID.class));
    ValueDescriptor ecidField = new ValueDescriptor(String.class, "ecid", fieldAnnotations);
    fields.add(ecidField);

    EventFactory f = EventFactory.create(fieldAnnotations, fields);

    String ecidValue = "131739871298371279812";
    try (Recording r = new Recording()) {
        r.start();
        Event event = f.newEvent();
        event.set(0, ecidValue);
        event.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        Events.assertField(events.get(0), "ecid").equal(ecidValue);
    }
    EventType type = f.getEventType();
    ECID e = type.getAnnotation(ECID.class);
    if (e == null) {
        throw new Exception("Missing ECID annotation");
    }
}
 
Example #8
Source File: TestEventFactoryRegistration.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Create an unregistered event
    List<AnnotationElement> annotations = new ArrayList<>();
    annotations.add(new AnnotationElement(Registered.class, false));
    EventFactory factory = EventFactory.create(annotations, Collections.emptyList());

    try {
        factory.getEventType();
        Asserts.fail("Should not be able to get event type from an unregistered event");
    } catch(IllegalStateException ise) {
        // OK as expected
    }

    // Now, register the event
    factory.register();
    EventType eventType = factory.getEventType();
    verifyRegistered(factory.getEventType());


    // Now, unregister the event
    factory.unregister();

    verifyUnregistered(eventType);

    // Create a registered event
    factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    verifyRegistered(eventType);

}
 
Example #9
Source File: TestEventFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(EVENT_TYPE_SHOULD_COMMIT.getName()).withoutStackTrace();
    r.enable(EVENT_TYPE_SHOULD_NOT_COMMIT.getName()).withoutStackTrace();

    // Commit before start, should not be included
    ef1 = EventFactory.create(EVENT_TYPE_SHOULD_NOT_COMMIT.getAnnotations(), EVENT_TYPE_SHOULD_NOT_COMMIT.getFields());

    Event event1 = ef1.newEvent();

    setEventValues(event1, ef1, EVENT_TYPE_SHOULD_NOT_COMMIT);
    event1.commit();

    r.start();
    // Commit after start, should be included
    ef2 = EventFactory.create(EVENT_TYPE_SHOULD_COMMIT.getAnnotations(),  EVENT_TYPE_SHOULD_COMMIT.getFields());

    Event event2 = ef2.newEvent();
    setEventValues(event2, ef2, EVENT_TYPE_SHOULD_COMMIT);
    event2.commit();

    r.stop();

    RecordingFile es = Events.copyTo(r);
    EventType e1 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_NOT_COMMIT.getName());
    assertEquals(e1, ef1.getEventType());

    EventType e2 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_COMMIT.getName());
    assertEquals(e2, ef2.getEventType());

    verifyEvent(es);
}
 
Example #10
Source File: TestDynamicAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testArray() throws Exception {
    List<AnnotationElement> annotations = new ArrayList<>();
    Map<String, Object> values = new HashMap<>();
    values.put("stringArray", new String[] {"zero", "one"});
    values.put("intArray", new int[] {0, 1});
    values.put("longArray", new long[] {0L, 1L});
    values.put("floatArray", new float[] {0.0f, 1.0f});
    values.put("doubleArray", new double[] {0.0, 1.0});
    values.put("booleanArray", new boolean[] {false, true});
    values.put("shortArray", new short[] {(short)0, (short)1});
    values.put("byteArray", new byte[] {(byte)0, (byte)1});
    values.put("charArray", new char[] {'0','1'});

    annotations.add(new AnnotationElement(Array.class, values));
    EventFactory f = EventFactory.create(annotations, Collections.emptyList());
    Array a = f.getEventType().getAnnotation(Array.class);
    if (a == null) {
        throw new Exception("Missing array annotation");
    }
    verifyArrayAnnotation(a);
    System.out.println("Event metadata is correct");
    try (Recording r = new Recording()) {
        r.start();
        Event e = f.newEvent();
        e.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        RecordedEvent re = events.get(0);
        Array arrayAnnotation = re.getEventType().getAnnotation(Array.class);
        if (arrayAnnotation== null) {
            throw new Exception("Missing array annotation");
        }
        verifyArrayAnnotation(arrayAnnotation);
        System.out.println("Persisted event metadata is correct");
    }
}
 
Example #11
Source File: TestDynamicAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testECID() throws Exception {
    List<ValueDescriptor> fields = new ArrayList<>();

    List<AnnotationElement> fieldAnnotations = new ArrayList<>();
    fieldAnnotations.add(new AnnotationElement(ECID.class));
    ValueDescriptor ecidField = new ValueDescriptor(String.class, "ecid", fieldAnnotations);
    fields.add(ecidField);

    EventFactory f = EventFactory.create(fieldAnnotations, fields);

    String ecidValue = "131739871298371279812";
    try (Recording r = new Recording()) {
        r.start();
        Event event = f.newEvent();
        event.set(0, ecidValue);
        event.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        Events.assertField(events.get(0), "ecid").equal(ecidValue);
    }
    EventType type = f.getEventType();
    ECID e = type.getAnnotation(ECID.class);
    if (e == null) {
        throw new Exception("Missing ECID annotation");
    }
}
 
Example #12
Source File: TestEventFactoryRegistration.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Create an unregistered event
    List<AnnotationElement> annotations = new ArrayList<>();
    annotations.add(new AnnotationElement(Registered.class, false));
    EventFactory factory = EventFactory.create(annotations, Collections.emptyList());

    try {
        factory.getEventType();
        Asserts.fail("Should not be able to get event type from an unregistered event");
    } catch(IllegalStateException ise) {
        // OK as expected
    }

    // Now, register the event
    factory.register();
    EventType eventType = factory.getEventType();
    verifyRegistered(factory.getEventType());


    // Now, unregister the event
    factory.unregister();

    verifyUnregistered(eventType);

    // Create a registered event
    factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    verifyRegistered(eventType);

}
 
Example #13
Source File: TestDynamicAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void testECID() throws Exception {
    List<ValueDescriptor> fields = new ArrayList<>();

    List<AnnotationElement> fieldAnnotations = new ArrayList<>();
    fieldAnnotations.add(new AnnotationElement(ECID.class));
    ValueDescriptor ecidField = new ValueDescriptor(String.class, "ecid", fieldAnnotations);
    fields.add(ecidField);

    EventFactory f = EventFactory.create(fieldAnnotations, fields);

    String ecidValue = "131739871298371279812";
    try (Recording r = new Recording()) {
        r.start();
        Event event = f.newEvent();
        event.set(0, ecidValue);
        event.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        Events.assertField(events.get(0), "ecid").equal(ecidValue);
    }
    EventType type = f.getEventType();
    ECID e = type.getAnnotation(ECID.class);
    if (e == null) {
        throw new Exception("Missing ECID annotation");
    }
}
 
Example #14
Source File: TestDynamicAnnotations.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void testArray() throws Exception {
    List<AnnotationElement> annotations = new ArrayList<>();
    Map<String, Object> values = new HashMap<>();
    values.put("stringArray", new String[] {"zero", "one"});
    values.put("intArray", new int[] {0, 1});
    values.put("longArray", new long[] {0L, 1L});
    values.put("floatArray", new float[] {0.0f, 1.0f});
    values.put("doubleArray", new double[] {0.0, 1.0});
    values.put("booleanArray", new boolean[] {false, true});
    values.put("shortArray", new short[] {(short)0, (short)1});
    values.put("byteArray", new byte[] {(byte)0, (byte)1});
    values.put("charArray", new char[] {'0','1'});

    annotations.add(new AnnotationElement(Array.class, values));
    EventFactory f = EventFactory.create(annotations, Collections.emptyList());
    Array a = f.getEventType().getAnnotation(Array.class);
    if (a == null) {
        throw new Exception("Missing array annotation");
    }
    verifyArrayAnnotation(a);
    System.out.println("Event metadata is correct");
    try (Recording r = new Recording()) {
        r.start();
        Event e = f.newEvent();
        e.commit();
        r.stop();
        List<RecordedEvent> events = Events.fromRecording(r);
        Events.hasEvents(events);
        RecordedEvent re = events.get(0);
        Array arrayAnnotation = re.getEventType().getAnnotation(Array.class);
        if (arrayAnnotation== null) {
            throw new Exception("Missing array annotation");
        }
        verifyArrayAnnotation(arrayAnnotation);
        System.out.println("Persisted event metadata is correct");
    }
}
 
Example #15
Source File: TestEventFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.enable(EVENT_TYPE_SHOULD_COMMIT.getName()).withoutStackTrace();
    r.enable(EVENT_TYPE_SHOULD_NOT_COMMIT.getName()).withoutStackTrace();

    // Commit before start, should not be included
    ef1 = EventFactory.create(EVENT_TYPE_SHOULD_NOT_COMMIT.getAnnotations(), EVENT_TYPE_SHOULD_NOT_COMMIT.getFields());

    Event event1 = ef1.newEvent();

    setEventValues(event1, ef1, EVENT_TYPE_SHOULD_NOT_COMMIT);
    event1.commit();

    r.start();
    // Commit after start, should be included
    ef2 = EventFactory.create(EVENT_TYPE_SHOULD_COMMIT.getAnnotations(),  EVENT_TYPE_SHOULD_COMMIT.getFields());

    Event event2 = ef2.newEvent();
    setEventValues(event2, ef2, EVENT_TYPE_SHOULD_COMMIT);
    event2.commit();

    r.stop();

    RecordingFile es = Events.copyTo(r);
    EventType e1 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_NOT_COMMIT.getName());
    assertEquals(e1, ef1.getEventType());

    EventType e2 = findEventType(es.readEventTypes(), EVENT_TYPE_SHOULD_COMMIT.getName());
    assertEquals(e2, ef2.getEventType());

    verifyEvent(es);
}
 
Example #16
Source File: TestEventFactoryRegisterTwice.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventFactory factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    EventType eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    // Now, register the event
    factory.register();

    verifyRegistered(eventType);

    // Now, register the event again
    factory.register();

    verifyRegistered(eventType);
}
 
Example #17
Source File: TestEventFactoryRegisterTwice.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventFactory factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    EventType eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    // Now, register the event
    factory.register();

    verifyRegistered(eventType);

    // Now, register the event again
    factory.register();

    verifyRegistered(eventType);
}
 
Example #18
Source File: JFRSecurityTestSuite.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void doInConstrainedEnvironment() throws Throwable {
    checkNoDirectAccess();

    assertPermission(() -> {
                FlightRecorder.getFlightRecorder();
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                FlightRecorder.register(MyEvent.class);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.unregister(MyEvent.class);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.addPeriodicEvent(MyEvent.class, periodicEvent);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.removePeriodicEvent(periodicEvent);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.addListener(frl);
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
            FlightRecorder.removeListener(frl);
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                new MyEvent().commit();
            }, FlightRecorderPermission.class, "registerEvent", true);

    assertPermission(() -> {
                    Configuration.create(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                EventFactory.create(new ArrayList<AnnotationElement>(),
                        new ArrayList<ValueDescriptor>());
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                new AnnotationElement(Name.class, "com.example.HelloWorld");
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
            new ValueDescriptor(MyEvent.class, "",
                    new ArrayList<AnnotationElement>());
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                new Recording();
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                    new RecordingFile(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                    RecordingFile.readAllEvents(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                    EventType.getEventType(MyEvent2.class);
            }, FlightRecorderPermission.class, "registerEvent", true);
}
 
Example #19
Source File: TestLargeJavaEvent512k.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void setEventValues(Event event, EventFactory f, EventTypePrototype prototype, Map<String, Object> fields) {
    for (Map.Entry<String, Object> entry : fields.entrySet()) {
        int index = prototype.getFieldIndex(entry.getKey());
        event.set(index, entry.getValue());
    }
}
 
Example #20
Source File: TestLargeJavaEvent512k.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    final String name = "MyLargeJavaEvent512k"; // name of synthetically generated event
    final String fieldNamePrefix = "myfield";
    final int numberOfFields = 64; // 64*8k = 512k event size
    final Map<String, Object> eventMap = new HashMap<>();
    final int numberOfThreads = 10; // 10 threads will run the test
    final int numberOfEventsPerThread = 50; // each thread will generate 50 events

    List<ValueDescriptor> fields = new ArrayList<>();
    for (int i = 0; i < numberOfFields; ++i) {
        String fieldName = fieldNamePrefix + i;
        eventMap.put(fieldName, largeString());
        fields.add(new ValueDescriptor(String.class, fieldName));
    }

    EventTypePrototype prototype = new EventTypePrototype(name,Collections.emptyList(),  fields);

    EventFactory ef = EventFactory.create(prototype.getAnnotations(), prototype.getFields());

    Recording r = new Recording();
    r.enable(prototype.getName()).withThreshold(Duration.ofNanos(0)).withoutStackTrace();
    r.start();

    Thread.UncaughtExceptionHandler eh = (t, e) -> TestLargeJavaEvent512k.setError();

    Stressor.execute(numberOfThreads, eh, () -> {
        for (int n = 0; n < numberOfEventsPerThread; ++n) {
            try {
                Event event = ef.newEvent();
                setEventValues(event, ef, prototype, eventMap);
                event.commit();
                Thread.sleep(1);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    r.stop();
    try {
        if (hasError()) {
            throw new RuntimeException("One (or several) of the threads had an exception/error, test failed");
        }
        verifyEvents(r, numberOfThreads, numberOfEventsPerThread, eventMap);
    } finally {
        r.close();
    }
}
 
Example #21
Source File: TestUnsupportedVM.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    if (Boolean.getBoolean("prepare-recording")) {
        Recording r = new Recording(Configuration.getConfiguration("default"));
        r.start();
        r.stop();
        r.dump(RECORDING_FILE);
        r.close();
        return;
    }

    System.out.println("jdk.jfr.unsupportedvm=" + System.getProperty("jdk.jfr.unsupportedvm"));
    // Class FlightRecorder
    if (FlightRecorder.isAvailable()) {
        throw new AssertionError("JFR should not be available on an unsupported VM");
    }

    if (FlightRecorder.isInitialized()) {
        throw new AssertionError("JFR should not be initialized on an unsupported VM");
    }

    assertIllegalStateException(() -> FlightRecorder.getFlightRecorder());
    assertSwallow(() -> FlightRecorder.addListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.removeListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.register(MyEvent.class));
    assertSwallow(() -> FlightRecorder.unregister(MyEvent.class));
    assertSwallow(() -> FlightRecorder.addPeriodicEvent(MyEvent.class, new Runnable() { public void run() {} }));
    assertSwallow(() -> FlightRecorder.removePeriodicEvent(new Runnable() { public void run() {} }));

    // Class Configuration
    if (!Configuration.getConfigurations().isEmpty()) {
        throw new AssertionError("Configuration files should not exist on an unsupported VM");
    }
    Path jfcFile = Utils.createTempFile("empty", ".jfr");
    assertIOException(() -> Configuration.getConfiguration("default"));
    assertIOException(() -> Configuration.create(jfcFile));
    assertIOException(() -> Configuration.create(new FileReader(jfcFile.toFile())));

    // Class EventType
    assertInternalError(() -> EventType.getEventType(MyEvent.class));

    // Class EventFactory
    assertInternalError(() -> EventFactory.create(new ArrayList<>(), new ArrayList<>()));

    // Create a static event
    MyEvent myEvent = new MyEvent();
    myEvent.begin();
    myEvent.end();
    myEvent.shouldCommit();
    myEvent.commit();

    // Trigger class initialization failure
    for (Class<?> c : APIClasses) {
        assertNoClassInitFailure(c);
    }

    // jdk.jfr.consumer.*
    // Only run this part of tests if we are on VM
    // that can produce a recording file
    if (Files.exists(RECORDING_FILE)) {
        for(RecordedEvent re : RecordingFile.readAllEvents(RECORDING_FILE)) {
            System.out.println(re);
        }
    }
}
 
Example #22
Source File: TestLargeJavaEvent64k.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void setEventValues(Event event, EventFactory f, EventTypePrototype prototype, Map<String, Object> fields) {
    for (Map.Entry<String, Object> entry : fields.entrySet()) {
        int index = prototype.getFieldIndex(entry.getKey());
        event.set(index, entry.getValue());
    }
}
 
Example #23
Source File: TestLargeJavaEvent64k.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    final String name = "MyLargeJavaEvent64k"; // name of synthetically generated event
    final String fieldNamePrefix = "myfield";
    final int numberOfFields = 8; // 8*8k = ~64k event size
    final Map<String, Object> eventMap = new HashMap<>();
    final int numberOfThreads = 100; // 50 threads will run the test
    final int numberOfEventsPerThread = 50; // each thread will generate 50 events

    List<ValueDescriptor> fields = new ArrayList<>();
    for (int i = 0; i < numberOfFields; ++i) {
        String fieldName = fieldNamePrefix + i;
        eventMap.put(fieldName, largeString());
        fields.add(new ValueDescriptor(String.class, fieldName));
    }

    EventTypePrototype prototype = new EventTypePrototype(name,Collections.emptyList(),  fields);

    EventFactory ef = EventFactory.create(prototype.getAnnotations(), prototype.getFields());

    Recording r = new Recording();
    r.enable(prototype.getName()).withThreshold(Duration.ofNanos(0)).withoutStackTrace();
    r.start();

    Thread.UncaughtExceptionHandler eh = (t, e) -> TestLargeJavaEvent64k.setError();

    Stressor.execute(numberOfThreads, eh, () -> {
        for (int n = 0; n < numberOfEventsPerThread; ++n) {
            try {
                Event event = ef.newEvent();
                setEventValues(event, ef, prototype, eventMap);
                event.commit();
                Thread.sleep(1);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    r.stop();
    try {
        if (hasError()) {
            throw new RuntimeException("One (or several) of the threads had an exception/error, test failed");
        }
        verifyEvents(r, numberOfThreads, numberOfEventsPerThread, eventMap);
    } finally {
        r.close();
    }
}
 
Example #24
Source File: TestEventFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void setEventValues(Event event, EventFactory f, EventTypePrototype eventTypeProto) {
    for (Map.Entry<String, Object> entry : EVENT_VALUES.entrySet()) {
        int index = eventTypeProto.getFieldIndex(entry.getKey());
        event.set(index, entry.getValue());
    }
}
 
Example #25
Source File: TestEventFactory.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void setEventValues(Event event, EventFactory f, EventTypePrototype eventTypeProto) {
    for (Map.Entry<String, Object> entry : EVENT_VALUES.entrySet()) {
        int index = eventTypeProto.getFieldIndex(entry.getKey());
        event.set(index, entry.getValue());
    }
}
 
Example #26
Source File: TestLargeJavaEvent64k.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    final String name = "MyLargeJavaEvent64k"; // name of synthetically generated event
    final String fieldNamePrefix = "myfield";
    final int numberOfFields = 8; // 8*8k = ~64k event size
    final Map<String, Object> eventMap = new HashMap<>();
    final int numberOfThreads = 100; // 50 threads will run the test
    final int numberOfEventsPerThread = 50; // each thread will generate 50 events

    List<ValueDescriptor> fields = new ArrayList<>();
    for (int i = 0; i < numberOfFields; ++i) {
        String fieldName = fieldNamePrefix + i;
        eventMap.put(fieldName, largeString());
        fields.add(new ValueDescriptor(String.class, fieldName));
    }

    EventTypePrototype prototype = new EventTypePrototype(name,Collections.emptyList(),  fields);

    EventFactory ef = EventFactory.create(prototype.getAnnotations(), prototype.getFields());

    Recording r = new Recording();
    r.enable(prototype.getName()).withThreshold(Duration.ofNanos(0)).withoutStackTrace();
    r.start();

    Thread.UncaughtExceptionHandler eh = (t, e) -> TestLargeJavaEvent64k.setError();

    Stressor.execute(numberOfThreads, eh, () -> {
        for (int n = 0; n < numberOfEventsPerThread; ++n) {
            try {
                Event event = ef.newEvent();
                setEventValues(event, ef, prototype, eventMap);
                event.commit();
                Thread.sleep(1);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    r.stop();
    try {
        if (hasError()) {
            throw new RuntimeException("One (or several) of the threads had an exception/error, test failed");
        }
        verifyEvents(r, numberOfThreads, numberOfEventsPerThread, eventMap);
    } finally {
        r.close();
    }
}
 
Example #27
Source File: JFRSecurityTestSuite.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void doInConstrainedEnvironment() throws Throwable {
    checkNoDirectAccess();

    assertPermission(() -> {
                FlightRecorder.getFlightRecorder();
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                FlightRecorder.register(MyEvent.class);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.unregister(MyEvent.class);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.addPeriodicEvent(MyEvent.class, periodicEvent);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.removePeriodicEvent(periodicEvent);
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                FlightRecorder.addListener(frl);
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
            FlightRecorder.removeListener(frl);
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                new MyEvent().commit();
            }, FlightRecorderPermission.class, "registerEvent", true);

    assertPermission(() -> {
                    Configuration.create(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                EventFactory.create(new ArrayList<AnnotationElement>(),
                        new ArrayList<ValueDescriptor>());
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                new AnnotationElement(Name.class, "com.example.HelloWorld");
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
            new ValueDescriptor(MyEvent.class, "",
                    new ArrayList<AnnotationElement>());
            }, FlightRecorderPermission.class, "registerEvent", false);

    assertPermission(() -> {
                new Recording();
            }, FlightRecorderPermission.class, "accessFlightRecorder", false);

    assertPermission(() -> {
                    new RecordingFile(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                    RecordingFile.readAllEvents(Paths.get(protectedLocationPath));
            }, FilePermission.class, protectedLocationPath, false);

    assertPermission(() -> {
                    EventType.getEventType(MyEvent2.class);
            }, FlightRecorderPermission.class, "registerEvent", true);
}
 
Example #28
Source File: TestLargeJavaEvent512k.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void setEventValues(Event event, EventFactory f, EventTypePrototype prototype, Map<String, Object> fields) {
    for (Map.Entry<String, Object> entry : fields.entrySet()) {
        int index = prototype.getFieldIndex(entry.getKey());
        event.set(index, entry.getValue());
    }
}
 
Example #29
Source File: TestLargeJavaEvent512k.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    final String name = "MyLargeJavaEvent512k"; // name of synthetically generated event
    final String fieldNamePrefix = "myfield";
    final int numberOfFields = 64; // 64*8k = 512k event size
    final Map<String, Object> eventMap = new HashMap<>();
    final int numberOfThreads = 10; // 10 threads will run the test
    final int numberOfEventsPerThread = 50; // each thread will generate 50 events

    List<ValueDescriptor> fields = new ArrayList<>();
    for (int i = 0; i < numberOfFields; ++i) {
        String fieldName = fieldNamePrefix + i;
        eventMap.put(fieldName, largeString());
        fields.add(new ValueDescriptor(String.class, fieldName));
    }

    EventTypePrototype prototype = new EventTypePrototype(name,Collections.emptyList(),  fields);

    EventFactory ef = EventFactory.create(prototype.getAnnotations(), prototype.getFields());

    Recording r = new Recording();
    r.enable(prototype.getName()).withThreshold(Duration.ofNanos(0)).withoutStackTrace();
    r.start();

    Thread.UncaughtExceptionHandler eh = (t, e) -> TestLargeJavaEvent512k.setError();

    Stressor.execute(numberOfThreads, eh, () -> {
        for (int n = 0; n < numberOfEventsPerThread; ++n) {
            try {
                Event event = ef.newEvent();
                setEventValues(event, ef, prototype, eventMap);
                event.commit();
                Thread.sleep(1);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    });
    r.stop();
    try {
        if (hasError()) {
            throw new RuntimeException("One (or several) of the threads had an exception/error, test failed");
        }
        verifyEvents(r, numberOfThreads, numberOfEventsPerThread, eventMap);
    } finally {
        r.close();
    }
}
 
Example #30
Source File: TestUnsupportedVM.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
    if (Boolean.getBoolean("prepare-recording")) {
        Recording r = new Recording(Configuration.getConfiguration("default"));
        r.start();
        r.stop();
        r.dump(RECORDING_FILE);
        r.close();
        return;
    }

    System.out.println("jdk.jfr.unsupportedvm=" + System.getProperty("jdk.jfr.unsupportedvm"));
    // Class FlightRecorder
    if (FlightRecorder.isAvailable()) {
        throw new AssertionError("JFR should not be available on an unsupported VM");
    }

    if (FlightRecorder.isInitialized()) {
        throw new AssertionError("JFR should not be initialized on an unsupported VM");
    }

    assertIllegalStateException(() -> FlightRecorder.getFlightRecorder());
    assertSwallow(() -> FlightRecorder.addListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.removeListener(new FlightRecorderListener() {}));
    assertSwallow(() -> FlightRecorder.register(MyEvent.class));
    assertSwallow(() -> FlightRecorder.unregister(MyEvent.class));
    assertSwallow(() -> FlightRecorder.addPeriodicEvent(MyEvent.class, new Runnable() { public void run() {} }));
    assertSwallow(() -> FlightRecorder.removePeriodicEvent(new Runnable() { public void run() {} }));

    // Class Configuration
    if (!Configuration.getConfigurations().isEmpty()) {
        throw new AssertionError("Configuration files should not exist on an unsupported VM");
    }
    Path jfcFile = Utils.createTempFile("empty", ".jfr");
    assertIOException(() -> Configuration.getConfiguration("default"));
    assertIOException(() -> Configuration.create(jfcFile));
    assertIOException(() -> Configuration.create(new FileReader(jfcFile.toFile())));

    // Class EventType
    assertInternalError(() -> EventType.getEventType(MyEvent.class));

    // Class EventFactory
    assertInternalError(() -> EventFactory.create(new ArrayList<>(), new ArrayList<>()));

    // Create a static event
    MyEvent myEvent = new MyEvent();
    myEvent.begin();
    myEvent.end();
    myEvent.shouldCommit();
    myEvent.commit();

    // Trigger class initialization failure
    for (Class<?> c : APIClasses) {
        assertNoClassInitFailure(c);
    }

    // jdk.jfr.consumer.*
    // Only run this part of tests if we are on VM
    // that can produce a recording file
    if (Files.exists(RECORDING_FILE)) {
        for(RecordedEvent re : RecordingFile.readAllEvents(RECORDING_FILE)) {
            System.out.println(re);
        }
    }
}