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

#include <stdio.h>
#include <assert.h>
#include <unistd.h>
10
#include <uuid.h>
Philip Carns's avatar
Philip Carns committed
11 12 13
#include <abt.h>
#include <abt-snoozer.h>
#include <margo.h>
14
#include <libpmemobj.h>
Philip Carns's avatar
Philip Carns committed
15

Philip Carns's avatar
Philip Carns committed
16 17
#include "bake-bulk-rpc.h"

18 19 20 21 22 23
struct bake_bulk_root
{
    /* TODO: sync up types with bake-bulk.h; for now using uuid directly */
    uuid_t target_id;
};

Philip Carns's avatar
Philip Carns committed
24 25 26
/* TODO: this should not be global in the long run; server may provide access
 * to multiple targets
 */
27 28 29 30 31
/* note that these are deliberately not static for now so we can get to them from
 * rpc.c
 */
PMEMobjpool *g_pmem_pool = NULL;
struct bake_bulk_root *g_bake_bulk_root = NULL;
Philip Carns's avatar
Philip Carns committed
32

Philip Carns's avatar
Philip Carns committed
33 34 35 36 37 38 39 40
int main(int argc, char **argv) 
{
    int ret;
    margo_instance_id mid;
    ABT_xstream handler_xstream;
    ABT_pool handler_pool;
    hg_context_t *hg_context;
    hg_class_t *hg_class;
41 42
    char target_string[64];
    PMEMoid root_oid;
Philip Carns's avatar
Philip Carns committed
43

44
    if(argc != 3)
Philip Carns's avatar
Philip Carns committed
45
    {
46 47
        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");
Philip Carns's avatar
Philip Carns committed
48 49 50
        return(-1);
    }

51
    /* open pmem pool */
52 53
    g_pmem_pool = pmemobj_open(argv[2], NULL);
    if(!g_pmem_pool)
54 55 56 57 58
    {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        return(-1);
    }
    
59 60 61 62
    /* find root */
    root_oid = pmemobj_root(g_pmem_pool, sizeof(*g_bake_bulk_root));
    g_bake_bulk_root = pmemobj_direct(root_oid);
    if(uuid_is_null(g_bake_bulk_root->target_id))
63
    {
64
        uuid_generate(g_bake_bulk_root->target_id);
65 66
        pmemobj_persist(g_pmem_pool, g_bake_bulk_root, sizeof(*g_bake_bulk_root));
    }
67 68 69
    uuid_unparse(g_bake_bulk_root->target_id, target_string);
    fprintf(stderr, "BAKE target ID: %s\n", target_string);

Philip Carns's avatar
Philip Carns committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    /* boilerplate HG initialization steps */
    /***************************************/
    hg_class = HG_Init(argv[1], HG_TRUE);
    if(!hg_class)
    {
        fprintf(stderr, "Error: HG_Init()\n");
        return(-1);
    }
    hg_context = HG_Context_create(hg_class);
    if(!hg_context)
    {
        fprintf(stderr, "Error: HG_Context_create()\n");
        HG_Finalize(hg_class);
        return(-1);
    }

    /* set up argobots */
    /***************************************/
    ret = ABT_init(argc, argv);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_init()\n");
        return(-1);
    }

    /* set primary ES to idle without polling */
    ret = ABT_snoozer_xstream_self_set();
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_snoozer_xstream_self_set()\n");
        return(-1);
    }

    /* Find primary pool to use for running rpc handlers */
    ret = ABT_xstream_self(&handler_xstream);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_self()\n");
        return(-1);
    }
    ret = ABT_xstream_get_main_pools(handler_xstream, 1, &handler_pool);
    if(ret != 0)
    {
        fprintf(stderr, "Error: ABT_xstream_get_main_pools()\n");
        return(-1);
    }

    /* actually start margo */
    /* provide argobots pools for driving communication progress and
     * executing rpc handlers as well as class and context for Mercury
     * communication.
     */
    /***************************************/
Philip Carns's avatar
Philip Carns committed
123
    mid = margo_init_pool(handler_pool, handler_pool, hg_context);
Philip Carns's avatar
Philip Carns committed
124 125
    assert(mid);

126
    /* register RPCs */
Philip Carns's avatar
Philip Carns committed
127 128
    MERCURY_REGISTER(hg_class, "bake_bulk_shutdown_rpc", void, void, 
        bake_bulk_shutdown_ult_handler);
129 130 131
    MERCURY_REGISTER(hg_class, "bake_bulk_create_rpc", bake_bulk_create_in_t, 
        bake_bulk_create_out_t,
        bake_bulk_create_ult_handler);
132 133 134
    MERCURY_REGISTER(hg_class, "bake_bulk_write_rpc", bake_bulk_write_in_t, 
        bake_bulk_write_out_t,
        bake_bulk_write_ult_handler);
Philip Carns's avatar
Philip Carns committed
135 136 137
    MERCURY_REGISTER(hg_class, "bake_bulk_persist_rpc", bake_bulk_persist_in_t, 
        bake_bulk_persist_out_t,
        bake_bulk_persist_ult_handler);
138 139 140
    MERCURY_REGISTER(hg_class, "bake_bulk_get_size_rpc", bake_bulk_get_size_in_t, 
        bake_bulk_get_size_out_t,
        bake_bulk_get_size_ult_handler);
141 142 143
    MERCURY_REGISTER(hg_class, "bake_bulk_read_rpc", bake_bulk_read_in_t, 
        bake_bulk_read_out_t,
        bake_bulk_read_ult_handler);
Philip Carns's avatar
Philip Carns committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    /* 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);

    ABT_finalize();

    HG_Context_destroy(hg_context);
    HG_Finalize(hg_class);

166
    pmemobj_close(g_pmem_pool);
167

Philip Carns's avatar
Philip Carns committed
168 169 170
    return(0);
}