MySQL 9.7.0
Source Code Documentation
my_system_api_cgroup.cc File Reference

Functions to retrieve total physical memory and total number of logical CPUs available to the server by reading the limits set by cgroups. More...

#include <unistd.h>
#include <cassert>
#include <climits>
#include <cstdint>
#include <fstream>
#include <functional>
#include <optional>
#include <string_view>
#include "my_config.h"
#include "my_system_api.h"

Namespaces

namespace  my_system_cgroup
 
namespace  anonymous_namespace{my_system_api_cgroup.cc}
 

Functions

std::optional< uint64_t > my_system_cgroup::get_cgroup_memory (const std::optional< uint64_t > &self_memory, const std::optional< uint64_t > &root_memory)
 Determine cgroup memory given memory limits from self and root cgroups. More...
 
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. More...
 
std::optional< std::string > anonymous_namespace{my_system_api_cgroup.cc}::get_cgroup_path (std::string_view pattern)
 Determine which cgroup we belong to. More...
 
static bool anonymous_namespace{my_system_api_cgroup.cc}::is_valid_path (const std::string_view &path)
 
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. More...
 
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. More...
 
std::optional< uint64_t > anonymous_namespace{my_system_api_cgroup.cc}::cgroup_v1_memory ()
 
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. More...
 
std::optional< uint64_t > anonymous_namespace{my_system_api_cgroup.cc}::cgroup_v2_memory ()
 
bool is_running_in_cgroup ()
 Determines if the current process is running in a container. More...
 
bool does_cgroup_limit_resources ()
 Determines if cgroup restricts resources. More...
 
uint64_t my_cgroup_mem_limit ()
 Read the memory limit set by the container. More...
 

Variables

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

Detailed Description

Functions to retrieve total physical memory and total number of logical CPUs available to the server by reading the limits set by cgroups.

Function Documentation

◆ does_cgroup_limit_resources()

bool does_cgroup_limit_resources ( )

Determines if cgroup restricts resources.

Returns
true if cgroup restricts resources like memory

◆ is_running_in_cgroup()

bool is_running_in_cgroup ( )

Determines if the current process is running in a container.

Returns
true if running in either cgroup v1 or cgroup v2, false otherwise

◆ my_cgroup_mem_limit()

uint64_t my_cgroup_mem_limit ( )

Read the memory limit set by the container.

Try cgroup v2, and then cgroup v1

Returns
memory limit set by cgroup v2 or cgroup v1; or 0
Note
Return value of 0 implies either no limits are set or server is not running in a container