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

7 8
#include "bake-config.h"

Philip Carns's avatar
Philip Carns committed
9
#include <assert.h>
10
#include <libpmemobj.h>
11 12
#include <unistd.h>
#include <fcntl.h>
13 14
#include <margo.h>
#include <margo-bulk-pool.h>
15 16 17
#include <remi/remi-client.h>
#include <remi/remi-server.h>
#include "bake-server.h"
18
#include "uthash.h"
19
#include "bake-rpc.h"
20
#include "bake-timing.h"
Philip Carns's avatar
Philip Carns committed
21

22 23 24 25 26 27
DECLARE_MARGO_RPC_HANDLER(bake_shutdown_ult)
DECLARE_MARGO_RPC_HANDLER(bake_create_ult)
DECLARE_MARGO_RPC_HANDLER(bake_write_ult)
DECLARE_MARGO_RPC_HANDLER(bake_eager_write_ult)
DECLARE_MARGO_RPC_HANDLER(bake_persist_ult)
DECLARE_MARGO_RPC_HANDLER(bake_create_write_persist_ult)
28
DECLARE_MARGO_RPC_HANDLER(bake_eager_create_write_persist_ult)
29 30 31 32 33 34 35
DECLARE_MARGO_RPC_HANDLER(bake_get_size_ult)
DECLARE_MARGO_RPC_HANDLER(bake_get_data_ult)
DECLARE_MARGO_RPC_HANDLER(bake_read_ult)
DECLARE_MARGO_RPC_HANDLER(bake_eager_read_ult)
DECLARE_MARGO_RPC_HANDLER(bake_probe_ult)
DECLARE_MARGO_RPC_HANDLER(bake_noop_ult)
DECLARE_MARGO_RPC_HANDLER(bake_remove_ult)
36
DECLARE_MARGO_RPC_HANDLER(bake_migrate_region_ult)
37
DECLARE_MARGO_RPC_HANDLER(bake_migrate_target_ult)
38

39
/* definition of BAKE root data structure (just a uuid for now) */
40
typedef struct
41
{   
42
    bake_target_id_t pool_id;
43
} bake_root_t;
44

45
/* definition of internal BAKE region_id_t identifier for libpmemobj back end */
46 47
typedef struct
{
48 49
    PMEMoid oid;
} pmemobj_region_id_t;
50

51
typedef struct {
52
#ifdef USE_SIZECHECK_HEADERS
53
    uint64_t size;
54
#endif
55 56 57
    char data[1];
} region_content_t;

58 59 60 61 62
typedef struct
{
    PMEMobjpool* pmem_pool;
    bake_root_t* pmem_root;
    bake_target_id_t target_id;
63 64
    char* root;
    char* filename;
65
    size_t xfer_buffer_size;
66 67 68
    size_t xfer_buffer_count;
    uint32_t xfer_concurrency;
    margo_bulk_pool_t xfer_bulk_pool;
69 70 71
    UT_hash_handle hh;
} bake_pmem_entry_t;

72
typedef struct bake_server_context_t
73
{
74
    margo_instance_id mid;
75 76 77
    ABT_rwlock lock; // write-locked during migration, read-locked by all other
    // operations. There should be something better to avoid locking everything
    // but we are going with that for simplicity for now.
78 79
    uint64_t num_targets;
    bake_pmem_entry_t* targets;
80
    hg_id_t bake_create_write_persist_id;
81 82
    remi_client_t remi_client;
    remi_provider_t remi_provider;
83
} bake_server_context_t;
84

85 86 87 88 89 90 91 92 93 94 95 96
struct pipeline_ult_arg {
    margo_instance_id   mid;            // margo instance
    void * local_buf_ptr;
    unsigned long local_buf_size;
    margo_bulk_pool_t   buf_pool;       // pool of buffers
    hg_addr_t           remote_addr;    // remote address
    hg_bulk_t           remote_bulk;    // remote bulk handle for transfers
    size_t              remote_offset;  // remote offset at which to take the data
    int             ret;            // return value of the xfer_ult function
};


97 98 99 100 101 102 103 104 105 106 107 108 109
typedef struct xfer_args {
    margo_instance_id   mid;            // margo instance
    size_t              size;           // size of data to transfer
    char*               target;         // start address where data should land in local process
    size_t              buf_size;       // size of buffers in the pool of buffers
    margo_bulk_pool_t   buf_pool;       // pool of buffers
    hg_addr_t           remote_addr;    // remote address
    hg_bulk_t           remote_bulk;    // remote bulk handle for transfers
    size_t              remote_offset;  // remote offset at which to take the data
    int32_t             op_type;        // type of operation (PUSH or PULL)
    int32_t             ret;            // return value of the xfer_ult function
} xfer_args;

110
static void bake_server_finalize_cb(void *data);
111

112
static int bake_target_post_migration_callback(remi_fileset_t fileset, void* provider);
113

114
static void pipeline_ult(void* _args);
115 116
static void xfer_ult(xfer_args* args);

117
int bake_makepool(
118 119 120
        const char *pool_name,
        size_t pool_size,
        mode_t pool_mode)
121
{
122
    PMEMobjpool *pool;
123
    PMEMoid root_oid;
124
    bake_root_t *root;
125

126
    pool = pmemobj_create(pool_name, NULL, pool_size, pool_mode);
127
    if(!pool)
128
    {
129
        fprintf(stderr, "pmemobj_create: %s\n", pmemobj_errormsg());
Matthieu Dorier's avatar
Matthieu Dorier committed
130
        return BAKE_ERR_PMEM;
131 132 133
    }

    /* find root */
134
    root_oid = pmemobj_root(pool, sizeof(bake_root_t));
135 136 137
    root = pmemobj_direct(root_oid);

    /* store the target id for this bake pool at the root */
138
    uuid_generate(root->pool_id.id);
139
    pmemobj_persist(pool, root, sizeof(bake_root_t));
140 141 142

    pmemobj_close(pool);

Matthieu Dorier's avatar
Matthieu Dorier committed
143
    return BAKE_SUCCESS;
144 145
}

