At Sat, 3 Nov 2012 23:09:47 +0800, Liu Yuan wrote: > diff --git a/include/util.h b/include/util.h > index 5fb19c2..a0d8186 100644 > --- a/include/util.h > +++ b/include/util.h > @@ -38,6 +38,7 @@ > #endif > > #define notrace __attribute__((no_instrument_function)) > +#define __packed __attribute((packed)) > > #define uninitialized_var(x) (x = x) > > @@ -68,6 +69,10 @@ static inline void *zalloc(size_t size) > return calloc(1, size); > } > > +#define __round_mask(x, y) ((__typeof__(x))((y)-1)) > +#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) Can I use roundup() which is already defined in util.h? > diff --git a/sheep/journal_file.c b/sheep/journal_file.c > new file mode 100644 > index 0000000..110a034 > --- /dev/null > +++ b/sheep/journal_file.c > @@ -0,0 +1,386 @@ > +/* > + * Copyright (C) 2012 Taobao Inc. > + * > + * Liu Yuan <namei.unix at gmail.com> > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License version > + * 2 as published by the Free Software Foundation. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program. If not, see <http://www.gnu.org/licenses/>. > + */ > +#include <errno.h> > +#include <fcntl.h> > +#include <sys/stat.h> > +#include <sys/types.h> > +#include <unistd.h> > +#include <pthread.h> > +#include <stdint.h> > +#include <sys/mman.h> > + > +#include "sheep_priv.h" > + > +struct journal_file { > + int fd; > + off_t pos; > + int commit_fd; > + uatomic_bool in_commit; > +}; > + > +struct journal_descriptor { > + uint32_t magic; > + uint64_t oid; > + uint64_t offset; > + uint64_t size; > + uint8_t create; > + uint8_t pad[479]; > +} __packed; Though we use a packed attribute here, it looks better to align each member on an 8-byte boundary so that GCC makes an efficient code. struct journal_descriptor { uint32_t magic; uint32_t reserved; uint64_t oid; uint64_t offset; uint64_t size; uint8_t create; uint8_t __pad[475]; } __packed; > + > +/* JOURNAL_DESC + JOURNAL_MARKER must be 512 algined for DIO */ > +#define JOURNAL_DESC_MAGIC 0xfee1900d > +#define JOURNAL_DESC_SIZE 508 > +#define JOURNAL_MARKER_SIZE 4 /* Use marker to detect partial write */ > +#define JOURNAL_META_SIZE SECTOR_SIZE > + > +#define JOURNAL_FILE_SIZE (1024*1024*256) /* 256M */ > +#define JOURNAL_END_MARKER 0xdeadbeef > + > +static const char *jfile_name[2] = { "journal_file0", "journal_file1", }; > +static int jfile_fds[2]; > + > +static struct journal_file jfile; > +static pthread_spinlock_t jfile_lock; > + > +static int zero_out_jfile(int fd) > +{ > + char *buf; > + ssize_t wlen; > + > + buf = valloc(JOURNAL_FILE_SIZE); > + memset(buf, 0, JOURNAL_FILE_SIZE); > + wlen = xpwrite(fd, buf, JOURNAL_FILE_SIZE, 0); Isn't it efficent to use truncate and fallocate like as follows? ftruncate(fd, 0); prealloc(fd, JOURNAL_FILE_SIZE); Thanks, Kazutaka |