src/jdk.jfr/share/classes/jdk/jfr/internal/RequestEngine.java
changeset 58863 c16ac7a2eba4
parent 52899 325c95779368
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
     1 /*
     1 /*
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    28 import java.security.AccessControlContext;
    28 import java.security.AccessControlContext;
    29 import java.security.AccessController;
    29 import java.security.AccessController;
    30 import java.security.PrivilegedAction;
    30 import java.security.PrivilegedAction;
    31 import java.util.ArrayList;
    31 import java.util.ArrayList;
    32 import java.util.Collection;
    32 import java.util.Collection;
       
    33 import java.util.Iterator;
    33 import java.util.List;
    34 import java.util.List;
    34 import java.util.Objects;
    35 import java.util.Objects;
    35 import java.util.concurrent.CopyOnWriteArrayList;
    36 import java.util.concurrent.CopyOnWriteArrayList;
    36 import java.util.function.Predicate;
    37 import java.util.function.Predicate;
    37 import jdk.jfr.Event;
    38 import jdk.jfr.Event;
    94         }
    95         }
    95     }
    96     }
    96 
    97 
    97     private final static List<RequestHook> entries = new CopyOnWriteArrayList<>();
    98     private final static List<RequestHook> entries = new CopyOnWriteArrayList<>();
    98     private static long lastTimeMillis;
    99     private static long lastTimeMillis;
       
   100     private static long flushInterval = Long.MAX_VALUE;
       
   101     private static long streamDelta;
    99 
   102 
   100     public static void addHook(AccessControlContext acc, PlatformEventType type, Runnable hook) {
   103     public static void addHook(AccessControlContext acc, PlatformEventType type, Runnable hook) {
   101         Objects.requireNonNull(acc);
   104         Objects.requireNonNull(acc);
   102         addHookInternal(acc, type, hook);
   105         addHookInternal(acc, type, hook);
   103     }
   106     }
   207             // to handle time adjustments
   210             // to handle time adjustments
   208             // for example Daylight Savings
   211             // for example Daylight Savings
   209             lastTimeMillis = now;
   212             lastTimeMillis = now;
   210             return 0;
   213             return 0;
   211         }
   214         }
   212         for (RequestHook he : entries) {
   215         Iterator<RequestHook> hookIterator = entries.iterator();
       
   216         while(hookIterator.hasNext()) {
       
   217             RequestHook he = hookIterator.next();
   213             long left = 0;
   218             long left = 0;
   214             PlatformEventType es = he.type;
   219             PlatformEventType es = he.type;
   215             // Not enabled, skip.
   220             // Not enabled, skip.
   216             if (!es.isEnabled() || es.isEveryChunk()) {
   221             if (!es.isEnabled() || es.isEveryChunk()) {
   217                 continue;
   222                 continue;
   226             if (r_delta >= r_period) {
   231             if (r_delta >= r_period) {
   227                 // Bug 9000556 - don't try to compensate
   232                 // Bug 9000556 - don't try to compensate
   228                 // for wait > period
   233                 // for wait > period
   229                 r_delta = 0;
   234                 r_delta = 0;
   230                 he.execute();
   235                 he.execute();
   231                 ;
       
   232             }
   236             }
   233 
   237 
   234             // calculate time left
   238             // calculate time left
   235             left = (r_period - r_delta);
   239             left = (r_period - r_delta);
   236 
   240 
   248 
   252 
   249             if (min == 0 || left < min) {
   253             if (min == 0 || left < min) {
   250                 min = left;
   254                 min = left;
   251             }
   255             }
   252         }
   256         }
       
   257         // Flush should happen after all periodic events has been emitted
       
   258         // Repeat of the above algorithm, but using the stream interval.
       
   259         if (flushInterval != Long.MAX_VALUE) {
       
   260             long r_period = flushInterval;
       
   261             long r_delta = streamDelta;
       
   262             r_delta += delta;
       
   263             if (r_delta >= r_period) {
       
   264                 r_delta = 0;
       
   265                 MetadataRepository.getInstance().flush();
       
   266                 Utils.notifyFlush();
       
   267             }
       
   268             long left = (r_period - r_delta);
       
   269             if (left < 0) {
       
   270                 left = 0;
       
   271             }
       
   272             streamDelta = r_delta;
       
   273             if (min == 0 || left < min) {
       
   274                 min = left;
       
   275             }
       
   276         }
       
   277 
   253         lastTimeMillis = now;
   278         lastTimeMillis = now;
   254         return min;
   279         return min;
   255     }
   280     }
       
   281 
       
   282     static void setFlushInterval(long millis) {
       
   283         // Don't accept shorter interval than 1 s.
       
   284         long interval = millis < 1000 ? 1000  : millis;
       
   285         flushInterval = interval;
       
   286         if (interval < flushInterval) {
       
   287             synchronized (JVM.FILE_DELTA_CHANGE) {
       
   288                 JVM.FILE_DELTA_CHANGE.notifyAll();
       
   289             }
       
   290         }
       
   291     }
   256 }
   292 }