146
int bake_provider_register(
147
        margo_instance_id mid,
148
        uint16_t provider_id,
149 150
        ABT_pool abt_pool,
        bake_provider_t* provider)
151
{
152
    bake_server_context_t *tmp_svr_ctx;
153 154
    int ret;
    /* check if a provider with the same provider id already exists */
155
    {
156 157
        hg_id_t id;
        hg_bool_t flag;
158
        margo_provider_registered_name(mid, "bake_probe_rpc", provider_id, &id, &flag);
159
        if(flag == HG_TRUE) {
160
            fprintf(stderr, "bake_provider_register(): a BAKE provider with the same id (%d) already exists\n", provider_id);
Matthieu Dorier's avatar
Matthieu Dorier committed
161
            return BAKE_ERR_MERCURY;
162
        }
163
    }
164 165 166
    /* check if a REMI provider exists with the same provider id */
    {
        int flag;
167 168
        // TODO pass an actual ABT-IO instance
        remi_provider_registered(mid, provider_id, &flag, NULL, NULL, NULL);
169 170 171 172 173
        if(flag) {
            fprintf(stderr, "bake_provider_register(): a REMI provider with the same (%d) already exists\n", provider_id);
            return BAKE_ERR_REMI;
        }
    }
174

175 176 177
    /* allocate the resulting structure */    
    tmp_svr_ctx = calloc(1,sizeof(*tmp_svr_ctx));
    if(!tmp_svr_ctx)
Matthieu Dorier's avatar
Matthieu Dorier committed
178
        return BAKE_ERR_ALLOCATION;
Philip Carns's avatar
Philip Carns committed
179

180 181
    tmp_svr_ctx->mid = mid;

182 183 184 185 186 187 188
    /* Create rwlock */
    ret = ABT_rwlock_create(&(tmp_svr_ctx->lock));
    if(ret != ABT_SUCCESS) {
        free(tmp_svr_ctx);
        return BAKE_ERR_ARGOBOTS;
    }

189
    /* register RPCs */
190
    hg_id_t rpc_id;
191
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_rpc",
192
            bake_create_in_t, bake_create_out_t, 
193 194 195
            bake_create_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_write_rpc",
196
            bake_write_in_t, bake_write_out_t, 
197 198 199
            bake_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_write_rpc",
200
            bake_eager_write_in_t, bake_eager_write_out_t, 
201 202 203
            bake_eager_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_read_rpc",
204
            bake_eager_read_in_t, bake_eager_read_out_t, 
205 206 207
            bake_eager_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_persist_rpc",
208
            bake_persist_in_t, bake_persist_out_t, 
209 210 211
            bake_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_write_persist_rpc",
212
            bake_create_write_persist_in_t, bake_create_write_persist_out_t,
213 214
            bake_create_write_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
215 216 217 218
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_create_write_persist_rpc",
            bake_eager_create_write_persist_in_t, bake_eager_create_write_persist_out_t,
            bake_eager_create_write_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
219
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_get_size_rpc",
220
            bake_get_size_in_t, bake_get_size_out_t, 
221 222
            bake_get_size_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
223 224 225 226
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_get_data_rpc",
            bake_get_data_in_t, bake_get_data_out_t, 
            bake_get_data_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
227
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_read_rpc",
228
            bake_read_in_t, bake_read_out_t, 
229 230 231
            bake_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_probe_rpc",
232
            bake_probe_in_t, bake_probe_out_t, bake_probe_ult, 
233 234 235 236 237
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_noop_rpc",
            void, void, bake_noop_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
238 239 240
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_remove_rpc",
            bake_remove_in_t, bake_remove_out_t, bake_remove_ult,
            provider_id, abt_pool);
241
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
242 243
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_migrate_region_rpc",
            bake_migrate_region_in_t, bake_migrate_region_out_t, bake_migrate_region_ult,
244 245
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
246 247 248 249
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_migrate_target_rpc",
            bake_migrate_target_in_t, bake_migrate_target_out_t, bake_migrate_target_ult,
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
250

Matthieu Dorier's avatar
Matthieu Dorier committed
251 252 253 254 255 256 257 258 259 260 261
    /* get a client-side version of the bake_create_write_persist RPC */
    hg_bool_t flag;
    margo_registered_name(mid, "bake_create_write_persist_rpc", &rpc_id, &flag);
    if(flag) {
        tmp_svr_ctx->bake_create_write_persist_id = rpc_id;
    } else {
        tmp_svr_ctx->bake_create_write_persist_id =
        MARGO_REGISTER(mid, "bake_create_write_persist_rpc",
                bake_create_write_persist_in_t, bake_create_write_persist_out_t, NULL);
    }

262
    /* register a REMI client */
263 264
    // TODO actually use an ABT-IO instance
    ret = remi_client_init(mid, ABT_IO_INSTANCE_NULL, &(tmp_svr_ctx->remi_client));
265
    if(ret != REMI_SUCCESS) {
266
        // XXX unregister RPCs, cleanup tmp_svr_ctx before returning
267 268
        return BAKE_ERR_REMI;
    }
269 270

    /* register a REMI provider */
271 272
    // TODO actually use an ABT-IO instance
    ret = remi_provider_register(mid, ABT_IO_INSTANCE_NULL, provider_id, abt_pool, &(tmp_svr_ctx->remi_provider));
