Preparatory work before implementing proper cloud writing.
[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.  There are actually a couple of
19  * different tasks that are performed here:
20  *   - Forcing data to the log if needed to reclaim memory or simply if the
21  *     data has been dirty in memory long enough.
22  *   - Writing batches of data to the cloud.
23  */
24
25 static void writeback_complete(gpointer a, gpointer i)
26 {
27     BlueSkyInode *inode = (BlueSkyInode *)i;
28
29     if (bluesky_verbose) {
30         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
31               "Writeback for inode %"PRIu64" complete", inode->inum);
32     }
33
34     g_mutex_lock(inode->lock);
35
36     inode->change_commit = inode->change_pending;
37     inode->change_pending = 0;
38     if (inode->change_count == inode->change_commit) {
39         /* If inode is no longer dirty... */
40         inode->change_time = 0;
41         g_mutex_lock(inode->fs->lock);
42         bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
43         inode->dirty_list = NULL;
44         g_mutex_unlock(inode->fs->lock);
45     }
46
47     g_mutex_unlock(inode->lock);
48 }
49
50 static void flushd_dirty_inode(BlueSkyInode *inode)
51 {
52     BlueSkyFS *fs = inode->fs;
53
54     g_mutex_lock(fs->lock);
55     bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
56     inode->dirty_list = NULL;
57     g_mutex_unlock(fs->lock);
58
59     /* Inode is clean; nothing to do. */
60     if (inode->change_count == inode->change_commit)
61         return;
62
63     /* Inode writeback is in progress; put back on the dirty list. */
64     if (inode->change_pending) {
65         /* Waiting for an earlier writeback to finish, so don't start a new
66          * writeback yet. */
67         g_mutex_lock(fs->lock);
68         inode->change_time = bluesky_get_current_time();
69         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
70         inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
71         g_mutex_unlock(fs->lock);
72         return;
73     }
74
75     if (bluesky_verbose) {
76         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
77             "Starting flush of inode %"PRIu64, inode->inum);
78     }
79     inode->change_pending = inode->change_count;
80
81     /* Create a store barrier.  All operations part of the writeback will be
82      * added to this barrier, so when the barrier completes we know that the
83      * writeback is finished. */
84     BlueSkyStoreAsync *barrier = bluesky_store_async_new(fs->store);
85     barrier->op = STORE_OP_BARRIER;
86
87     bluesky_inode_start_sync(inode, barrier);
88
89     bluesky_store_async_add_notifier(barrier, writeback_complete, inode);
90     bluesky_store_async_submit(barrier);
91     bluesky_store_async_unref(barrier);
92 }
93
94 /* Try to flush dirty data to disk, either due to memory pressure or due to
95  * timeouts. */
96 static void flushd_dirty(BlueSkyFS *fs)
97 {
98     int64_t start_time = bluesky_get_current_time();
99     g_mutex_lock(fs->lock);
100
101     while (1) {
102         BlueSkyInode *inode;
103         if (fs->dirty_list.prev == NULL)
104             break;
105         inode = fs->dirty_list.prev->data;
106
107         if (bluesky_verbose) {
108             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
109                   "Considering flushing inode %"PRIu64, inode->inum);
110         }
111
112         /* Stop processing dirty inodes if we both have enough memory available
113          * and the oldest inode is sufficiently new that it need not be flushed
114          * out. */
115         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
116         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
117                 && elapsed < WRITEBACK_DELAY)
118             break;
119         if (inode->change_time > start_time)
120             break;
121
122         bluesky_inode_ref(inode);
123
124         g_mutex_unlock(fs->lock);
125
126         g_mutex_lock(inode->lock);
127         flushd_dirty_inode(inode);
128         g_mutex_unlock(inode->lock);
129         bluesky_inode_unref(inode);
130
131         g_mutex_lock(fs->lock);
132     }
133
134     g_mutex_unlock(fs->lock);
135 }
136
137 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
138 static void drop_caches(BlueSkyInode *inode)
139 {
140     if (inode->type == BLUESKY_REGULAR)
141         bluesky_file_drop_cached(inode);
142 }
143
144 /* Drop clean data fromt the cache if needed due to memory pressure. */
145 static void flushd_clean(BlueSkyFS *fs)
146 {
147     g_mutex_lock(fs->lock);
148
149     size_t inode_count = g_hash_table_size(fs->inodes);
150     if (!inode_count)
151         inode_count = 1;
152
153     while (inode_count-- > 0) {
154         if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
155             break;
156
157         BlueSkyInode *inode;
158         if (fs->accessed_list.prev == NULL)
159             break;
160         inode = fs->accessed_list.prev->data;
161
162         if (bluesky_verbose) {
163             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
164                   "Considering dropping cached data for inode %"PRIu64,
165                   inode->inum);
166         }
167
168         bluesky_inode_ref(inode);
169
170         g_mutex_unlock(fs->lock);
171
172         g_mutex_lock(inode->lock);
173
174         g_mutex_lock(fs->lock);
175         bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
176         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
177         g_mutex_unlock(fs->lock);
178
179         drop_caches(inode);
180
181         g_mutex_unlock(inode->lock);
182         bluesky_inode_unref(inode);
183
184         g_mutex_lock(fs->lock);
185     }
186
187     g_mutex_unlock(fs->lock);
188 }
189
190 /* Run the flush daemon for a single iteration, though if it is already
191  * executing returns immediately. */
192 static gpointer flushd_task(BlueSkyFS *fs)
193 {
194     if (!g_mutex_trylock(fs->flushd_lock))
195         return NULL;
196     flushd_dirty(fs);
197     bluesky_cloudlog_write_log(fs);
198     flushd_clean(fs);
199     g_mutex_unlock(fs->flushd_lock);
200
201     return NULL;
202 }
203
204 void bluesky_flushd_invoke(BlueSkyFS *fs)
205 {
206     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
207 }
208
209 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
210 {
211     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
212         && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
213         return;
214
215     if (bluesky_verbose) {
216         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
217               "Too much data; invoking flushd: dirty=%d total=%d",
218               g_atomic_int_get(&fs->cache_dirty),
219               g_atomic_int_get(&fs->cache_total));
220     }
221
222     bluesky_flushd_invoke(fs);
223 }
224
225 /* Start a perpetually-running thread that flushes the cache occasionally. */
226 static gpointer flushd_thread(BlueSkyFS *fs)
227 {
228     while (TRUE) {
229         bluesky_flushd_invoke(fs);
230         struct timespec delay;
231         delay.tv_sec = 2;
232         delay.tv_nsec = 0;
233         nanosleep(&delay, NULL);
234     }
235
236     return NULL;
237 }
238
239 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
240 {
241     g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);
242 }