server.cpp 3.04 KB
Newer Older
Matthieu Dorier's avatar
Matthieu Dorier committed
1 2 3 4 5
/*
 * (C) 2018 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
6
#include <pybind11/pybind11.h>
Matthieu Dorier's avatar
Matthieu Dorier committed
7 8 9 10 11 12 13 14
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <margo.h>
#include <bake.h>
#include <bake-server.h>

15
namespace py11 = pybind11;
Matthieu Dorier's avatar
Matthieu Dorier committed
16

17 18 19
typedef py11::capsule pymargo_instance_id;
typedef py11::capsule pymargo_addr;
typedef py11::capsule pybake_provider_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
20

21 22 23 24 25
#define MID2CAPSULE(__mid)   py11::capsule((void*)(__mid), "margo_instance_id", nullptr)
#define ADDR2CAPSULE(__addr) py11::capsule((void*)(__addr), "hg_addr_t", nullptr)
#define BAKEPR2CAPSULE(__bpr) py11::capsule((void*)(__bpr), "bake_provider_t", nullptr)

static pybake_provider_t pybake_provider_register(pymargo_instance_id mid, uint8_t provider_id) {
Matthieu Dorier's avatar
Matthieu Dorier committed
26
    bake_provider_t provider;
27
    int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider);
28 29
    if(ret != 0) return py11::none();
    else return BAKEPR2CAPSULE(provider);
Matthieu Dorier's avatar
Matthieu Dorier committed
30 31
}

32 33
static py11::object pybake_provider_add_storage_target(
        pybake_provider_t provider,
Matthieu Dorier's avatar
Matthieu Dorier committed
34 35 36
        const std::string& target_name) {
    bake_target_id_t target_id;
    std::memset(&target_id, 0, sizeof(target_id));
37
    int ret = bake_provider_add_storage_target(
Matthieu Dorier's avatar
Matthieu Dorier committed
38
                provider, target_name.c_str(), &target_id);
39 40 41 42
    if(ret != 0) {
        return py11::none();
    }
    return py11::cast(target_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
43
}
44

Matthieu Dorier's avatar
Matthieu Dorier committed
45
static bool pybake_provider_remove_storage_target(
46
        pybake_provider_t provider,
Matthieu Dorier's avatar
Matthieu Dorier committed
47 48 49 50 51 52
        bake_target_id_t target_id)
{
    return 0 == bake_provider_remove_storage_target(provider, target_id);
}

static bool pybake_provider_remove_all_storage_targets(
53
        pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
54 55 56 57 58
{
    return 0 == bake_provider_remove_all_storage_targets(provider);
}

static uint64_t pybake_provider_count_storage_targets(
59
        pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
60 61 62 63 64 65
{
    uint64_t n = 0;
    bake_provider_count_storage_targets(provider, &n);
    return n;
}

66 67
static py11::list pybake_provider_list_storage_targets(
            pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
68 69 70
{
    std::vector<bake_target_id_t> result;
    uint64_t n = pybake_provider_count_storage_targets(provider);
71
    if(n == 0) return py11::list();
Matthieu Dorier's avatar
Matthieu Dorier committed
72 73
    result.resize(n);
    bake_provider_list_storage_targets(provider, result.data());
74
    py11::list list_result;
Matthieu Dorier's avatar
Matthieu Dorier committed
75 76 77 78 79 80 81 82 83
    for(const auto& t : result) list_result.append(t);
    return list_result;
}

static bool pybake_make_pool(const std::string& pool_name,
                size_t pool_size, mode_t mode) {
    return 0 == bake_makepool(pool_name.c_str(), pool_size, mode);
}

84
PYBIND11_MODULE(_pybakeserver, m)
Matthieu Dorier's avatar
Matthieu Dorier committed
85
{
86 87 88 89 90 91 92 93
    py11::module::import("_pybaketarget");
    m.def("register", &pybake_provider_register);
    m.def("add_storage_target", &pybake_provider_add_storage_target);
    m.def("remove_storage_target", &pybake_provider_remove_storage_target);
    m.def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
    m.def("count_storage_targets", &pybake_provider_count_storage_targets);
    m.def("list_storage_targets", &pybake_provider_list_storage_targets);
    m.def("make_pool", &pybake_make_pool);
Matthieu Dorier's avatar
Matthieu Dorier committed
94
}