2 * Copyright (c) International Business Machines Corp., 2001-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define FILE_OFFSET_BITS 64
21 #include <sys/types.h>
44 /* All these functions read the global mainconfig->bufferedio variable
45 * to determine if they are to do buffered i/o or normal.
47 * ha, well, they're supposed to anyway...!!! TODO -SR 2006/05/14
50 static void do_stats(struct timeval *start, struct timeval *end,
51 ffsb_thread_t *ft, ffsb_fs_t *fs, syscall_t sys)
59 timersub(end, start, &diff);
61 value = 1000000 * diff.tv_sec + diff.tv_usec;
63 if (ft && ft_needs_stats(ft, sys))
64 ft_add_stat(ft, sys, value);
65 if (fs && fs_needs_stats(fs, sys))
66 fs_add_stat(fs, sys, value);
69 static int fhopenhelper(char *filename, char *bufflags, int flags,
70 ffsb_thread_t *ft, ffsb_fs_t *fs)
73 struct timeval start, end;
74 int need_stats = ft_needs_stats(ft, SYS_OPEN) ||
75 fs_needs_stats(fs, SYS_OPEN);
80 gettimeofday(&start, NULL);
82 fd = open64(filename, flags, S_IRWXU);
89 gettimeofday(&end, NULL);
90 do_stats(&start, &end, ft, fs, SYS_OPEN);
96 int fhopenread(char *filename, ffsb_thread_t *ft, ffsb_fs_t *fs)
99 int directio = fs_get_directio(fs);
103 return fhopenhelper(filename, "r", flags, ft, fs);
106 int fhopenappend(char *filename, ffsb_thread_t *ft, ffsb_fs_t *fs)
108 int flags = O_APPEND | O_WRONLY;
109 int directio = fs_get_directio(fs);
113 return fhopenhelper(filename, "a", flags, ft, fs);
116 int fhopenwrite(char *filename, ffsb_thread_t *ft, ffsb_fs_t *fs)
118 int flags = O_WRONLY;
119 int directio = fs_get_directio(fs);
123 return fhopenhelper(filename, "w", flags, ft, fs);
126 int fhopencreate(char *filename, ffsb_thread_t *ft, ffsb_fs_t *fs)
128 int flags = O_CREAT | O_RDWR | O_TRUNC;
129 int directio = fs_get_directio(fs);
133 return fhopenhelper(filename, "rw", flags, ft, fs);
136 void fhread(int fd, void *buf, uint64_t size, ffsb_thread_t *ft, ffsb_fs_t *fs)
139 struct timeval start, end;
140 int need_stats = ft_needs_stats(ft, SYS_READ) ||
141 fs_needs_stats(fs, SYS_READ);
143 assert(size <= SIZE_MAX);
145 gettimeofday(&start, NULL);
146 realsize = read(fd, buf, size);
149 gettimeofday(&end, NULL);
150 do_stats(&start, &end, ft, fs, SYS_READ);
153 if (realsize != size) {
154 printf("Read %lld instead of %llu bytes.\n",
155 (unsigned long long)realsize, (unsigned long long)size);
161 void fhwrite(int fd, void *buf, uint32_t size, ffsb_thread_t *ft, ffsb_fs_t *fs)
164 struct timeval start, end;
165 int need_stats = ft_needs_stats(ft, SYS_WRITE) ||
166 fs_needs_stats(fs, SYS_WRITE);
168 assert(size <= SIZE_MAX);
170 gettimeofday(&start, NULL);
172 realsize = write(fd, buf, size);
175 gettimeofday(&end, NULL);
176 do_stats(&start, &end, ft, fs, SYS_WRITE);
179 if (realsize != size) {
180 printf("Wrote %zd instead of %d bytes.\n"
181 "Probably out of disk space\n", realsize, size);
187 void fhseek(int fd, uint64_t offset, int whence, ffsb_thread_t *ft,
191 struct timeval start, end;
192 int need_stats = ft_needs_stats(ft, SYS_LSEEK) ||
193 fs_needs_stats(fs, SYS_LSEEK);
195 if ((whence == SEEK_CUR) && (offset == 0))
199 gettimeofday(&start, NULL);
201 res = lseek64(fd, offset, whence);
204 gettimeofday(&end, NULL);
205 do_stats(&start, &end, ft, fs, SYS_LSEEK);
207 if ((whence == SEEK_SET) && (res != offset))
211 if (whence == SEEK_SET)
212 fprintf(stderr, "tried to seek to %"PRIu64"\n", offset);
214 fprintf(stderr, "tried to seek from current "
215 "position to %"PRIu64"\n", offset);
222 void fhclose(int fd, ffsb_thread_t *ft, ffsb_fs_t *fs)
224 struct timeval start, end;
225 int need_stats = ft_needs_stats(ft, SYS_CLOSE) ||
226 fs_needs_stats(fs, SYS_CLOSE);
229 gettimeofday(&start, NULL);
234 gettimeofday(&end, NULL);
235 do_stats(&start, &end, ft, fs, SYS_CLOSE);
239 void fhstat(char *name, ffsb_thread_t *ft, ffsb_fs_t *fs)
241 struct timeval start, end;
242 struct stat tmp_stat;
244 int need_stats = ft_needs_stats(ft, SYS_STAT) ||
245 fs_needs_stats(fs, SYS_CLOSE);
248 gettimeofday(&start, NULL);
250 if (stat(name, &tmp_stat)) {
251 fprintf (stderr, "stat call failed for file %s\n", name);
256 gettimeofday(&end, NULL);
257 do_stats(&start, &end, ft, fs, SYS_STAT);
261 int writefile_helper(int fd, uint64_t size, uint32_t blocksize, char *buf,
262 struct ffsb_thread *ft, struct ffsb_fs *fs)
264 uint64_t iterations, a;
267 iterations = size / blocksize;
268 last = size % blocksize;
270 for (a = 0; a < iterations; a++)
271 fhwrite(fd, buf, blocksize, ft, fs);
275 fhwrite(fd, buf, last, ft, fs);