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.settings;
  27 
  28 import java.util.Objects;
  29 import java.util.Set;
  30 
  31 import jdk.jfr.Description;
  32 import jdk.jfr.Label;
  33 import jdk.jfr.MetadataDefinition;
  34 import jdk.jfr.Name;
  35 import jdk.jfr.internal.PlatformEventType;
  36 import jdk.jfr.internal.Control;
  37 import jdk.jfr.internal.Type;
  38 import jdk.jfr.internal.Utils;
  39 
  40 @MetadataDefinition
  41 @Label("Period")
  42 @Description("Record event at interval")
  43 @Name(Type.SETTINGS_PREFIX + "Period")
  44 public final class PeriodSetting extends Control {
  45     private static final long typeId = Type.getTypeId(PeriodSetting.class);
  46 
  47     public static final String EVERY_CHUNK = "everyChunk";
  48     public static final String BEGIN_CHUNK = "beginChunk";
  49     public static final String END_CHUNK = "endChunk";
  50     public static final String NAME = "period";
  51     private final PlatformEventType eventType;
  52     private String value = EVERY_CHUNK;
  53 
  54     public PeriodSetting(PlatformEventType eventType, String defaultValue) {
  55         super(defaultValue);
  56         this.eventType = Objects.requireNonNull(eventType);
  57     }
  58 
  59     @Override
  60     public String combine(Set<String> values) {
  61 
  62         boolean beginChunk = false;
  63         boolean endChunk = false;
  64         Long min = null;
  65         String text = null;
  66         for (String value : values) {
  67             switch (value) {
  68             case EVERY_CHUNK:
  69                 beginChunk = true;
  70                 endChunk = true;
  71                 break;
  72             case BEGIN_CHUNK:
  73                 beginChunk = true;
  74                 break;
  75             case END_CHUNK:
  76                 endChunk = true;
  77                 break;
  78             default:
  79                 long l = Utils.parseTimespanWithInfinity(value);
  80                 // Always accept first specified value
  81                 if (min == null) {
  82                     text = value;
  83                     min = l;
  84                 } else {
  85                     if (l < min) {
  86                         text = value;
  87                         min = l;
  88                     }
  89                 }
  90             }
  91         }
  92         // A specified interval trumps *_CHUNK
  93         if (min != null) {
  94             return text;
  95         }
  96         if (beginChunk && !endChunk) {
  97             return BEGIN_CHUNK;
  98         }
  99         if (!beginChunk && endChunk) {
 100             return END_CHUNK;
 101         }
 102         return EVERY_CHUNK; // also default
 103     }
 104 
 105     @Override
 106     public void setValue(String value) {
 107         switch (value) {
 108         case EVERY_CHUNK:
 109             eventType.setPeriod(0, true, true);
 110             break;
 111         case BEGIN_CHUNK:
 112             eventType.setPeriod(0, true, false);
 113             break;
 114         case END_CHUNK:
 115             eventType.setPeriod(0, false, true);
 116             break;
 117         default:
 118             long nanos = Utils.parseTimespanWithInfinity(value);
 119             if (nanos != Long.MAX_VALUE) {
 120                 eventType.setPeriod(nanos / 1_000_000, false, false);
 121             } else {
 122                 eventType.setPeriod(Long.MAX_VALUE, false, false);
 123             }
 124         }
 125         this.value = value;
 126     }
 127 
 128     @Override
 129     public String getValue() {
 130         return value;
 131     }
 132 
 133     public static boolean isType(long typeId) {
 134         return PeriodSetting.typeId == typeId;
 135     }
 136 }