GitLab maintenance scheduled for Tomorrow, 2020-08-11, from 17:00 to 18:00 CT - Services will be unavailable during this time.

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

7
#include <assert.h>
Philip Carns's avatar
Philip Carns committed
8
#include <bake-bulk.h>
9
#include <margo.h>
Philip Carns's avatar
Philip Carns committed
10
#include "uthash.h"
11
#include "bake-bulk-rpc.h"
12

13 14 15 16 17 18 19 20 21 22 23
/* Refers to a single Mercury/Margo initialization, for now this is shared by
 * all remote targets.  In the future we probably need to support multiple in
 * case we run atop more than one transport at a time.
 */
struct hg_instance
{
    margo_instance_id mid;  
    hg_class_t *hg_class;
    hg_context_t *hg_context;
    int refct;
    
24
    hg_id_t bake_bulk_probe_id;
25 26 27 28 29 30
    hg_id_t bake_bulk_shutdown_id; 
    hg_id_t bake_bulk_create_id;
    hg_id_t bake_bulk_write_id;
    hg_id_t bake_bulk_persist_id;
    hg_id_t bake_bulk_get_size_id;
    hg_id_t bake_bulk_read_id;
Philip Carns's avatar
Philip Carns committed
31
    hg_id_t bake_bulk_noop_id;
32 33 34
};

/* Refers to an instance connected to a specific target */
35 36
struct bake_instance
{
37 38 39
    bake_target_id_t bti;   /* persistent identifier for this target */
    hg_addr_t dest;         /* resolved Mercury address */
    UT_hash_handle hh;
40 41
};

42
struct bake_instance *instance_hash;
43

44 45 46 47 48 49
struct hg_instance g_hginst = {
    .mid = MARGO_INSTANCE_NULL,
    .hg_class = NULL,
    .hg_context = NULL,
    .refct = 0,
};
50

51
static int hg_instance_init(const char *mercury_dest)
52
{
53 54
    /* have we already started a Mercury instance? */
    if(g_hginst.refct > 0)
Philip Carns's avatar
Philip Carns committed
55 56
    {
        g_hginst.refct++;
57
        return(0);
Philip Carns's avatar
Philip Carns committed
58
    }
59 60 61 62 63 64 65

    /* boilerplate HG initialization steps */
    /***************************************/
    /* NOTE: the listening address is not actually used in this case (the
     * na_listen flag is false); but we pass in the *target* server address
     * here to make sure that Mercury starts up the correct transport
     */
66 67
    g_hginst.hg_class = HG_Init(mercury_dest, HG_FALSE);
    if(!g_hginst.hg_class)
68 69 70
    {
        return(-1);
    }
71 72
    g_hginst.hg_context = HG_Context_create(g_hginst.hg_class);
    if(!g_hginst.hg_context)
73
    {
74
        HG_Finalize(g_hginst.hg_class);
75 76 77 78
        return(-1);
    }

    /* register RPCs */
79 80 81 82
    g_hginst.bake_bulk_probe_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
        "bake_bulk_probe_rpc", void, bake_bulk_probe_out_t, 
        NULL);
83 84
    g_hginst.bake_bulk_shutdown_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
85 86
        "bake_bulk_shutdown_rpc", void, void, 
        NULL);
87 88
    g_hginst.bake_bulk_create_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
89 90 91 92
        "bake_bulk_create_rpc", 
        bake_bulk_create_in_t,
        bake_bulk_create_out_t,
        NULL);
93 94
    g_hginst.bake_bulk_write_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
95 96 97 98
        "bake_bulk_write_rpc", 
        bake_bulk_write_in_t,
        bake_bulk_write_out_t,
        NULL);
99 100
    g_hginst.bake_bulk_persist_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
101 102 103 104
        "bake_bulk_persist_rpc", 
        bake_bulk_persist_in_t,
        bake_bulk_persist_out_t,
        NULL);
105 106
    g_hginst.bake_bulk_get_size_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
107 108 109 110
        "bake_bulk_get_size_rpc", 
        bake_bulk_get_size_in_t,
        bake_bulk_get_size_out_t,
        NULL);
111 112
    g_hginst.bake_bulk_read_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
113 114 115 116
        "bake_bulk_read_rpc", 
        bake_bulk_read_in_t,
        bake_bulk_read_out_t,
        NULL);
Philip Carns's avatar
Philip Carns committed
117 118 119 120 121 122
    g_hginst.bake_bulk_noop_id = 
        MERCURY_REGISTER(g_hginst.hg_class, 
        "bake_bulk_noop_rpc", 
        void,
        void,
        NULL);
123

124 125
    g_hginst.mid = margo_init(0, 0, g_hginst.hg_context);
    if(!g_hginst.mid)
