bake-server.hpp 6.34 KB
Newer Older
1
2
/*
 * (C) 2019 The University of Chicago
3
 *
4
5
6
7
8
9
10
11
12
13
14
 * See COPYRIGHT in top-level directory.
 */
#ifndef __BAKE_SERVER_HPP
#define __BAKE_SERVER_HPP

#include <string>
#include <vector>
#include <bake.hpp>
#include <bake-server.h>

#define _CHECK_RET(__ret) \
15
    if (__ret != BAKE_SUCCESS) throw exception(__ret)
16
17
18

namespace bake {

Matthieu Dorier's avatar
Matthieu Dorier committed
19
20
21
22
23
24
25
/**
 * @brief Creates a pool at a given path, with a given size and mode.
 *
 * @param pool_name Pool name.
 * @param pool_size Pool size.
 * @param pool_mode Mode.
 */
26
27
inline void create_raw_target(const std::string& path, size_t size)
{
28
    int ret = bake_create_raw_target(path.c_str(), size);
29
30
31
    _CHECK_RET(ret);
}

Matthieu Dorier's avatar
Matthieu Dorier committed
32
33
34
/**
 * @brief The provider class is the C++ equivalent to a bake_provider_t.
 */
35
36
class provider {

37
38
    margo_instance_id m_mid      = MARGO_INSTANCE_NULL;
    bake_provider_t   m_provider = NULL;
39

40
41
    provider(margo_instance_id              mid,
             uint16_t                       provider_id,
42
             const bake_provider_init_info* args)
43
44
        : m_mid(mid)
    {
45
        int ret = bake_provider_register(mid, provider_id, args, &m_provider);
46
47
48
        _CHECK_RET(ret);
    }

49
50
    static void finalize_callback(void* args)
    {
51
52
53
54
        auto* p = static_cast<provider*>(args);
        delete p;
    }

55
  public:
Matthieu Dorier's avatar
Matthieu Dorier committed
56
57
58
59
60
61
    /**
     * @brief Factory method to create an instance of provider.
     *
     * @param mid Margo instance id.
     * @param provider_id Provider id.
     * @param pool Argobots pool.
62
63
     * @param config JSON config.
     * @param abtid ABT-IO instance.
64
     * @param remi REMI provider.
Matthieu Dorier's avatar
Matthieu Dorier committed
65
66
67
     *
     * @return Pointer to newly created provider.
     */
68
69
70
71
72
    static provider* create(margo_instance_id  mid,
                            uint16_t           provider_id = 0,
                            ABT_pool           pool        = ABT_POOL_NULL,
                            const std::string& config      = "",
                            abt_io_instance_id abtio = ABT_IO_INSTANCE_NULL,
73
74
                            void*              remi_provider = NULL,
                            void*              remi_client   = NULL)
75
76
77
78
79
    {
        bake_provider_init_info args = BAKE_PROVIDER_INIT_INFO_INITIALIZER;
        args.json_config             = config.c_str();
        args.rpc_pool                = pool;
        args.aid                     = abtio;
80
81
        args.remi_provider           = remi_provider;
        args.remi_client             = remi_client;
82
83
84
85
86
87
88
89
90
91
92
93
        return create(mid, provider_id, &args);
    }

    /**
     * @brief Factory method to create an instance of provider.
     *
     * @param mid Margo instance id.
     * @param provider_id Provider id.
     * @param args bake_provider_init_info structure.
     *
     * @return Pointer to newly created provider.
     */
94
95
96
97
    static provider* create(margo_instance_id              mid,
                            uint16_t                       provider_id,
                            const bake_provider_init_info* args = nullptr)
    {
98
        auto p = new provider(mid, provider_id, args);
99
100
101
102
        margo_provider_push_finalize_callback(mid, p, &finalize_callback, p);
        return p;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
103
104
105
    /**
     * @brief Deleted copy constructor.
     */
106
    provider(const provider&) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
107
108
109
110

    /**
     * @brief Deleted move constructor.
     */
111
    provider(provider&& other) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
112
113
114
115

    /**
     * @brief Deleted copy-assignment operator.
     */
116
    provider& operator=(const provider&) = delete;
Matthieu Dorier's avatar
Matthieu Dorier committed
117
118
119
120

    /**
     * @brief Deleted move-assignment operator.
     */
121
122
    provider& operator=(provider&& other) = delete;

Matthieu Dorier's avatar
Matthieu Dorier committed
123
124
125
    /**
     * @brief Destructor.
     */
126
127
    ~provider()
    {
128
        margo_provider_pop_finalize_callback(m_mid, this);
129
        bake_provider_deregister(m_provider);
130
131
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
132
133
134
135
136
137
138
139
    /**
     * @brief Adds a storage target to the provider.
     * The target must have been created beforehand.
     *
     * @param target_name Path to the target.
     *
     * @return a target object.
     */
140
141
    target attach_target(const std::string& target_name)
    {
142
        target t;
143
144
        int ret = bake_provider_attach_target(m_provider, target_name.c_str(),
                                              &(t.m_tid));
145
146
147
148
        _CHECK_RET(ret);
        return t;
    }

149
150
151
152
153
154
155
156
    /**
     * @brief Create a storage target and attach it to the provider.
     *
     * @param target_name Path to the target.
     * @param size Target size.
     *
     * @return a target object.
     */
157
158
    target create_target(const std::string& target_name, size_t size)
    {
159
        target t;
160
161
        int ret = bake_provider_create_target(m_provider, target_name.c_str(),
                                              size, &(t.m_tid));
162
163
164
165
        _CHECK_RET(ret);
        return t;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
166
167
168
169
170
171
172
    /**
     * @brief Removes the storage target from the provider.
     * This does not removes the storage target from the device, it
     * simply makes it unaccessible through this provider.
     *
     * @param t target to remove.
     */
173
174
    void detach_target(const target& t)
    {
175
        int ret = bake_provider_detach_target(m_provider, t.m_tid);
176
177
178
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
179
180
181
    /**
     * @brief Removes all the storage targets managed by the provider.
     */
182
183
    void detach_all_targets()
    {
184
        int ret = bake_provider_detach_all_targets(m_provider);
185
186
187
        _CHECK_RET(ret);
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
188
189
190
191
192
    /**
     * @brief Count the number of storage targets managed by the provider.
     *
     * @return number of storage targets.
     */
193
194
    uint64_t count_targets() const
    {
195
        uint64_t count;
196
        int      ret = bake_provider_count_targets(m_provider, &count);
197
198
199
200
        _CHECK_RET(ret);
        return count;
    }

Matthieu Dorier's avatar
Matthieu Dorier committed
201
202
203
204
205
    /**
     * @brief Lists all the storage targets managed by the provider.
     *
     * @return Vector of targets.
     */
206
207
208
209
    std::vector<target> list_targets() const
    {
        uint64_t                      count = count_targets();
        std::vector<target>           result(count);
210
        std::vector<bake_target_id_t> tgts(count);
211
        int ret = bake_provider_list_targets(m_provider, tgts.data());
212
        _CHECK_RET(ret);
213
        for (unsigned i = 0; i < count; i++) { result[i].m_tid = tgts[i]; }
214
215
        return result;
    }
216

217
218
    std::string get_config() const
    {
219
        char* cfg = bake_provider_get_config(m_provider);
220
        if (!cfg) return std::string();
221
222
223
        auto str_cfg = std::string(cfg);
        free(cfg);
        return str_cfg;
224
    }
225
226
};

227
} // namespace bake
228
229
230
231

#undef _CHECK_RET

#endif