Home | History | Annotate | Download | only in android
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 package org.tensorflow.contrib.android;
     17 
     18 /** Accumulate and analyze stats from metadata obtained from Session.Runner.run. */
     19 public class RunStats implements AutoCloseable {
     20 
     21   /**
     22    * Options to be provided to a {@link org.tensorflow.Session.Runner} to enable stats accumulation.
     23    */
     24   public static byte[] runOptions() {
     25     return fullTraceRunOptions;
     26   }
     27 
     28   public RunStats() {
     29     nativeHandle = allocate();
     30   }
     31 
     32   @Override
     33   public void close() {
     34     if (nativeHandle != 0) {
     35       delete(nativeHandle);
     36     }
     37     nativeHandle = 0;
     38   }
     39 
     40   /** Accumulate stats obtained when executing a graph. */
     41   public synchronized void add(byte[] runMetadata) {
     42     add(nativeHandle, runMetadata);
     43   }
     44 
     45   /** Summary of the accumulated runtime stats. */
     46   public synchronized String summary() {
     47     return summary(nativeHandle);
     48   }
     49 
     50   private long nativeHandle;
     51 
     52   // Hack: This is what a serialized RunOptions protocol buffer with trace_level: FULL_TRACE ends
     53   // up as.
     54   private static byte[] fullTraceRunOptions = new byte[] {0x08, 0x03};
     55 
     56   private static native long allocate();
     57 
     58   private static native void delete(long handle);
     59 
     60   private static native void add(long handle, byte[] runMetadata);
     61 
     62   private static native String summary(long handle);
     63 }
     64