libfoedus-core
FOEDUS Core Library
|
Represents memory shared between processes. More...
Represents memory shared between processes.
This class was initially a part of AlignedMemory, but we separated it out to reflect the special semantics of this type of memory.
Definition at line 92 of file shared_memory.hpp.
#include <shared_memory.hpp>
Public Member Functions | |
SharedMemory () noexcept | |
Empty constructor which allocates nothing. More... | |
SharedMemory (const SharedMemory &other)=delete | |
SharedMemory & | operator= (const SharedMemory &other)=delete |
SharedMemory (SharedMemory &&other) noexcept | |
Move constructor that steals the memory block from other. More... | |
SharedMemory & | operator= (SharedMemory &&other) noexcept |
Move assignment operator that steals the memory block from other. More... | |
~SharedMemory () | |
Automatically releases the memory. More... | |
ErrorStack | alloc (const std::string &meta_path, uint64_t size, int numa_node, bool use_hugepages) |
Newly allocate a shared memory of given size on given NUMA node. More... | |
void | attach (const std::string &meta_path, bool use_hugepages) |
Attach an already-allocated shared memory so that this object points to the memory. More... | |
const std::string & | get_meta_path () const |
Returns the path of the meta file. More... | |
char * | get_block () const |
Returns the memory block. More... | |
int | get_shmid () const |
Returns the ID of this shared memory. More... | |
key_t | get_shmkey () const |
Returns the key of this shared memory. More... | |
pid_t | get_owner_pid () const |
If non-zero, it means the ID of the process that allocated the shared memory. More... | |
bool | is_null () const |
Returns if this object doesn't hold a valid memory block. More... | |
bool | is_owned () const |
Returns if this process owns this memory and is responsible to delete it. More... | |
uint64_t | get_size () const |
Returns the byte size of the memory block. More... | |
int | get_numa_node () const |
Where the physical memory is allocated. More... | |
void | mark_for_release () |
Marks the shared memory as being removed so that it will be reclaimed when all processes detach it. More... | |
void | release_block () |
Releases the memory block IF this process has an ownership. More... | |
Friends | |
std::ostream & | operator<< (std::ostream &o, const SharedMemory &v) |
|
inlinenoexcept |
Empty constructor which allocates nothing.
Definition at line 95 of file shared_memory.hpp.
|
delete |
|
noexcept |
Move constructor that steals the memory block from other.
Definition at line 47 of file shared_memory.cpp.
|
inline |
Automatically releases the memory.
Definition at line 114 of file shared_memory.hpp.
References release_block().
ErrorStack foedus::memory::SharedMemory::alloc | ( | const std::string & | meta_path, |
uint64_t | size, | ||
int | numa_node, | ||
bool | use_hugepages | ||
) |
Newly allocate a shared memory of given size on given NUMA node.
This method should be called only at the master process.
[in] | meta_path | This method will create a temporary meta file of this path and use it to generate a unique ID via ftok() and also put the size of the memory as content. It must be unique, for example '/tmp/foedus_shm_[master-PID]_vpool_[node-ID]'. |
[in] | size | Byte size of the memory block. Actual allocation is at least of this size. |
[in] | numa_node | Where the physical memory is allocated. Use for binding via libnuma. |
[in] | use_hugepages | Whether to use hugepages. |
Definition at line 67 of file shared_memory.cpp.
References ERROR_STACK_MSG, foedus::fs::exists(), foedus::debugging::get_rdtsc(), foedus::kErrorCodeSocShmAllocFailed, foedus::kRetOk, foedus::assorted::os_error(), and release_block().
Referenced by foedus::soc::SharedMemoryRepo::allocate_shared_memories().
void foedus::memory::SharedMemory::attach | ( | const std::string & | meta_path, |
bool | use_hugepages | ||
) |
Attach an already-allocated shared memory so that this object points to the memory.
[in] | meta_path | Path of the temporary meta file that contains memory size and shared memory ID. |
[in] | use_hugepages | Whether to use hugepages. |
Definition at line 157 of file shared_memory.cpp.
References foedus::fs::exists(), foedus::assorted::os_error(), and release_block().
Referenced by foedus::soc::SharedMemoryRepo::attach_shared_memories().
|
inline |
Returns the memory block.
Definition at line 144 of file shared_memory.hpp.
Referenced by foedus::soc::SharedMemoryRepo::allocate_shared_memories(), foedus::soc::SharedMemoryRepo::attach_shared_memories(), foedus::soc::SharedMemoryRepo::get_global_memory(), foedus::soc::SharedMemoryRepo::get_node_memory(), and foedus::memory::operator<<().
|
inline |
Returns the path of the meta file.
Definition at line 142 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
Where the physical memory is allocated.
Definition at line 158 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
If non-zero, it means the ID of the process that allocated the shared memory.
Definition at line 150 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
Returns the ID of this shared memory.
Definition at line 146 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
Returns the key of this shared memory.
Definition at line 148 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
Returns the byte size of the memory block.
Definition at line 156 of file shared_memory.hpp.
Referenced by foedus::memory::operator<<().
|
inline |
Returns if this object doesn't hold a valid memory block.
Definition at line 152 of file shared_memory.hpp.
References CXX11_NULLPTR.
Referenced by foedus::soc::SharedMemoryRepo::attach_shared_memories(), and foedus::soc::SharedMemoryRepo::deallocate_shared_memories().
bool foedus::memory::SharedMemory::is_owned | ( | ) | const |
Returns if this process owns this memory and is responsible to delete it.
Definition at line 63 of file shared_memory.cpp.
Referenced by foedus::memory::operator<<(), and release_block().
void foedus::memory::SharedMemory::mark_for_release | ( | ) |
Marks the shared memory as being removed so that it will be reclaimed when all processes detach it.
This is part of deallocation of shared memory in master process. After calling this method, no process can attach this shared memory. So, do not call this too early. However, on the other hand, do not call this too late. If the master process dies for an unexpected reason, the shared memory remains until next reboot. Call it as soon as child processes ack-ed that they have attached the memory or that there are some issues the master process should exit. This method is idempotent, meaning you can safely call this many times.
Definition at line 213 of file shared_memory.cpp.
Referenced by foedus::soc::SharedMemoryRepo::mark_for_release(), and release_block().
|
delete |
|
noexcept |
Move assignment operator that steals the memory block from other.
Definition at line 50 of file shared_memory.cpp.
References release_block().
void foedus::memory::SharedMemory::release_block | ( | ) |
Releases the memory block IF this process has an ownership.
Definition at line 222 of file shared_memory.cpp.
References is_owned(), mark_for_release(), foedus::assorted::os_error(), and foedus::fs::remove().
Referenced by alloc(), attach(), foedus::soc::SharedMemoryRepo::deallocate_shared_memories(), operator=(), and ~SharedMemory().
|
friend |
Definition at line 245 of file shared_memory.cpp.