Panda3D
|
00001 // Filename: memoryUsage.I 00002 // Created by: drose (25May00) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: MemoryUsage::track_memory_usage 00018 // Access: Public, Static 00019 // Description: Returns true if the user has Configured the variable 00020 // 'track-memory-usage' to true, indicating that this 00021 // class will be in effect. If this returns false, the 00022 // user has indicated not to do any of this. 00023 //////////////////////////////////////////////////////////////////// 00024 INLINE bool MemoryUsage:: 00025 get_track_memory_usage() { 00026 return get_global_ptr()->_track_memory_usage; 00027 } 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: MemoryUsage::record_pointer 00031 // Access: Public, Static 00032 // Description: Indicates that the given pointer has been recently 00033 // allocated. 00034 //////////////////////////////////////////////////////////////////// 00035 INLINE void MemoryUsage:: 00036 record_pointer(ReferenceCount *ptr) { 00037 get_global_ptr()->ns_record_pointer(ptr); 00038 } 00039 00040 //////////////////////////////////////////////////////////////////// 00041 // Function: MemoryUsage::update_type 00042 // Access: Public, Static 00043 // Description: Associates the indicated type with the given pointer. 00044 // This should be called by functions (e.g. the 00045 // constructor) that know more specifically what type of 00046 // thing we've got; otherwise, the MemoryUsage database 00047 // will know only that it's a "ReferenceCount". 00048 //////////////////////////////////////////////////////////////////// 00049 INLINE void MemoryUsage:: 00050 update_type(ReferenceCount *ptr, TypeHandle type) { 00051 get_global_ptr()->ns_update_type(ptr, type); 00052 } 00053 00054 //////////////////////////////////////////////////////////////////// 00055 // Function: MemoryUsage::update_type 00056 // Access: Public, Static 00057 // Description: Associates the indicated type with the given pointer. 00058 // This flavor of update_type() also passes in the 00059 // pointer as a TypedObject, and useful for objects that 00060 // are, in fact, TypedObjects. Once the MemoryUsage 00061 // database has the pointer as a TypedObject it doesn't 00062 // need any more help. 00063 //////////////////////////////////////////////////////////////////// 00064 INLINE void MemoryUsage:: 00065 update_type(ReferenceCount *ptr, TypedObject *typed_ptr) { 00066 get_global_ptr()->ns_update_type(ptr, typed_ptr); 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function: MemoryUsage::remove_pointer 00071 // Access: Public, Static 00072 // Description: Indicates that the given pointer has been recently 00073 // freed. 00074 //////////////////////////////////////////////////////////////////// 00075 INLINE void MemoryUsage:: 00076 remove_pointer(ReferenceCount *ptr) { 00077 get_global_ptr()->ns_remove_pointer(ptr); 00078 } 00079 00080 //////////////////////////////////////////////////////////////////// 00081 // Function: MemoryUsage::is_tracking 00082 // Access: Public, Static 00083 // Description: Returns true if the MemoryUsage object is currently 00084 // tracking memory (e.g. track-memory-usage is 00085 // configured #t). 00086 //////////////////////////////////////////////////////////////////// 00087 INLINE bool MemoryUsage:: 00088 is_tracking() { 00089 return get_global_ptr()->_track_memory_usage; 00090 } 00091 00092 //////////////////////////////////////////////////////////////////// 00093 // Function: MemoryUsage::is_counting 00094 // Access: Public, Static 00095 // Description: Returns true if the MemoryUsage object is currently 00096 // at least counting memory (e.g. this is a Windows 00097 // debug build), even if it's not fully tracking it. 00098 //////////////////////////////////////////////////////////////////// 00099 INLINE bool MemoryUsage:: 00100 is_counting() { 00101 return get_global_ptr()->_count_memory_usage; 00102 } 00103 00104 //////////////////////////////////////////////////////////////////// 00105 // Function: MemoryUsage::get_current_cpp_size 00106 // Access: Public, Static 00107 // Description: Returns the total number of bytes of allocated memory 00108 // consumed by C++ objects, not including the memory 00109 // previously frozen. 00110 //////////////////////////////////////////////////////////////////// 00111 INLINE size_t MemoryUsage:: 00112 get_current_cpp_size() { 00113 return get_global_ptr()->_current_cpp_size; 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: MemoryUsage::get_total_cpp_size 00118 // Access: Public, Static 00119 // Description: Returns the total number of bytes of allocated memory 00120 // consumed by C++ objects, including the memory 00121 // previously frozen. 00122 //////////////////////////////////////////////////////////////////// 00123 INLINE size_t MemoryUsage:: 00124 get_total_cpp_size() { 00125 return get_global_ptr()->_total_cpp_size; 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: MemoryUsage::get_panda_heap_single_size 00130 // Access: Public, Static 00131 // Description: Returns the total number of bytes allocated from the 00132 // heap from code within Panda, for individual objects. 00133 //////////////////////////////////////////////////////////////////// 00134 INLINE size_t MemoryUsage:: 00135 get_panda_heap_single_size() { 00136 return AtomicAdjust::get(get_global_ptr()->_total_heap_single_size); 00137 } 00138 00139 //////////////////////////////////////////////////////////////////// 00140 // Function: MemoryUsage::get_panda_heap_array_size 00141 // Access: Public, Static 00142 // Description: Returns the total number of bytes allocated from the 00143 // heap from code within Panda, for arrays. 00144 //////////////////////////////////////////////////////////////////// 00145 INLINE size_t MemoryUsage:: 00146 get_panda_heap_array_size() { 00147 return AtomicAdjust::get(get_global_ptr()->_total_heap_array_size); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: MemoryUsage::get_panda_heap_overhead 00152 // Access: Public, Static 00153 // Description: Returns the extra bytes allocated from the system 00154 // that are not immediately used for holding allocated 00155 // objects. This can only be determined if 00156 // ALTERNATIVE_MALLOC is enabled. 00157 //////////////////////////////////////////////////////////////////// 00158 INLINE size_t MemoryUsage:: 00159 get_panda_heap_overhead() { 00160 #if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2) 00161 MemoryUsage *mu = get_global_ptr(); 00162 return AtomicAdjust::get(mu->_requested_heap_size) - AtomicAdjust::get(mu->_total_heap_single_size) - AtomicAdjust::get(mu->_total_heap_array_size); 00163 #else 00164 return 0; 00165 #endif 00166 } 00167 00168 //////////////////////////////////////////////////////////////////// 00169 // Function: MemoryUsage::get_panda_mmap_size 00170 // Access: Public, Static 00171 // Description: Returns the total number of bytes allocated from the 00172 // virtual memory pool from code within Panda. 00173 //////////////////////////////////////////////////////////////////// 00174 INLINE size_t MemoryUsage:: 00175 get_panda_mmap_size() { 00176 return AtomicAdjust::get(get_global_ptr()->_total_mmap_size); 00177 } 00178 00179 //////////////////////////////////////////////////////////////////// 00180 // Function: MemoryUsage::get_external_size 00181 // Access: Public, Static 00182 // Description: Returns the total number of bytes of allocated memory 00183 // in the heap that Panda didn't seem to be responsible 00184 // for. This includes a few bytes for very low-level 00185 // objects (like ConfigVariables) that cannot use Panda 00186 // memory tracking because they are so very low-level. 00187 // 00188 // This also includes all of the memory that might have 00189 // been allocated by a high-level interpreter, like 00190 // Python. 00191 // 00192 // This number is only available if Panda is able to 00193 // hook into the actual heap callback. 00194 //////////////////////////////////////////////////////////////////// 00195 INLINE size_t MemoryUsage:: 00196 get_external_size() { 00197 MemoryUsage *mu = get_global_ptr(); 00198 if (mu->_count_memory_usage) { 00199 // We can only possibly know this with memory counting, which 00200 // tracks every malloc call. 00201 00202 #if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2) 00203 // With alternative malloc, none of the Panda allocated memory 00204 // shows up in total_size, so anything there is external. 00205 return mu->_total_size; 00206 #else 00207 // Without alternative malloc, the Panda allocated memory is also 00208 // included in total_size, so we have to subtract it out. 00209 return mu->_total_size - mu->_total_heap_single_size - mu->_total_heap_array_size; 00210 #endif 00211 } else { 00212 return 0; 00213 } 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: MemoryUsage::get_total_size 00218 // Access: Public, Static 00219 // Description: Returns the total size of allocated memory consumed 00220 // by the process, as nearly as can be determined. 00221 //////////////////////////////////////////////////////////////////// 00222 INLINE size_t MemoryUsage:: 00223 get_total_size() { 00224 MemoryUsage *mu = get_global_ptr(); 00225 if (mu->_count_memory_usage) { 00226 return mu->_total_size + mu->_requested_heap_size; 00227 } else { 00228 #if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2) 00229 return mu->_requested_heap_size; 00230 #else 00231 return AtomicAdjust::get(mu->_total_heap_single_size) + AtomicAdjust::get(mu->_total_heap_array_size); 00232 #endif 00233 } 00234 } 00235 00236 //////////////////////////////////////////////////////////////////// 00237 // Function: MemoryUsage::get_num_pointers 00238 // Access: Public, Static 00239 // Description: Returns the number of pointers currently active. 00240 //////////////////////////////////////////////////////////////////// 00241 INLINE int MemoryUsage:: 00242 get_num_pointers() { 00243 return get_global_ptr()->ns_get_num_pointers(); 00244 } 00245 00246 //////////////////////////////////////////////////////////////////// 00247 // Function: MemoryUsage::get_pointers 00248 // Access: Public, Static 00249 // Description: Fills the indicated MemoryUsagePointers with the set 00250 // of all pointers currently active. 00251 //////////////////////////////////////////////////////////////////// 00252 INLINE void MemoryUsage:: 00253 get_pointers(MemoryUsagePointers &result) { 00254 get_global_ptr()->ns_get_pointers(result); 00255 } 00256 00257 //////////////////////////////////////////////////////////////////// 00258 // Function: MemoryUsage::get_pointers_of_type 00259 // Access: Public, Static 00260 // Description: Fills the indicated MemoryUsagePointers with the set 00261 // of all pointers of the indicated type currently 00262 // active. 00263 //////////////////////////////////////////////////////////////////// 00264 INLINE void MemoryUsage:: 00265 get_pointers_of_type(MemoryUsagePointers &result, TypeHandle type) { 00266 get_global_ptr()->ns_get_pointers_of_type(result, type); 00267 } 00268 00269 //////////////////////////////////////////////////////////////////// 00270 // Function: MemoryUsage::get_pointers_of_age 00271 // Access: Public, Static 00272 // Description: Fills the indicated MemoryUsagePointers with the set 00273 // of all pointers that were allocated within the range 00274 // of the indicated number of seconds ago. 00275 //////////////////////////////////////////////////////////////////// 00276 INLINE void MemoryUsage:: 00277 get_pointers_of_age(MemoryUsagePointers &result, double from, double to) { 00278 get_global_ptr()->ns_get_pointers_of_age(result, from, to); 00279 } 00280 00281 //////////////////////////////////////////////////////////////////// 00282 // Function: MemoryUsage::get_pointers_with_zero_count 00283 // Access: Public, Static 00284 // Description: Fills the indicated MemoryUsagePointers with the set 00285 // of all currently active pointers (that is, pointers 00286 // allocated since the last call to freeze(), and not 00287 // yet freed) that have a zero reference count. 00288 // 00289 // Generally, an undeleted pointer with a zero reference 00290 // count means its reference count has never been 00291 // incremented beyond zero (since once it has been 00292 // incremented, the only way it can return to zero would 00293 // free the pointer). This may include objects that are 00294 // allocated statically or on the stack, which are never 00295 // intended to be deleted. Or, it might represent a 00296 // programmer or compiler error. 00297 // 00298 // This function has the side-effect of incrementing 00299 // each of their reference counts by one, thus 00300 // preventing them from ever being freed--but since they 00301 // hadn't been freed anyway, probably no additional harm 00302 // is done. 00303 //////////////////////////////////////////////////////////////////// 00304 INLINE void MemoryUsage:: 00305 get_pointers_with_zero_count(MemoryUsagePointers &result) { 00306 get_global_ptr()->ns_get_pointers_with_zero_count(result); 00307 } 00308 00309 //////////////////////////////////////////////////////////////////// 00310 // Function: MemoryUsage::freeze 00311 // Access: Public, Static 00312 // Description: 'Freezes' all pointers currently stored so that they 00313 // are no longer reported; only newly allocate pointers 00314 // from this point on will appear in future information 00315 // requests. This makes it easier to differentiate 00316 // between continuous leaks and one-time memory 00317 // allocations. 00318 //////////////////////////////////////////////////////////////////// 00319 INLINE void MemoryUsage:: 00320 freeze() { 00321 get_global_ptr()->ns_freeze(); 00322 } 00323 00324 //////////////////////////////////////////////////////////////////// 00325 // Function: MemoryUsage::show_current_types 00326 // Access: Public, Static 00327 // Description: Shows the breakdown of types of all of the 00328 // active pointers. 00329 //////////////////////////////////////////////////////////////////// 00330 INLINE void MemoryUsage:: 00331 show_current_types() { 00332 get_global_ptr()->ns_show_current_types(); 00333 } 00334 00335 //////////////////////////////////////////////////////////////////// 00336 // Function: MemoryUsage::show_trend_types 00337 // Access: Public, Static 00338 // Description: Shows the breakdown of types of all of the 00339 // pointers allocated and freed since the last call to 00340 // freeze(). 00341 //////////////////////////////////////////////////////////////////// 00342 INLINE void MemoryUsage:: 00343 show_trend_types() { 00344 get_global_ptr()->ns_show_trend_types(); 00345 } 00346 00347 //////////////////////////////////////////////////////////////////// 00348 // Function: MemoryUsage::show_current_ages 00349 // Access: Public, Static 00350 // Description: Shows the breakdown of ages of all of the 00351 // active pointers. 00352 //////////////////////////////////////////////////////////////////// 00353 INLINE void MemoryUsage:: 00354 show_current_ages() { 00355 get_global_ptr()->ns_show_current_ages(); 00356 } 00357 00358 //////////////////////////////////////////////////////////////////// 00359 // Function: MemoryUsage::show_trend_ages 00360 // Access: Public, Static 00361 // Description: Shows the breakdown of ages of all of the 00362 // pointers allocated and freed since the last call to 00363 // freeze(). 00364 //////////////////////////////////////////////////////////////////// 00365 INLINE void MemoryUsage:: 00366 show_trend_ages() { 00367 get_global_ptr()->ns_show_trend_ages(); 00368 }