126
    {
127 128
        HG_Context_destroy(g_hginst.hg_context);
        HG_Finalize(g_hginst.hg_class);
129 130
        return(-1);
    }
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    g_hginst.refct = 1;

    return(0);
}

void hg_instance_finalize(void)
{
    g_hginst.refct--;

    assert(g_hginst.refct > -1);

    if(g_hginst.refct == 0)
    {
        margo_finalize(g_hginst.mid);
        HG_Context_destroy(g_hginst.hg_context);
        HG_Finalize(g_hginst.hg_class);
    }
}

int bake_probe_instance(
    const char *mercury_dest,
    bake_target_id_t *bti)
{
    hg_return_t hret;
    int ret;
156 157
    bake_bulk_probe_out_t out;
    hg_handle_t handle;
158
    struct bake_instance *new_instance;
159 160 161 162

    ret = hg_instance_init(mercury_dest);
    if(ret < 0)
        return(ret);
163

164 165 166 167 168 169 170 171
    new_instance = calloc(1, sizeof(*new_instance));
    if(!new_instance)
    {
        hg_instance_finalize();
        return(-1);
    }

    hret = margo_addr_lookup(g_hginst.mid, mercury_dest, &new_instance->dest);
172 173
    if(hret != HG_SUCCESS)
    {
174
        free(new_instance);
175
        hg_instance_finalize();
176 177 178
        return(-1);
    }

179
    /* create handle */
180
    hret = HG_Create(g_hginst.hg_context, new_instance->dest, 
181 182 183
        g_hginst.bake_bulk_probe_id, &handle);
    if(hret != HG_SUCCESS)
    {
184 185
        free(new_instance);
        hg_instance_finalize();
186 187 188 189 190 191
        return(-1);
    }

    hret = margo_forward(g_hginst.mid, handle, NULL);
    if(hret != HG_SUCCESS)
    {
192
        free(new_instance);
193
        HG_Destroy(handle);
194
        hg_instance_finalize();
195 196 197 198 199 200
        return(-1);
    }

    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
201
        free(new_instance);
202
        HG_Destroy(handle);
203
        hg_instance_finalize();
204 205 206 207 208
        return(-1);
    }

    ret = out.ret;
    *bti = out.bti;
209
    new_instance->bti = out.bti;
210 211 212 213

    HG_Free_output(handle, &out);
    HG_Destroy(handle);

214 215 216 217 218 219 220 221 222 223 224
    if(ret != 0)
    {
        free(new_instance);
        hg_instance_finalize();
    }
    else
    {
        /* TODO: safety check that it isn't already there.  Here or earlier? */
        HASH_ADD(hh, instance_hash, bti, sizeof(new_instance->bti), new_instance);
    }

225
    return(ret);
226 227 228 229 230
}
  
void bake_release_instance(
    bake_target_id_t bti)
{
231 232 233 234 235 236 237 238 239
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return;
    
    HASH_DELETE(hh, instance_hash, instance);
    HG_Addr_free(g_hginst.hg_class, instance->dest);
    free(instance);
240
    hg_instance_finalize();
241

242 243 244 245 246 247 248
    return;
}

int bake_shutdown_service(bake_target_id_t bti)
{
    hg_return_t hret;
    hg_handle_t handle;
249 250 251 252 253
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
254 255

    /* create handle */
256
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
257
        g_hginst.bake_bulk_shutdown_id, &handle);
258 259 260 261 262
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }

263
    hret = margo_forward(g_hginst.mid, handle, NULL);
264 265 266 267 268 269 270 271 272
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    HG_Destroy(handle);
    return(0);
}
Philip Carns's avatar
Philip Carns committed
273

Philip Carns's avatar
Philip Carns committed
274 275 276 277 278 279 280
int bake_bulk_write(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void const *buf,
    uint64_t buf_size)
{
281 282 283 284 285
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_write_in_t in;
    bake_bulk_write_out_t out;
    int ret;
286 287 288 289 290
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
291 292 293 294

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;
Philip Carns's avatar
Philip Carns committed
295

296
    hret = HG_Bulk_create(g_hginst.hg_class, 1, (void**)(&buf), &buf_size, 
297 298 299 300 301 302 303
        HG_BULK_READ_ONLY, &in.bulk_handle);
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }
   
    /* create handle */
304
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
305
        g_hginst.bake_bulk_write_id, &handle);
306 307 308 309 310 311
    if(hret != HG_SUCCESS)
    {
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }

312
    hret = margo_forward(g_hginst.mid, handle, &in);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }

    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }
    
    ret = out.ret;

    HG_Free_output(handle, &out);
    HG_Destroy(handle);
    HG_Bulk_free(in.bulk_handle);
    return(ret);