273
    if(ret != REMI_SUCCESS) {
274
        // XXX unregister RPCs, cleanup tmp_svr_ctx before returning
275 276 277
        return BAKE_ERR_REMI;
    }
    ret = remi_provider_register_migration_class(tmp_svr_ctx->remi_provider,
278 279
            "bake", NULL,
            bake_target_post_migration_callback, NULL, tmp_svr_ctx);
280
    if(ret != REMI_SUCCESS) {
281
        // XXX unregister RPCs, cleanup tmp_svr_ctx before returning
282 283
        return BAKE_ERR_REMI;
    }
284

285 286 287
    /* install the bake server finalize callback */
    margo_push_finalize_callback(mid, &bake_server_finalize_cb, tmp_svr_ctx);

288 289
    if(provider != BAKE_PROVIDER_IGNORE)
        *provider = tmp_svr_ctx;
290

Matthieu Dorier's avatar
Matthieu Dorier committed
291
    return BAKE_SUCCESS;
292 293
}

294 295 296 297 298
int bake_provider_add_storage_target(
        bake_provider_t provider,
        const char *target_name,
        bake_target_id_t* target_id)
{
299
    int ret = BAKE_SUCCESS;
300
    bake_pmem_entry_t* new_entry = calloc(1, sizeof(*new_entry));
301 302
    new_entry->root = NULL;
    new_entry->filename = NULL;
303
    new_entry->xfer_buffer_size = 0;
304 305 306 307 308

    char* tmp = strrchr(target_name, '/');
    new_entry->filename = strdup(tmp);
    ptrdiff_t d = tmp - target_name;
    new_entry->root = strndup(target_name, d);
309 310 311 312

    new_entry->pmem_pool = pmemobj_open(target_name, NULL);
    if(!(new_entry->pmem_pool)) {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
313 314
        free(new_entry->filename);
        free(new_entry->root);
315
        free(new_entry);
Matthieu Dorier's avatar
Matthieu Dorier committed
316
        return BAKE_ERR_PMEM;
317 318 319 320 321 322 323 324 325 326 327 328
    }

    /* check to make sure the root is properly set */
    PMEMoid root_oid = pmemobj_root(new_entry->pmem_pool, sizeof(bake_root_t));
    new_entry->pmem_root = pmemobj_direct(root_oid);
    bake_target_id_t key = new_entry->pmem_root->pool_id;
    new_entry->target_id = key;

    if(uuid_is_null(key.id))
    {
        fprintf(stderr, "Error: BAKE pool %s is not properly initialized\n", target_name);
        pmemobj_close(new_entry->pmem_pool);
329 330
        free(new_entry->filename);
        free(new_entry->root);
331
        free(new_entry);
Matthieu Dorier's avatar
Matthieu Dorier committed
332
        return BAKE_ERR_UNKNOWN_TARGET;
333 334
    }

335 336
    /* write-lock the provider */
    ABT_rwlock_wrlock(provider->lock);
337 338 339 340 341 342 343 344
    /* insert in the provider's hash */
    HASH_ADD(hh, provider->targets, target_id, sizeof(bake_target_id_t), new_entry);
    /* check that it was inserted */
    bake_pmem_entry_t* check_entry = NULL;
    HASH_FIND(hh, provider->targets, &key, sizeof(bake_target_id_t), check_entry);
    if(check_entry != new_entry) {
        fprintf(stderr, "Error: BAKE could not insert new pmem pool into the hash\n");
        pmemobj_close(new_entry->pmem_pool);
345 346
        free(new_entry->filename);
        free(new_entry->root);
347
        free(new_entry);
348 349 350 351 352
        ret = BAKE_ERR_ALLOCATION;
    } else {
        provider->num_targets += 1;
        *target_id = key;
        ret = BAKE_SUCCESS;
353
    }
354 355 356
    /* unlock provider */
    ABT_rwlock_unlock(provider->lock);
    return ret;
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
}

static bake_pmem_entry_t* find_pmem_entry(
            bake_provider_t provider,
            bake_target_id_t target_id)
{
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
    return entry;
}

int bake_provider_remove_storage_target(
        bake_provider_t provider,
        bake_target_id_t target_id)
{
372 373
    int ret;
    ABT_rwlock_wrlock(provider->lock);
374 375
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
376 377 378 379 380 381 382 383 384 385 386 387
    if(!entry) {
        ret = BAKE_ERR_UNKNOWN_TARGET;
    } else {
        pmemobj_close(entry->pmem_pool);
        HASH_DEL(provider->targets, entry);
        free(entry->filename);
        free(entry->root);
        free(entry);
        ret = BAKE_SUCCESS;
    }
    ABT_rwlock_unlock(provider->lock);
    return ret;
388 389 390 391 392
}

int bake_provider_remove_all_storage_targets(
        bake_provider_t provider)
{
393
    ABT_rwlock_wrlock(provider->lock);
394 395 396 397
    bake_pmem_entry_t *p, *tmp;
    HASH_ITER(hh, provider->targets, p, tmp) {
        HASH_DEL(provider->targets, p);
        pmemobj_close(p->pmem_pool);
398
        margo_bulk_pool_destroy(p->xfer_bulk_pool);
399 400
        free(p->filename);
        free(p->root);
401 402
        free(p);
    }
403
    provider->num_targets = 0;
404
    ABT_rwlock_unlock(provider->lock);
Matthieu Dorier's avatar
Matthieu Dorier committed
405
    return BAKE_SUCCESS;
406 407 408 409 410 411
}

int bake_provider_count_storage_targets(
        bake_provider_t provider,
        uint64_t* num_targets)
{
412
    ABT_rwlock_rdlock(provider->lock);
413
    *num_targets = provider->num_targets;
414
    ABT_rwlock_unlock(provider->lock);
Matthieu Dorier's avatar
Matthieu Dorier committed
415
    return BAKE_SUCCESS;
416 417 418 419 420 421
}

