bake-copy-to.c 6.09 KB
Newer Older
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

9
#include <stdio.h>
10
#include <stdlib.h>
11 12
#include <assert.h>
#include <unistd.h>
Philip Carns's avatar
Philip Carns committed
13 14 15 16
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
17

18
#include "bake-client.h"
19

20
/* client program that will copy a POSIX file into a BAKE region */
21 22 23

int main(int argc, char **argv) 
{
24 25 26 27 28
    int i;
    char cli_addr_prefix[64] = {0};
    char *svr_addr_str;
    hg_addr_t svr_addr;
    margo_instance_id mid;
29
    bake_client_t bcl;
30
    bake_provider_handle_t bph;
31
    uint8_t mplex_id;
32
    uint32_t target_number;
33 34
    hg_return_t hret;
    int ret;
35
    bake_region_id_t rid;
Philip Carns's avatar
Philip Carns committed
36 37 38
    int fd;
    struct stat statbuf;
    char* local_region;
39 40
    int region_fd;
    char region_file[128];
41
    char region_str[128];
Philip Carns's avatar
Philip Carns committed
42
#ifdef USE_SIZECHECK_HEADERS
43
    uint64_t  check_size;
Philip Carns's avatar
Philip Carns committed
44
#endif
45
 
46
    if(argc != 5)
47
    {
48 49
        fprintf(stderr, "Usage: bake-copy-to <local file> <server addr> <mplex id> <target number>\n");
        fprintf(stderr, "  Example: ./bake-copy-to /tmp/foo.dat tcp://localhost:1234 1 3\n");
50
        return(-1);
51 52
    }
    svr_addr_str = argv[2];
53
    mplex_id = atoi(argv[3]);
54 55 56 57
    target_number = atoi(argv[4]);

    uint64_t num_targets;
    bake_target_id_t bti[target_number];
58

Philip Carns's avatar
Philip Carns committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    fd = open(argv[1], O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        return(-1);
    }
    ret = fstat(fd, &statbuf);
    if(ret < 0)
    {
        perror("fstat");
        close(fd);
        return(-1);
    }

    local_region = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
74
    if(local_region == MAP_FAILED)
Philip Carns's avatar
Philip Carns committed
75 76 77 78 79 80
    {
        perror("mmap");
        close(fd);
        return(-1);
    }

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /* 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");
        munmap(local_region, statbuf.st_size);
        close(fd);
        return -1;
    }

96 97 98
    ret = bake_client_init(mid, &bcl);
    if(ret != 0)
    {
99
        bake_perror("Error: bake_client_init()", ret);
100 101 102 103 104 105
        margo_finalize(mid);
        munmap(local_region, statbuf.st_size);
        close(fd);
        return -1;
    }

106 107 108 109
    hret = margo_addr_lookup(mid, svr_addr_str, &svr_addr);
    if(hret != HG_SUCCESS)
    {
        fprintf(stderr, "Error: margo_addr_lookup()\n");
110
        bake_client_finalize(bcl);
111 112 113 114 115 116
        margo_finalize(mid);
        munmap(local_region, statbuf.st_size);
        close(fd);
        return(-1);
    }

117 118 119 120 121 122 123 124
    ret = bake_provider_handle_create(bcl, svr_addr, mplex_id, &bph);
    if(ret != 0)
    {
        margo_addr_free(mid, svr_addr);
        bake_client_finalize(bcl);
        margo_finalize(mid);
        munmap(local_region, statbuf.st_size);
        close(fd);
125
        bake_perror("Error: bake_provider_handle_create()", ret);
126 127 128
        return(-1);
    }

129
    ret = bake_probe(bph, target_number, bti, &num_targets);
130 131
    if(ret < 0)
    {
132
        bake_provider_handle_release(bph);
133
        margo_addr_free(mid, svr_addr);
134
        bake_client_finalize(bcl);
135
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
136 137
        munmap(local_region, statbuf.st_size);
        close(fd);
138
        bake_perror( "Error: bake_probe()", ret);
139 140 141
        return(-1);
    }

142
    if(num_targets < target_number) {
Rob Latham's avatar
Rob Latham committed
143
        fprintf(stderr, "Error: provider has only %llu storage targets\n", (long long unsigned int)num_targets);
144 145 146 147 148 149 150 151
        margo_addr_free(mid, svr_addr);
        bake_client_finalize(bcl);
        margo_finalize(mid);
        munmap(local_region, statbuf.st_size);
        close(fd);
        return -1;
    }

Philip Carns's avatar
Philip Carns committed
152
    /* create region */
153
    ret = bake_create(bph, bti[target_number-1], statbuf.st_size, &rid);
Philip Carns's avatar
Philip Carns committed
154
    if(ret != 0)
155
    {
156
        bake_provider_handle_release(bph);
157
        margo_addr_free(mid, svr_addr);
158
        bake_client_finalize(bcl);
159
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
160 161
        munmap(local_region, statbuf.st_size);
        close(fd);
162
        bake_perror("Error: bake_create()", ret);
163 164 165
        return(-1);
    }

166 167 168
    bake_print_dbg_region_id_t(region_str, 127, rid);
    printf("# created bake region %s\n", region_str);

Philip Carns's avatar
Philip Carns committed
169
    /* transfer data */
170
    ret = bake_write(
171
        bph,
Philip Carns's avatar
Philip Carns committed
172 173 174 175 176 177
        rid,
        0,
        local_region,
        statbuf.st_size);
    if(ret != 0)
    {
178
        bake_provider_handle_release(bph);
179
        margo_addr_free(mid, svr_addr);
180
        bake_client_finalize(bcl);
181
        margo_finalize(mid);
Philip Carns's avatar
Philip Carns committed
182 183
        munmap(local_region, statbuf.st_size);
        close(fd);
184
        bake_perror("bake_write():", ret);
Philip Carns's avatar
Philip Carns committed
185 186
        return(-1);
    }
187

Philip Carns's avatar
Philip Carns committed
188 189
    munmap(local_region, statbuf.st_size);
    close(fd);
Philip Carns's avatar
Philip Carns committed
190

191
    ret = bake_persist(bph, rid, 0, statbuf.st_size);
Philip Carns's avatar
Philip Carns committed
192 193
    if(ret != 0)
    {
194
        bake_provider_handle_release(bph);
195
        margo_addr_free(mid, svr_addr);
196
        bake_client_finalize(bcl);
197
        margo_finalize(mid);
198
        bake_perror("Error: bake_persist()", ret);
Philip Carns's avatar
Philip Carns committed
199 200
        return(-1);
    }
201

202
#ifdef USE_SIZECHECK_HEADERS
203
    /* safety check size */
204
    ret = bake_get_size(bph, rid, &check_size);
205 206
    if(ret != 0)
    {
207
        bake_provider_handle_release(bph);
208
        margo_addr_free(mid, svr_addr);
209
        bake_client_finalize(bcl);
210
        margo_finalize(mid);
211
        bake_perror("Error: bake_get_size()", ret);
212 213 214 215 216 217 218 219
        return(-1);
    }

    if(check_size != statbuf.st_size)
    {
        fprintf(stderr, "Error: size mismatch!\n");
        return(-1);
    }
220 221 222 223 224 225
#endif
   
    bake_provider_handle_release(bph);
    margo_addr_free(mid, svr_addr);
    bake_client_finalize(bcl);
    margo_finalize(mid);
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    sprintf(region_file, "/tmp/bb-copy-rid.XXXXXX");
    region_fd = mkstemp(region_file);
    if(region_fd < 0)
    {
        perror("mkstemp");
    }
    else
    {
        ret = write(region_fd, &rid, sizeof(rid));
        if(ret < 0)
        {
            perror("write");
        }
        else
        {
            printf("RID written to %s\n", region_file);
243
            close(region_fd);
244 245
        }
    }
Philip Carns's avatar
Philip Carns committed
246
   
247 248 249
    return(0);
}