/alps/fastwriter

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/fastwriter

« back to all changes in this revision

Viewing changes to default.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-12-13 16:54:10 UTC
  • Revision ID: csa@dside.dyndns.org-20111213165410-b36e4989rogac4fg
Few synchronization and alignment related fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
typedef struct {
42
42
    int fd;
 
43
 
 
44
    int sync_mode;
43
45
    
44
46
    size_t prior_size;          /**< original size of file */
45
47
    size_t preallocated;        /**< preallocated bytes */
56
58
    int open_flags = (O_CREAT|O_WRONLY|O_NOATIME|O_LARGEFILE);
57
59
    int open_mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
58
60
 
59
 
#ifdef SYNC_MODE
60
 
    open_flags |= O_DIRECT;//|O_SYNC;
61
 
#endif /* SYNC_MODE */
62
61
    
63
62
    fastwriter_default_t *ctx;
64
63
 
72
71
 
73
72
    fw->ctx = ctx;
74
73
 
 
74
#ifdef SYNC_MODE
 
75
    open_flags |= O_DIRECT;
 
76
    ctx->sync_mode = 1;
 
77
#endif /* SYNC_MODE */
 
78
 
 
79
    ctx->prior_size = 0;
 
80
 
75
81
    if (!strcmp(fs, "raw")) {
76
82
        ctx->wr_block = EXT4_WRITEBLOCK;
77
83
        ctx->pa_block = 0;
 
84
        ctx->prior_size = (size_t)-1;
78
85
    } else if (!strcmp(fs, "ext4")) {
79
86
        ctx->wr_block = EXT4_WRITEBLOCK;
80
87
        ctx->pa_block = EXT4_PREALLOCATE;
95
102
    ctx->fd = open(name, open_flags, open_mode);
96
103
    if (ctx->fd < 0) return errno;
97
104
 
98
 
    ctx->prior_size = 0;
99
 
    
100
 
#ifndef HAVE_LINUX_FALLOC_H
101
105
    if (((open_flags&FASTWRITER_FLAGS_OVERWRITE)==0)&&(strcmp(fs, "raw"))) {
102
106
        ctx->prior_size = lseek(ctx->fd, 0, SEEK_END);
 
107
# ifdef SYNC_MODE       
 
108
        if (ctx->prior_size%FASTWRITER_SYNCIO_ALIGN) {
 
109
            close(ctx->fd);
 
110
            
 
111
            ctx->fd = open(name, open_flags&~O_DIRECT, open_mode);
 
112
            if (ctx->fd < 0) return errno;
 
113
            
 
114
            ctx->prior_size = lseek(ctx->fd, 0, SEEK_END);
 
115
            
 
116
            ctx->sync_mode = 0;
 
117
        }
 
118
# endif /* SYNC_MODE */
103
119
    }
104
 
#endif /* HAVE_LINUX_FALLOC_H */
105
120
 
106
121
    ctx->preallocated = 0;
107
122
 
114
129
        fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
115
130
 
116
131
        if (ctx->fd >= 0) {
117
 
#ifndef HAVE_LINUX_FALLOC_H
118
 
            if (ctx->prior_size) {
119
 
                ftrucate(ctx->fd, ctx->prior_size + fw->written);
 
132
#if defined(SYNC_MODE)||!defined(HAVE_LINUX_FALLOC_H)
 
133
            if (ctx->prior_size != (size_t)-1) {
 
134
                ftruncate(ctx->fd, ctx->prior_size + fw->written);
120
135
            }
121
 
#endif /* HAVE_LINUX_FALLOC_H */
 
136
#endif
122
137
            close(ctx->fd);
123
138
        }
124
139
        
130
145
 
131
146
int fastwriter_default_write(fastwriter_t *fw, fastwriter_write_flags_t flags, size_t size, void *data, size_t *written) {
132
147
    size_t sum = 0;
 
148
    size_t delta = 0;
133
149
    ssize_t res;
134
150
    fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
135
 
    
 
151
 
136
152
    if ((flags&FASTWRITER_WRITE_FLAG_FORCE)==0) {
137
153
        if (size < ctx->wr_block) {
138
154
            *written = 0;
141
157
    
142
158
        size -= size % ctx->wr_block;
143
159
    }
 
160
    
144
161
 
145
162
    if ((ctx->pa_block)&&((fw->written + size) > ctx->preallocated)) {
146
163
#ifdef HAVE_LINUX_FALLOC_H
153
170
            ctx->preallocated += ctx->pa_block;
154
171
        }
155
172
    }
 
173
 
 
174
#ifdef SYNC_MODE
 
175
        // we expect this to happen only at last iteration (buffer is multiply of the required align)
 
176
    if ((ctx->sync_mode)&&(size%FASTWRITER_SYNCIO_ALIGN)) {
 
177
        delta = FASTWRITER_SYNCIO_ALIGN - size%FASTWRITER_SYNCIO_ALIGN;
 
178
    }
 
179
#endif /* SYNC_MODE */
156
180
    
157
181
    do {
158
 
        res = write(ctx->fd, data, size);
 
182
        res = write(ctx->fd, data + sum, size + delta - sum);
 
183
//      printf("%i %i %p %zu %i\n", res, ctx->fd, data, size, delta);
159
184
        if (res < 0) {
160
185
            *written = sum;
161
186
            return errno;