MySQL 9.7.0
Source Code Documentation
anonymous_namespace{my_system_api_cgroup.cc} Namespace Reference

Functions

template<typename... Args>
bool read_line_from_file (const std::string_view &path, Args &...args)
 Utility: Read the first line from the file specified in path and copy its contents into the arguments passed. More...
 
std::optional< std::string > get_cgroup_path (std::string_view pattern)
 Determine which cgroup we belong to. More...
 
static bool is_valid_path (const std::string_view &path)
 
std::optional< uint64_t > cgroup_memory (const std::string_view &info_prefix, const std::string &mem_prefix, const std::string &mem_suffix, const bool with_root, std::function< bool(const std::string_view &)> v2_default_handler=nullptr)
 Read memory limits from cgroups. More...
 
uint64_t handle_v1_memory_default (uint64_t memory)
 Determine if the cgroup v1 memory is the default value and return 0 if it is. More...
 
std::optional< uint64_t > cgroup_v1_memory ()
 
bool handle_v2_memory_default (const std::string_view &path)
 Identify if cgroup v2 memory is the default or if could not be determined. More...
 
std::optional< uint64_t > cgroup_v2_memory ()
 

Variables

constexpr std::string_view cgroup_info {"/proc/self/cgroup"}
 Path containing information about which cgroup we belong to. More...
 
constexpr std::string_view v1_info_prefix {":memory:"}
 cgroup v1 pattern to extract cgroup path from cgroup_info More...
 
const std::string v1_mem_prefix {"/sys/fs/cgroup/memory"}
 cgroup v1 prefix to read memory limits More...
 
const std::string v1_mem_suffix {"/memory.limit_in_bytes"}
 cgroup v1 suffix to read memory limits More...
 
constexpr std::string_view v2_info_prefix {"0::"}
 cgroup v2 pattern to extract cgroup path from cgroup_info More...
 
const std::string v2_mem_prefix {"/sys/fs/cgroup"}
 cgroup v2 prefix to read memory limits More...
 
const std::string v2_mem_suffix {"/memory.max"}
 cgroup v2 suffix to read memory limits More...
 

Function Documentation

◆ cgroup_memory()

std::optional< uint64_t > anonymous_namespace{my_system_api_cgroup.cc}::cgroup_memory ( const std::string_view &  info_prefix,
const std::string &  mem_prefix,
const std::string &  mem_suffix,
const bool  with_root,
std::function< bool(const std::string_view &)>  v2_default_handler = nullptr 
)

Read memory limits from cgroups.

This wrapper is common for both cgroup v1, v2

Parameters
[in]info_prefixPattern used to extract cgroup from proc/self/cgroup
[in]mem_prefixcgroup path prefix to identify memory controller
[in]mem_suffixcgroup path suffix to identify file with memory limit
[in]with_rootTrue if limits are to be read from root cgroup, false otherwise (read cgroup from info_prefix)
[in]v2_default_handlerFunction to handle default memory in cgroup v2
Returns
memory read from cgroup on success, 0 if default (unlimited), nullopt otherwise
Note
Default handler is required cgroup v2 and not v1 because as v1 writes 0 or fixed value which can still be parsed, but v2 writes string 'max' which requires special handling

◆ cgroup_v1_memory()

std::optional< uint64_t > anonymous_namespace{my_system_api_cgroup.cc}::cgroup_v1_memory ( )

◆ cgroup_v2_memory()

std::optional< uint64_t > anonymous_namespace{my_system_api_cgroup.cc}::cgroup_v2_memory ( )

◆ get_cgroup_path()

std::optional< std::string > anonymous_namespace{my_system_api_cgroup.cc}::get_cgroup_path ( std::string_view  pattern)

Determine which cgroup we belong to.

This is common for cgroup v1 and v2.

Returns
path to the cgroup or std::nullopt on failure

◆ handle_v1_memory_default()

uint64_t anonymous_namespace{my_system_api_cgroup.cc}::handle_v1_memory_default ( uint64_t  memory)

Determine if the cgroup v1 memory is the default value and return 0 if it is.

Return the input if it is not the default value or if the default could not be determined.

Parameters
[in]memorymemory read from cgroup v1 paths
Returns
0 if memory value is default, input memory value otherwise

◆ handle_v2_memory_default()

bool anonymous_namespace{my_system_api_cgroup.cc}::handle_v2_memory_default ( const std::string_view &  path)

Identify if cgroup v2 memory is the default or if could not be determined.

Parameters
[in]pathPath to the memory v2 cgroup path
Returns
true if memory value is default, false otherwise

◆ is_valid_path()

static bool anonymous_namespace{my_system_api_cgroup.cc}::is_valid_path ( const std::string_view &  path)
inlinestatic

◆ read_line_from_file()

template<typename... Args>
bool anonymous_namespace{my_system_api_cgroup.cc}::read_line_from_file ( const std::string_view &  path,
Args &...  args 
)

Utility: Read the first line from the file specified in path and copy its contents into the arguments passed.

Parameters
[in]pathPath to file
[out]argsPass the arguments that you expect to read from the file in the order of their appearance in the file
Returns
true if able to read and parse the file, false otherwise

Variable Documentation

◆ cgroup_info

constexpr std::string_view anonymous_namespace{my_system_api_cgroup.cc}::cgroup_info {"/proc/self/cgroup"}
constexpr

Path containing information about which cgroup we belong to.

◆ v1_info_prefix

constexpr std::string_view anonymous_namespace{my_system_api_cgroup.cc}::v1_info_prefix {":memory:"}
constexpr

cgroup v1 pattern to extract cgroup path from cgroup_info

◆ v1_mem_prefix

const std::string anonymous_namespace{my_system_api_cgroup.cc}::v1_mem_prefix {"/sys/fs/cgroup/memory"}

cgroup v1 prefix to read memory limits

◆ v1_mem_suffix

const std::string anonymous_namespace{my_system_api_cgroup.cc}::v1_mem_suffix {"/memory.limit_in_bytes"}

cgroup v1 suffix to read memory limits

◆ v2_info_prefix

constexpr std::string_view anonymous_namespace{my_system_api_cgroup.cc}::v2_info_prefix {"0::"}
constexpr

cgroup v2 pattern to extract cgroup path from cgroup_info

◆ v2_mem_prefix

const std::string anonymous_namespace{my_system_api_cgroup.cc}::v2_mem_prefix {"/sys/fs/cgroup"}

cgroup v2 prefix to read memory limits

◆ v2_mem_suffix

const std::string anonymous_namespace{my_system_api_cgroup.cc}::v2_mem_suffix {"/memory.max"}

cgroup v2 suffix to read memory limits