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  /// This stores the special cache data associated with each node.
153  /// It is indexed by uniqueId.
154  UT_ValArray<SOP_CacheData *> myNodeReferences;
155 
156  int64 myTotalMem;
157  int64 myTimeStamp;
158 
159  int myNumCooks;
160 
161  int64 myMaxMemory;
162  SOP_CacheMemoryState myMemoryState;
163  SOP_CacheUnloadState myUnloadState;
164 
165  /// This is a double linked list of nodes who are currently
166  /// marked as checkpoint nodes. In otherwords: They are active,
167  /// want to be unloaded, and have more than one output.
168  SOP_CacheData *myCheckPointHead, *myCheckPointTail;
169 
170  /// This is a double linked list of all nodes that are loaded and
171  /// hence potentially unloadable.
172  SOP_CacheData *myLoadedHead, *myLoadedTail;
173 
174  /// This is an array of recently cooked nodes that are candidates
175  /// for unloading.
176  UT_ValArray<SOP_CacheData *> myCookedNodes;
177 
178  UT_SymbolMap<sop_checkpoint *> myCheckpoints;
179 
180  /// The global utCache which gives us unload requests and to whom
181  /// we alert of any changes.
182  SOP_BaseCache *myBaseCache;
183 };
184 
185 
186 #endif
187 
GT_API const UT_StringHolder time
SOP_CacheMemoryState getMemoryState() const
Returns the current memory state.
GLsizeiptr size
Definition: glcorearb.h:663
SOP_CacheUnloadState
long long int64
Definition: SYS_Types.h:107
int64 getMaxMemory() const
Returns maximum allowed memory.
SOP_CacheUnloadState getUnloadState() const
#define SOP_API
Definition: SOP_API.h:10
SOP_CacheMemoryState
int64 getTotalMem() const
Returns total used cache memory in bytes.