Try to clean up the locking for LRU lists.
[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     g_mutex_lock(fs->lock);
128     bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
129     inode->dirty_list = NULL;
130     g_mutex_unlock(fs->lock);
131
132     /* Inode is clean; nothing to do. */
133     if (inode->change_count == inode->change_commit)
134         return;
135
136     /* Inode writeback is in progress; put back on the dirty list. */
137     if (inode->change_pending) {
138         /* Waiting for an earlier writeback to finish, so don't start a new
139          * writeback yet. */
140         g_mutex_lock(fs->lock);
141         inode->change_time = bluesky_get_current_time();
142         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
143         inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
144         g_mutex_unlock(fs->lock);
145         return;
146     }
147
148     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
149           "Starting flush of inode %"PRIu64, inode->inum);
150     inode->change_pending = inode->change_count;
151
152     /* Create a store barrier.  All operations part of the writeback will be
153      * added to this barrier, so when the barrier completes we know that the
154      * writeback is finished. */
155     BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
156     barrier->op = STORE_OP_BARRIER;
157
158     bluesky_inode_start_sync(inode, barrier);
159
160     bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
161     bluesky_store_async_submit(barrier);
162     bluesky_store_async_unref(barrier);
163 }
164
165 /* Try to flush dirty data to disk, either due to memory pressure or due to
166  * timeouts. */
167 static void flushd_dirty(BlueSkyFS *fs)
168 {
169     int64_t start_time = bluesky_get_current_time();
170     g_mutex_lock(fs->lock);
171
172     while (1) {
173         BlueSkyInode *inode;
174         if (fs->dirty_list.prev == NULL)
175             break;
176         inode = fs->dirty_list.prev->data;
177
178         g_print("Considering flushing inode %"PRIu64"\n", inode->inum);
179
180         /* Stop processing dirty inodes if we both have enough memory available
181          * and the oldest inode is sufficiently new that it need not be flushed
182          * out. */
183         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
184         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
185                 && elapsed < WRITEBACK_DELAY)
186             break;
187         if (inode->change_time > start_time)
188             break;
189
190         bluesky_inode_ref(inode);
191
192         g_mutex_unlock(fs->lock);
193
194         g_mutex_lock(inode->lock);
195         flushd_dirty_inode(inode);
196         g_mutex_unlock(inode->lock);
197         bluesky_inode_unref(inode);
198
199         g_mutex_lock(fs->lock);
200     }
201
202     g_mutex_unlock(fs->lock);
203 }
204
205 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
206 static void drop_caches(BlueSkyInode *inode)
207 {
208     if (inode->type == BLUESKY_REGULAR)
209         bluesky_file_drop_cached(inode);
210 }
211
212 /* Drop clean data fromt the cache if needed due to memory pressure. */
213 static void flushd_clean(BlueSkyFS *fs)
214 {
215     g_mutex_lock(fs->lock);
216
217     size_t inode_count = g_hash_table_size(fs->inodes);
218     if (!inode_count)
219         inode_count = 1;
220
221     while (inode_count-- > 0) {
222         if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
223             break;
224
225         BlueSkyInode *inode;
226         if (fs->accessed_list.prev == NULL)
227             break;
228         inode = fs->accessed_list.prev->data;
229
230         g_print("Considering dropping cached data for inode %"PRIu64"\n",
231                 inode->inum);
232
233         bluesky_inode_ref(inode);
234
235         g_mutex_unlock(fs->lock);
236
237         g_mutex_lock(inode->lock);
238
239         g_mutex_lock(fs->lock);
240         bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
241         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
242         g_mutex_unlock(fs->lock);
243
244         drop_caches(inode);
245
246         g_mutex_unlock(inode->lock);
247         bluesky_inode_unref(inode);
248
249         g_mutex_lock(fs->lock);
250     }
251
252     g_mutex_unlock(fs->lock);
253 }
254
255 /* Run the flush daemon for a single iteration, though if it is already
256  * executing returns immediately. */
257 static gpointer flushd_task(BlueSkyFS *fs)
258 {
259     if (!g_mutex_trylock(fs->flushd_lock))
260         return NULL;
261     flushd_dirty(fs);
262     flushd_clean(fs);
263     g_mutex_unlock(fs->flushd_lock);
264
265     return NULL;
266 }
267
268 void bluesky_flushd_invoke(BlueSkyFS *fs)
269 {
270     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
271 }
272
273 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
274 {
275     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
276         && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
277         return;
278
279     g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
280           "Too much data; invoking flushd: dirty=%d total=%d",
281           g_atomic_int_get(&fs->cache_dirty),
282           g_atomic_int_get(&fs->cache_total));
283
284     bluesky_flushd_invoke(fs);
285 }