TensorFlow Serving C++ API Documentation
resource_tracker.h
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 #ifndef TENSORFLOW_SERVING_RESOURCES_RESOURCE_TRACKER_H_
17 #define TENSORFLOW_SERVING_RESOURCES_RESOURCE_TRACKER_H_
18 
19 #include <memory>
20 #include <vector>
21 
22 #include "tensorflow_serving/core/loader.h"
23 #include "tensorflow_serving/resources/resource_util.h"
24 
25 namespace tensorflow {
26 namespace serving {
27 
28 // A class that keeps track of the available and spoken-for resources in a
29 // serving system. It can decide whether enough resources are available to load
30 // a new servable.
31 //
32 // This class is not thread-safe.
34  public:
35  static Status Create(const ResourceAllocation& total_resources,
36  std::unique_ptr<ResourceUtil> util,
37  std::unique_ptr<ResourceTracker>* tracker);
38  ~ResourceTracker() = default;
39 
40  // Determines whether enough resources are available to load 'servable', i.e.
41  // is it guaranteed to fit in the gap between the used and total resources?
42  // If so, adds the servable's resource allocation to the used resources and
43  // sets 'success' to true. Otherwise, leaves the used resources unchanged and
44  // sets 'success' to false. Upon encountering illegal data, e.g. if 'servable'
45  // emits an invalid resource estimate, returns an error status.
46  Status ReserveResources(const Loader& servable, bool* success);
47 
48  // Recomputes the used resources from scratch, given every loader whose
49  // servable is either loaded or transitioning to/from being loaded,
50  // specifically:
51  // * servables approved for loading (their resources are reserved),
52  // * servables in the process of loading,
53  // * servables that are currently loaded,
54  // * servables in the process of unloading.
55  Status RecomputeUsedResources(const std::vector<const Loader*>& servables);
56 
57  const ResourceAllocation& total_resources() const { return total_resources_; }
58  const ResourceAllocation& used_resources() const { return used_resources_; }
59 
60  private:
61  ResourceTracker(const ResourceAllocation& total_resources,
62  std::unique_ptr<ResourceUtil> util);
63 
64  // A ResourceUtil object to use for operations and comparisons on allocations.
65  const std::unique_ptr<ResourceUtil> util_;
66 
67  // The total resources the system has. Must be bound. Kept normalized.
68  const ResourceAllocation total_resources_;
69 
70  // The resources currently set aside for servables that are loaded, or
71  // transitioning to/from being loaded. May be bound or unbound. Kept
72  // normalized.
73  //
74  // Under normal conditions, less than or equal to 'total_resources_'.
75  ResourceAllocation used_resources_;
76 
77  TF_DISALLOW_COPY_AND_ASSIGN(ResourceTracker);
78 };
79 
80 } // namespace serving
81 } // namespace tensorflow
82 
83 #endif // TENSORFLOW_SERVING_RESOURCES_RESOURCE_TRACKER_H_