bb-copy-from.c 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

16
#include "bake-bulk-client.h"
17 18 19 20 21

/* client program that will copy a bake bulk region out to a POSIX file */

int main(int argc, char **argv) 
{
22 23 24 25 26
    int i;
    char cli_addr_prefix[64] = {0};
    char *svr_addr_str;
    hg_addr_t svr_addr;
    margo_instance_id mid;
27
    bake_target_id_t bti;
28 29
    hg_return_t hret;
    int ret;
30 31 32 33 34 35 36 37 38 39 40
    bake_bulk_region_id_t rid;
    int fd;
    char* local_region;
    int region_fd;
    uint64_t check_size;
 
    if(argc != 4)
    {
        fprintf(stderr, "Usage: bb-copy-from <server addr> <identifier file> <output file>\n");
        fprintf(stderr, "  Example: ./bb-copy-from tcp://localhost:1234 /tmp/bb-copy-rid.0GjOlu /tmp/output.dat\n");
        return(-1);
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    }
    svr_addr_str = argv[1];

    /* initialize Margo using the transport portion of the server
     * address (i.e., the part before the first : character if present)
     */
    for(i=0; (i<63 && svr_addr_str[i] != '\0' && svr_addr_str[i] != ':'); i++)
        cli_addr_prefix[i] = svr_addr_str[i];

    mid = margo_init(cli_addr_prefix, MARGO_CLIENT_MODE, 0, -1);
    if(mid == MARGO_INSTANCE_NULL)
    {
        fprintf(stderr, "Error: margo_init()\n");
        return -1;
    }

    hret = margo_addr_lookup(mid, svr_addr_str, &svr_addr);
    if(hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: margo_addr_lookup()\n");
        margo_finalize(mid);
        return(-1);
    }
64

65
    ret = bake_probe_instance(mid, svr_addr, &bti);
66 67 68
    if(ret < 0)
    {
        fprintf(stderr, "Error: bake_probe_instance()\n");
69 70
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
71 72 73
        return(-1);
    }

74
    region_fd = open(argv[2], O_RDONLY);
75 76
    if(region_fd < 0)
    {
77
        perror("open rid");
78
        bake_release_instance(bti);
79 80
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
81 82 83 84 85 86 87 88 89
        return(-1);
    }

    ret = read(region_fd, &rid, sizeof(rid));
    if(ret != sizeof(rid))
    {
        perror("read");
        close(region_fd);
        bake_release_instance(bti);
90 91
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
92 93 94 95 96
        return(-1);
    }
    close(region_fd);

    ret = bake_bulk_get_size(bti, rid, &check_size);
97
    if(ret != 0)
98 99 100
    {
        fprintf(stderr, "Error: bake_bulk_get_size()\n");
        bake_release_instance(bti);
101 102
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
103 104 105
        return(-1);
    }

106
    fd = open(argv[3], O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR);
107 108
    if(fd < 0)
    {
109
        perror("open output");
110
        bake_release_instance(bti);
111 112
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
113 114 115
        return(-1);
    }

116 117 118 119 120 121
    ret = ftruncate(fd, check_size);
    if(ret < 0)
    {
        perror("ftruncate");
        close(fd);
        bake_release_instance(bti);
122 123
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
124 125 126
        return(-1);
    }

127
    local_region = mmap(NULL, check_size, PROT_WRITE, MAP_SHARED, fd, 0);
128
    if(local_region == MAP_FAILED)
129 130 131 132
    {
        perror("mmap");
        close(fd);
        bake_release_instance(bti);
133 134
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        return(-1);
    }

    /* transfer data */
    ret = bake_bulk_read(
        bti,
        rid,
        0,
        local_region,
        check_size);
    if(ret != 0)
    {
        munmap(local_region, check_size);
        close(fd);
        bake_release_instance(bti);
150 151
        margo_addr_free(mid, svr_addr);
        margo_finalize(mid);
152 153 154 155 156 157 158
        fprintf(stderr, "Error: bake_bulk_read()\n");
        return(-1);
    }

    munmap(local_region, check_size);
    close(fd);
    bake_release_instance(bti);
159 160
    margo_addr_free(mid, svr_addr);
    margo_finalize(mid);
161 162 163 164

    return(0);
}