1 /* Copyright 2016 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 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_UTILS_H_
17 #define TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_UTILS_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/core/framework/graph.pb.h"
23 #include "tensorflow/core/lib/core/errors.h"
24 #include "tensorflow/core/platform/env.h"
25 #include "tensorflow/core/profiler/tfprof_options.h"
26 #include "tensorflow/core/protobuf/config.pb.h"
27 
28 namespace tensorflow {
29 namespace tfprof {
30 string FormatNumber(int64 n);
31 
32 string FormatTime(int64 micros);
33 
34 string FormatMemory(int64 bytes);
35 
36 string FormatShapes(const std::vector<int64>& shapes);
37 
38 tensorflow::Status ParseCmdLine(const string& line, string* cmd,
39                                 tensorflow::tfprof::Options* opts);
40 
41 string StringReplace(const string& str, const string& oldsub,
42                      const string& newsub);
43 
44 template <typename T>
ReadProtoFile(Env * env,const string & fname,T * proto,bool binary_first)45 Status ReadProtoFile(Env* env, const string& fname, T* proto,
46                      bool binary_first) {
47   string out;
48   Status s = ReadFileToString(env, fname, &out);
49   if (!s.ok()) return s;
50 
51   if (binary_first) {
52     if (ReadBinaryProto(tensorflow::Env::Default(), fname, proto).ok()) {
53       return Status();
54     } else if (protobuf::TextFormat::ParseFromString(out, proto)) {
55       return Status();
56     }
57   } else {
58     if (protobuf::TextFormat::ParseFromString(out, proto)) {
59       return Status();
60     } else if (ReadBinaryProto(tensorflow::Env::Default(), fname, proto).ok()) {
61       return Status();
62     }
63   }
64   return errors::InvalidArgument("Cannot parse proto file.");
65 }
66 
67 void PrintHelp();
68 
69 // Generate helper message based on the command and options.
70 string QueryDoc(const string& cmd, const Options& opts);
71 
72 }  // namespace tfprof
73 }  // namespace tensorflow
74 
75 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_TFPROF_UTILS_H_
76