#define DBPF_OPEN lsfs_open //Yes
#define DBPF_WRITE lsfs_write
#define DBPF_READ lsfs_read
#define DBPF_CLOSE lsfs_close //Yes
#define DBPF_LSEEK lsfs_lseek
#define DBPF_UNLINK lsfs_unlink //Yes
#define DBPF_SYNC lsfs_fsync
#define DBPF_RESIZE lsfs_ftruncate //Yes
#define DBPF_FSTAT lsfs_fstat //Yes
/*
* Prepare the trace for LSFS (refer to Dong' work), in the format of
* "time | pid | opcode |parameter |return value"
* Since time and pid are common, they are provided in here.
* Other fields should be provided by the corresponding functions,
* such as DBPF_OPEN, DBPF_WRITE, ...
*/
#define lsfs_print_system_time(output) \
do { \
struct timeval tv; \
gettimeofday(&tv, 0); \
fprintf(output, "%010ld.%06ld ", tv.tv_sec, tv.tv_usec); \
} while (0)
#define lsfs_print_current_pid(output) \
do { \
pid_t pid = 9999; \
fprintf(output, "%d ", pid); \
} while (0)
#define lsfs_print_trace(output, fmt, args...) \
do { \
fprintf(output, fmt, ##args); \
fflush(output); \
} while (0)
// ssize_t = int, size_t = unsigned int, off_t = long, mode_t =
unsigned short
static inline int lsfs_open(const char *pathname, int flags,
mode_t mode)
{
int ret;
lsfs_print_system_time(stderr);
ret=open(pathname, flags, mode);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %s = %d\n", "OPEN", pathname, ret);
return ret;
}
static inline ssize_t lsfs_write(int fd, const void *buf, size_t
count)
{
ssize_t ret;
lsfs_print_system_time(stderr);
ret=write(fd, buf, count);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "WRITE", fd, ret);
return ret;
}
static inline ssize_t lsfs_read(int fd, void *buf, size_t count)
{
ssize_t ret;
lsfs_print_system_time(stderr);
ret=read(fd, buf, count);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "READ", fd, ret);
return ret;
}
static inline int lsfs_close(int fd)
{
int ret;
lsfs_print_system_time(stderr);
ret=close(fd);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "CLOSE", fd, ret);
return ret;
}
static inline off_t lsfs_lseek(int fd, off_t offset, int whence)
{
off_t ret;
lsfs_print_system_time(stderr);
ret=lseek(fd, offset, whence);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %lld\n", "LSEEK", fd, ret);
return ret;
}
static inline int lsfs_unlink(const char *pathname)
{
int ret;
lsfs_print_system_time(stderr);
ret=unlink(pathname);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %s = %d\n", "UNLINK", pathname,
ret);
return ret;
}
static inline int lsfs_fsync(int fd)
{
int ret;
lsfs_print_system_time(stderr);
ret=fsync(fd);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "FSYNC", fd, ret);
return ret;
}
static inline int lsfs_ftruncate(int fd, off_t length)
{
int ret;
lsfs_print_system_time(stderr);
ret=ftruncate(fd,length);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "FTRUNCATE", fd, ret);
return ret;
}
static inline int lsfs_fstat(int fd, struct stat *buf)
{
int ret;
lsfs_print_system_time(stderr);
ret=fstat(fd, buf);
lsfs_print_system_time(stderr);
lsfs_print_current_pid(stderr);
lsfs_print_trace(stderr, "%s %d = %d\n", "FSTAT", fd, ret);
return ret;
}