xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/request_cost.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_COMMON_RUNTIME_REQUEST_COST_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_REQUEST_COST_H_
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "absl/container/flat_hash_map.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/time/time.h"
26 
27 namespace tensorflow {
28 
29 // RequestCost collects the costs for processing an rpc request.
30 class RequestCost {
31  public:
32   // Records costs. The inputs should be pairs of cost type and cost.
33   // It's thread-safe, and can be called from different threads.
34   void RecordCost(
35       const std::vector<std::pair<absl::string_view, absl::Duration>>& costs);
36 
37   // Gets all types of costs for processing an rpc request.
38   // It's thread-safe. It's expected to be called at the end of processing an
39   // rpc request, when all the costs have been collected.
40   absl::flat_hash_map<std::string, absl::Duration> GetCosts() const;
41 
42  private:
43   mutable absl::Mutex mutex_;
44   // Map from cost type to cost.
45   absl::flat_hash_map<std::string, absl::Duration> cost_map_
46       ABSL_GUARDED_BY(mutex_);
47 };
48 
49 }  // namespace tensorflow
50 
51 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_REQUEST_COST_H_
52