1 /*
   2  * Copyright (c) 2013, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.event.runtime;
  27 
  28 import static jdk.test.lib.Asserts.assertEquals;
  29 import static jdk.test.lib.Asserts.assertTrue;
  30 
  31 import java.lang.management.ManagementFactory;
  32 
  33 import com.sun.management.HotSpotDiagnosticMXBean;
  34 
  35 import jdk.jfr.Recording;
  36 import jdk.jfr.consumer.RecordedEvent;
  37 import jdk.test.lib.jfr.EventNames;
  38 import jdk.test.lib.jfr.Events;
  39 
  40 /**
  41  * @test TestVmFlagChangedEvent
  42  * @key jfr
  43  *
  44  * @library /lib /
  45  *
  46  *
  47  * @run main/othervm jdk.jfr.event.runtime.TestVmFlagChangedEvent
  48  */
  49 public final class TestVmFlagChangedEvent {
  50 
  51     public static void main(String[] args) throws Throwable {
  52         EventFlag[] eventFlags = {
  53             new EventFlag(EventNames.LongFlagChanged, "CMSWaitDuration", "2500"),
  54             new EventFlag(EventNames.StringFlagChanged, "HeapDumpPath", "/a/sample/path"),
  55             new EventFlag(EventNames.BooleanFlagChanged, "HeapDumpOnOutOfMemoryError", "true")
  56         };
  57 
  58         Recording recording = new Recording();
  59         for (EventFlag eventFlag : eventFlags) {
  60             recording.enable(eventFlag.eventName);
  61         }
  62 
  63         recording.start();
  64         HotSpotDiagnosticMXBean mbean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  65         for (EventFlag eventFlag : eventFlags) {
  66             eventFlag.update(mbean);
  67         }
  68         recording.stop();
  69 
  70         for (RecordedEvent event : Events.fromRecording(recording)) {
  71             final String flagName = Events.assertField(event, "name").getValue();
  72             System.out.println("flag name=" + flagName);
  73             for (EventFlag eventFlag : eventFlags) {
  74                 if (flagName.equals(eventFlag.eventLabel)) {
  75                     System.out.println("Event:" + event);
  76                     Object newValue = Events.assertField(event, "newValue").getValue();
  77                     Object oldValue = Events.assertField(event, "oldValue").getValue();
  78                     System.out.println("newValue:" + asText(newValue));
  79                     System.out.println("oldValue:" + asText(oldValue));
  80                     assertEquals(eventFlag.newValue, asText(newValue), "Wrong new value: expected" + eventFlag.newValue);
  81                     assertEquals(eventFlag.oldValue, asText(oldValue), "Wrong old value: expected" + eventFlag.oldValue);
  82                     Events.assertField(event, "origin").equal("Management");
  83                     eventFlag.isFound = true;
  84                     break;
  85                 }
  86             }
  87         }
  88         for (EventFlag eventFlag : eventFlags) {
  89             assertTrue(eventFlag.isFound, "Missing flag change for: " + eventFlag.eventLabel);
  90         }
  91     }
  92 
  93     private static String asText(Object value) {
  94         if (value == null) {
  95             return ""; // HotSpotDiagnosticMXBean interface return "" for unset values
  96         }
  97         return String.valueOf(value);
  98     }
  99 
 100     private static class EventFlag {
 101         final String eventName;
 102         final String eventLabel;
 103         String newValue;
 104         String oldValue;
 105         boolean isFound = false;
 106 
 107         EventFlag(String eventName, String eventLabel, String newValue) {
 108             this.eventName = eventName;
 109             this.eventLabel = eventLabel;
 110             this.newValue = newValue;
 111         }
 112 
 113         void update(HotSpotDiagnosticMXBean mbean) {
 114             this.oldValue = mbean.getVMOption(eventLabel).getValue();
 115             mbean.setVMOption(eventLabel, newValue);
 116         }
 117     }
 118 }