bake-bulk-server.c 5.14 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

Philip Carns's avatar
Philip Carns committed
19 20 21
/* TODO: this should not be global in the long run; server may provide access
 * to multiple targets
 */
22 23 24 25 26
/* 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
27

Philip Carns's avatar
Philip Carns committed
28 29 30 31 32 33 34 35
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;
36 37
    char target_string[64];
    PMEMoid root_oid;
Philip Carns's avatar
Philip Carns committed
38

39
    if(argc != 3)
Philip Carns's avatar
Philip Carns committed
40
    {
41 42
        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
43 44 45
        return(-1);
    }

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

Philip Carns's avatar
Philip Carns committed
65 66 67 68 69 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
    /* 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
118
    mid = margo_init_pool(handler_pool, handler_pool, hg_context);
Philip Carns's avatar
Philip Carns committed
119 120
    assert(mid);

121
    /* register RPCs */
Philip Carns's avatar
Philip Carns committed
122 123
    MERCURY_REGISTER(hg_class, "bake_bulk_shutdown_rpc", void, void, 
        bake_bulk_shutdown_ult_handler);
124 125 126
    MERCURY_REGISTER(hg_class, "bake_bulk_create_rpc", bake_bulk_create_in_t, 
        bake_bulk_create_out_t,
        bake_bulk_create_ult_handler);
127 128 129
    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
130 131 132
    MERCURY_REGISTER(hg_class, "bake_bulk_persist_rpc", bake_bulk_persist_in_t, 
        bake_bulk_persist_out_t,
        bake_bulk_persist_ult_handler);
133 134 135
    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);
136 137 138
    MERCURY_REGISTER(hg_class, "bake_bulk_read_rpc", bake_bulk_read_in_t, 
        bake_bulk_read_out_t,
        bake_bulk_read_ult_handler);
139 140 141
    MERCURY_REGISTER(hg_class, "bake_bulk_probe_rpc", void, 
        bake_bulk_probe_out_t,
        bake_bulk_probe_ult_handler);
Philip Carns's avatar
Philip Carns committed
142 143 144
    MERCURY_REGISTER(hg_class, "bake_bulk_noop_rpc", void, 
        void,
        bake_bulk_noop_ult_handler);
Philip Carns's avatar
Philip Carns committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

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

167
    pmemobj_close(g_pmem_pool);
168

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