00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Jeff Lait 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: SOP_CacheManager.h ( SOP Library, C++) 00015 * 00016 * COMMENTS: 00017 */ 00018 00019 #ifndef __SOP_CacheManager__ 00020 #define __SOP_CacheManager__ 00021 00022 #include "SOP_API.h" 00023 class SOP_CacheData; 00024 00025 enum SOP_CacheUnloadState 00026 { 00027 UNLOAD_NEVER, 00028 UNLOAD_FLAG, 00029 UNLOAD_ALWAYS 00030 }; 00031 00032 enum SOP_CacheMemoryState 00033 { 00034 MEMORY_NEVER, 00035 MEMORY_ALWAYS 00036 }; 00037 00038 class SOP_BaseCache; 00039 00040 /// The SOP_CacheManager tracks all active SOPs and what their current 00041 /// memory usage is. 00042 /// 00043 /// It can then examine the network to determine if they are cullable. 00044 /// It can also watch memory usage to see what SOPs should be removed 00045 /// when. 00046 class SOP_API SOP_CacheManager 00047 { 00048 public: 00049 SOP_CacheManager(); 00050 ~SOP_CacheManager(); 00051 00052 static SOP_CacheManager *getManager(); 00053 00054 /// When a new SOP is created we alert the cache manager through this. 00055 void registerSOP(SOP_Node *node); 00056 00057 /// When a SOP is destroyed, we alert the cache manager through this. 00058 void unregisterSOP(SOP_Node *node); 00059 00060 /// Returns whether we think the given SOP is loaded in memory or not. 00061 bool isLoaded(const SOP_Node *node); 00062 00063 /// When a SOP locks another SOP, this is used. 00064 void alertLockSOP(SOP_Node *locker, 00065 SOP_Node *lockee); 00066 00067 /// When a SOP is unlocked, we are alerted through this. 00068 void alertUnlockSOP(SOP_Node *unlocker, 00069 SOP_Node *unlockee); 00070 00071 /// When a SOP is recooked, we are alerted through this. 00072 /// These must occur in matched pairs. The cache manager 00073 /// uses these to determine when all cooking is done so it 00074 /// can clean up any orphaned checkpoints. 00075 void alertCookStartSOP(SOP_Node *node, float time); 00076 void alertCookEndSOP(SOP_Node *node); 00077 00078 /// Trigger this alert when the gdp of a node has changed without 00079 /// going through the normal cook or unload mechanism. This is mostly 00080 /// for handling locked sops. If we ever bring back the modeler, 00081 /// it's operations would also trigger this. The cache then knows 00082 /// to recalculate the memory usage of the SOP. 00083 void alertGeometryChangedSOP(SOP_Node *node); 00084 00085 /// When a SOP is unloaded, we are alerted through this. 00086 void alertUnloadSOP(SOP_Node *node); 00087 00088 /// Whenever a SOP's data is accessed (view getCookedGeoHandle) 00089 /// we are alerted by this mechanism. 00090 void alertTouchedSOP(SOP_Node *node); 00091 00092 /// These manage the checkpoints associated with the cache. 00093 /// These are epochal time periods, nodes which are older 00094 /// than myLastCheckpoint have not been touched recently enough 00095 /// so are candidates for unloading. 00096 00097 /// This marks the current time as a checkpoint. To avoid deletion, 00098 /// nodes must keep above the last two checkpoint. (Last two, or the 00099 /// act of setting the checkpoint would implicitly delete all current 00100 /// nodes) 00101 void setCheckpoint(const char *mark); 00102 00103 /// This unloads all nodes that don't reside above myLastCheckpoint. 00104 /// It will also adjust myLastCheckpoint to be myCurrentCheckpoint, 00105 /// thus allowing hasNewCheckpoint to detect if there is a new 00106 /// operation pending. 00107 void unloadToCheckpoint(const char *mark); 00108 00109 /// Returns true if the checkpoints have changed since the last 00110 /// unloadToCheckpoint. 00111 bool hasNewCheckpoint(const char *mark); 00112 00113 /// Unloads all nodes until the used memory drops below the given 00114 /// amount. Nodes are culled from oldest to newest. Nodes are 00115 /// not culled if currently in use (much like the checkpoint code) 00116 void cullToMemory(int64 amount); 00117 00118 /// Culls to the maximum memory setting of the cache. 00119 /// Only does anything if myMemoryState is set to always. 00120 void cullToMaxMemory(); 00121 00122 /// Used internally to get the data. 00123 SOP_CacheData *getData(int idx) const; 00124 00125 /// Used as the interface to sopcache, which is installed in 00126 /// MOT_Command.C 00127 void parseCommand(CMD_Args &args); 00128 00129 SOP_CacheUnloadState getUnloadState() const 00130 { return myUnloadState; } 00131 00132 /// Returns total used cache memory in bytes 00133 int64 getTotalMem() const 00134 { return myTotalMem; } 00135 00136 /// Returns the current memory state. 00137 SOP_CacheMemoryState getMemoryState() const 00138 { return myMemoryState; } 00139 00140 /// Returns maximum allowed memory 00141 int64 getMaxMemory() const 00142 { return myMaxMemory; } 00143 00144 /// Sets the maximum memory limit. This will trigger 00145 /// another cull pass, so may result in memory being freed. 00146 void setMaxMemory(int64 size); 00147 00148 /// Clears the cache, freeing everything that can be freed. 00149 /// Returns the number of nodes that have been cleared. 00150 int clearCache(); 00151 00152 private: 00153 /// This stores the special cache data associated with each node. 00154 /// It is indexed by uniqueId. 00155 UT_PtrArray<SOP_CacheData *> myNodeReferences; 00156 00157 int64 myTotalMem; 00158 int64 myTimeStamp; 00159 00160 int myNumCooks; 00161 00162 int64 myMaxMemory; 00163 SOP_CacheMemoryState myMemoryState; 00164 SOP_CacheUnloadState myUnloadState; 00165 00166 /// This is a double linked list of nodes who are currently 00167 /// marked as checkpoint nodes. In otherwords: They are active, 00168 /// want to be unloaded, and have more than one output. 00169 SOP_CacheData *myCheckPointHead, *myCheckPointTail; 00170 00171 /// This is a double linked list of all nodes that are loaded and 00172 /// hence potentially unloadable. 00173 SOP_CacheData *myLoadedHead, *myLoadedTail; 00174 00175 /// This is an array of recently cooked nodes that are candidates 00176 /// for unloading. 00177 UT_PtrArray<SOP_CacheData *> myCookedNodes; 00178 00179 UT_SymbolTable myCheckpoints; 00180 00181 /// The global utCache which gives us unload requests and to whom 00182 /// we alert of any changes. 00183 SOP_BaseCache *myBaseCache; 00184 }; 00185 00186 00187 #endif 00188
1.5.9