int bake_provider_list_storage_targets(
        bake_provider_t provider,
        bake_target_id_t* targets)
{
422
    ABT_rwlock_rdlock(provider->lock);
423 424 425 426 427 428
    bake_pmem_entry_t *p, *tmp;
    uint64_t i = 0;
    HASH_ITER(hh, provider->targets, p, tmp) {
        targets[i] = p->target_id;
        i += 1;
    }
429
    ABT_rwlock_unlock(provider->lock);
Matthieu Dorier's avatar
Matthieu Dorier committed
430
    return BAKE_SUCCESS;
431 432
}

433
int bake_provider_set_target_xfer_buffer(
434 435
        bake_provider_t provider,
        bake_target_id_t target_id,
436
        size_t count,
437 438 439 440 441 442 443 444 445 446
        size_t size)
{
    int ret = BAKE_SUCCESS;
    ABT_rwlock_rdlock(provider->lock);
    bake_pmem_entry_t* entry = find_pmem_entry(provider, target_id);
    if(entry == NULL) {
        ret = -1;
        goto finish;
    }
    entry->xfer_buffer_size = size;
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    entry->xfer_buffer_count = count;
    if(entry->xfer_bulk_pool) {
        margo_bulk_pool_destroy(entry->xfer_bulk_pool);
    }
    if(size && count) {
       hg_return_t hret = margo_bulk_pool_create(
                provider->mid, 
                count,
                size,
                HG_BULK_READWRITE,
                &(entry->xfer_bulk_pool));
       if(hret != HG_SUCCESS) {
           ret = -1;
           goto finish;
        }
    }
finish:
    ABT_rwlock_unlock(provider->lock);
    return ret;
}

int bake_provider_set_target_xfer_concurrency(
        bake_provider_t provider,
        bake_target_id_t target_id,
        uint32_t num_threads)
{
    int ret = BAKE_SUCCESS;
    ABT_rwlock_rdlock(provider->lock);
    bake_pmem_entry_t* entry = find_pmem_entry(provider, target_id);
    if(entry == NULL) {
        ret = -1;
        goto finish;
    }
    entry->xfer_concurrency = num_threads;
481 482 483 484 485
finish:
    ABT_rwlock_unlock(provider->lock);
    return ret;
}

486
/* service a remote RPC that creates a BAKE region */
487
static void bake_create_ult(hg_handle_t handle)
488
{
489
    TIMERS_INITIALIZE("start","alloc","persist","respond");
490 491
    bake_create_out_t out;
    bake_create_in_t in;
492 493
    hg_return_t hret;
    pmemobj_region_id_t* prid;
494
    ABT_rwlock lock = ABT_RWLOCK_NULL;
495

496 497 498 499
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    bake_provider_t svr_ctx = 
500
        margo_registered_data(mid, info->id);
501
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
502
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
503
        goto finish;
504 505
    }

506
    hret = margo_get_input(handle, &in);
Matthieu Dorier's avatar
Matthieu Dorier committed
507 508
    if(hret != HG_SUCCESS) {
        out.ret = BAKE_ERR_MERCURY;
509
        goto finish;
Matthieu Dorier's avatar
Matthieu Dorier committed
510
    }
511 512 513
    /* lock provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);
514

515 516 517
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
Matthieu Dorier's avatar
Matthieu Dorier committed
518
        out.ret = BAKE_ERR_UNKNOWN_TARGET;
519
        goto finish;
520 521
    }

522
    /* TODO: this check needs to be somewhere else */
523
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
524

525 526 527
    memset(&out, 0, sizeof(out));

    prid = (pmemobj_region_id_t*)out.rid.data;
Matthieu Dorier's avatar
Matthieu Dorier committed
528

529
#ifdef USE_SIZECHECK_HEADERS
530
    size_t content_size = in.region_size + sizeof(uint64_t);
531 532 533 534
#else
    size_t content_size = in.region_size;
#endif

535 536
    TIMERS_END_STEP(0);

Matthieu Dorier's avatar
Matthieu Dorier committed
537
    int ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
538 539
            content_size, 0, NULL, NULL);
    if(ret != 0) {
Matthieu Dorier's avatar
Matthieu Dorier committed
540
        out.ret = BAKE_ERR_PMEM;
541
        goto finish;
542 543
    }

544 545
    TIMERS_END_STEP(1);

546 547 548
    region_content_t* region = (region_content_t*)pmemobj_direct(prid->oid);
    if(!region) {
        out.ret = BAKE_ERR_PMEM;
549
        goto finish;
550
    }
551
#ifdef USE_SIZECHECK_HEADERS
552
    region->size = in.region_size;
553
#endif
554
    PMEMobjpool* pmem_pool = pmemobj_pool_by_oid(prid->oid);
555 556 557
#ifdef USE_SIZECHECK_HEADERS
    pmemobj_persist(pmem_pool, region, sizeof(region->size));
#endif
558

559 560
    TIMERS_END_STEP(2);

Matthieu Dorier's avatar
Matthieu Dorier committed
561
    out.ret = BAKE_SUCCESS;
562 563

finish:
564 565 566
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
    margo_respond(handle, &out);
567 568
    TIMERS_END_STEP(3);
    TIMERS_FINALIZE();
Matthieu Dorier's avatar
Matthieu Dorier committed
569
    margo_free_input(handle, &in);
570
    margo_destroy(handle);
571 572
    return;
}
573
DEFINE_MARGO_RPC_HANDLER(bake_create_ult)
574

575
    /* service a remote RPC that writes to a BAKE region */
