1 /*
   2  * Copyright (c) 2014, 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 package jdk.vm.ci.hotspot.services;
  24 
  25 import jdk.vm.ci.hotspot.services.EmptyEventProvider.EmptyCompilationEvent;
  26 import jdk.vm.ci.hotspot.services.EmptyEventProvider.EmptyCompilerFailureEvent;
  27 import jdk.vm.ci.services.JVMCIPermission;
  28 
  29 /**
  30  * Service-provider class for logging compiler related events.
  31  */
  32 public abstract class EventProvider {
  33 
  34     private static Void checkPermission() {
  35         SecurityManager sm = System.getSecurityManager();
  36         if (sm != null) {
  37             sm.checkPermission(new JVMCIPermission());
  38         }
  39         return null;
  40     }
  41 
  42     @SuppressWarnings("unused")
  43     EventProvider(Void ignore) {
  44     }
  45 
  46     /**
  47      * Initializes a new instance of this class.
  48      *
  49      * @throws SecurityException if a security manager has been installed and it denies
  50      *             {@link JVMCIPermission}
  51      */
  52     protected EventProvider() {
  53         this(checkPermission());
  54     }
  55 
  56     /**
  57      * Creates and returns an empty implementation for {@link EventProvider}. This implementation
  58      * can be used when no logging is requested.
  59      */
  60     public static EventProvider createEmptyEventProvider() {
  61         return new EmptyEventProvider();
  62     }
  63 
  64     /**
  65      * Creates and returns an empty implementation for {@link CompilationEvent}.
  66      */
  67     public static CompilationEvent createEmptyCompilationEvent() {
  68         return new EmptyCompilationEvent();
  69     }
  70 
  71     /**
  72      * Creates and returns an empty implementation for {@link CompilationEvent}.
  73      */
  74     public static CompilerFailureEvent createEmptyCompilerFailureEvent() {
  75         return new EmptyCompilerFailureEvent();
  76     }
  77 
  78     /**
  79      * An instant event is an event that is not considered to have taken any time.
  80      */
  81     public interface InstantEvent {
  82         /**
  83          * Commits the event.
  84          */
  85         void commit();
  86 
  87         /**
  88          * Determines if this particular event instance would be committed to the data stream right
  89          * now if application called {@link #commit()}. This in turn depends on whether the event is
  90          * enabled and possible other factors.
  91          *
  92          * @return if this event would be committed on a call to {@link #commit()}.
  93          */
  94         boolean shouldWrite();
  95     }
  96 
  97     /**
  98      * Timed events describe an operation that somehow consumes time.
  99      */
 100     public interface TimedEvent extends InstantEvent {
 101         /**
 102          * Starts the timing for this event.
 103          */
 104         void begin();
 105 
 106         /**
 107          * Ends the timing period for this event.
 108          */
 109         void end();
 110     }
 111 
 112     /**
 113      * Creates a new {@link CompilationEvent}.
 114      *
 115      * @return a compilation event
 116      */
 117     public abstract CompilationEvent newCompilationEvent();
 118 
 119     /**
 120      * A compilation event.
 121      */
 122     public interface CompilationEvent extends TimedEvent {
 123         void setMethod(String method);
 124 
 125         void setCompileId(int compileId);
 126 
 127         void setCompileLevel(int compileLevel);
 128 
 129         void setSucceeded(boolean succeeded);
 130 
 131         void setIsOsr(boolean isOsr);
 132 
 133         void setCodeSize(int codeSize);
 134 
 135         void setInlinedBytes(int inlinedBytes);
 136     }
 137 
 138     /**
 139      * Creates a new {@link CompilerFailureEvent}.
 140      *
 141      * @return a compiler failure event
 142      */
 143     public abstract CompilerFailureEvent newCompilerFailureEvent();
 144 
 145     /**
 146      * A compiler failure event.
 147      */
 148     public interface CompilerFailureEvent extends InstantEvent {
 149         void setCompileId(int compileId);
 150 
 151         void setMessage(String message);
 152     }
 153 }