aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/android/jni/run_stats_jni.cc
blob: 707853b59befc2625145ad96952fbf9f66d62b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

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.
==============================================================================*/

#include "tensorflow/contrib/android/jni/run_stats_jni.h"

#include <jni.h>
#include <sstream>

#include "tensorflow/core/protobuf/config.pb.h"
#include "tensorflow/core/util/stat_summarizer.h"

using tensorflow::RunMetadata;
using tensorflow::StatSummarizer;

namespace {
StatSummarizer* requireHandle(JNIEnv* env, jlong handle) {
  if (handle == 0) {
    env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
                  "close() has been called on the RunStats object");
    return nullptr;
  }
  return reinterpret_cast<StatSummarizer*>(handle);
}
}  // namespace

#define RUN_STATS_METHOD(name) \
  JNICALL Java_org_tensorflow_contrib_android_RunStats_##name

JNIEXPORT jlong RUN_STATS_METHOD(allocate)(JNIEnv* env, jclass clazz) {
  static_assert(sizeof(jlong) >= sizeof(StatSummarizer*),
                "Cannot package C++ object pointers as a Java long");
  tensorflow::StatSummarizerOptions opts;
  return reinterpret_cast<jlong>(new StatSummarizer(opts));
}

JNIEXPORT void RUN_STATS_METHOD(delete)(JNIEnv* env, jclass clazz,
                                        jlong handle) {
  if (handle == 0) return;
  delete reinterpret_cast<StatSummarizer*>(handle);
}

JNIEXPORT void RUN_STATS_METHOD(add)(JNIEnv* env, jclass clazz, jlong handle,
                                     jbyteArray run_metadata) {
  StatSummarizer* s = requireHandle(env, handle);
  if (s == nullptr) return;
  jbyte* data = env->GetByteArrayElements(run_metadata, nullptr);
  int size = static_cast<int>(env->GetArrayLength(run_metadata));
  tensorflow::RunMetadata proto;
  if (!proto.ParseFromArray(data, size)) {
    env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"),
                  "runMetadata does not seem to be a serialized RunMetadata "
                  "protocol message");
  } else if (proto.has_step_stats()) {
    s->ProcessStepStats(proto.step_stats());
  }
  env->ReleaseByteArrayElements(run_metadata, data, JNI_ABORT);
}

JNIEXPORT jstring RUN_STATS_METHOD(summary)(JNIEnv* env, jclass clazz,
                                            jlong handle) {
  StatSummarizer* s = requireHandle(env, handle);
  if (s == nullptr) return nullptr;
  std::stringstream ret;
  ret << s->GetStatsByMetric("Top 10 CPU", StatSummarizer::BY_TIME, 10)
      << s->GetStatsByNodeType() << s->ShortSummary();
  return env->NewStringUTF(ret.str().c_str());
}

#undef RUN_STATS_METHOD