server.cpp 4.34 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>
7
8
#include <sstream>
#include <stdexcept>
Matthieu Dorier's avatar
Matthieu Dorier committed
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <margo.h>
#include <bake.h>
#include <bake-server.h>

17
namespace py11 = pybind11;
Matthieu Dorier's avatar
Matthieu Dorier committed
18

19
20
21
22
23
24
25
26
#define HANDLE_ERROR(__func, __ret) do {\
    if(__ret != BAKE_SUCCESS) {\
        std::stringstream ss;\
        ss << #__func << "() failed (ret = " << __ret << ")";\
        throw std::runtime_error(ss.str());\
    }\
} while(0)

27
28
29
typedef py11::capsule pymargo_instance_id;
typedef py11::capsule pymargo_addr;
typedef py11::capsule pybake_provider_t;
Matthieu Dorier's avatar
Matthieu Dorier committed
30

31
32
33
34
35
#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
36
    bake_provider_t provider;
37
    int ret = bake_provider_register(mid, provider_id, BAKE_ABT_POOL_DEFAULT, &provider);
38
39
    HANDLE_ERROR(bake_provider_register, ret);
    return BAKEPR2CAPSULE(provider);
Matthieu Dorier's avatar
Matthieu Dorier committed
40
41
}

42
43
static py11::object pybake_provider_add_storage_target(
        pybake_provider_t provider,
Matthieu Dorier's avatar
Matthieu Dorier committed
44
45
46
        const std::string& target_name) {
    bake_target_id_t target_id;
    std::memset(&target_id, 0, sizeof(target_id));
47
    int ret = bake_provider_add_storage_target(
Matthieu Dorier's avatar
Matthieu Dorier committed
48
                provider, target_name.c_str(), &target_id);
49
    HANDLE_ERROR(bake_provider_add_storage_target, ret);
50
    return py11::cast(target_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
51
}
52

53
static void pybake_provider_remove_storage_target(
54
        pybake_provider_t provider,
Matthieu Dorier's avatar
Matthieu Dorier committed
55
56
        bake_target_id_t target_id)
{
57
58
    int ret = bake_provider_remove_storage_target(provider, target_id);
    HANDLE_ERROR(bake_provider_remove_storage_target, ret);
Matthieu Dorier's avatar
Matthieu Dorier committed
59
60
}

61
static void pybake_provider_remove_all_storage_targets(
62
        pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
63
{
64
65
    int ret = bake_provider_remove_all_storage_targets(provider);
    HANDLE_ERROR(bake_provider_remove_all_storage_targets, ret);
Matthieu Dorier's avatar
Matthieu Dorier committed
66
67
68
}

static uint64_t pybake_provider_count_storage_targets(
69
        pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
70
71
{
    uint64_t n = 0;
72
73
    int ret = bake_provider_count_storage_targets(provider, &n);
    HANDLE_ERROR(bake_provider_count_storage_targets, ret);
Matthieu Dorier's avatar
Matthieu Dorier committed
74
75
76
    return n;
}

77
78
static py11::list pybake_provider_list_storage_targets(
            pybake_provider_t provider)
Matthieu Dorier's avatar
Matthieu Dorier committed
79
80
81
{
    std::vector<bake_target_id_t> result;
    uint64_t n = pybake_provider_count_storage_targets(provider);
82
    if(n == 0) return py11::list();
Matthieu Dorier's avatar
Matthieu Dorier committed
83
84
    result.resize(n);
    bake_provider_list_storage_targets(provider, result.data());
85
    py11::list list_result;
Matthieu Dorier's avatar
Matthieu Dorier committed
86
87
88
89
    for(const auto& t : result) list_result.append(t);
    return list_result;
}

90
static void pybake_make_pool(const std::string& pool_name,
Matthieu Dorier's avatar
Matthieu Dorier committed
91
                size_t pool_size, mode_t mode) {
92
93
94
95
    int ret = bake_makepool(pool_name.c_str(), pool_size, mode);
    HANDLE_ERROR(bake_makepool, ret);
}

96
#if 0
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
static void pybake_provider_set_target_xfer_buffer(
        pybake_provider_t provider,
        bake_target_id_t target_id,
        size_t count,
        size_t size) {
    int ret = bake_provider_set_target_xfer_buffer(provider, target_id, count, size);
    HANDLE_ERROR(bake_provider_set_target_xfer_buffer, ret);
}

static void pybake_provider_set_target_xfer_concurrency(
        pybake_provider_t provider,
        bake_target_id_t target_id,
        uint32_t num_threads) {
    int ret = bake_provider_set_target_xfer_concurrency(provider, target_id, num_threads);
    HANDLE_ERROR(bake_provider_set_target_xfer_concurrency, ret);
Matthieu Dorier's avatar
Matthieu Dorier committed
112
}
113
#endif
Matthieu Dorier's avatar
Matthieu Dorier committed
114

115
PYBIND11_MODULE(_pybakeserver, m)
Matthieu Dorier's avatar
Matthieu Dorier committed
116
{
117
118
119
120
121
122
123
124
    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);
125
#if 0
126
127
    m.def("set_target_xfer_buffer", &pybake_provider_set_target_xfer_buffer);
    m.def("set_target_xfer_concurrency", &pybake_provider_set_target_xfer_concurrency);
128
#endif
Matthieu Dorier's avatar
Matthieu Dorier committed
129
}