root/branches/windows-client/test/client/sysint/io-test.c @ 8638

Revision 8638, 6.3 KB (checked in by sampson, 2 years ago)

Testing client

Line 
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7#include <time.h>
8#include "client.h"
9#ifndef WIN32
10#include <sys/time.h>
11#include <unistd.h>
12#endif
13#include <sys/types.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <assert.h>
17#include "pvfs2-util.h"
18#include "pvfs2-mgmt.h"
19#include "pvfs2-internal.h"
20
21#ifdef WIN32
22#define snprintf    _snprintf
23#define rindex      strrchr
24#endif
25
26#define DEFAULT_IO_SIZE 8*1024*1024
27
28int main(int argc, char **argv)
29{
30    PVFS_sysresp_lookup resp_lk;
31    PVFS_sysresp_create resp_cr;
32    PVFS_sysresp_io resp_io;
33    char *filename = NULL;
34    int ret = -1, io_size = DEFAULT_IO_SIZE;
35    int *io_buffer = NULL;
36    int i, errors, buffer_size;
37    PVFS_fs_id fs_id;
38    char name[512] = {0};
39    char *entry_name = NULL;
40    PVFS_credentials credentials;
41    PVFS_object_ref parent_refn;
42    PVFS_sys_attr attr;
43    PVFS_object_ref pinode_refn;
44    PVFS_Request file_req;
45    PVFS_Request mem_req;
46    void *buffer = NULL;
47    PVFS_sysresp_getattr resp_getattr;
48    PVFS_handle *dfile_array = NULL;
49
50    if (argc != 2)
51    {
52        fprintf(stderr, "Usage: %s <file name>\n", argv[0]);
53        return (-1);
54    }
55
56    /* create a buffer for running I/O on */
57    io_buffer = (int *) malloc(io_size * sizeof(int));
58    if (!io_buffer)
59    {
60        return (-1);
61    }
62
63    /* put some data in the buffer so we can verify */
64    for (i = 0; i < io_size; i++)
65    {
66        io_buffer[i] = i;
67    }
68
69    ret = PVFS_util_init_defaults();
70    if (ret < 0)
71    {
72        PVFS_perror("PVFS_util_init_defaults", ret);
73        return (-1);
74    }
75    ret = PVFS_util_get_default_fsid(&fs_id);
76    if (ret < 0)
77    {
78        PVFS_perror("PVFS_util_get_default_fsid", ret);
79        return (-1);
80    }
81
82    if (argv[1][0] == '/')
83    {
84        snprintf(name, 512, "%s", argv[1]);
85    }
86    else
87    {
88        snprintf(name, 512, "/%s", argv[1]);
89    }
90
91    PVFS_util_gen_credentials(&credentials);
92    ret = PVFS_sys_lookup(fs_id, name, &credentials,
93                          &resp_lk, PVFS2_LOOKUP_LINK_FOLLOW, NULL);
94    if (ret == -PVFS_ENOENT)
95    {
96        PVFS_sysresp_getparent gp_resp;
97
98        printf("IO-TEST: lookup failed; creating new file.\n");
99
100        memset(&gp_resp, 0, sizeof(PVFS_sysresp_getparent));
101        ret = PVFS_sys_getparent(fs_id, name, &credentials, &gp_resp, NULL);
102        if (ret < 0)
103        {
104            PVFS_perror("PVFS_sys_getparent failed", ret);
105            return ret;
106        }
107
108        attr.owner = credentials.uid;
109        attr.group = credentials.gid;
110        attr.perms = PVFS_U_WRITE | PVFS_U_READ;
111        attr.atime = attr.ctime = attr.mtime = time(NULL);
112        attr.mask = PVFS_ATTR_SYS_ALL_SETABLE;
113        parent_refn = gp_resp.parent_ref;
114
115        entry_name = rindex(name, (int)'/');
116        assert(entry_name);
117        entry_name++;
118        assert(entry_name);
119
120        ret = PVFS_sys_create(entry_name, parent_refn, attr,
121                              &credentials, NULL, &resp_cr, NULL, NULL);
122        if (ret < 0)
123        {
124            PVFS_perror("PVFS_sys_create() failure", ret);
125            return (-1);
126        }
127
128        pinode_refn.fs_id = fs_id;
129        pinode_refn.handle = resp_cr.ref.handle;
130    }
131    else
132    {
133        printf("IO-TEST: lookup succeeded; performing I/O on "
134               "existing file.\n");
135
136        pinode_refn.fs_id = fs_id;
137        pinode_refn.handle = resp_lk.ref.handle;
138    }
139
140        /**************************************************************
141         * carry out I/O operation
142         */
143
144    printf("IO-TEST: performing write on handle: %ld, fs: %d\n",
145           (long) pinode_refn.handle, (int) pinode_refn.fs_id);
146
147    buffer = io_buffer;
148    buffer_size = io_size * sizeof(int);
149
150    /*
151      file datatype is tiled, so we can get away with a trivial type
152      here
153    */
154    file_req = PVFS_BYTE;
155
156    ret = PVFS_Request_contiguous(io_size * sizeof(int),
157                                  PVFS_BYTE, &(mem_req));
158    if (ret < 0)
159    {
160        PVFS_perror("PVFS_request_contiguous failure", ret);
161        return (-1);
162    }
163
164    ret = PVFS_sys_write(pinode_refn, file_req, 0, buffer, mem_req,
165                         &credentials, &resp_io, NULL);
166    if (ret < 0)
167    {
168        PVFS_perror("PVFS_sys_write failure", ret);
169        return (-1);
170    }
171
172    printf("IO-TEST: wrote %d bytes.\n", (int) resp_io.total_completed);
173
174    /* uncomment and try out the readback-and-verify stuff that follows
175     * once reading back actually works */
176    memset(io_buffer, 0, io_size * sizeof(int));
177
178    /* verify */
179    printf("IO-TEST: performing read on handle: %ld, fs: %d\n",
180           (long) pinode_refn.handle, (int) pinode_refn.fs_id);
181
182    ret = PVFS_sys_read(pinode_refn, file_req, 0, buffer, mem_req,
183                        &credentials, &resp_io, NULL);
184    if (ret < 0)
185    {
186        PVFS_perror("PVFS_sys_read failure", ret);
187        return (-1);
188    }
189    printf("IO-TEST: read %d bytes.\n", (int) resp_io.total_completed);
190    if ((io_size * sizeof(int)) != resp_io.total_completed)
191    {
192        fprintf(stderr, "Error: SHORT READ! skipping verification...\n");
193    }
194    else
195    {
196        errors = 0;
197        for (i = 0; i < io_size; i++)
198        {
199            if (i != io_buffer[i])
200            {
201                fprintf(stderr,
202                        "error: element %d differs: should be %d, is %d\n", i,
203                        i, io_buffer[i]);
204                errors++;
205            }
206        }
207        if (errors != 0)
208        {
209            fprintf(stderr, "ERROR: found %d errors\n", errors);
210        }
211        else
212        {
213            printf("IO-TEST: no errors found.\n");
214        }
215    }
216
217    /* test out some of the mgmt functionality */
218    ret = PVFS_sys_getattr(pinode_refn, PVFS_ATTR_SYS_ALL_NOSIZE,
219                           &credentials, &resp_getattr, NULL);
220    if (ret < 0)
221    {
222        PVFS_perror("PVFS_sys_getattr", ret);
223        return (-1);
224    }
225
226    printf("Target file had %d datafiles:\n", resp_getattr.attr.dfile_count);
227
228    dfile_array = (PVFS_handle *) malloc(resp_getattr.attr.dfile_count
229                                         * sizeof(PVFS_handle));
230    if (!dfile_array)
231    {
232        perror("malloc");
233        return (-1);
234    }
235
236    ret = PVFS_mgmt_get_dfile_array(pinode_refn, &credentials,
237                                    dfile_array, resp_getattr.attr.dfile_count, NULL);
238    if (ret < 0)
239    {
240        PVFS_perror("PVFS_mgmt_get_dfile_array", ret);
241        return (-1);
242    }
243    for (i = 0; i < resp_getattr.attr.dfile_count; i++)
244    {
245        printf("%llu\n", llu(dfile_array[i]));
246    }
247
248        /**************************************************************
249         * shut down pending interfaces
250         */
251
252    ret = PVFS_sys_finalize();
253    if (ret < 0)
254    {
255        fprintf(stderr, "Error: PVFS_sys_finalize() failed with errcode = %d\n",
256                ret);
257        return (-1);
258    }
259
260    free(filename);
261    free(io_buffer);
262    return (0);
263}
264
265/*
266 * Local variables:
267 *  c-indent-level: 4
268 *  c-basic-offset: 4
269 * End:
270 *
271 * vim: ts=8 sts=4 sw=4 expandtab
272 */
Note: See TracBrowser for help on using the browser.