4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
30 * lustre/tests/lp_utils.c
32 * Author: You Feng <youfeng@clusterfs.com>
40 #include <sys/types.h>
41 #include <asm/types.h>
44 #include <sys/ioctl.h>
49 #define MAX_PROCESSES 8
56 struct timeval t1, t2;
60 static char datestring[80];
64 timestamp = time(NULL);
65 strftime(datestring, 80, "%T", localtime(×tamp));
72 if (verbose > 0 && rank == 0) {
73 gettimeofday(&t1, NULL);
74 printf("%s:\tBeginning %s\n", timestamp(), str);
83 MPI_Barrier(MPI_COMM_WORLD);
84 if (verbose > 0 && rank == 0) {
85 gettimeofday(&t2, NULL);
87 elapsed = t2.tv_sec - t1.tv_sec +
88 (float)(t2.tv_usec - t1.tv_usec) / 1000000;
90 printf("%s:\tFinished %-15s(%.2f min)\n",
91 timestamp(), str, elapsed / 60);
93 printf("%s:\tFinished %-15s(%.3f sec)\n",
94 timestamp(), str, elapsed);
100 void dump_diff(char *orig_buf, char *buf, int size, long _off)
105 printf("commpared buf size %d, at offset %lu\n\n", size, _off);
108 printf("original buf:\n");
110 end = orig_buf + size;
113 printf(" %8lx", *(long *)p);
125 printf("different data: diff_data(orig_data)\n");
132 if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
133 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
135 *(unsigned long *)(orig_buf + off));
141 printf("\n %d total differents found\n\n", diff);
145 void lp_gethostname(void)
147 if (gethostname(hostname, 1024) == -1) {
148 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
149 MPI_Abort(MPI_COMM_WORLD, 2);
153 /* This function does not FAIL if the requested "name" does not exit.
154 * This is just to clean up any files or directories left over from
157 void remove_file_or_dir(char *name)
160 char errmsg[MAX_FILENAME_LEN + 20];
162 if (stat(name, &statbuf) != -1) {
163 if (S_ISREG(statbuf.st_mode)) {
164 printf("stale file found\n");
165 if (unlink(name) == -1) {
166 sprintf(errmsg, "unlink of %s", name);
170 if (S_ISDIR(statbuf.st_mode)) {
171 printf("stale directory found\n");
172 if (rmdir(name) == -1) {
173 sprintf(errmsg, "rmdir of %s", name);
180 void create_file(char *name, long filesize, int fill)
182 static char filename[MAX_FILENAME_LEN];
183 char errmsg[MAX_FILENAME_LEN + 20];
188 long left = filesize;
190 /* Process 0 creates the test file(s) */
192 sprintf(filename, "%s/%s", testdir, name);
193 remove_file_or_dir(filename);
194 if ((fd = creat(filename, FILEMODE)) == -1) {
195 sprintf(errmsg, "create of file %s", filename);
199 if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
201 sprintf(errmsg, "lseek of file %s", filename);
204 if (write(fd, &zero, 1) == -1) {
206 sprintf(errmsg, "write of file %s", filename);
210 if (filesize > 0 && fill) {
211 if (lseek(fd, 0, SEEK_SET) == -1) {
213 sprintf(errmsg, "lseek of file %s", filename);
216 memset(buf, c, 1024);
218 if ((rc = write(fd, buf,
219 left > (1024 * 8) ? (1024 * 8) : left)) == -1) {
221 sprintf(errmsg, "write of file %s",
228 if (close(fd) == -1) {
229 sprintf(errmsg, "close of file %s", filename);
235 void check_stat(char *filename, struct stat *state, struct stat *old_state)
237 char errmsg[MAX_FILENAME_LEN + 20];
239 if (stat(filename, state) == -1) {
240 sprintf(errmsg, "stat of file %s", filename);
244 if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
246 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
251 void remove_file(char *name)
253 char filename[MAX_FILENAME_LEN];
254 char errmsg[MAX_FILENAME_LEN + 20];
256 /* Process 0 remove the file(s) */
258 sprintf(filename, "%s/%s", testdir, name);
259 if (unlink(filename) == -1) {
260 sprintf(errmsg, "unlink of file %s", filename);
266 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
269 long long off, data[2];
270 int cp, left = buf_size;
276 cp = left > sizeof(data) ? sizeof(data) : left;