bake-bulk-rpc.c 8.02 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <assert.h>
Philip Carns's avatar
Philip Carns committed
8 9
#include <libpmemobj.h>

Philip Carns's avatar
Philip Carns committed
10 11
#include "bake-bulk-rpc.h"

Philip Carns's avatar
Philip Carns committed
12 13 14
/* TODO: this should not be global in the long run; server may provide access
 * to multiple targets
 */
15
extern PMEMobjpool *g_pmem_pool;
Philip Carns's avatar
Philip Carns committed
16

Philip Carns's avatar
Philip Carns committed
17 18 19 20 21 22
/* definition of internal region_id_t identifier for libpmemobj back end */
typedef struct {
    PMEMoid oid;
    uint64_t size;
} pmemobj_region_id_t;

Philip Carns's avatar
Philip Carns committed
23
/* service a remote RPC that instructs the server daemon to shut down */
24
static void bake_bulk_shutdown_ult(hg_handle_t handle)
Philip Carns's avatar
Philip Carns committed
25 26 27 28 29
{
    hg_return_t hret;
    struct hg_info *hgi;
    margo_instance_id mid;

30
    printf("Got RPC request to shutdown.\n");
Philip Carns's avatar
Philip Carns committed
31

32
    hgi = HG_Get_info(handle);
Philip Carns's avatar
Philip Carns committed
33 34 35
    assert(hgi);
    mid = margo_hg_class_to_instance(hgi->hg_class);

36
    hret = margo_respond(mid, handle, NULL);
Philip Carns's avatar
Philip Carns committed
37 38
    assert(hret == HG_SUCCESS);

39
    HG_Destroy(handle);
Philip Carns's avatar
Philip Carns committed
40 41 42 43 44 45 46 47 48 49

    /* NOTE: we assume that the server daemon is using
     * margo_wait_for_finalize() to suspend until this RPC executes, so there
     * is no need to send any extra signal to notify it.
     */
    margo_finalize(mid);

    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_shutdown_ult)
50 51 52 53 54

/* service a remote RPC that creates a bulk region */
static void bake_bulk_create_ult(hg_handle_t handle)
{
    bake_bulk_create_out_t out;
Philip Carns's avatar
Philip Carns committed
55 56
    bake_bulk_create_in_t in;
    hg_return_t hret;
Philip Carns's avatar
Philip Carns committed
57
    pmemobj_region_id_t* prid;
58

Philip Carns's avatar
Philip Carns committed
59 60
    /* TODO: this check needs to be somewhere else */
    assert(sizeof(pmemobj_region_id_t) <= BAKE_BULK_REGION_ID_DATA_SIZE);
61
    printf("Got RPC request to create bulk region.\n");
Philip Carns's avatar
Philip Carns committed
62
    
63 64
    memset(&out, 0, sizeof(out));

Philip Carns's avatar
Philip Carns committed
65 66 67 68 69 70 71 72
    hret = HG_Get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }
73

Philip Carns's avatar
Philip Carns committed
74 75
    prid = (pmemobj_region_id_t*)out.rid.data;
    prid->size = in.region_size;
76
    out.ret = pmemobj_alloc(g_pmem_pool, &prid->oid, in.region_size, 0, NULL, NULL);
77

Philip Carns's avatar
Philip Carns committed
78
    HG_Free_input(handle, &in);
Philip Carns's avatar
Philip Carns committed
79 80
    HG_Respond(handle, NULL, NULL, &out);
    HG_Destroy(handle);
81 82 83 84
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_create_ult)

85 86 87 88 89 90
/* service a remote RPC that writes to a bulk region */
static void bake_bulk_write_ult(hg_handle_t handle)
{
    bake_bulk_write_out_t out;
    bake_bulk_write_in_t in;
    hg_return_t hret;
Philip Carns's avatar
Philip Carns committed
91 92 93 94 95
    char* buffer;
    hg_size_t size;
    hg_bulk_t bulk_handle;
    struct hg_info *hgi;
    margo_instance_id mid;
Philip Carns's avatar
Philip Carns committed
96
    pmemobj_region_id_t* prid;
97 98 99 100 101

    printf("Got RPC request to write bulk region.\n");
    
    memset(&out, 0, sizeof(out));

Philip Carns's avatar
Philip Carns committed
102 103 104 105
    hgi = HG_Get_info(handle);
    assert(hgi);
    mid = margo_hg_class_to_instance(hgi->hg_class);

106 107 108 109 110 111 112 113 114
    hret = HG_Get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

Philip Carns's avatar
Philip Carns committed
115
    prid = (pmemobj_region_id_t*)in.rid.data;
Philip Carns's avatar
Philip Carns committed
116 117

    /* find memory address for target object */
Philip Carns's avatar
Philip Carns committed
118
    buffer = pmemobj_direct(prid->oid);
Philip Carns's avatar
Philip Carns committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    if(!buffer)
    {
        out.ret = -1;
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    size = HG_Bulk_get_size(in.bulk_handle);

    /* create bulk handle for local side of transfer */
    hret = HG_Bulk_create(hgi->hg_class, 1, (void**)(&buffer), &size, 
        HG_BULK_WRITE_ONLY, &bulk_handle);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PULL, hgi->addr, in.bulk_handle,
        0, bulk_handle, 0, size);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Bulk_free(bulk_handle);
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    out.ret = 0;
155

Philip Carns's avatar
Philip Carns committed
156 157
    HG_Bulk_free(bulk_handle);
    HG_Free_input(handle, &in);
158 159 160 161 162 163
    HG_Respond(handle, NULL, NULL, &out);
    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_write_ult)

