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) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/tests/lp_utils.c
34 * Author: You Feng <youfeng@clusterfs.com>
42 #include <sys/types.h>
45 #include <sys/ioctl.h>
48 #include "lustre/lustre_user.h"
49 #include "lustre/tests/lp_utils.h"
51 #define MAX_PROCESSES 8
58 struct timeval t1, t2;
61 static char datestring[80];
65 timestamp = time(NULL);
66 strftime(datestring, 80, "%T", localtime(×tamp));
71 void begin(char *str) {
72 if (verbose > 0 && rank == 0) {
73 gettimeofday(&t1, NULL);
74 printf("%s:\tBeginning %s\n", timestamp(), str);
82 MPI_Barrier(MPI_COMM_WORLD);
83 if (verbose > 0 && rank == 0) {
84 gettimeofday(&t2, NULL);
85 elapsed = (t2.tv_sec + ((float)t2.tv_usec/1000000))
86 - (t1.tv_sec + ((float)t1.tv_usec/1000000));
88 printf("%s:\tFinished %-15s(%.2f min)\n",
89 timestamp(), str, elapsed / 60);
91 printf("%s:\tFinished %-15s(%.3f sec)\n",
92 timestamp(), str, elapsed);
99 void dump_diff(char *orig_buf, char *buf, int size, long _off)
104 printf("commpared buf size %d, at offset %lu\n\n", size, _off);
107 printf("original buf:\n");
109 end = orig_buf + size;
112 printf(" %8lx", *(long *)p);
117 if (i%8) printf("\n\n");
122 printf("different data: diff_data(orig_data)\n");
129 if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
130 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
132 *(unsigned long *)(orig_buf + off));
138 printf("\n %d total differents found\n\n", diff);
142 void lp_gethostname(void)
144 if (gethostname(hostname, 1024) == -1) {
145 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
146 MPI_Abort(MPI_COMM_WORLD, 2);
150 /* This function does not FAIL if the requested "name" does not exit.
151 * This is just to clean up any files or directories left over from
154 void remove_file_or_dir(char *name)
157 char errmsg[MAX_FILENAME_LEN + 20];
159 if (stat(name, &statbuf) != -1) {
160 if (S_ISREG(statbuf.st_mode)) {
161 printf("stale file found\n");
162 if (unlink(name) == -1) {
163 sprintf(errmsg, "unlink of %s", name);
167 if (S_ISDIR(statbuf.st_mode)) {
168 printf("stale directory found\n");
169 if (rmdir(name) == -1) {
170 sprintf(errmsg, "rmdir of %s", name);
177 void create_file(char *name, long filesize, int fill)
179 static char filename[MAX_FILENAME_LEN];
180 char errmsg[MAX_FILENAME_LEN + 20];
185 long left = filesize;
187 /* Process 0 creates the test file(s) */
189 sprintf(filename, "%s/%s", testdir, name);
190 remove_file_or_dir(filename);
191 if ((fd = creat(filename, FILEMODE)) == -1) {
192 sprintf(errmsg, "create of file %s", filename);
196 if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
198 sprintf(errmsg, "lseek of file %s", filename);
201 if (write(fd, &zero, 1) == -1) {
203 sprintf(errmsg, "write of file %s", filename);
207 if (filesize > 0 && fill) {
208 if (lseek(fd, 0, SEEK_SET) == -1) {
210 sprintf(errmsg, "lseek of file %s", filename);
213 memset(buf, c, 1024);
215 if ((rc = write(fd, buf,
216 left > (1024 * 8) ? (1024 * 8) : left))
219 sprintf(errmsg, "write of file %s", filename);
225 if (close(fd) == -1) {
226 sprintf(errmsg, "close of file %s", filename);
232 void check_stat(char *filename, struct stat *state, struct stat *old_state)
234 char errmsg[MAX_FILENAME_LEN+20];
236 if (stat(filename, state) == -1) {
237 sprintf(errmsg, "stat of file %s", filename);
241 if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
243 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
248 void remove_file(char *name)
250 char filename[MAX_FILENAME_LEN];
251 char errmsg[MAX_FILENAME_LEN + 20];
253 /* Process 0 remove the file(s) */
255 sprintf(filename, "%s/%s", testdir, name);
256 if (unlink(filename) == -1) {
257 sprintf(errmsg, "unlink of file %s", filename);
263 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
266 long long off, data[2];
267 int cp, left = buf_size;
273 cp = left > sizeof(data) ? sizeof(data) : left;