Atoms Crowd  4.1.0
CacheManager.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Globals.h>
11 #include <AtomsCore/Metadata/MapMetadata.h>
12 #include <AtomsCore/Metadata/PoseMetadata.h>
13 #include <set>
14 #include <list>
15 
16 namespace Atoms
17 {
19  /* The cache manager is a container for multiple cache frames. It store those frames in memory until the
20  * max memory is reached or until the removeCache function is called. When the cache manager reaches is max memory,
21  * it starts to unload cache frames based on the time elapsed from the last request.
22  * Every acces to a cache must be preceded by a request* call. The cache manager converts this requests in tasks and stores
23  * them in a queue. If two task requeste the same agents or different agents for the same file, they ill be merged in a single task.
24  * These tasks can be precessed using the executeTasks() function. In this way task of multiple agents
25  * in the same cache frame are merged togheter to imporve to performance.
26  */
27  class ATOMS_EXPORT CacheManager
28  {
29  public:
30 
31  class FrameData
32  {
33  public:
34  FrameData() :
35  data(nullptr),
36  time(0),
37  memSize(0)
38  {
39 
40  }
41 
42  AtomsPtr<AtomsCore::MapMetadata> data;
43  long time;
44  size_t memSize;
45  std::set<int> agents;
46  };
47 
48  typedef std::map<std::string, std::map<int, FrameData>> MapType;
49 
51  class Task
52  {
53  public:
54  Task() :
55  cachePath(),
56  cacheName(),
57  frame(0),
58  type(0),
59  multithread(false)
60  {
61  }
62 
63  enum Type
64  {
65  kPose = 0,
66  kMeta = 1,
67  kHeader = 2,
68  kFrame = 3,
69  };
70 
72  std::string cachePath;
73 
75  std::string cacheName;
76 
78  int frame;
79 
81  std::set<unsigned int> ids;
82 
84  short type;
85 
88  };
89 
92 
94  const AtomsCore::Pose* getAgentPose(const std::string& cachePath, const std::string& cacheName, int frame, int agentId) const;
95 
97  const AtomsCore::MapMetadata* getAgentMetadata(const std::string& cachePath, const std::string& cacheName, int frame, int agentId) const;
98 
100  void getBlendAgentPose(const std::string& cachePath, const std::string& cacheName, double frame, int agentId, const AtomsCore::Skeleton& skeleton, AtomsCore::Pose& pose) const;
101 
103  void getBlendAgentMetadata(const std::string& cachePath, const std::string& cacheName, double frame, int agentId, AtomsCore::MapMetadata& mapMeta) const;
104 
106  const AtomsCore::MapMetadata* getAgentFrame(const std::string& cachePath, const std::string& cacheName, int frame, int agentId) const;
107 
109  const AtomsCore::MapMetadata* getHeaderFrame(const std::string& cachePath, const std::string& cacheName, int frame) const;
110 
112  const AtomsCore::MapMetadata* loadHeaderFrame(const std::string& cachePath, const std::string& cacheName, int frame);
113 
115  void requestFrameHeader(const std::string& cachePath, const std::string& cacheName, int frame, bool multithread=false);
116 
118  void requestAgentFrame(const std::string& cachePath, const std::string& cacheName, int frame, const std::vector<int>& ids, bool multithread = false);
119 
121  void requestAgentPose(const std::string& cachePath, const std::string& cacheName, int frame, const std::vector<int>& ids, bool multithread = false);
122 
124  void requestAgentMetadata(const std::string& cachePath, const std::string& cacheName, int frame, const std::vector<int>& ids, bool multithread = false);
125 
127  /*
128  \param clearNotUsedCache If se tto true the manager removes all the caches not used by the task in the queue
129  */
130  void executeTasks(bool clearNotUsedCache = true);
131 
133  void clear();
134 
136  const MapType& agentsMetadata() const;
137 
139  const MapType& agentsPoses() const;
140 
142  const MapType& frameHeaders() const;
143 
145  const MapType& agentsFrames() const;
146 
148  void blendMetadata(AtomsCore::MapMetadata &outMetadata, std::vector<const AtomsCore::MapMetadata*> &inMetadatas, const std::vector<double> &inputWeightVector) const;
149 
151  void blendPose(const AtomsCore::Skeleton &skeleton, std::vector<const AtomsCore::Pose*>& posePorts, std::vector<double>& weightPorts, AtomsCore::Pose& outPose) const;
152 
154  size_t memoryUsage() const;
155 
157  size_t maxMemory() const;
158 
160  void setMaxMemory(size_t value);
161 
162  // ! Checks if a cache is already loaded in the manager
163  bool hasCache(const std::string& cachePath, const std::string& cacheName);
164 
166  void removeCache(const std::string& cachePath, const std::string& cacheName);
167 
169  void loadFullCache(const std::string& cachePath, const std::string& cacheName);
170 
172  void loadCacheFrames(const std::string& cachePath, const std::string& cacheName, int start, int end);
173 
174  private:
175 
177  void requestAgentData(const std::string& cachePath, const std::string& cacheName, int frame, const std::vector<int>& ids, short taskType, bool multithread=false);
178 
180  size_t computeMemoryUsage();
181 
183  void cleanExtraMemory();
184 
186  CacheManager();
187 
189  ~CacheManager();
190 
192  CacheManager(const CacheManager&);
193 
195  CacheManager& operator=(const CacheManager&);
196 
198  std::vector<Task> m_taskQueue;
199 
201  MapType m_agentsMetadata;
202 
204  MapType m_agentsPoses;
205 
207  MapType m_frameHeaders;
208 
210  MapType m_agentsFrames;
211 
213  std::map<std::string, int> m_referenceCount;
214 
216  std::map<std::string, std::list<FrameData>> m_agentsMetadataPool;
217  std::map<std::string, std::list<FrameData>> m_agentsPosesPool;
218  std::map<std::string, std::list<FrameData>> m_agentsFramesPool;
219 
221  size_t m_currentMemorySize;
222 
224  size_t m_maxMemory;
225  };
226 }
Atoms::CacheManager::getBlendAgentMetadata
void getBlendAgentMetadata(const std::string &cachePath, const std::string &cacheName, double frame, int agentId, AtomsCore::MapMetadata &mapMeta) const
Gets the blended agent metadatas.
Atoms::CacheManager::Task::ids
std::set< unsigned int > ids
Agent ids.
Definition: CacheManager.h:81
Atoms::CacheManager::getAgentPose
const AtomsCore::Pose * getAgentPose(const std::string &cachePath, const std::string &cacheName, int frame, int agentId) const
Gets the agent pose.
Atoms::CacheManager::loadCacheFrames
void loadCacheFrames(const std::string &cachePath, const std::string &cacheName, int start, int end)
Loads frames of a cahce inside the manager.
Atoms::CacheManager::loadFullCache
void loadFullCache(const std::string &cachePath, const std::string &cacheName)
Loads a full cache inside the manager.
Atoms::CacheManager::clear
void clear()
Clear all the caches.
Atoms::CacheManager::executeTasks
void executeTasks(bool clearNotUsedCache=true)
Executes all the tasks in the queue.
Atoms::CacheManager::agentsPoses
const MapType & agentsPoses() const
Gets agents poses.
Atoms::CacheManager::maxMemory
size_t maxMemory() const
Gets the max memory the manager can use.
Atoms::CacheManager::Task::type
short type
File type.
Definition: CacheManager.h:84
Atoms::CacheManager::instance
static CacheManager & instance()
Singleton access.
Atoms::CacheManager::agentsFrames
const MapType & agentsFrames() const
Get cache frames.
Atoms::CacheManager::getBlendAgentPose
void getBlendAgentPose(const std::string &cachePath, const std::string &cacheName, double frame, int agentId, const AtomsCore::Skeleton &skeleton, AtomsCore::Pose &pose) const
Gets the blended agent pose.
Atoms::CacheManager::getAgentFrame
const AtomsCore::MapMetadata * getAgentFrame(const std::string &cachePath, const std::string &cacheName, int frame, int agentId) const
Gets cache frame data.
Atoms::CacheManager::requestAgentFrame
void requestAgentFrame(const std::string &cachePath, const std::string &cacheName, int frame, const std::vector< int > &ids, bool multithread=false)
Requests a cache frame.
Atoms::CacheManager::Task::cacheName
std::string cacheName
Cache name.
Definition: CacheManager.h:75
Atoms::CacheManager::setMaxMemory
void setMaxMemory(size_t value)
Sets the max memory the manager can use.
Atoms::CacheManager::agentsMetadata
const MapType & agentsMetadata() const
Gets agents metadatas.
Atoms::CacheManager::requestAgentPose
void requestAgentPose(const std::string &cachePath, const std::string &cacheName, int frame, const std::vector< int > &ids, bool multithread=false)
Requests a agent pose.
Atoms::CacheManager::Task
Task class.
Definition: CacheManager.h:52
Atoms::CacheManager::FrameData
Definition: CacheManager.h:32
Atoms::CacheManager::Task::frame
int frame
Cache frame.
Definition: CacheManager.h:78
Atoms::CacheManager::removeCache
void removeCache(const std::string &cachePath, const std::string &cacheName)
Removes a cache from the manager.
Atoms
Atoms namespace.
Definition: Agent.h:28
Atoms::CacheManager::getAgentMetadata
const AtomsCore::MapMetadata * getAgentMetadata(const std::string &cachePath, const std::string &cacheName, int frame, int agentId) const
Gets the agent metadata.
Atoms::CacheManager::Task::cachePath
std::string cachePath
Cache path.
Definition: CacheManager.h:72
Atoms::CacheManager
Cache manager.
Definition: CacheManager.h:28
AtomsCore::MapMetadata
MapMetadata class.
Definition: MapMetadata.h:24
AtomsCore::Pose
Pose class.
Definition: Pose.h:32
Atoms::CacheManager::loadHeaderFrame
const AtomsCore::MapMetadata * loadHeaderFrame(const std::string &cachePath, const std::string &cacheName, int frame)
Load cache frame header data.
AtomsCore::Skeleton
Skeleton class.
Definition: Skeleton.h:68
Atoms::CacheManager::Task::multithread
bool multithread
Use multitrhead during the deserialization.
Definition: CacheManager.h:87
Atoms::CacheManager::frameHeaders
const MapType & frameHeaders() const
Gets frame headers.
Atoms::CacheManager::blendPose
void blendPose(const AtomsCore::Skeleton &skeleton, std::vector< const AtomsCore::Pose * > &posePorts, std::vector< double > &weightPorts, AtomsCore::Pose &outPose) const
Blends poses.
Atoms::CacheManager::memoryUsage
size_t memoryUsage() const
Gets the memory used by the manager.
Atoms::CacheManager::getHeaderFrame
const AtomsCore::MapMetadata * getHeaderFrame(const std::string &cachePath, const std::string &cacheName, int frame) const
Get cache frame header data.
Atoms::CacheManager::blendMetadata
void blendMetadata(AtomsCore::MapMetadata &outMetadata, std::vector< const AtomsCore::MapMetadata * > &inMetadatas, const std::vector< double > &inputWeightVector) const
Blends mapMetadata.
Atoms::CacheManager::requestFrameHeader
void requestFrameHeader(const std::string &cachePath, const std::string &cacheName, int frame, bool multithread=false)
Requests a cache frame header.
Atoms::CacheManager::requestAgentMetadata
void requestAgentMetadata(const std::string &cachePath, const std::string &cacheName, int frame, const std::vector< int > &ids, bool multithread=false)
Requests a agent metadatas.