1 /*
2 *
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31
32 /*
33 * thruput.c
34 *
35 * tests "throughput" of a specified device by reading as quickly as possible
36 *
37 */
38
39 #include <stdio.h>
40 #include <sys/time.h>
41 #ifdef __QNX__
42 # include <fcntl.h>
43 #else
44 # include <sys/fcntl.h>
45 #endif
46 #include <stdlib.h>
47 #include <unistd.h>
48
49 int main(int argc, char **argv)
50 {
51 int count = 2 << 20;
52 int savecount;
53 int blocksize = 4096;
54 char *dev;
55 int fd;
56 struct timeval start;
57 struct timeval end;
58 char *buffer;
59 double timelag;
60 int opt;
61 int graph = 0;
62
63 if (argc < 2) {
64 fprintf(stderr, "usage: %s devicefile [-g] [-b blocksize] [-l bytes]\n", argv[0]);
65 fprintf(stderr, " -g for 'graphable' output\n");
66 exit(1);
67 }
68
69 dev = argv[1];
70 argv++;
71 argc--;
72
73 /* parse args
74 */
75 do {
76 opt = getopt (argc, argv, "b:l:g");
77 switch (opt)
78 {
79 case 'g':
80 graph = 1;
81 break;
82 case 'b':
83 blocksize = atoi(optarg);
84 break;
85 case 'l':
86 count = atoi(optarg);
87 break;
88 }
89 }
90 while (opt > 0);
91
92 if (!graph)
93 printf("%s: Reading %d bytes from %s, blocksize %d.\n",
94 argv[0], count, dev, blocksize);
95
96 /* open */
97 fd = open(dev, O_RDONLY);
98 if (fd < 0) {
99 fprintf(stderr, "%s: Unable to open device %s\n", argv[0], dev);
100 perror(argv[0]);
101 exit(1);
102 }
103
104 /* allocate */
105 buffer = malloc(blocksize);
106 if (buffer == NULL) {
107 fprintf(stderr, "%s: Unable to allocate %d bytes\n", argv[0], blocksize);
108 perror(argv[0]);
109 exit(1);
110 }
111
112 /* timestamp */
113 gettimeofday(&start, NULL);
114
115 /* read */
116 savecount = count;
117 while (count > 0) {
118 int block = count;
119 int status;
120 if (block > blocksize) block = blocksize;
121
122 status = read(fd, buffer, block);
123
124 if (status < 0) {
125 fprintf(stderr, "%s: Read failed\n", argv[0]);
126 perror(argv[0]);
127 exit(1);
128 }
129
130 count -= status;
131 if (status == 0) {
132 fprintf(stderr, "%s: Zero-length read\n", argv[0]);
133 }
134 }
135
136 /* timestamp */
137 gettimeofday(&end, NULL);
138
139 /* calc */
140 timelag =
141 ((end.tv_usec - start.tv_usec) / 1000000.0) +
142 end.tv_sec - start.tv_sec;
143
144 /* print */
145 if (graph)
146 printf("%d \t%d \t%f\n", savecount, blocksize, timelag);
147 else
148 printf("%s: %d bytes transferred in %f seconds => %f bytes/sec.\n",
149 argv[0], savecount, timelag, savecount/timelag);
150
151 return 0;
152 }
153
154
155
156
157
158
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.