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