Whamcloud - gitweb
b=15266,16809
[fs/lustre-release.git] / lustre / tests / 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 <sys/stat.h>
48 #include <unistd.h>
49 #include <sys/ioctl.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <liblustre.h>
53 #include "lustre/lustre_user.h"
54 #include "lustre/tests/lp_utils.h"
55
56 #define MAX_PROCESSES 8
57
58 int verbose = 0;
59 int debug = 0;
60
61 char hostname[1024];
62
63 struct timeval t1, t2;
64
65 char *timestamp() {
66         static char datestring[80];
67         time_t timestamp;
68
69         fflush(stdout);
70         timestamp = time(NULL);
71         strftime(datestring, 80, "%T", localtime(&timestamp));
72
73         return datestring;
74 }
75
76 inline void begin(char *str) {
77         if (verbose > 0 && rank == 0) {
78                 gettimeofday(&t1, NULL);
79                 printf("%s:\tBeginning %s\n", timestamp(), str);
80                 fflush(stdout);
81         }
82 }
83
84 inline void end(char *str) {
85         float elapsed;
86
87         MPI_Barrier(MPI_COMM_WORLD);
88         if (verbose > 0 && rank == 0) {
89                 gettimeofday(&t2, NULL);
90                 elapsed = (t2.tv_sec + ((float)t2.tv_usec/1000000))
91                           - (t1.tv_sec + ((float)t1.tv_usec/1000000));
92                 if (elapsed >= 60) {
93                         printf("%s:\tFinished %-15s(%.2f min)\n",
94                                timestamp(), str, elapsed / 60);
95                 } else {
96                         printf("%s:\tFinished %-15s(%.3f sec)\n",
97                                timestamp(), str, elapsed);
98
99                 }
100                 fflush(stdout);
101         }
102 }
103
104 void dump_diff(char *orig_buf, char *buf, int size, long _off)
105 {
106         int i, diff, off;
107         char *p, *end;
108
109         printf("commpared buf size %d, at offset %lu\n\n", size, _off);
110
111         if (orig_buf) {
112                 printf("original buf:\n");
113                 p = orig_buf;
114                 end = orig_buf + size;
115                 i = 1;
116                 while (p < end) {
117                         printf(" %8lx", *(long *)p);
118                         p += sizeof(long);
119                         if (i++%8 == 0)
120                                 printf("\n");
121                 }
122                 if (i%8) printf("\n\n");
123                 else printf("\n");
124         }
125
126         if (buf) {
127                 printf("different data: diff_data(orig_data)\n");
128                 diff = 0;
129                 off = 0;
130                 i = 1;
131                 p = buf;
132                 end = buf + size;
133                 while (p < end) {
134                         if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
135                                 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
136                                        *(unsigned long *)p,
137                                        *(unsigned long *)(orig_buf + off));
138                                 diff++;
139                         }
140                         off += sizeof(long);
141                         p += sizeof(long);
142                 }
143                 printf("\n %d total differents found\n\n", diff);
144         }
145 }
146
147 void lp_gethostname(void)
148 {
149         if (gethostname(hostname, 1024) == -1) {
150                 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
151                 MPI_Abort(MPI_COMM_WORLD, 2);
152         }
153 }
154
155 /* This function does not FAIL if the requested "name" does not exit.
156  * This is just to clean up any files or directories left over from
157  * previous runs
158  */
159 void remove_file_or_dir(char *name)
160 {
161         struct stat statbuf;
162         char errmsg[MAX_FILENAME_LEN + 20];
163
164         if (stat(name, &statbuf) != -1) {
165                 if (S_ISREG(statbuf.st_mode)) {
166                         printf("stale file found\n");
167                         if (unlink(name) == -1) {
168                                 sprintf(errmsg, "unlink of %s", name);
169                                 FAIL(errmsg);
170                         }
171                 }
172                 if (S_ISDIR(statbuf.st_mode)) {
173                         printf("stale directory found\n");
174                         if (rmdir(name) == -1) {
175                                 sprintf(errmsg, "rmdir of %s", name);
176                                 FAIL(errmsg);
177                         }
178                 }
179         }
180 }
181
182 void create_file(char *name, long filesize, int fill)
183 {
184         static char filename[MAX_FILENAME_LEN];
185         char errmsg[MAX_FILENAME_LEN + 20];
186         char buf[1024 * 8];
187         char c = 'A' + size;
188         int fd, rc;
189         short zero = 0;
190         long left = filesize;
191
192         /* Process 0 creates the test file(s) */
193         if (rank == 0) {
194                 sprintf(filename, "%s/%s", testdir, name);
195                 remove_file_or_dir(filename);
196                 if ((fd = creat(filename, FILEMODE)) == -1) {
197                         sprintf(errmsg, "create of file %s", filename);
198                         FAIL(errmsg);
199                 }
200                 if (filesize > 0) {
201                         if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
202                                 close(fd);
203                                 sprintf(errmsg, "lseek of file %s", filename);
204                                 FAIL(errmsg);
205                         }
206                         if (write(fd, &zero, 1) == -1) {
207                                 close(fd);
208                                 sprintf(errmsg, "write of file %s", filename);
209                                 FAIL(errmsg);
210                         }
211                 }
212                 if (filesize > 0 && fill) {
213                         if (lseek(fd, 0, SEEK_SET) == -1) {
214                                 close(fd);
215                                 sprintf(errmsg, "lseek of file %s", filename);
216                                 FAIL(errmsg);
217                         }
218                         memset(buf, c, 1024);
219                         while (left > 0) {
220                                 if ((rc = write(fd, buf,
221                                                 left > (1024 * 8) ? (1024 * 8) : left))
222                                     == -1) {
223                                         close(fd);
224                                         sprintf(errmsg, "write of file %s", filename);
225                                         FAIL(errmsg);
226                                 }
227                                 left -= rc;
228                         }
229                 }
230                 if (close(fd) == -1) {
231                         sprintf(errmsg, "close of file %s", filename);
232                         FAIL(errmsg);
233                 }
234         }
235 }
236
237 void check_stat(char *filename, struct stat *state, struct stat *old_state)
238 {
239         char errmsg[MAX_FILENAME_LEN+20];
240
241         if (stat(filename, state) == -1) {
242                 sprintf(errmsg, "stat of file %s", filename);
243                 FAIL(errmsg);
244         }
245
246         if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
247                 errno = 0;
248                 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
249                 FAIL(errmsg);
250         }
251 }
252
253 void remove_file(char *name)
254 {
255         char filename[MAX_FILENAME_LEN];
256         char errmsg[MAX_FILENAME_LEN + 20];
257
258         /* Process 0 remove the file(s) */
259         if (rank == 0) {
260                 sprintf(filename, "%s/%s", testdir, name);
261                 if (unlink(filename) == -1) {
262                         sprintf(errmsg, "unlink of file %s", filename);
263                         FAIL(errmsg);
264                 }
265         }
266 }
267
268 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
269 {
270         char *p = buf;
271         long long off, data[2];
272         int cp, left = buf_size;
273
274         data[0] = rank;
275         off = _off;
276         while (left > 0) {
277                 data[1] = off;
278                 cp = left > sizeof(data) ? sizeof(data) : left;
279                 memcpy(p, data, cp);
280                 off += cp;
281                 p += cp;
282                 left -= cp;
283         }
284 }