Rework cache flushing logic--this version should work much better.
[bluesky.git] / bluesky / cache.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <glib.h>
11 #include <string.h>
12 #include <inttypes.h>
13
14 #include "bluesky-private.h"
15
16 #define WRITEBACK_DELAY (20 * 1000000)
17
18 /* Filesystem caching and cache coherency. */
19
20 static void writeback_complete(gpointer a, gpointer i)
21 {
22     BlueSkyInode *inode = (BlueSkyInode *)i;
23
24     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
25           "Writeback for inode %"PRIu64" complete", inode->inum);
26
27     g_mutex_lock(inode->lock);
28
29     inode->change_commit = inode->change_pending;
30     inode->change_pending = 0;
31     if (inode->change_count == inode->change_commit) {
32         /* If inode is no longer dirty... */
33         inode->change_time = 0;
34         g_mutex_lock(inode->fs->lock);
35         bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
36         inode->dirty_list = NULL;
37         g_mutex_unlock(inode->fs->lock);
38     }
39
40     g_mutex_unlock(inode->lock);
41 }
42
43 #if 0
44 static void flushd_inode(gpointer value, gpointer user_data)
45 {
46     BlueSkyFS *fs = (BlueSkyFS *)user_data;
47
48     BlueSkyInode *inode = (BlueSkyInode *)value;
49
50     g_mutex_lock(inode->lock);
51
52     if (inode->change_count == inode->change_commit) {
53         uint64_t delay = bluesky_get_current_time() - inode->access_time;
54         if (delay >= CACHE_CLEAN_DELAY) {
55             drop_caches(inode);
56
57             /* If the only references are the one we hold and the one in the
58              * filesystem inum->inode hash table...  First check the refcount
59              * without the lock for speed, but if the check looks good verify
60              * it after taking the filesystem lock. */
61             if (inode->refcount == 2) {
62                 g_mutex_lock(fs->lock);
63                 if (inode->refcount == 2) {
64                     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
65                           "Trying to drop inode %"PRIu64" from cache",
66                           inode->inum);
67                     if (g_hash_table_remove(fs->inodes, &inode->inum))
68                         bluesky_inode_unref(inode);
69                 }
70                 bluesky_list_unlink(&inode->fs->accessed_list,
71                                     inode->accessed_list);
72                 inode->accessed_list = NULL;
73                 bluesky_list_unlink(&inode->fs->dirty_list,
74                                     inode->dirty_list);
75                 inode->dirty_list = NULL;
76                 g_mutex_unlock(fs->lock);
77             }
78         }
79
80         g_mutex_unlock(inode->lock);
81         bluesky_inode_unref(inode);
82         return;
83     }
84
85     if (inode->change_pending) {
86         /* Waiting for an earlier writeback to finish, so don't start a new
87          * writeback yet. */
88         g_mutex_unlock(inode->lock);
89         bluesky_inode_unref(inode);
90         return;
91     }
92
93     uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
94     if (elapsed < WRITEBACK_DELAY) {
95         /* Give a bit more time before starting writeback. */
96         g_mutex_unlock(inode->lock);
97         bluesky_inode_unref(inode);
98         return;
99     }
100
101     inode->change_pending = inode->change_count;
102
103     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
104           "Starting flush of inode %"PRIu64, inode->inum);
105
106     /* Create a store barrier.  All operations part of the writeback will be
107      * added to this barrier, so when the barrier completes we know that the
108      * writeback is finished. */
109     BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
110     barrier->op = STORE_OP_BARRIER;
111
112     bluesky_inode_start_sync(inode, barrier);
113
114     bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
115     bluesky_store_async_submit(barrier);
116     bluesky_store_async_unref(barrier);
117
118     g_mutex_unlock(inode->lock);
119     bluesky_inode_unref(inode);
120 }
121 #endif
122
123 static void flushd_dirty_inode(BlueSkyInode *inode)
124 {
125     BlueSkyFS *fs = inode->fs;
126
127     /* Inode is clean; nothing to do. */
128     if (inode->change_count == inode->change_commit)
129         return;
130
131     /* Inode writeback is in progress; put back on the dirty list. */
132     if (inode->change_pending) {
133         /* Waiting for an earlier writeback to finish, so don't start a new
134          * writeback yet. */
135         g_mutex_lock(fs->lock);
136         inode->change_time = bluesky_get_current_time();
137         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
138         inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
139         g_mutex_unlock(fs->lock);
140         return;
141     }
142
143     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
144           "Starting flush of inode %"PRIu64, inode->inum);
145     inode->change_pending = inode->change_count;
146
147     /* Create a store barrier.  All operations part of the writeback will be
148      * added to this barrier, so when the barrier completes we know that the
149      * writeback is finished. */
150     BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
151     barrier->op = STORE_OP_BARRIER;
152
153     bluesky_inode_start_sync(inode, barrier);
154
155     bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
156     bluesky_store_async_submit(barrier);
157     bluesky_store_async_unref(barrier);
158 }
159
160 /* Try to flush dirty data to disk, either due to memory pressure or due to
161  * timeouts. */
162 static void flushd_dirty(BlueSkyFS *fs)
163 {
164     int64_t start_time = bluesky_get_current_time();
165     g_mutex_lock(fs->lock);
166
167     while (1) {
168         BlueSkyInode *inode;
169         if (fs->dirty_list.prev == NULL)
170             break;
171         inode = fs->dirty_list.prev->data;
172
173         g_print("Considering flushing inode %"PRIu64"\n", inode->inum);
174
175         /* Stop processing dirty inodes if we both have enough memory available
176          * and the oldest inode is sufficiently new that it need not be flushed
177          * out. */
178         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
179         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
180                 && elapsed < WRITEBACK_DELAY)
181             break;
182         if (inode->change_time > start_time)
183             break;
184
185         bluesky_inode_ref(inode);
186
187         bluesky_list_unlink(&fs->dirty_list, fs->dirty_list.prev);
188         inode->dirty_list = NULL;
189
190         g_mutex_unlock(fs->lock);
191
192         g_mutex_lock(inode->lock);
193         flushd_dirty_inode(inode);
194         g_mutex_unlock(inode->lock);
195         bluesky_inode_unref(inode);
196
197         g_mutex_lock(fs->lock);
198     }
199
200     g_mutex_unlock(fs->lock);
201 }
202
203 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
204 static void drop_caches(BlueSkyInode *inode)
205 {
206     if (inode->type == BLUESKY_REGULAR)
207         bluesky_file_drop_cached(inode);
208 }
209
210 /* Drop clean data fromt the cache if needed due to memory pressure. */
211 static void flushd_clean(BlueSkyFS *fs)
212 {
213     g_mutex_lock(fs->lock);
214
215     size_t inode_count = g_hash_table_size(fs->inodes);
216     if (!inode_count)
217         inode_count = 1;
218
219     while (inode_count-- > 0) {
220         if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
221             break;
222
223         BlueSkyInode *inode;
224         if (fs->accessed_list.prev == NULL)
225             break;
226         inode = fs->accessed_list.prev->data;
227
228         g_print("Considering dropping cached data for inode %"PRIu64"\n",
229                 inode->inum);
230
231         bluesky_inode_ref(inode);
232
233         bluesky_list_unlink(&fs->accessed_list, fs->accessed_list.prev);
234         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
235
236         g_mutex_unlock(fs->lock);
237
238         g_mutex_lock(inode->lock);
239         drop_caches(inode);
240         g_mutex_unlock(inode->lock);
241         bluesky_inode_unref(inode);
242
243         g_mutex_lock(fs->lock);
244     }
245
246     g_mutex_unlock(fs->lock);
247 }
248
249 /* Run the flush daemon for a single iteration, though if it is already
250  * executing returns immediately. */
251 static gpointer flushd_task(BlueSkyFS *fs)
252 {
253     if (!g_mutex_trylock(fs->flushd_lock))
254         return NULL;
255     flushd_dirty(fs);
256     flushd_clean(fs);
257     g_mutex_unlock(fs->flushd_lock);
258
259     return NULL;
260 }
261
262 void bluesky_flushd_invoke(BlueSkyFS *fs)
263 {
264     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
265 }
266
267 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
268 {
269     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
270         && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
271         return;
272
273     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
274           "Too much data; invoking flushd: dirty=%d total=%d",
275           g_atomic_int_get(&fs->cache_dirty),
276           g_atomic_int_get(&fs->cache_total));
277
278     bluesky_flushd_invoke(fs);
279 }