TensorFlow Serving C++ API Documentation
resource_tracker.cc
1 /* Copyright 2016 Google Inc. 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_serving/resources/resource_tracker.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/lib/core/status.h"
26 #include "tensorflow_serving/resources/resources.pb.h"
27 
28 namespace tensorflow {
29 namespace serving {
30 
31 Status ResourceTracker::Create(const ResourceAllocation& total_resources,
32  std::unique_ptr<ResourceUtil> util,
33  std::unique_ptr<ResourceTracker>* tracker) {
34  TF_RETURN_IF_ERROR(util->VerifyValidity(total_resources));
35  const ResourceAllocation normalized_total_resources =
36  util->Normalize(total_resources);
37  if (!util->IsBound(normalized_total_resources)) {
38  return errors::InvalidArgument("total_resources must be bound: ",
39  total_resources.DebugString());
40  }
41  tracker->reset(
42  new ResourceTracker(normalized_total_resources, std::move(util)));
43  return Status();
44 }
45 
46 Status ResourceTracker::ReserveResources(const Loader& servable,
47  bool* success) {
48  ResourceAllocation servable_resources;
49  TF_RETURN_IF_ERROR(servable.EstimateResources(&servable_resources));
50  TF_RETURN_IF_ERROR(util_->VerifyValidity(servable_resources));
51 
52  ResourceAllocation conservative_proposed_used_resources =
53  util_->Overbind(used_resources_);
54  util_->Add(servable_resources, &conservative_proposed_used_resources);
55 
56  if (util_->LessThanOrEqual(conservative_proposed_used_resources,
57  total_resources_)) {
58  util_->Add(servable_resources, &used_resources_);
59  *success = true;
60  } else {
61  LOG(WARNING) << "Insufficient resources to load servable "
62  << "\ntotal resources:\n"
63  << total_resources_.DebugString()
64  << "used/reserved resources:\n"
65  << used_resources_.DebugString()
66  << "resources requested by servable:\n"
67  << servable_resources.DebugString();
68  *success = false;
69  }
70 
71  return Status();
72 }
73 
74 Status ResourceTracker::RecomputeUsedResources(
75  const std::vector<const Loader*>& servables) {
76  used_resources_.Clear();
77  for (const Loader* servable : servables) {
78  ResourceAllocation servable_resources;
79  TF_RETURN_IF_ERROR(servable->EstimateResources(&servable_resources));
80  TF_RETURN_IF_ERROR(util_->VerifyValidity(servable_resources));
81  util_->Add(servable_resources, &used_resources_);
82  }
83  return Status();
84 }
85 
86 ResourceTracker::ResourceTracker(const ResourceAllocation& total_resources,
87  std::unique_ptr<ResourceUtil> util)
88  : util_(std::move(util)), total_resources_(total_resources) {}
89 
90 } // namespace serving
91 } // namespace tensorflow