1 /*
2  ** Copyright 2011, The Android Open Source Project
3  **
4  ** Licensed under the Apache License, Version 2.0 (the "License");
5  ** you may not use this file except in compliance with the License.
6  ** You may obtain a copy of the License at
7  **
8  **     http://www.apache.org/licenses/LICENSE-2.0
9  **
10  ** Unless required by applicable law or agreed to in writing, software
11  ** distributed under the License is distributed on an "AS IS" BASIS,
12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  ** See the License for the specific language governing permissions and
14  ** limitations under the License.
15  */
16 
17 #ifndef ANDROID_FRAMEWORKS_ML_NN_DRIVER_CACHE_NN_CACHE_NN_CACHE_H
18 #define ANDROID_FRAMEWORKS_ML_NN_DRIVER_CACHE_NN_CACHE_NN_CACHE_H
19 
20 #include "BlobCache.h"
21 
22 #include <functional>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26 
27 // ----------------------------------------------------------------------------
28 namespace android {
29 // ----------------------------------------------------------------------------
30 
31 class NNCache {
32    public:
33     typedef BlobCache::Select Select;
34     typedef BlobCache::Capacity Capacity;
35     typedef BlobCache::Policy Policy;
36 
defaultPolicy()37     static Policy defaultPolicy() { return BlobCache::defaultPolicy(); }
38 
39     // get returns a pointer to the singleton NNCache object.  This
40     // singleton object will never be destroyed.
41     static NNCache* get();
42 
43     // initialize puts the NNCache into an initialized state, such
44     // that it is able to insert and retrieve entries from the cache.
45     // When not in the initialized state the getBlob and setBlob
46     // methods will return without performing any cache operations.
47     //
48     // The NNCache will cache key/value pairs with key and value sizes
49     // less than or equal to maxKeySize and maxValueSize,
50     // respectively. The total combined size of ALL cache entries (key
51     // sizes plus value sizes) will not exceed maxTotalSize.
52     void initialize(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize,
53                     Policy policy = defaultPolicy());
54 
55     // terminate puts the NNCache back into the uninitialized state.  When
56     // in this state the getBlob and setBlob methods will return without
57     // performing any cache operations.
58     void terminate();
59 
60     // setBlob attempts to insert a new key/value blob pair into the cache.
61     void setBlob(const void* key, ssize_t keySize, const void* value, ssize_t valueSize);
62 
63     // getBlob attempts to retrieve the value blob associated with a given key
64     // blob from cache.
65     ssize_t getBlob(const void* key, ssize_t keySize, void* value, ssize_t valueSize);
66     ssize_t getBlob(const void* key, ssize_t keySize, void** value,
67                     std::function<void*(size_t)> alloc);
68     template <typename T>
getBlob(const void * key,size_t keySize,T ** value,std::function<void * (size_t)> alloc)69     ssize_t getBlob(const void* key, size_t keySize, T** value,
70                     std::function<void*(size_t)> alloc) {
71         void* valueVoid;
72         const ssize_t size = getBlob(key, keySize, &valueVoid, alloc);
73         *value = static_cast<T*>(valueVoid);
74         return size;
75     }
76 
77     // setCacheFilename sets the name of the file that should be used to store
78     // cache contents from one program invocation to another.
79     void setCacheFilename(const char* filename);
80 
81    private:
82     // Creation and (the lack of) destruction is handled internally.
83     NNCache();
84     ~NNCache();
85 
86     // Copying is disallowed.
87     NNCache(const NNCache&) = delete;
88     void operator=(const NNCache&) = delete;
89 
90     // getBlobCacheLocked returns the BlobCache object being used to store the
91     // key/value blob pairs.  If the BlobCache object has not yet been created,
92     // this will do so, loading the serialized cache contents from disk if
93     // possible.
94     BlobCache* getBlobCacheLocked();
95 
96     // saveBlobCache attempts to save the current contents of mBlobCache to
97     // disk.
98     void saveBlobCacheLocked();
99 
100     // loadBlobCache attempts to load the saved cache contents from disk into
101     // mBlobCache.
102     void loadBlobCacheLocked();
103 
104     // mInitialized indicates whether the NNCache is in the initialized
105     // state.  It is initialized to false at construction time, and gets set to
106     // true when initialize is called.  It is set back to false when terminate
107     // is called.  When in this state, the cache behaves as normal.  When not,
108     // the getBlob and setBlob methods will return without performing any cache
109     // operations.
110     bool mInitialized;
111 
112     // mMaxKeySize is the maximum key size that will be cached.
113     size_t mMaxKeySize;
114 
115     // mMaxValueSize is the maximum value size that will be cached.
116     size_t mMaxValueSize;
117 
118     // mMaxTotalSize is the maximum size that all cache entries can occupy. This
119     // includes space for both keys and values.
120     size_t mMaxTotalSize;
121 
122     // mPolicy is the policy for cleaning the cache.
123     Policy mPolicy;
124 
125     // mBlobCache is the cache in which the key/value blob pairs are stored.  It
126     // is initially NULL, and will be initialized by getBlobCacheLocked the
127     // first time it's needed.
128     std::unique_ptr<BlobCache> mBlobCache;
129 
130     // mFilename is the name of the file for storing cache contents in between
131     // program invocations.  It is initialized to an empty string at
132     // construction time, and can be set with the setCacheFilename method.  An
133     // empty string indicates that the cache should not be saved to or restored
134     // from disk.
135     std::string mFilename;
136 
137     // mSavePending indicates whether or not a deferred save operation is
138     // pending.  Each time a key/value pair is inserted into the cache via
139     // setBlob, a deferred save is initiated if one is not already pending.
140     // This will wait some amount of time and then trigger a save of the cache
141     // contents to disk.
142     bool mSavePending;
143 
144     // mMutex is the mutex used to prevent concurrent access to the member
145     // variables. It must be locked whenever the member variables are accessed.
146     mutable std::mutex mMutex;
147 
148     // sCache is the singleton NNCache object.
149     static NNCache sCache;
150 };
151 
152 // ----------------------------------------------------------------------------
153 };  // namespace android
154 // ----------------------------------------------------------------------------
155 
156 #endif  // ANDROID_FRAMEWORKS_ML_NN_DRIVER_CACHE_NN_CACHE_NN_CACHE_H
157