• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/c/eager/c_api_experimental.h"
17 
18 #include "tensorflow/c/c_api.h"
19 #include "tensorflow/c/eager/c_api_internal.h"
20 #include "tensorflow/core/profiler/rpc/client/capture_profile.h"
21 #include "tensorflow/core/profiler/rpc/profiler_server.h"
22 
23 using tensorflow::string;
24 
TFE_OpConsumeInput(TFE_Op * op,TFE_TensorHandle * h,TF_Status * status)25 void TFE_OpConsumeInput(TFE_Op* op, TFE_TensorHandle* h, TF_Status* status) {
26   op->operation.ConsumeInput(h->handle);
27 }
28 
TFE_NewProfiler(TFE_ProfilerContext * ctx)29 TFE_Profiler* TFE_NewProfiler(TFE_ProfilerContext* ctx) {
30   return new TFE_Profiler(ctx);
31 }
32 
TFE_ProfilerIsOk(TFE_Profiler * profiler)33 bool TFE_ProfilerIsOk(TFE_Profiler* profiler) {
34   return profiler->profiler->Status().ok();
35 }
36 
TFE_DeleteProfiler(TFE_Profiler * profiler)37 void TFE_DeleteProfiler(TFE_Profiler* profiler) { delete profiler; }
38 
TFE_ProfilerSerializeToString(TFE_Context * ctx,TFE_Profiler * profiler,TF_Buffer * buf,TF_Status * status)39 void TFE_ProfilerSerializeToString(TFE_Context* ctx, TFE_Profiler* profiler,
40                                    TF_Buffer* buf, TF_Status* status) {
41   TFE_ContextAsyncWait(ctx, status);
42   if (TF_GetCode(status) != TF_OK) return;
43   string content;
44   status->status = profiler->profiler->SerializeToString(&content);
45   void* data = tensorflow::port::Malloc(content.length());
46   content.copy(static_cast<char*>(data), content.length(), 0);
47   buf->data = data;
48   buf->length = content.length();
49   buf->data_deallocator = [](void* data, size_t length) {
50     tensorflow::port::Free(data);
51   };
52 }
53 
TFE_NewProfilerContext()54 TFE_ProfilerContext* TFE_NewProfilerContext() {
55   return new TFE_ProfilerContext;
56 }
57 
TFE_ProfilerContextSetEagerContext(TFE_ProfilerContext * profiler_context,TFE_Context * eager_context)58 void TFE_ProfilerContextSetEagerContext(TFE_ProfilerContext* profiler_context,
59                                         TFE_Context* eager_context) {
60   profiler_context->profiler_context.eager_context = &eager_context->context;
61 }
62 
TFE_DeleteProfilerContext(TFE_ProfilerContext * profiler_context)63 void TFE_DeleteProfilerContext(TFE_ProfilerContext* profiler_context) {
64   delete profiler_context;
65 }
66 
TFE_StartProfilerServer(TFE_ProfilerContext * context,int port)67 void TFE_StartProfilerServer(TFE_ProfilerContext* context, int port) {
68   // Release child thread intentionally. The child thread can be terminate by
69   // terminating the main thread.
70   tensorflow::StartProfilerServer(&context->profiler_context, port).release();
71 }
72 
TFE_ContextEnableGraphCollection(TFE_Context * ctx)73 void TFE_ContextEnableGraphCollection(TFE_Context* ctx) {
74   ctx->context.SetShouldStoreGraphs(true);
75 }
76 
TFE_ContextDisableGraphCollection(TFE_Context * ctx)77 void TFE_ContextDisableGraphCollection(TFE_Context* ctx) {
78   ctx->context.SetShouldStoreGraphs(false);
79 }
80 
TFE_ProfilerClientStartTracing(const char * service_addr,const char * logdir,const char * worker_list,bool include_dataset_ops,int duration_ms,int num_tracing_attempts)81 bool TFE_ProfilerClientStartTracing(const char* service_addr,
82                                     const char* logdir, const char* worker_list,
83                                     bool include_dataset_ops, int duration_ms,
84                                     int num_tracing_attempts) {
85   tensorflow::Status s =
86       tensorflow::profiler::client::ValidateHostPortPair(service_addr);
87   if (!s.ok()) {
88     return false;
89   }
90   s = tensorflow::profiler::client::StartTracing(
91       service_addr, logdir, worker_list, include_dataset_ops, duration_ms,
92       num_tracing_attempts);
93   return s.ok();
94 }
95