576
static void bake_write_ult(hg_handle_t handle)
577
{
578
    TIMERS_INITIALIZE("start","bulk_create","bulk_xfer","respond");
579 580
    bake_write_out_t out;
    bake_write_in_t in;
581
    in.bulk_handle = HG_BULK_NULL;
582
    hg_return_t hret;
Matthieu Dorier's avatar
Matthieu Dorier committed
583
    hg_addr_t src_addr = HG_ADDR_NULL;
584 585 586
    char* memory;
    char* buffer = NULL;
    size_t xfer_buf_size = 0;
587 588
    size_t xfer_buf_count = 0;
    uint32_t max_num_threads = 0;
Matthieu Dorier's avatar
Matthieu Dorier committed
589
    hg_bulk_t bulk_handle = HG_BULK_NULL;
590
    const struct hg_info *hgi;
591 592
    margo_instance_id mid;
    pmemobj_region_id_t* prid;
593
    ABT_rwlock lock = ABT_RWLOCK_NULL;
594 595 596

    memset(&out, 0, sizeof(out));

597 598
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
599
    ABT_pool handler_pool = margo_hg_handle_get_handler_pool(handle);
600
    hgi = margo_get_info(handle);
601
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
602
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
603
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
604
        goto finish;
605
    }
606 607 608
    /* read-lock the provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);
609

610
    hret = margo_get_input(handle, &in);
611 612
    if(hret != HG_SUCCESS)
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
613
        out.ret = BAKE_ERR_MERCURY;
614
        goto finish;
615 616 617 618 619
    }

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

    /* find memory address for target object */
620 621 622
    region_content_t* region = pmemobj_direct(prid->oid);

    if(!region)
623
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
624
        out.ret = BAKE_ERR_UNKNOWN_REGION;
625
        goto finish;
626
    }
627

628
#ifdef USE_SIZECHECK_HEADERS
629 630
    if(in.region_offset + in.bulk_size > region->size) {
        out.ret = BAKE_ERR_OUT_OF_BOUNDS;
631
        goto finish;
632
    }
633
#endif
634

635 636 637 638
    /* find enclosing pool and target id */
    PMEMobjpool *pool = pmemobj_pool_by_oid(prid->oid);
    PMEMoid root_oid = pmemobj_root(pool, 0);
    bake_root_t* root = pmemobj_direct(root_oid);
639

640 641 642 643
    /* find the pmem entry */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, root->pool_id);
    if(entry == NULL) {
        out.ret = BAKE_ERR_UNKNOWN_TARGET;
644
        goto finish;
645
    }
646 647 648
    xfer_buf_size   = entry->xfer_buffer_size;
    xfer_buf_count  = entry->xfer_buffer_count;
    max_num_threads = entry->xfer_concurrency;
649

650
    memory = region->data + in.region_offset;
651

652 653
    if(in.remote_addr_str)
    {
654
        /* a proxy address was provided to pull write data from */
655 656 657
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
Matthieu Dorier's avatar
Matthieu Dorier committed
658
            out.ret = BAKE_ERR_MERCURY;
659
            goto finish;
660 661 662 663 664 665 666 667
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    TIMERS_END_STEP(0);

    if(xfer_buf_size == 0 || xfer_buf_size > in.bulk_size) { // direct transfer to  device in one go

        /* create bulk handle for local side of transfer */
        hret = margo_bulk_create(mid, 1, (void**)(&memory), &in.bulk_size,
                HG_BULK_WRITE_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS)
        {
            out.ret = BAKE_ERR_MERCURY;
            goto finish;
        }

        TIMERS_END_STEP(1);

        hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
                in.bulk_offset, bulk_handle, 0, in.bulk_size);
        if(hret != HG_SUCCESS)
        {
            out.ret = BAKE_ERR_MERCURY;
            goto finish;
        }

    } else { // multiple transfers using intermediate buffer
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

        // (1) compute the maximum number of ULTs that can handle this transfer
        // as well as the number of individual transfers needed given the buffer sizes

        // number of xfers of up to xfer_buf_size needed in total
        size_t num_xfers_needed = in.bulk_size / xfer_buf_size;
        if(num_xfers_needed * xfer_buf_size < in.bulk_size) num_xfers_needed += 1;
        // number of threads that will be spawned
        uint32_t num_threads = num_xfers_needed;
        num_threads = num_threads < max_num_threads ? num_threads : max_num_threads;
        // maximum number of xfers per thread
        size_t xfer_per_thread = num_xfers_needed / num_threads;
        if(xfer_per_thread * num_threads < num_xfers_needed) xfer_per_thread += 1;

        // (2) create the array of arguments and ULTs
        xfer_args* args  = alloca(sizeof(*args)*num_threads);
        ABT_thread* ults = alloca(sizeof(*ults)*num_threads);
        unsigned int i;
        size_t current_offset = 0;
        size_t remaining_size = in.bulk_size;
        size_t current_size = xfer_per_thread * xfer_buf_size;

        for(i=0; i < num_threads; i++) {

            current_size = current_size > remaining_size ? remaining_size : current_size;

            args[i].mid           = mid;
            args[i].size          = current_size;
            args[i].target        = memory + current_offset;
            args[i].buf_size      = xfer_buf_size;
            args[i].buf_pool      = entry->xfer_bulk_pool;
            args[i].remote_addr   = src_addr;
            args[i].remote_bulk   = in.bulk_handle;
            args[i].remote_offset = current_offset;
            args[i].op_type       = HG_BULK_PULL;
            args[i].ret           = 0;

            ABT_thread_create(handler_pool, (void (*)(void*))xfer_ult, args+i, ABT_THREAD_ATTR_NULL, ults+i);

            current_offset += current_size;
            remaining_size -= current_size;
        }

        // (3) join and free the ULTs
        ABT_thread_join_many(num_threads, ults);
        ABT_thread_free_many(num_threads, ults);
738

Philip Carns's avatar
Philip Carns committed
739 740
    }

