1 /* Copyright 2019 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_UTILS_OP_METRICS_DB_UTILS_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_OP_METRICS_DB_UTILS_H_
18 
19 #include <string>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "absl/strings/string_view.h"
23 #include "tensorflow/core/platform/macros.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
26 
27 namespace tensorflow {
28 namespace profiler {
29 
30 // The name of OpMetrics to represent the idle time.
31 TF_CONST_INIT extern const absl::string_view kIdle;
32 
33 // Helps build an op metrics database (borrowed).
34 // Enables fast lookup of existing ops and prevents the creation of duplicate
35 // ops. It is the user's responsibility to ensure an op metrics database
36 // outlives its builder, and that no ops are added to the database outside of
37 // the builder.
38 class OpMetricsDbBuilder {
39  public:
40   // Create with a borrowed op database.
41   // REQUIRED: The op database must be empty.
42   explicit OpMetricsDbBuilder(OpMetricsDb* db);
43 
44  protected:
45   // Looks up the given OP name. If it is already in the database,
46   // return its OpMetrics; otherwise, insert a new one.
47   OpMetrics* LookupOrInsertNewOpMetrics(uint64 hlo_module_id,
48                                         absl::string_view name);
49 
db()50   OpMetricsDb* db() { return db_; }
51 
52  private:
53   // Map op (hlo_module_id, name) to the corresponding metrics in the op
54   // database.
55   absl::flat_hash_map<uint64 /*hlo_module_id*/,
56                       absl::flat_hash_map<std::string /*name*/, OpMetrics*>>
57       op_metrics_map_;
58 
59   // The op database.
60   OpMetricsDb* db_;
61 };
62 
63 // Returns the ratio of time that is idle (no op execution) over total time.
64 double IdleTimeRatio(const OpMetricsDb& metrics_db);
65 
66 // Returns the idle time in picoseconds.
67 uint64 IdleTimePs(const OpMetricsDb& metrics_db);
68 
69 // Adds an op representing idle time, i.e., the amount of time spent without any
70 // op execution.
71 // REQUIRED: All ops must have been added to the database and the total time
72 // must have been set.
73 void AddIdleOp(OpMetricsDb* db);
74 
75 // Returns true if the given metrics represents idle time.
IsIdleOp(const OpMetrics & metrics)76 inline bool IsIdleOp(const OpMetrics& metrics) {
77   return metrics.name() == kIdle;
78 }
79 
80 // Converts from the device op metrics to Tf-op metrics.
81 OpMetricsDb CreateTfMetricsDbFromDeviceOpMetricsDb(
82     const OpMetricsDb& device_op_metrics_db, bool with_idle = true);
83 }  // namespace profiler
84 }  // namespace tensorflow
85 
86 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_OP_METRICS_DB_UTILS_H_
87