Work to unify the cloud segment writing with other cache management.
[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 #if 0
26 static void writeback_complete(gpointer a, gpointer i)
27 {
28     BlueSkyInode *inode = (BlueSkyInode *)i;
29
30     if (bluesky_verbose) {
31         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
32               "Writeback for inode %"PRIu64" complete", inode->inum);
33     }
34
35     g_mutex_lock(inode->lock);
36
37     inode->change_commit = inode->change_pending;
38     inode->change_pending = 0;
39     if (inode->change_count == inode->change_commit) {
40         /* If inode is no longer dirty... */
41         inode->change_time = 0;
42         g_mutex_lock(inode->fs->lock);
43         bluesky_list_unlink(&inode->fs->dirty_list, inode->dirty_list);
44         inode->dirty_list = NULL;
45         g_mutex_unlock(inode->fs->lock);
46     }
47
48     g_mutex_unlock(inode->lock);
49 }
50 #endif
51
52 static void flushd_dirty_inode(BlueSkyInode *inode)
53 {
54     BlueSkyFS *fs = inode->fs;
55
56     g_mutex_lock(fs->lock);
57     bluesky_list_unlink(&fs->unlogged_list, inode->unlogged_list);
58     inode->unlogged_list = NULL;
59     g_mutex_unlock(fs->lock);
60
61     /* Inode is clean; nothing to do. */
62     if (inode->change_count == inode->change_commit)
63         return;
64
65     if (bluesky_verbose) {
66         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
67             "Starting flush of inode %"PRIu64, inode->inum);
68     }
69
70     bluesky_inode_start_sync(inode);
71 }
72
73 /* Try to flush dirty data to disk, either due to memory pressure or due to
74  * timeouts. */
75 static void flushd_dirty(BlueSkyFS *fs)
76 {
77     int64_t start_time = bluesky_get_current_time();
78     g_mutex_lock(fs->lock);
79
80     while (1) {
81         BlueSkyInode *inode;
82         if (fs->unlogged_list.prev == NULL)
83             break;
84         inode = fs->unlogged_list.prev->data;
85
86         if (bluesky_verbose) {
87             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
88                   "Considering flushing inode %"PRIu64, inode->inum);
89         }
90
91         /* Stop processing dirty inodes if we both have enough memory available
92          * and the oldest inode is sufficiently new that it need not be flushed
93          * out. */
94         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
95         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
96                 && elapsed < WRITEBACK_DELAY)
97             break;
98         if (inode->change_time > start_time)
99             break;
100
101         bluesky_inode_ref(inode);
102
103         g_mutex_unlock(fs->lock);
104
105         g_mutex_lock(inode->lock);
106         flushd_dirty_inode(inode);
107         g_mutex_unlock(inode->lock);
108         bluesky_inode_unref(inode);
109
110         g_mutex_lock(fs->lock);
111     }
112
113     g_mutex_unlock(fs->lock);
114 }
115
116 /* Try to flush dirty data to the cloud. */
117 static void flushd_cloud(BlueSkyFS *fs)
118 {
119     int64_t start_time = bluesky_get_current_time();
120     g_mutex_lock(fs->lock);
121
122     while (1) {
123         BlueSkyInode *inode;
124         if (fs->dirty_list.prev == NULL)
125             break;
126         inode = fs->dirty_list.prev->data;
127
128         if (bluesky_verbose) {
129             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
130                   "Flushing inode %"PRIu64" to cloud", inode->inum);
131         }
132
133         /* Stop processing dirty inodes if we both have enough memory available
134          * and the oldest inode is sufficiently new that it need not be flushed
135          * out. */
136         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
137         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
138                 && elapsed < WRITEBACK_DELAY)
139             break;
140         if (inode->change_time > start_time)
141             break;
142
143         bluesky_inode_ref(inode);
144
145         g_mutex_unlock(fs->lock);
146
147         g_mutex_lock(inode->lock);
148         flushd_dirty_inode(inode);
149         g_mutex_lock(fs->lock);
150         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
151         inode->dirty_list = NULL;
152         g_mutex_unlock(fs->lock);
153
154         BlueSkyCloudLog *log = inode->committed_item;
155         bluesky_cloudlog_ref(log);
156         g_mutex_unlock(inode->lock);
157
158         if (log != NULL)
159             bluesky_cloudlog_serialize(log, fs);
160         bluesky_inode_unref(inode);
161         bluesky_cloudlog_ref(log);
162
163         g_mutex_lock(fs->lock);
164     }
165
166     g_mutex_unlock(fs->lock);
167     bluesky_cloudlog_flush(fs);
168 }
169
170 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
171 static void drop_caches(BlueSkyInode *inode)
172 {
173     if (inode->type == BLUESKY_REGULAR)
174         bluesky_file_drop_cached(inode);
175 }
176
177 /* Drop clean data from the cache if needed due to memory pressure. */
178 static void flushd_clean(BlueSkyFS *fs)
179 {
180     g_mutex_lock(fs->lock);
181
182     size_t inode_count = g_hash_table_size(fs->inodes);
183     if (!inode_count)
184         inode_count = 1;
185
186     while (inode_count-- > 0) {
187 #if 0
188         if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
189             break;
190 #endif
191
192         BlueSkyInode *inode;
193         if (fs->accessed_list.prev == NULL)
194             break;
195         inode = fs->accessed_list.prev->data;
196
197         if (bluesky_verbose) {
198             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
199                   "Considering dropping cached data for inode %"PRIu64,
200                   inode->inum);
201         }
202
203         bluesky_inode_ref(inode);
204
205         g_mutex_unlock(fs->lock);
206
207         g_mutex_lock(inode->lock);
208
209         g_mutex_lock(fs->lock);
210         bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
211         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
212         g_mutex_unlock(fs->lock);
213
214         drop_caches(inode);
215
216         g_mutex_unlock(inode->lock);
217         bluesky_inode_unref(inode);
218
219         g_mutex_lock(fs->lock);
220     }
221
222     g_mutex_unlock(fs->lock);
223 }
224
225 /* Run the flush daemon for a single iteration, though if it is already
226  * executing returns immediately. */
227 static gpointer flushd_task(BlueSkyFS *fs)
228 {
229     if (!g_mutex_trylock(fs->flushd_lock))
230         return NULL;
231     flushd_dirty(fs);
232     flushd_cloud(fs);
233     flushd_clean(fs);
234     g_mutex_unlock(fs->flushd_lock);
235
236     return NULL;
237 }
238
239 void bluesky_flushd_invoke(BlueSkyFS *fs)
240 {
241     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
242 }
243
244 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
245 {
246     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
247         && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
248         return;
249
250     if (bluesky_verbose) {
251         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
252               "Too much data; invoking flushd: dirty=%d total=%d",
253               g_atomic_int_get(&fs->cache_dirty),
254               g_atomic_int_get(&fs->cache_total));
255     }
256
257     bluesky_flushd_invoke(fs);
258 }
259
260 /* Start a perpetually-running thread that flushes the cache occasionally. */
261 static gpointer flushd_thread(BlueSkyFS *fs)
262 {
263     while (TRUE) {
264         bluesky_flushd_invoke(fs);
265         struct timespec delay;
266         delay.tv_sec = 2;
267         delay.tv_nsec = 0;
268         nanosleep(&delay, NULL);
269     }
270
271     return NULL;
272 }
273
274 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
275 {
276     g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);
277 }