741 742
    TIMERS_END_STEP(2);

Matthieu Dorier's avatar
Matthieu Dorier committed
743
    out.ret = BAKE_SUCCESS;
744

745 746 747 748
finish:
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
    margo_respond(handle, &out);
749 750
    TIMERS_END_STEP(3);
    TIMERS_FINALIZE();
751
    free(buffer);
752 753
    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
754 755 756
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_destroy(handle);
757 758
    return;
}
759
DEFINE_MARGO_RPC_HANDLER(bake_write_ult)
760

761
    /* service a remote RPC that writes to a BAKE region in eager mode */
762
static void bake_eager_write_ult(hg_handle_t handle)
763
{
764
    TIMERS_INITIALIZE("start","memcpy","respond");
765 766
    bake_eager_write_out_t out;
    bake_eager_write_in_t in;
767 768
    in.buffer = NULL;
    in.size = 0;
769
    hg_return_t hret;
770 771 772 773
    char* buffer = NULL;
    hg_bulk_t bulk_handle = HG_BULK_NULL;
    pmemobj_region_id_t* prid = NULL;
    ABT_rwlock lock = ABT_RWLOCK_NULL;
774 775 776

    memset(&out, 0, sizeof(out));

777 778 779
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
780
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
781
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
782
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
783
        goto finish;
784 785
    }

786
    hret = margo_get_input(handle, &in);
787 788
    if(hret != HG_SUCCESS)
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
789
        out.ret = BAKE_ERR_MERCURY;
790
        goto finish;
791 792 793 794
    }

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

795 796 797 798
    /* lock provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);

799
    /* find memory address for target object */
800 801
    region_content_t* region = pmemobj_direct(prid->oid);
    if(!region)
802
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
803
        out.ret = BAKE_ERR_PMEM;
804
        goto finish;
805
    }
806

807
#ifdef USE_SIZECHECK_HEADERS
808 809
    if(in.size + in.region_offset > region->size) {
        out.ret = BAKE_ERR_OUT_OF_BOUNDS;
810
        goto finish;
811
    }
812
#endif
813

814 815
    TIMERS_END_STEP(0);

816
    buffer = region->data + in.region_offset;
817 818 819

    memcpy(buffer, in.buffer, in.size);

820 821
    TIMERS_END_STEP(1);

Matthieu Dorier's avatar
Matthieu Dorier committed
822
    out.ret = BAKE_SUCCESS;
823

824 825 826
finish:
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
827
    margo_respond(handle, &out);
828 829
    TIMERS_END_STEP(2);
    TIMERS_FINALIZE();
830
    margo_free_input(handle, &in);
831
    margo_destroy(handle);
832 833
    return;
}
834
DEFINE_MARGO_RPC_HANDLER(bake_eager_write_ult)
835

836
    /* service a remote RPC that persists to a BAKE region */
837
static void bake_persist_ult(hg_handle_t handle)
838
{
839
    TIMERS_INITIALIZE("start","persist","respond");
840 841
    bake_persist_out_t out;
    bake_persist_in_t in;
842
    hg_return_t hret;
843
    char* buffer = NULL;
844
    pmemobj_region_id_t* prid;
845
    ABT_rwlock lock = ABT_RWLOCK_NULL;
846

847 848
    memset(&out, 0, sizeof(out));

849 850 851
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
852
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
853
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
854
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
855
        goto finish;
856 857
    }

858
    hret = margo_get_input(handle, &in);
859
    if(hret != HG_SUCCESS)
860
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
861
        out.ret = BAKE_ERR_MERCURY;
862
        goto finish;
863
    }
864

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

867 868 869
    /* lock provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);
870
    /* find memory address for target object */
871 872
    region_content_t* region = pmemobj_direct(prid->oid);
    if(!region)
Philip Carns's avatar
Philip Carns committed
873
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
874
        out.ret = BAKE_ERR_PMEM;
875
        goto finish;
Philip Carns's avatar
Philip Carns committed
876
    }
877
    buffer = region->data;
878

879 880
    TIMERS_END_STEP(0);

881
    /* TODO: should this have an abt shim in case it blocks? */
882
    PMEMobjpool* pmem_pool = pmemobj_pool_by_oid(prid->oid);
883
    pmemobj_persist(pmem_pool, buffer + in.offset, in.size);
884

885 886
    TIMERS_END_STEP(1);

Matthieu Dorier's avatar
Matthieu Dorier committed
887
    out.ret = BAKE_SUCCESS;
888

889 890 891
finish:
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
892
    margo_respond(handle, &out);
893
    TIMERS_END_STEP(2);
894
    margo_free_input(handle, &in);
895
    margo_destroy(handle);
896 897
    return;
}
898
DEFINE_MARGO_RPC_HANDLER(bake_persist_ult)
899

