Whamcloud - gitweb
b=19856
[fs/lustre-release.git] / lustre / tests / mpi / lp_utils.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/tests/lp_utils.c
37  *
38  * Author: You Feng <youfeng@clusterfs.com>
39  */
40
41 #include <mpi.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <time.h>
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <asm/types.h>
48 #include <sys/stat.h>
49 #include <unistd.h>
50 #include <sys/ioctl.h>
51 #include <fcntl.h>
52 #include <errno.h>
53 #include <libcfs/libcfs.h>
54 #include "lustre/lustre_user.h"
55 #include "lp_utils.h"
56
57 #define MAX_PROCESSES 8
58
59 int verbose = 0;
60 int debug = 0;
61
62 char hostname[1024];
63
64 struct timeval t1, t2;
65
66 char *timestamp() {
67         static char datestring[80];
68         time_t timestamp;
69
70         fflush(stdout);
71         timestamp = time(NULL);
72         strftime(datestring, 80, "%T", localtime(&timestamp));
73
74         return datestring;
75 }
76
77 inline void begin(char *str) {
78         if (verbose > 0 && rank == 0) {
79                 gettimeofday(&t1, NULL);
80                 printf("%s:\tBeginning %s\n", timestamp(), str);
81                 fflush(stdout);
82         }
83 }
84
85 inline void end(char *str) {
86         float elapsed;
87
88         MPI_Barrier(MPI_COMM_WORLD);
89         if (verbose > 0 && rank == 0) {
90                 gettimeofday(&t2, NULL);
91                 elapsed = (t2.tv_sec + ((float)t2.tv_usec/1000000))
92                           - (t1.tv_sec + ((float)t1.tv_usec/1000000));
93                 if (elapsed >= 60) {
94                         printf("%s:\tFinished %-15s(%.2f min)\n",
95                                timestamp(), str, elapsed / 60);
96                 } else {
97                         printf("%s:\tFinished %-15s(%.3f sec)\n",
98                                timestamp(), str, elapsed);
99
100                 }
101                 fflush(stdout);
102         }
103 }
104
105 void dump_diff(char *orig_buf, char *buf, int size, long _off)
106 {
107         int i, diff, off;
108         char *p, *end;
109
110         printf("commpared buf size %d, at offset %lu\n\n", size, _off);
111
112         if (orig_buf) {
113                 printf("original buf:\n");
114                 p = orig_buf;
115                 end = orig_buf + size;
116                 i = 1;
117                 while (p < end) {
118                         printf(" %8lx", *(long *)p);
119                         p += sizeof(long);
120                         if (i++%8 == 0)
121                                 printf("\n");
122                 }
123                 if (i%8) printf("\n\n");
124                 else printf("\n");
125         }
126
127         if (buf) {
128                 printf("different data: diff_data(orig_data)\n");
129                 diff = 0;
130                 off = 0;
131                 i = 1;
132                 p = buf;
133                 end = buf + size;
134                 while (p < end) {
135                         if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
136                                 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
137                                        *(unsigned long *)p,
138                                        *(unsigned long *)(orig_buf + off));
139                                 diff++;
140                         }
141                         off += sizeof(long);
142                         p += sizeof(long);
143                 }
144                 printf("\n %d total differents found\n\n", diff);
145         }
146 }
147
148 void lp_gethostname(void)
149 {
150         if (gethostname(hostname, 1024) == -1) {
151                 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
152                 MPI_Abort(MPI_COMM_WORLD, 2);
153         }
154 }
155
156 /* This function does not FAIL if the requested "name" does not exit.
157  * This is just to clean up any files or directories left over from
158  * previous runs
159  */
160 void remove_file_or_dir(char *name)
161 {
162         struct stat statbuf;
163         char errmsg[MAX_FILENAME_LEN + 20];
164
165         if (stat(name, &statbuf) != -1) {
166                 if (S_ISREG(statbuf.st_mode)) {
167                         printf("stale file found\n");
168                         if (unlink(name) == -1) {
169                                 sprintf(errmsg, "unlink of %s", name);
170                                 FAIL(errmsg);
171                         }
172                 }
173                 if (S_ISDIR(statbuf.st_mode)) {
174                         printf("stale directory found\n");
175                         if (rmdir(name) == -1) {
176                                 sprintf(errmsg, "rmdir of %s", name);
177                                 FAIL(errmsg);
178                         }
179                 }
180         }
181 }
182
183 void create_file(char *name, long filesize, int fill)
184 {
185         static char filename[MAX_FILENAME_LEN];
186         char errmsg[MAX_FILENAME_LEN + 20];
187         char buf[1024 * 8];
188         char c = 'A' + size;
189         int fd, rc;
190         short zero = 0;
191         long left = filesize;
192
193         /* Process 0 creates the test file(s) */
194         if (rank == 0) {
195                 sprintf(filename, "%s/%s", testdir, name);
196                 remove_file_or_dir(filename);
197                 if ((fd = creat(filename, FILEMODE)) == -1) {
198                         sprintf(errmsg, "create of file %s", filename);
199                         FAIL(errmsg);
200                 }
201                 if (filesize > 0) {
202                         if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
203                                 close(fd);
204                                 sprintf(errmsg, "lseek of file %s", filename);
205                                 FAIL(errmsg);
206                         }
207                         if (write(fd, &zero, 1) == -1) {
208                                 close(fd);
209                                 sprintf(errmsg, "write of file %s", filename);
210                                 FAIL(errmsg);
211                         }
212                 }
213                 if (filesize > 0 && fill) {
214                         if (lseek(fd, 0, SEEK_SET) == -1) {
215                                 close(fd);
216                                 sprintf(errmsg, "lseek of file %s", filename);
217                                 FAIL(errmsg);
218                         }
219                         memset(buf, c, 1024);
220                         while (left > 0) {
221                                 if ((rc = write(fd, buf,
222                                                 left > (1024 * 8) ? (1024 * 8) : left))
223                                     == -1) {
224                                         close(fd);
225                                         sprintf(errmsg, "write of file %s", filename);
226                                         FAIL(errmsg);
227                                 }
228                                 left -= rc;
229                         }
230                 }
231                 if (close(fd) == -1) {
232                         sprintf(errmsg, "close of file %s", filename);
233                         FAIL(errmsg);
234                 }
235         }
236 }
237
238 void check_stat(char *filename, struct stat *state, struct stat *old_state)
239 {
240         char errmsg[MAX_FILENAME_LEN+20];
241
242         if (stat(filename, state) == -1) {
243                 sprintf(errmsg, "stat of file %s", filename);
244                 FAIL(errmsg);
245         }
246
247         if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
248                 errno = 0;
249                 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
250                 FAIL(errmsg);
251         }
252 }
253
254 void remove_file(char *name)
255 {
256         char filename[MAX_FILENAME_LEN];
257         char errmsg[MAX_FILENAME_LEN + 20];
258
259         /* Process 0 remove the file(s) */
260         if (rank == 0) {
261                 sprintf(filename, "%s/%s", testdir, name);
262                 if (unlink(filename) == -1) {
263                         sprintf(errmsg, "unlink of file %s", filename);
264                         FAIL(errmsg);
265                 }
266         }
267 }
268
269 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
270 {
271         char *p = buf;
272         long long off, data[2];
273         int cp, left = buf_size;
274
275         data[0] = rank;
276         off = _off;
277         while (left > 0) {
278                 data[1] = off;
279                 cp = left > sizeof(data) ? sizeof(data) : left;
280                 memcpy(p, data, cp);
281                 off += cp;
282                 p += cp;
283                 left -= cp;
284         }
285 }