1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/contrib/android/jni/run_stats_jni.h"
17
18#include <jni.h>
19#include <sstream>
20
21#include "tensorflow/core/protobuf/config.pb.h"
22#include "tensorflow/core/util/stat_summarizer.h"
23
24using tensorflow::RunMetadata;
25using tensorflow::StatSummarizer;
26
27namespace {
28StatSummarizer* requireHandle(JNIEnv* env, jlong handle) {
29  if (handle == 0) {
30    env->ThrowNew(env->FindClass("java/lang/IllegalStateException"),
31                  "close() has been called on the RunStats object");
32    return nullptr;
33  }
34  return reinterpret_cast<StatSummarizer*>(handle);
35}
36}  // namespace
37
38#define RUN_STATS_METHOD(name) \
39  JNICALL Java_org_tensorflow_contrib_android_RunStats_##name
40
41JNIEXPORT jlong RUN_STATS_METHOD(allocate)(JNIEnv* env, jclass clazz) {
42  static_assert(sizeof(jlong) >= sizeof(StatSummarizer*),
43                "Cannot package C++ object pointers as a Java long");
44  tensorflow::StatSummarizerOptions opts;
45  return reinterpret_cast<jlong>(new StatSummarizer(opts));
46}
47
48JNIEXPORT void RUN_STATS_METHOD(delete)(JNIEnv* env, jclass clazz,
49                                        jlong handle) {
50  if (handle == 0) return;
51  delete reinterpret_cast<StatSummarizer*>(handle);
52}
53
54JNIEXPORT void RUN_STATS_METHOD(add)(JNIEnv* env, jclass clazz, jlong handle,
55                                     jbyteArray run_metadata) {
56  StatSummarizer* s = requireHandle(env, handle);
57  if (s == nullptr) return;
58  jbyte* data = env->GetByteArrayElements(run_metadata, nullptr);
59  int size = static_cast<int>(env->GetArrayLength(run_metadata));
60  tensorflow::RunMetadata proto;
61  if (!proto.ParseFromArray(data, size)) {
62    env->ThrowNew(env->FindClass("java/lang/IllegalArgumentException"),
63                  "runMetadata does not seem to be a serialized RunMetadata "
64                  "protocol message");
65  } else if (proto.has_step_stats()) {
66    s->ProcessStepStats(proto.step_stats());
67  }
68  env->ReleaseByteArrayElements(run_metadata, data, JNI_ABORT);
69}
70
71JNIEXPORT jstring RUN_STATS_METHOD(summary)(JNIEnv* env, jclass clazz,
72                                            jlong handle) {
73  StatSummarizer* s = requireHandle(env, handle);
74  if (s == nullptr) return nullptr;
75  std::stringstream ret;
76  ret << s->GetStatsByMetric("Top 10 CPU", StatSummarizer::BY_TIME, 10)
77      << s->GetStatsByNodeType() << s->ShortSummary();
78  return env->NewStringUTF(ret.str().c_str());
79}
80
81#undef RUN_STATS_METHOD
82