Whamcloud - gitweb
b=17747
[fs/lustre-release.git] / lustre / tests / sleeptest.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
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <time.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #define BUFSIZE (4096)
47
48 #define min(a,b) ((a) < (b) ? (a) : (b))
49
50 int main(int argc, char *argv[])
51 {
52
53         FILE *w_str;
54         int read_fd;
55         int rc, iter;
56         int line, delta, next;
57         int sleeptime = 0;
58         char *now_time;
59         const char ok_chars[] = "MonTueWedThuFriSatSun"
60                                 "JanFebMarAprMayJunJulAugSepOctNovDec"
61                                 "Line 0123456789 of file, written at:\n";
62
63         char buf_r[BUFSIZE];
64
65         char pathname[256] = "/mnt/lustre/linetest_";
66         char *host;
67
68         if (argc > 1) {
69                 strncpy(pathname, argv[1], 255);
70                 pathname[255] = '\0';
71         }
72
73         host = getenv("HOSTNAME");
74         if (host)
75                 strcat(pathname, host);
76
77         if (argc > 2)
78                 sleeptime = strtoul(argv[2], NULL, 0);
79
80         if (sleeptime == 0)
81                 sleeptime = 30;
82
83         printf("Test file used is: %s at %ds intervals\n", pathname, sleeptime);
84
85         w_str = fopen(pathname, "wb");
86         if (w_str == NULL) {
87                 perror("fopen");
88                 exit(1);
89         }
90         read_fd = open(pathname, O_RDONLY);
91         if (read_fd < 0) {
92                 perror("open");
93                 exit(1);
94         }
95
96         next = 1;
97         delta = 17;
98         iter = 1;
99         while (1) {
100                 time_t now;
101                 struct tm *t;
102                 long offset;
103
104                 now = time((time_t *)NULL);
105                 t = localtime(&now);
106                 now_time = asctime(t);
107
108                 printf("iter: %d\n", iter);
109
110                 for (line=next; line<(next+delta); line++) {
111                         rc = fprintf(w_str, "Line %8d of file, written at: %s",
112                                      line, now_time);
113                         /* \n comes from ctime() result */
114                         if (rc <= 0) {
115                                 perror("fprintf");
116                                 exit(4);
117                         }
118                         rc = fflush(w_str);
119                         if (rc != 0) {
120                                 perror("fflush");
121                                 exit(5);
122                         }
123                 }
124                 next += delta;
125
126                 /* Check for corruption */
127                 offset = ftell(w_str);
128                 rc = lseek(read_fd, offset & ~4095, SEEK_SET);
129                 if (rc != (offset & ~4095)) {
130                         perror("lseek");
131                         exit(7);
132                 }
133
134                 rc = read(read_fd, buf_r, min(100, offset & 4095));
135                 if (rc != min(100, offset & 4095)) {
136                         printf("rc: %d, off %lu buf: '%s'\n", rc,offset,buf_r);
137                         exit(8);
138                 }
139                 buf_r[rc] = 0;
140                 /* Chars from "C" days/months, and above Line */
141                 if (strspn(buf_r, ok_chars) != rc) {
142                         printf("Corruption detected at %lu on %s",
143                                offset & ~4095, now_time);
144                         exit(9);
145                 }
146
147                 sleep(sleeptime);
148                 iter++;
149         }
150
151 }