900 901
static void bake_create_write_persist_ult(hg_handle_t handle)
{
902
    TIMERS_INITIALIZE("start","alloc","bulk_create","bulk_xfer","persist","respond");
903 904
    bake_create_write_persist_out_t out;
    bake_create_write_persist_in_t in;
905 906 907
    in.bulk_handle = HG_BULK_NULL;
    in.remote_addr_str = NULL;
    hg_addr_t src_addr = HG_ADDR_NULL;
908
    ABT_pool handler_pool;
909
    char* buffer = NULL;
910 911
    char* memory = NULL;
    size_t xfer_buf_size = 0;
912 913
    size_t xfer_buf_count = 0;
    uint32_t max_num_threads = 0;
914 915
    hg_bulk_t bulk_handle = HG_BULK_NULL;
    const struct hg_info *hgi = NULL;
916 917 918 919
    margo_instance_id mid;
    hg_return_t hret;
    int ret;
    pmemobj_region_id_t* prid;
920
    ABT_rwlock lock = ABT_RWLOCK_NULL;
921 922 923

    memset(&out, 0, sizeof(out));

924
    mid = margo_hg_handle_get_instance(handle);
925
    handler_pool = margo_hg_handle_get_handler_pool(handle);
926
    assert(mid);
927
    hgi = margo_get_info(handle);
928
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
929
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
930
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
931
        goto finish;
932 933 934 935
    }

    /* TODO: this check needs to be somewhere else */
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
936 937 938 939

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
940
        out.ret = BAKE_ERR_MERCURY;
941
        goto finish;
942 943
    }

944 945 946
    /* lock provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);
947 948 949
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
Matthieu Dorier's avatar
Matthieu Dorier committed
950
        out.ret = BAKE_ERR_UNKNOWN_TARGET;
951
        goto finish;
952
    }
953

954 955 956
    xfer_buf_size   = entry->xfer_buffer_size;
    xfer_buf_count  = entry->xfer_buffer_count;
    max_num_threads = entry->xfer_concurrency;
957

958
#ifdef USE_SIZECHECK_HEADERS
959
    size_t content_size = in.bulk_size + sizeof(uint64_t);
960 961 962 963
#else
    size_t content_size = in.bulk_size;
#endif

964 965
    TIMERS_END_STEP(0);

966
    prid = (pmemobj_region_id_t*)out.rid.data;
967

968
    ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
969
            content_size, 0, NULL, NULL);
970 971
    if(ret != 0)
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
972
        out.ret = BAKE_ERR_PMEM;
973
        goto finish;
974 975
    }

976 977
    TIMERS_END_STEP(1);

978
    /* find memory address for target object */
979 980
    region_content_t* region = pmemobj_direct(prid->oid);
    if(!region)
981
    {
Matthieu Dorier's avatar
Matthieu Dorier committed
982
        out.ret = BAKE_ERR_PMEM;
983
        goto finish;
984
    }
985
#ifdef USE_SIZECHECK_HEADERS
986
    region->size = in.bulk_size;
987
#endif
988
    memory = region->data;
989

990 991 992 993 994 995
    if(in.remote_addr_str)
    {
        /* a proxy address was provided to pull write data from */
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
Matthieu Dorier's avatar
Matthieu Dorier committed
996
            out.ret = BAKE_ERR_MERCURY;
997
            goto finish;
998 999 1000 1001 1002 1003 1004
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }
1005

1006 1007
    if(xfer_buf_size == 0 
    || xfer_buf_count == 0
1008
#if 0
1009
    || xfer_buf_size > in.bulk_size) { // don't use an intermediate buffer
1010 1011 1012
#else
    ) {
#endif
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        /* create bulk handle for local side of transfer */
        hret = margo_bulk_create(mid, 1, (void**)(&memory), &in.bulk_size,
            HG_BULK_WRITE_ONLY, &bulk_handle);
        if(hret != HG_SUCCESS)
        {
            out.ret = BAKE_ERR_MERCURY;
            goto finish;
        }

        TIMERS_END_STEP(2);

        hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
                in.bulk_offset, bulk_handle, 0, in.bulk_size);
        if(hret != HG_SUCCESS)
        {
            out.ret = BAKE_ERR_MERCURY;
            goto finish;
        }

    } else {

1034
#if 0
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        // (1) compute the maximum number of ULTs that can handle this transfer
        // as well as the number of individual transfers needed given the buffer sizes

        // number of xfers of up to xfer_buf_size needed in total
        size_t num_xfers_needed = in.bulk_size / xfer_buf_size;
        if(num_xfers_needed * xfer_buf_size < in.bulk_size) num_xfers_needed += 1;
        // number of threads that will be spawned
        uint32_t num_threads = num_xfers_needed;
        num_threads = num_threads < max_num_threads ? num_threads : max_num_threads;
        // maximum number of xfers per thread
        size_t xfer_per_thread = num_xfers_needed / num_threads;
        if(xfer_per_thread * num_threads < num_xfers_needed) xfer_per_thread += 1;

        // (2) create the array of arguments and ULTs
        xfer_args* args  = alloca(sizeof(*args)*num_threads);
        ABT_thread* ults = alloca(sizeof(*ults)*num_threads);
        unsigned int i;
1052
        size_t current_offset = 0;
1053 1054
        size_t remaining_size = in.bulk_size;
        size_t current_size = xfer_per_thread * xfer_buf_size;
1055

1056
        for(i=0; i < num_threads; i++) {
1057

1058
            current_size = current_size > remaining_size ? remaining_size : current_size;
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
            args[i].mid           = mid;
            args[i].size          = current_size;
            args[i].target        = memory + current_offset;
            args[i].buf_size      = xfer_buf_size;
            args[i].buf_pool      = entry->xfer_bulk_pool;
            args[i].remote_addr   = src_addr;
            args[i].remote_bulk   = in.bulk_handle;
            args[i].remote_offset = current_offset;
            args[i].op_type       = HG_BULK_PULL;
            args[i].ret           = 0;
1070

1071
            ABT_thread_create(handler_pool, (void (*)(void*))xfer_ult, args+i, ABT_THREAD_ATTR_NULL, ults+i);
1072 1073 1074 1075 1076

            current_offset += current_size;
            remaining_size -= current_size;
        }

1077 1078 1079
        // (3) join and free the ULTs
        ABT_thread_join_many(num_threads, ults);
        ABT_thread_free_many(num_threads, ults);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
#else
        /* experimental pipelining implementation */
        int i=0;
        int j=0;
        ABT_thread tid_array[16]; /* TODO: dynamic, or reuse as completed */
        struct pipeline_ult_arg arg_array[16];
        unsigned long issued = 0;

        while(issued < in.bulk_size)
        {
            assert(i < 16); /* TODO: dynamic, or reuse as completed */
            arg_array[i].local_buf_ptr = memory + issued;
            arg_array[i].local_buf_size = xfer_buf_size;
            if(arg_array[i].local_buf_size > (in.bulk_size - issued))
                arg_array[i].local_buf_size  = in.bulk_size - issued;
            arg_array[i].mid           = mid;
            arg_array[i].buf_pool      = entry->xfer_bulk_pool;
            arg_array[i].remote_addr   = src_addr;
            arg_array[i].remote_bulk   = in.bulk_handle;
            arg_array[i].remote_offset = issued;
            arg_array[i].ret           = 0;

            /* TODO: use handler pool or a dedicated pool elsewhere? */
            ABT_thread_create(handler_pool, pipeline_ult, &arg_array[i], ABT_THREAD_ATTR_NULL, &tid_array[i]);

            issued += arg_array[i].local_buf_size;
            i++;
        }
1108

1109 1110 1111 1112 1113 1114 1115
        while(j<i)
        {
            ABT_thread_join(tid_array[j]);
            j++;
        }

#endif
1116 1117
    }

