Work to allow mmap-ed log entries to be used for data blocks.
[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->dirty_list, inode->dirty_list);
58     inode->dirty_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     /* Inode writeback is in progress; put back on the dirty list. */
66     if (inode->change_pending) {
67         /* Waiting for an earlier writeback to finish, so don't start a new
68          * writeback yet. */
69         g_mutex_lock(fs->lock);
70         inode->change_time = bluesky_get_current_time();
71         bluesky_list_unlink(&fs->dirty_list, inode->dirty_list);
72         inode->dirty_list = bluesky_list_prepend(&fs->dirty_list, inode);
73         g_mutex_unlock(fs->lock);
74         return;
75     }
76
77     if (bluesky_verbose) {
78         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
79             "Starting flush of inode %"PRIu64, inode->inum);
80     }
81     inode->change_pending = inode->change_count;
82
83     bluesky_inode_start_sync(inode);
84 }
85
86 /* Try to flush dirty data to disk, either due to memory pressure or due to
87  * timeouts. */
88 static void flushd_dirty(BlueSkyFS *fs)
89 {
90     int64_t start_time = bluesky_get_current_time();
91     g_mutex_lock(fs->lock);
92
93     while (1) {
94         BlueSkyInode *inode;
95         if (fs->dirty_list.prev == NULL)
96             break;
97         inode = fs->dirty_list.prev->data;
98
99         if (bluesky_verbose) {
100             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
101                   "Considering flushing inode %"PRIu64, inode->inum);
102         }
103
104         /* Stop processing dirty inodes if we both have enough memory available
105          * and the oldest inode is sufficiently new that it need not be flushed
106          * out. */
107         uint64_t elapsed = bluesky_get_current_time() - inode->change_time;
108         if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_low_dirty
109                 && elapsed < WRITEBACK_DELAY)
110             break;
111         if (inode->change_time > start_time)
112             break;
113
114         bluesky_inode_ref(inode);
115
116         g_mutex_unlock(fs->lock);
117
118         g_mutex_lock(inode->lock);
119         flushd_dirty_inode(inode);
120         g_mutex_unlock(inode->lock);
121         bluesky_inode_unref(inode);
122
123         g_mutex_lock(fs->lock);
124     }
125
126     g_mutex_unlock(fs->lock);
127 }
128
129 /* Drop cached data for a given inode, if it is clean.  inode must be locked. */
130 static void drop_caches(BlueSkyInode *inode)
131 {
132     if (inode->type == BLUESKY_REGULAR)
133         bluesky_file_drop_cached(inode);
134 }
135
136 /* Drop clean data fromt the cache if needed due to memory pressure. */
137 static void flushd_clean(BlueSkyFS *fs)
138 {
139     g_mutex_lock(fs->lock);
140
141     size_t inode_count = g_hash_table_size(fs->inodes);
142     if (!inode_count)
143         inode_count = 1;
144
145     while (inode_count-- > 0) {
146         if (g_atomic_int_get(&fs->cache_total) < bluesky_watermark_medium_total)
147             break;
148
149         BlueSkyInode *inode;
150         if (fs->accessed_list.prev == NULL)
151             break;
152         inode = fs->accessed_list.prev->data;
153
154         if (bluesky_verbose) {
155             g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
156                   "Considering dropping cached data for inode %"PRIu64,
157                   inode->inum);
158         }
159
160         bluesky_inode_ref(inode);
161
162         g_mutex_unlock(fs->lock);
163
164         g_mutex_lock(inode->lock);
165
166         g_mutex_lock(fs->lock);
167         bluesky_list_unlink(&fs->accessed_list, inode->accessed_list);
168         inode->accessed_list = bluesky_list_prepend(&fs->accessed_list, inode);
169         g_mutex_unlock(fs->lock);
170
171         drop_caches(inode);
172
173         g_mutex_unlock(inode->lock);
174         bluesky_inode_unref(inode);
175
176         g_mutex_lock(fs->lock);
177     }
178
179     g_mutex_unlock(fs->lock);
180 }
181
182 /* Run the flush daemon for a single iteration, though if it is already
183  * executing returns immediately. */
184 static gpointer flushd_task(BlueSkyFS *fs)
185 {
186     if (!g_mutex_trylock(fs->flushd_lock))
187         return NULL;
188     flushd_dirty(fs);
189     bluesky_cloudlog_write_log(fs);
190     flushd_clean(fs);
191     g_mutex_unlock(fs->flushd_lock);
192
193     return NULL;
194 }
195
196 void bluesky_flushd_invoke(BlueSkyFS *fs)
197 {
198     g_thread_create((GThreadFunc)flushd_task, fs, FALSE, NULL);
199 }
200
201 void bluesky_flushd_invoke_conditional(BlueSkyFS *fs)
202 {
203     if (g_atomic_int_get(&fs->cache_dirty) < bluesky_watermark_high_dirty
204         && g_atomic_int_get(&fs->cache_total) < bluesky_watermark_high_total)
205         return;
206
207     if (bluesky_verbose) {
208         g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
209               "Too much data; invoking flushd: dirty=%d total=%d",
210               g_atomic_int_get(&fs->cache_dirty),
211               g_atomic_int_get(&fs->cache_total));
212     }
213
214     bluesky_flushd_invoke(fs);
215 }
216
217 /* Start a perpetually-running thread that flushes the cache occasionally. */
218 static gpointer flushd_thread(BlueSkyFS *fs)
219 {
220     while (TRUE) {
221         bluesky_flushd_invoke(fs);
222         struct timespec delay;
223         delay.tv_sec = 2;
224         delay.tv_nsec = 0;
225         nanosleep(&delay, NULL);
226     }
227
228     return NULL;
229 }
230
231 void bluesky_flushd_thread_launch(BlueSkyFS *fs)
232 {
233     g_thread_create((GThreadFunc)flushd_thread, fs, FALSE, NULL);
234 }