Philip Carns's avatar
Philip Carns committed
164 165 166 167 168 169 170
/* service a remote RPC that persists to a bulk region */
static void bake_bulk_persist_ult(hg_handle_t handle)
{
    bake_bulk_persist_out_t out;
    bake_bulk_persist_in_t in;
    hg_return_t hret;
    char* buffer;
Philip Carns's avatar
Philip Carns committed
171
    pmemobj_region_id_t* prid;
Philip Carns's avatar
Philip Carns committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185

    printf("Got RPC request to persist bulk region.\n");
    
    memset(&out, 0, sizeof(out));

    hret = HG_Get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

Philip Carns's avatar
Philip Carns committed
186
    prid = (pmemobj_region_id_t*)in.rid.data;
Philip Carns's avatar
Philip Carns committed
187 188

    /* find memory address for target object */
Philip Carns's avatar
Philip Carns committed
189
    buffer = pmemobj_direct(prid->oid);
Philip Carns's avatar
Philip Carns committed
190 191 192 193 194 195 196 197 198
    if(!buffer)
    {
        out.ret = -1;
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

Philip Carns's avatar
comment  
Philip Carns committed
199
    /* TODO: should this have an abt shim in case it blocks? */
200
    pmemobj_persist(g_pmem_pool, buffer, prid->size);
Philip Carns's avatar
Philip Carns committed
201 202 203 204 205 206 207 208 209 210

    out.ret = 0;

    HG_Free_input(handle, &in);
    HG_Respond(handle, NULL, NULL, &out);
    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_persist_ult)

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/* service a remote RPC that retrieves the size of a bulk region */
static void bake_bulk_get_size_ult(hg_handle_t handle)
{
    bake_bulk_get_size_out_t out;
    bake_bulk_get_size_in_t in;
    hg_return_t hret;
    pmemobj_region_id_t* prid;

    printf("Got RPC request to get_size bulk region.\n");
    
    memset(&out, 0, sizeof(out));

    hret = HG_Get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* kind of cheating here; the size is encoded in the RID */
    out.size = prid->size;
    out.ret = 0;

    HG_Free_input(handle, &in);
    HG_Respond(handle, NULL, NULL, &out);
    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_get_size_ult)

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
/* TODO consolidate with write handler; read and write are nearly identical */
/* service a remote RPC that reads to a bulk region */
static void bake_bulk_read_ult(hg_handle_t handle)
{
    bake_bulk_read_out_t out;
    bake_bulk_read_in_t in;
    hg_return_t hret;
    char* buffer;
    hg_size_t size;
    hg_bulk_t bulk_handle;
    struct hg_info *hgi;
    margo_instance_id mid;
    pmemobj_region_id_t* prid;

    printf("Got RPC request to read bulk region.\n");
    
    memset(&out, 0, sizeof(out));

    hgi = HG_Get_info(handle);
    assert(hgi);
    mid = margo_hg_class_to_instance(hgi->hg_class);

    hret = HG_Get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    size = HG_Bulk_get_size(in.bulk_handle);

    /* create bulk handle for local side of transfer */
    hret = HG_Bulk_create(hgi->hg_class, 1, (void**)(&buffer), &size, 
        HG_BULK_READ_ONLY, &bulk_handle);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PUSH, hgi->addr, in.bulk_handle,
        0, bulk_handle, 0, size);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        HG_Bulk_free(bulk_handle);
        HG_Free_input(handle, &in);
        HG_Respond(handle, NULL, NULL, &out);
        HG_Destroy(handle);
        return;
    }

    out.ret = 0;

    HG_Bulk_free(bulk_handle);
    HG_Free_input(handle, &in);
    HG_Respond(handle, NULL, NULL, &out);
    HG_Destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_bulk_read_ult)