1 /*
   2  * Copyright (c) 2016, 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.internal.handlers;
  27 
  28 import java.io.IOException;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 
  32 import jdk.jfr.EventType;
  33 import jdk.jfr.internal.EventControl;
  34 import jdk.jfr.internal.JVM;
  35 import jdk.jfr.internal.PlatformEventType;
  36 import jdk.jfr.internal.PrivateAccess;
  37 import jdk.jfr.internal.StringPool;
  38 
  39 // Users should not be subclass for security reasons.
  40 public abstract class EventHandler {
  41     // Accessed by generated sub class
  42     protected final PlatformEventType platformEventType;
  43 
  44     private final EventType eventType;
  45     private final EventControl eventControl;
  46 
  47     // Accessed by generated sub class
  48     EventHandler(boolean registered, EventType eventType, EventControl eventControl) {
  49         this.eventType = eventType;
  50         this.platformEventType = PrivateAccess.getInstance().getPlatformEventType(eventType);
  51         this.eventControl = eventControl;
  52         platformEventType.setRegistered(registered);
  53     }
  54 
  55     final protected StringPool createStringFieldWriter() {
  56         return new StringPool();
  57     }
  58 
  59     // Accessed by generated code in event class
  60     public final boolean shouldCommit(long duration) {
  61         return isEnabled() && duration >= platformEventType.getThresholdTicks();
  62     }
  63 
  64     // Accessed by generated code in event class
  65     // Accessed by generated sub class
  66     public final boolean isEnabled() {
  67         return platformEventType.isCommitable();
  68     }
  69 
  70     public final EventType getEventType() {
  71         return eventType;
  72     }
  73 
  74     public final PlatformEventType getPlatformEventType() {
  75         return platformEventType;
  76     }
  77 
  78     public final EventControl getEventControl() {
  79         return eventControl;
  80     }
  81 
  82     public static long timestamp() {
  83         return JVM.counterTime();
  84     }
  85 
  86     public static long duration(long startTime) {
  87         if (startTime == 0) {
  88             // User forgot to invoke begin, or instrumentation was
  89             // added after the user invoked begin.
  90             // Returning 0 will make it an instant event
  91             return 0;
  92         }
  93         return timestamp() - startTime;
  94     }
  95 
  96     // Prevent a malicious user from instantiating a generated event handlers.
  97     @Override
  98     public final Object clone() throws java.lang.CloneNotSupportedException {
  99         throw new CloneNotSupportedException();
 100     }
 101 
 102     private final void writeObject(ObjectOutputStream out) throws IOException {
 103         throw new IOException("Object cannot be serialized");
 104     }
 105 
 106     private final void readObject(ObjectInputStream in) throws IOException {
 107         throw new IOException("Class cannot be deserialized");
 108     }
 109 
 110     public boolean isRegistered() {
 111         return platformEventType.isRegistered();
 112     }
 113 
 114     public boolean setRegistered(boolean registered) {
 115        return platformEventType.setRegistered(registered);
 116     }
 117 }