1118 1119
    TIMERS_END_STEP(3);

1120
    /* TODO: should this have an abt shim in case it blocks? */
1121
    pmemobj_persist(entry->pmem_pool, region, content_size);
1122

Matthieu Dorier's avatar
Matthieu Dorier committed
1123
    out.ret = BAKE_SUCCESS;
1124

1125 1126
    TIMERS_END_STEP(4);

1127 1128 1129 1130
finish:
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
    margo_respond(handle, &out);
1131 1132
    TIMERS_END_STEP(5);
    TIMERS_FINALIZE();
1133
    if(in.remote_addr_str) {
1134
        margo_addr_free(mid, src_addr);
1135
    }
1136
    free(buffer);
1137 1138 1139 1140 1141 1142 1143
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_create_write_persist_ult)

1144 1145
static void bake_eager_create_write_persist_ult(hg_handle_t handle)
{
1146
    TIMERS_INITIALIZE("start","alloc","memcpy","persist","respond");
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
    bake_eager_create_write_persist_out_t out;
    bake_eager_create_write_persist_in_t in;
    in.buffer = NULL;
    in.size = 0;
    char* buffer = NULL;
    const struct hg_info *hgi = NULL;
    margo_instance_id mid;
    hg_return_t hret;
    int ret;

    pmemobj_region_id_t* prid;
    ABT_rwlock lock = ABT_RWLOCK_NULL;

    memset(&out, 0, sizeof(out));

    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
    if(!svr_ctx) {
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
        goto finish;
    }

    /* TODO: this check needs to be somewhere else */
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = BAKE_ERR_MERCURY;
        goto finish;
    }

    /* lock provider */
    lock = svr_ctx->lock;
    ABT_rwlock_rdlock(lock);
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
        out.ret = BAKE_ERR_UNKNOWN_TARGET;
        goto finish;
    }

#ifdef USE_SIZECHECK_HEADERS
    size_t content_size = in.size + sizeof(uint64_t);
#else
    size_t content_size = in.size;
#endif
    prid = (pmemobj_region_id_t*)out.rid.data;

1198 1199
    TIMERS_END_STEP(0);

1200 1201 1202 1203 1204 1205 1206 1207
    ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
            content_size, 0, NULL, NULL);
    if(ret != 0)
    {
        out.ret = BAKE_ERR_PMEM;
        goto finish;
    }

1208 1209
    TIMERS_END_STEP(1);

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    /* find memory address for target object */
    region_content_t* region = pmemobj_direct(prid->oid);
    if(!region)
    {
        out.ret = BAKE_ERR_PMEM;
        goto finish;
    }
#ifdef USE_SIZECHECK_HEADERS
    region->size = in.size;
#endif
    buffer = region->data;

    memcpy(buffer, in.buffer, in.size);

1224 1225
    TIMERS_END_STEP(2);

1226 1227 1228
    /* TODO: should this have an abt shim in case it blocks? */
    pmemobj_persist(entry->pmem_pool, region, content_size);

1229 1230
    TIMERS_END_STEP(3);

1231 1232 1233 1234 1235 1236
    out.ret = BAKE_SUCCESS;

finish:
    if(lock != ABT_RWLOCK_NULL)
        ABT_rwlock_unlock(lock);
    margo_respond(handle, &out);
1237 1238
    TIMERS_END_STEP(4);
    TIMERS_FINALIZE();
1239 1240 1241 1242 1243 1244 1245
    margo_free_input(handle, &in);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_eager_create_write_persist_ult)

/* service a remote RPC that retrieves the size of a BAKE region */
1246
static void bake_get_size_ult(hg_handle_t handle)
1247
{
1248
    TIMERS_INITIALIZE("start","respond");
1249 1250
    bake_get_size_out_t out;
    bake_get_size_in_t in;
1251 1252
    hg_return_t hret;
    pmemobj_region_id_t* prid;
1253
    ABT_rwlock lock = ABT_RWLOCK_NULL;
1254 1255 1256

    memset(&out, 0, sizeof(out));

1257 1258 1259
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
1260
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
1261
    if(!svr_ctx) {
Matthieu Dorier's avatar
Matthieu Dorier committed
1262
        out.ret = BAKE_ERR_UNKNOWN_PROVIDER;
1263
        goto finish;
1264
    }
1265

1266
    hret = margo_get_input(handle, &in);