1 /*
   2  * Copyright (c) 2014, 2015, Dynatrace and/or its affiliates. All rights reserved.
   3  * 
   4  * This file is part of the Lock Contention Tracing Subsystem for the HotSpot
   5  * Virtual Machine, which is developed at Christian Doppler Laboratory on
   6  * Monitoring and Evolution of Very-Large-Scale Software Systems. Please
   7  * contact us at <http://mevss.jku.at/> if you need additional information
   8  * or have any questions.
   9  *
  10  * This code is free software; you can redistribute it and/or modify it
  11  * under the terms of the GNU General Public License version 2 only, as
  12  * published by the Free Software Foundation.
  13  *
  14  * This code is distributed in the hope that it will be useful, but WITHOUT
  15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17  * version 2 for more details (a copy is included in the LICENSE file that
  18  * accompanied this code).
  19  *
  20  * You should have received a copy of the GNU General Public License version
  21  * 2 along with this work. If not, see <http://www.gnu.org/licenses/>.
  22  *
  23  */ 
  24 package sun.evtracing.processing.statistics.aggregator;
  25 
  26 import sun.evtracing.processing.statistics.metadata.Contention;
  27 
  28 public class TimeSpanContentionFilter implements ContentionProcessor {
  29 
  30         public static final long UNRESTRICTED = -1;
  31 
  32         private final ContentionProcessor delegate;
  33         private final long begin;
  34         private final long end;
  35 
  36         public TimeSpanContentionFilter(ContentionProcessor delegate, long begin, long end) {
  37                 assert begin == UNRESTRICTED || begin >= 0;
  38                 assert end == UNRESTRICTED || end >= 0;
  39                 assert (begin == UNRESTRICTED || end == UNRESTRICTED || begin <= end);
  40 
  41                 this.delegate = delegate;
  42                 this.begin = begin;
  43                 this.end = end;
  44         }
  45 
  46         @Override
  47         public void submit(Contention c) {
  48                 if (end == UNRESTRICTED || c.startTime < end) {
  49                         long endTime = c.startTime + c.duration;
  50                         if (begin == UNRESTRICTED || endTime > begin) {
  51                                 // truncate
  52                                 long adjStartTime = c.startTime;
  53                                 if (begin != UNRESTRICTED && c.startTime < begin) {
  54                                         adjStartTime = begin;
  55                                 }
  56                                 long adjEndTime = endTime;
  57                                 if (end != UNRESTRICTED && endTime > end) {
  58                                         adjEndTime = end;
  59                                 }
  60                                 if (adjStartTime != c.startTime || adjEndTime != endTime) {
  61                                         c = new Contention(c.group, c.contendingThread, c.ownerThread, c.contendingSite, c.ownerSite, c.blocker, adjStartTime, adjEndTime - adjStartTime);
  62                                 }
  63 
  64                                 delegate.submit(c);
  65                         }
  66                 }
  67         }
  68 
  69         @Override
  70         public void merge() {
  71                 delegate.merge();
  72         }
  73 
  74 }