Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / tests / write_time_limit.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 /* for O_DIRECT */
6 #define _GNU_SOURCE
7
8 #include<stdio.h>
9 #include<errno.h>
10 #include<fcntl.h>
11 #include<sys/types.h>
12 #include<sys/stat.h>
13 #include<unistd.h>
14 #include<signal.h>
15 #include<stdlib.h>
16 #include<string.h>
17
18 #define BUFFERSIZE 4096
19
20 /* This flag controls termination of the main loop. */
21 volatile sig_atomic_t keep_going = 1;
22
23 /* The signal handler just clears the flag and re-enables itself. */
24 void catch_alarm(int sig)
25 {
26         keep_going = 0;
27         signal(sig, catch_alarm);
28 }
29
30 int main(int argc, char **argv)
31 {
32         char *file;
33         unsigned char buf[BUFFERSIZE];
34         int fd, i, rc;
35         unsigned int test_time;
36         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
37
38         if (argc != 3) {
39                 printf("Invalid number of arguments.\n");
40                 printf("Usage %s <file> <test_time_in_seconds>\n", argv[0]);
41                 return -1;
42         }
43
44         file = argv[1];
45         test_time = atoi(argv[2]);
46
47         /* Establish a handler for SIGALRM signals. */
48         signal(SIGALRM, catch_alarm);
49
50         /* Set an alarm to go off in a little while. */
51         alarm(test_time);
52
53         fd = open(file, O_RDWR|O_TRUNC|O_CREAT|O_SYNC|O_LARGEFILE, mode);
54         if (fd < 0) {
55                 fprintf(stderr, "Error: Cannot open file named ");
56                 perror(file);
57                 return -1;
58         }
59
60         memset(buf, 1, BUFFERSIZE);
61         while (keep_going) {
62                 for (i = 0; i < 1024; i++) {
63                         rc = write(fd, buf, BUFFERSIZE);
64                         if (rc < 0) {
65                                 fprintf(stderr, "Error: Write error ");
66                                 perror(file);
67                                 exit(-1);
68                         } else if (rc != BUFFERSIZE) {
69                                 fprintf(stderr, "Error: Ddidn't write all data \n");
70                         }
71                 }
72
73                 if (ftruncate(fd, 0) < 0) {
74                         fprintf(stderr, "Error: Truncate error ");
75                         perror(file);
76                         exit(-1);
77                 }
78         }
79
80         if (close(fd) < 0) {
81                 fprintf(stderr, "Error: Cannot close ");
82                 perror(file);
83                 return -1;
84         }
85
86         return 0;
87 }