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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/lp_utils.c
38 * Author: You Feng <youfeng@clusterfs.com>
46 #include <sys/types.h>
49 #include <sys/ioctl.h>
52 #include "lustre/lustre_user.h"
53 #include "lustre/tests/lp_utils.h"
55 #define MAX_PROCESSES 8
62 struct timeval t1, t2;
65 static char datestring[80];
69 timestamp = time(NULL);
70 strftime(datestring, 80, "%T", localtime(×tamp));
75 inline void begin(char *str) {
76 if (verbose > 0 && rank == 0) {
77 gettimeofday(&t1, NULL);
78 printf("%s:\tBeginning %s\n", timestamp(), str);
83 inline void end(char *str) {
86 MPI_Barrier(MPI_COMM_WORLD);
87 if (verbose > 0 && rank == 0) {
88 gettimeofday(&t2, NULL);
89 elapsed = (t2.tv_sec + ((float)t2.tv_usec/1000000))
90 - (t1.tv_sec + ((float)t1.tv_usec/1000000));
92 printf("%s:\tFinished %-15s(%.2f min)\n",
93 timestamp(), str, elapsed / 60);
95 printf("%s:\tFinished %-15s(%.3f sec)\n",
96 timestamp(), str, elapsed);
103 void dump_diff(char *orig_buf, char *buf, int size, long _off)
108 printf("commpared buf size %d, at offset %lu\n\n", size, _off);
111 printf("original buf:\n");
113 end = orig_buf + size;
116 printf(" %8lx", *(long *)p);
121 if (i%8) printf("\n\n");
126 printf("different data: diff_data(orig_data)\n");
133 if (memcmp(p, orig_buf + off, sizeof(long)) != 0) {
134 printf("\toff: %5d,\tdata: %8lx (%8lx)\n", off,
136 *(unsigned long *)(orig_buf + off));
142 printf("\n %d total differents found\n\n", diff);
146 void lp_gethostname(void)
148 if (gethostname(hostname, 1024) == -1) {
149 fprintf(stderr, "gethostname: (%d)%s", errno, strerror(errno));
150 MPI_Abort(MPI_COMM_WORLD, 2);
154 /* This function does not FAIL if the requested "name" does not exit.
155 * This is just to clean up any files or directories left over from
158 void remove_file_or_dir(char *name)
161 char errmsg[MAX_FILENAME_LEN + 20];
163 if (stat(name, &statbuf) != -1) {
164 if (S_ISREG(statbuf.st_mode)) {
165 printf("stale file found\n");
166 if (unlink(name) == -1) {
167 sprintf(errmsg, "unlink of %s", name);
171 if (S_ISDIR(statbuf.st_mode)) {
172 printf("stale directory found\n");
173 if (rmdir(name) == -1) {
174 sprintf(errmsg, "rmdir of %s", name);
181 void create_file(char *name, long filesize, int fill)
183 static char filename[MAX_FILENAME_LEN];
184 char errmsg[MAX_FILENAME_LEN + 20];
189 long left = filesize;
191 /* Process 0 creates the test file(s) */
193 sprintf(filename, "%s/%s", testdir, name);
194 remove_file_or_dir(filename);
195 if ((fd = creat(filename, FILEMODE)) == -1) {
196 sprintf(errmsg, "create of file %s", filename);
200 if (lseek(fd, filesize - 1, SEEK_SET) == -1) {
202 sprintf(errmsg, "lseek of file %s", filename);
205 if (write(fd, &zero, 1) == -1) {
207 sprintf(errmsg, "write of file %s", filename);
211 if (filesize > 0 && fill) {
212 if (lseek(fd, 0, SEEK_SET) == -1) {
214 sprintf(errmsg, "lseek of file %s", filename);
217 memset(buf, c, 1024);
219 if ((rc = write(fd, buf,
220 left > (1024 * 8) ? (1024 * 8) : left))
223 sprintf(errmsg, "write of file %s", filename);
229 if (close(fd) == -1) {
230 sprintf(errmsg, "close of file %s", filename);
236 void check_stat(char *filename, struct stat *state, struct stat *old_state)
238 char errmsg[MAX_FILENAME_LEN+20];
240 if (stat(filename, state) == -1) {
241 sprintf(errmsg, "stat of file %s", filename);
245 if (memcmp(state, old_state, sizeof(struct stat)) != 0) {
247 sprintf(errmsg, LP_STAT_FMT, LP_STAT_ARGS);
252 void remove_file(char *name)
254 char filename[MAX_FILENAME_LEN];
255 char errmsg[MAX_FILENAME_LEN + 20];
257 /* Process 0 remove the file(s) */
259 sprintf(filename, "%s/%s", testdir, name);
260 if (unlink(filename) == -1) {
261 sprintf(errmsg, "unlink of file %s", filename);
267 void fill_stride(char *buf, int buf_size, long long rank, long long _off)
270 long long off, data[2];
271 int cp, left = buf_size;
277 cp = left > sizeof(data) ? sizeof(data) : left;