Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.util;
 
 
 
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static java.util.concurrent.TimeUnit.NANOSECONDS;
 
 public class CpuTimer
 {
     private static final ThreadMXBean THREAD_MX_BEAN = ManagementFactory.getThreadMXBean();
 
     private final long wallStartTime;
     private final long cpuStartTime;
     private final long userStartTime;
 
     private long intervalWallStart;
     private long intervalCpuStart;
     private long intervalUserStart;
 
     public CpuTimer()
     {
          = System.nanoTime();
 
          = ;
          = ;
          = ;
     }
 
     public CpuDuration startNewInterval()
     {
         long currentWallTime = System.nanoTime();
         long currentCpuTime = .getCurrentThreadCpuTime();
         long currentUserTime = .getCurrentThreadUserTime();
 
         CpuDuration cpuDuration = new CpuDuration(
                 nanosBetween(currentWallTime),
                 nanosBetween(currentCpuTime),
                 nanosBetween(currentUserTime));
 
          = currentWallTime;
          = currentCpuTime;
          = currentUserTime;
 
         return cpuDuration;
     }
 
     public CpuDuration elapsedIntervalTime()
     {
         long currentWallTime = System.nanoTime();
         long currentCpuTime = .getCurrentThreadCpuTime();
         long currentUserTime = .getCurrentThreadUserTime();
 
         return new CpuDuration(
                 nanosBetween(currentWallTime),
                 nanosBetween(currentCpuTime),
                 nanosBetween(currentUserTime));
     }
 
     public CpuDuration elapsedTime()
     {
         long currentWallTime = System.nanoTime();
         long currentCpuTime = .getCurrentThreadCpuTime();
         long currentUserTime = .getCurrentThreadUserTime();
 
         return new CpuDuration(
                 nanosBetween(currentWallTime),
                 nanosBetween(currentCpuTime),
                 nanosBetween(currentUserTime));
     }
 
     private Duration nanosBetween(long startlong end)
     {
         return new Duration(Math.abs(end - start), );
     }
 
     public static class CpuDuration
     {
         private final Duration wall;
         private final Duration cpu;
         private final Duration user;
 
        public CpuDuration()
        {
            this. = new Duration(0, );
            this. = new Duration(0, );
            this. = new Duration(0, );
        }
        public CpuDuration(Duration wallDuration cpuDuration user)
        {
            this. = wall;
            this. = cpu;
            this. = user;
        }
        public Duration getWall()
        {
            return ;
        }
        public Duration getCpu()
        {
            return ;
        }
        public Duration getUser()
        {
            return ;
        }
        public CpuDuration add(CpuDuration cpuDuration)
        {
            return new CpuDuration(
                    addDurations(cpuDuration.wall),
                    addDurations(cpuDuration.cpu),
                    addDurations(cpuDuration.user));
        }
        public CpuDuration subtract(CpuDuration cpuDuration)
        {
            return new CpuDuration(
                    subtractDurations(cpuDuration.wall),
                    subtractDurations(cpuDuration.cpu),
                    subtractDurations(cpuDuration.user));
        }
        private static Duration addDurations(Duration aDuration b)
        {
            return new Duration(a.getValue() + b.getValue(), );
        }
        private static Duration subtractDurations(Duration aDuration b)
        {
            return new Duration(Math.max(0, a.getValue() - b.getValue()), );
        }
        @Override
        public String toString()
        {
            return toStringHelper(this)
                    .add("wall")
                    .add("cpu")
                    .add("user")
                    .toString();
        }
    }
New to GrepCode? Check out our FAQ X