Philip Carns's avatar
Philip Carns committed
334 335
}

Philip Carns's avatar
Philip Carns committed
336 337 338 339 340
int bake_bulk_create(
    bake_target_id_t bti,
    uint64_t region_size,
    bake_bulk_region_id_t *rid)
{
341 342 343 344 345
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_create_in_t in;
    bake_bulk_create_out_t out;
    int ret;
346 347 348 349 350
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
351

352 353 354
    in.bti = bti;
    in.region_size = region_size;

355
    /* create handle */
356
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
357
        g_hginst.bake_bulk_create_id, &handle);
358 359 360 361 362
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }

363
    hret = margo_forward(g_hginst.mid, handle, &in);
364 365 366 367 368
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }
Philip Carns's avatar
Philip Carns committed
369

370 371 372 373 374 375 376 377 378 379 380 381 382
    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    ret = out.ret;
    *rid = out.rid;

    HG_Free_output(handle, &out);
    HG_Destroy(handle);
    return(ret);
Philip Carns's avatar
Philip Carns committed
383 384 385
}


Philip Carns's avatar
Philip Carns committed
386 387 388 389
int bake_bulk_persist(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid)
{
390 391 392 393 394
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_persist_in_t in;
    bake_bulk_persist_out_t out;
    int ret;
395 396 397 398 399
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
400 401 402 403 404

    in.bti = bti;
    in.rid = rid;

    /* create handle */
405
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
406
        g_hginst.bake_bulk_persist_id, &handle);
407 408 409 410 411
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }

412
    hret = margo_forward(g_hginst.mid, handle, &in);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    ret = out.ret;

    HG_Free_output(handle, &out);
    HG_Destroy(handle);
    return(ret);
Philip Carns's avatar
Philip Carns committed
431
}
Philip Carns's avatar
Philip Carns committed
432

433 434 435 436 437 438 439 440 441 442
int bake_bulk_get_size(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t *region_size)
{
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_get_size_in_t in;
    bake_bulk_get_size_out_t out;
    int ret;
443 444 445 446 447
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
448 449 450 451 452

    in.bti = bti;
    in.rid = rid;

    /* create handle */
453
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
454
        g_hginst.bake_bulk_get_size_id, &handle);
455 456 457 458 459
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }

460
    hret = margo_forward(g_hginst.mid, handle, &in);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    ret = out.ret;
    *region_size = out.size;

    HG_Free_output(handle, &out);
    HG_Destroy(handle);
    return(ret);
}

Philip Carns's avatar
Philip Carns committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
int bake_bulk_noop(
    bake_target_id_t bti)
{
    hg_return_t hret;
    hg_handle_t handle;
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);

    /* create handle */
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
        g_hginst.bake_bulk_noop_id, &handle);
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }

    hret = margo_forward(g_hginst.mid, handle, NULL);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        return(-1);
    }

    HG_Destroy(handle);
    return(0);
}

Philip Carns's avatar
Philip Carns committed
512 513 514 515 516 517 518
int bake_bulk_read(
    bake_target_id_t bti,
    bake_bulk_region_id_t rid,
    uint64_t region_offset,
    void *buf,
    uint64_t buf_size)
{
519 520 521 522 523
    hg_return_t hret;
    hg_handle_t handle;
    bake_bulk_read_in_t in;
    bake_bulk_read_out_t out;
    int ret;
524 525 526 527 528
    struct bake_instance *instance = NULL;

    HASH_FIND(hh, instance_hash, &bti, sizeof(bti), instance);
    if(!instance)
        return(-1);
529 530 531 532 533

    in.bti = bti;
    in.rid = rid;
    in.region_offset = region_offset;

534
    hret = HG_Bulk_create(g_hginst.hg_class, 1, (void**)(&buf), &buf_size, 
535 536 537 538 539 540 541
        HG_BULK_WRITE_ONLY, &in.bulk_handle);
    if(hret != HG_SUCCESS)
    {
        return(-1);
    }
   
    /* create handle */
542
    hret = HG_Create(g_hginst.hg_context, instance->dest, 
543
        g_hginst.bake_bulk_read_id, &handle);
544 545 546 547 548 549
    if(hret != HG_SUCCESS)
    {
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }

550
    hret = margo_forward(g_hginst.mid, handle, &in);
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }

    hret = HG_Get_output(handle, &out);
    if(hret != HG_SUCCESS)
    {
        HG_Destroy(handle);
        HG_Bulk_free(in.bulk_handle);
        return(-1);
    }
    
    ret = out.ret;

    HG_Free_output(handle, &out);
    HG_Destroy(handle);
    HG_Bulk_free(in.bulk_handle);
    return(ret);
Philip Carns's avatar
Philip Carns committed
572 573 574
}