pool.hpp 3.95 KiB
Newer Older
/*
  Copyright 2018-2020, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2020, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  SPDX-License-Identifier: MIT
*/

#ifndef GEKKOFS_DAEMON_DATA_PMDK_POOL_HPP
#define GEKKOFS_DAEMON_DATA_PMDK_POOL_HPP

#include <spdlog/spdlog.h>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

namespace pmdk {

/**
 * This class implements a RAII-enabled non-volatile memory pool based on
 * Intel's PMDK C library.
 */
class pool {

public:
    /**
     * Create a pool of non-volatile memory of a fixed `size` under
     * `parent_dir`. Give it a random name.
     *
     * @param[in]  parent_dir the parent directory where the memory pool backing 
     *                        file will be stored
     * @param[in] size the maximum capacity in bytes of the pool
     * @returns the newly created pool
     */
    pool(const fs::path& parent_dir, std::size_t size) noexcept;

    pool(pool&& rhs) = default;
    pool(const pool& other) = delete;
    pool& operator=(pool&& rhs) = default;
    pool& operator=(const pool& other) = delete;

    /**
     * Destroy an existing pool.
     */
    ~pool();

    /**
     * Returns the path to a pool's data storage in the file system.
     * 
     * @returns If the pool was correctly created, the function returns a 
     *          `boost::fylesystem::path` with the path to the file backing the
     *          pool in the file system. Otherwise, `boost::filesystem::path{}`
     *          is returned.
     */
    fs::path path() const noexcept;

    /**
     * Returns the address to a pool's data region in non-volatile memory.
     * 
     * @returns If the pool was correctly created, the function returns a 
     *          void pointer to the non-volatile memory region containing the
     *          pool's data. Otherwise, `nullptr` is returned.
     */
    void* data() const noexcept;

    /**
     * Returns the size of a pool's data region in non-volatile memory.
     * 
     * @returns If the pool was correctly created, the function returns the 
     *          size of the non-volatile memory region containing
     *          the pool's data. Otherwise, `0` is returned.
     */
    std::size_t size() const noexcept;

    /**
     * Returns whether a pool's data is actually stored in non-volatile 
     * memory.
     * 
     * @returns The function returns `true` if the pool was correctly created,
     *          and the pool's data is stored in NVM. Otherwise, `false` is
     *          returned.
     */
    bool is_persistent() const noexcept;

    /**
     *
     * Check whether a pool is valid.
     *
     * @returns `true` if the pool was correctly created. Otherwise it
     *           returns `false`.
     */
    explicit operator bool() const noexcept;

    /**
     *
     * Check whether a pool is invalid.
     *
     * @returns `true` if the pool was not correctly created. Otherwise it
     *           returns `false`.
     */
    bool operator!() const noexcept;

    /**
     *
     * Check whether a pool is valid.
     *
     * @returns `true` if the pool was correctly created. Otherwise it
     *           returns `false`.
     */
    bool valid() const noexcept;

private:
    /// Logger instance
    std::shared_ptr<spdlog::logger> log_;

    /// Path to the pool file in the file system
    fs::path path_;
    
    /// Address of the pool's non-volatile memory region
    void* data_ = nullptr;
    
    /// Size of the pool's non-volatile memory region
    std::size_t size_ = 0;

    /// Flag set if the pool file is actually stored in non-volatile memory
    bool is_persistent_ = false;

    /// Flag set if the pool was created correctly
    bool is_valid_ = false;
};

} // namespace pmdk

#endif // GEKKOFS_DAEMON_DATA_PMDK_POOL_HPP