Blender V4.3
memory_usage.cc File Reference
#include <algorithm>
#include <atomic>
#include <cassert>
#include <iostream>
#include <memory>
#include <mutex>
#include <vector>
#include "MEM_guardedalloc.h"
#include "mallocn_intern.hh"
#include "../../source/blender/blenlib/BLI_strict_flags.h"

Go to the source code of this file.

Functions

static std::shared_ptr< Global > & get_global_ptr ()
 
static Globalget_global ()
 
static Local & get_local_data ()
 
static void update_global_peak ()
 
void memory_usage_init ()
 
void memory_usage_block_alloc (const size_t size)
 
void memory_usage_block_free (const size_t size)
 
size_t memory_usage_block_num ()
 
size_t memory_usage_current ()
 
size_t memory_usage_peak ()
 
void memory_usage_peak_reset ()
 

Variables

static std::atomic< bool > use_local_counters = true
 
static constexpr int64_t peak_update_threshold = 1024 * 1024
 

Function Documentation

◆ get_global()

◆ get_global_ptr()

static std::shared_ptr< Global > & get_global_ptr ( )
static

Definition at line 113 of file memory_usage.cc.

Referenced by get_global().

◆ get_local_data()

static Local & get_local_data ( )
static

◆ memory_usage_block_alloc()

void memory_usage_block_alloc ( const size_t size)

◆ memory_usage_block_free()

void memory_usage_block_free ( const size_t size)

Definition at line 212 of file memory_usage.cc.

References get_global(), get_local_data(), LIKELY, and use_local_counters.

Referenced by MEM_lockfree_freeN().

◆ memory_usage_block_num()

size_t memory_usage_block_num ( void )

Definition at line 229 of file memory_usage.cc.

References get_global(), and lock.

Referenced by MEM_lockfree_get_memory_blocks_in_use().

◆ memory_usage_current()

◆ memory_usage_init()

void memory_usage_init ( void )

Definition at line 182 of file memory_usage.cc.

References get_local_data().

Referenced by MEM_init_memleak_detection().

◆ memory_usage_peak()

size_t memory_usage_peak ( void )

Get the approximate peak memory usage since the last call to memory_usage_peak_reset. This is approximate, because the peak usage is not updated after every allocation (see peak_update_threshold).

In the worst case, the peak memory usage is underestimated by peak_update_threshold * #threads. After large allocations (larger than the threshold), the peak usage is always updated so those allocations will always be taken into account.

Definition at line 264 of file memory_usage.cc.

References get_global(), and update_global_peak().

Referenced by MEM_lockfree_get_peak_memory(), and MEM_lockfree_printmemlist_stats().

◆ memory_usage_peak_reset()

void memory_usage_peak_reset ( void )

Definition at line 271 of file memory_usage.cc.

References get_global(), and memory_usage_current().

Referenced by MEM_lockfree_reset_peak_memory().

◆ update_global_peak()

static void update_global_peak ( )
static

Check if the current memory usage is higher than the peak and update it if yes.

Definition at line 166 of file memory_usage.cc.

References get_global(), lock, and memory_usage_current().

Referenced by memory_usage_block_alloc(), and memory_usage_peak().

Variable Documentation

◆ peak_update_threshold

int64_t peak_update_threshold = 1024 * 1024
staticconstexpr

When a thread allocated this amount of memory, the peak memory usage is updated. An alternative would be to update the global peak memory after every allocation, but that would cause much more overhead with little benefit.

Definition at line 111 of file memory_usage.cc.

Referenced by memory_usage_block_alloc().

◆ use_local_counters

std::atomic<bool> use_local_counters = true
static

This is true for most of the lifetime of the program. Only when it starts exiting this becomes false indicating that global counters should be used for correctness.

Definition at line 105 of file memory_usage.cc.

Referenced by memory_usage_block_alloc(), and memory_usage_block_free().