bake-bulk-server-daemon.c 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <margo.h>
#include <libpmemobj.h>
#include <bake-bulk-server.h>

int main(int argc, char **argv) 
{
    int ret;
    margo_instance_id mid;
    char target_string[64];
    PMEMoid root_oid;
    PMEMobjpool *bb_pmem_pool = NULL;
    struct bake_bulk_root *bb_pmem_root = NULL;

    if(argc != 3)
    {
        fprintf(stderr, "Usage: bake-bulk-server <HG listening addr> <pmem pool>\n");
        fprintf(stderr, "  Example: ./bake-bulk-server tcp://localhost:1234 /dev/shm/foo.dat\n");
        return(-1);
    }

    /* open pmem pool */
    bb_pmem_pool = pmemobj_open(argv[2], NULL);
    if(!bb_pmem_pool)
    {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        return(-1);
    }
    
    /* find root */
    root_oid = pmemobj_root(bb_pmem_pool, sizeof(*bb_pmem_root));
    bb_pmem_root = pmemobj_direct(root_oid);
    if(uuid_is_null(bb_pmem_root->target_id.id))
    {
        uuid_generate(bb_pmem_root->target_id.id);
        pmemobj_persist(bb_pmem_pool, bb_pmem_root, sizeof(*bb_pmem_root));
    }
    uuid_unparse(bb_pmem_root->target_id.id, target_string);
    fprintf(stderr, "BAKE target ID: %s\n", target_string);

50 51 52
    /* start margo */
    /* use the main xstream for driving progress and executing rpc handlers */
    mid = margo_init(argv[1], MARGO_SERVER_MODE, 0, -1);
53 54
    assert(mid);

55 56
    /* register the bake bulk server */
    bake_server_register(mid, bb_pmem_pool, bb_pmem_root);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    /* NOTE: at this point this server ULT has two options.  It can wait on
     * whatever mechanism it wants to (however long the daemon should run and
     * then call margo_finalize().  Otherwise, it can call
     * margo_wait_for_finalize() on the assumption that it should block until
     * some other entity calls margo_finalize().
     *
     * This example does the latter.  Margo will be finalized by a special
     * RPC from the client.
     *
     * This approach will allow the server to idle gracefully even when
     * executed in "single" mode, in which the main thread of the server
     * daemon and the progress thread for Mercury are executing in the same
     * ABT pool.
     */
    margo_wait_for_finalize(mid);

    pmemobj_close(bb_pmem_pool);

    return(0);
}