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