HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SOP_CacheManager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SOP_CacheManager.h ( SOP Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __SOP_CacheManager__
12 #define __SOP_CacheManager__
13 
14 #include "SOP_API.h"
15 
16 #include <UT/UT_SymbolTable.h>
17 #include <UT/UT_ValArray.h>
18 
19 class SOP_CacheData;
20 class SOP_Node;
21 class CMD_Args;
22 
24 {
28 };
29 
31 {
34 };
35 
36 class SOP_BaseCache;
37 class sop_checkpoint;
38 
39 /// The SOP_CacheManager tracks all active SOPs and what their current
40 /// memory usage is.
41 ///
42 /// It can then examine the network to determine if they are cullable.
43 /// It can also watch memory usage to see what SOPs should be removed
44 /// when.
46 {
47 public:
50 
51  static SOP_CacheManager *getManager();
52 
53  /// When a new SOP is created we alert the cache manager through this.
54  void registerSOP(SOP_Node *node);
55 
56  /// When a SOP is destroyed, we alert the cache manager through this.
57  void unregisterSOP(SOP_Node *node);
58 
59  /// Returns whether we think the given SOP is loaded in memory or not.
60  bool isLoaded(const SOP_Node *node);
61 
62  /// When a SOP locks another SOP, this is used.
63  void alertLockSOP(SOP_Node *locker,
64  SOP_Node *lockee);
65 
66  /// When a SOP is unlocked, we are alerted through this.
67  void alertUnlockSOP(SOP_Node *unlocker,
68  SOP_Node *unlockee);
69 
70  /// When a SOP is recooked, we are alerted through this.
71  /// These must occur in matched pairs. The cache manager
72  /// uses these to determine when all cooking is done so it
73  /// can clean up any orphaned checkpoints.
74  void alertCookStartSOP(SOP_Node *node, float time);
75  void alertCookEndSOP(SOP_Node *node);
76 
77  /// Trigger this alert when the gdp of a node has changed without
78  /// going through the normal cook or unload mechanism. This is mostly
79  /// for handling locked sops. If we ever bring back the modeler,
80  /// it's operations would also trigger this. The cache then knows
81  /// to recalculate the memory usage of the SOP.
82  void alertGeometryChangedSOP(SOP_Node *node);
83 
84  /// When a SOP is unloaded, we are alerted through this.
85  void alertUnloadSOP(SOP_Node *node);
86 
87  /// Whenever a SOP's data is accessed (view getCookedGeoHandle)
88  /// we are alerted by this mechanism.
89  void alertTouchedSOP(SOP_Node *node);
90 
91  /// These manage the checkpoints associated with the cache.
92  /// These are epochal time periods, nodes which are older
93  /// than myLastCheckpoint have not been touched recently enough
94  /// so are candidates for unloading.
95 
96  /// This marks the current time as a checkpoint. To avoid deletion,
97  /// nodes must keep above the last two checkpoint. (Last two, or the
98  /// act of setting the checkpoint would implicitly delete all current
99  /// nodes)
100  void setCheckpoint(const char *mark);
101 
102  /// This unloads all nodes that don't reside above myLastCheckpoint.
103  /// It will also adjust myLastCheckpoint to be myCurrentCheckpoint,
104  /// thus allowing hasNewCheckpoint to detect if there is a new
105  /// operation pending.
106  void unloadToCheckpoint(const char *mark);
107 
108  /// Returns true if the checkpoints have changed since the last
109  /// unloadToCheckpoint.
110  bool hasNewCheckpoint(const char *mark);
111 
112  /// Unloads all nodes until the used memory drops below the given
113  /// amount. Nodes are culled from oldest to newest. Nodes are
114  /// not culled if currently in use (much like the checkpoint code)
115  void cullToMemory(int64 amount);
116 
117  /// Culls to the maximum memory setting of the cache.
118  /// Only does anything if myMemoryState is set to always.
119  void cullToMaxMemory();
120 
121  /// Used internally to get the data.
122  SOP_CacheData *getData(int idx) const;
123 
124  /// Used as the interface to sopcache, which is installed in
125  /// MOT_Command.C
126  void parseCommand(CMD_Args &args);
127 
129  { return myUnloadState; }
130 
131  /// Returns total used cache memory in bytes
133  { return myTotalMem; }
134 
135  /// Returns the current memory state.
137  { return myMemoryState; }
138 
139  /// Returns maximum allowed memory
141  { return myMaxMemory; }
142 
143  /// Sets the maximum memory limit. This will trigger
144  /// another cull pass, so may result in memory being freed.
145  void setMaxMemory(int64 size);
146 
147  /// Clears the cache, freeing everything that can be freed.
148  /// Returns the number of nodes that have been cleared.
149  int clearCache();
150 
151 private:
152 
153  /// This stores the special cache data associated with each node.
154  /// It is indexed by uniqueId.
155  UT_ValArray<SOP_CacheData *> myNodeReferences;
156 
157  int64 myTotalMem;
158  int64 myTimeStamp;
159 
160  int myNumCooks;
161 
162  int64 myMaxMemory;
163  SOP_CacheMemoryState myMemoryState;
164  SOP_CacheUnloadState myUnloadState;
165 
166  /// This is a double linked list of nodes who are currently
167  /// marked as checkpoint nodes. In otherwords: They are active,
168  /// want to be unloaded, and have more than one output.
169  SOP_CacheData *myCheckPointHead, *myCheckPointTail;
170 
171  /// This is a double linked list of all nodes that are loaded and
172  /// hence potentially unloadable.
173  SOP_CacheData *myLoadedHead, *myLoadedTail;
174 
175  /// This is an array of recently cooked nodes that are candidates
176  /// for unloading.
177  UT_ValArray<SOP_CacheData *> myCookedNodes;
178 
179  UT_SymbolMap<sop_checkpoint *> myCheckpoints;
180 
181  /// The global utCache which gives us unload requests and to whom
182  /// we alert of any changes.
183  SOP_BaseCache *myBaseCache;
184 
185  /// Controlled by HOUDINI_DISABLE_SOP_MEMORY_TRACKING
186  bool myDisableMemoryTracking;
187 };
188 
189 
190 #endif
191 
GT_API const UT_StringHolder time
SOP_CacheMemoryState getMemoryState() const
Returns the current memory state.
SOP_CacheUnloadState
long long int64
Definition: SYS_Types.h:116
int64 getMaxMemory() const
Returns maximum allowed memory.
GLsizeiptr size
Definition: glcorearb.h:664
SOP_CacheUnloadState getUnloadState() const
#define SOP_API
Definition: SOP_API.h:10
**If you just want to fire and args
Definition: thread.h:609
SOP_CacheMemoryState
int64 getTotalMem() const
Returns total used cache memory in bytes.