Work on reducing memory pinned by the inode map.
[bluesky.git] / bluesky / imap.c
index cc8f1ba..6318256 100644 (file)
@@ -114,7 +114,7 @@ InodeMapEntry *bluesky_inode_map_lookup(GSequence *inode_map, uint64_t inum,
     }
 
     if (action != 0) {
-        bluesky_cloudlog_unref(range->serialized);
+        bluesky_cloudlog_unref_delayed(range->serialized);
         range->serialized = NULL;
     }
 
@@ -145,6 +145,7 @@ static void bluesky_inode_map_serialize_section(BlueSkyFS *fs,
         InodeMapEntry *entry = (InodeMapEntry *)g_sequence_get(i);
         uint64_t inum = GUINT64_TO_LE(entry->inum);
         g_string_append_len(buf, (const char *)&inum, sizeof(inum));
+        bluesky_cloudlog_ref(entry->item);
         g_array_append_val(log->links, entry->item);
         i = g_sequence_iter_next(i);
     }
@@ -156,6 +157,7 @@ static void bluesky_inode_map_serialize_section(BlueSkyFS *fs,
 
 BlueSkyCloudLog *bluesky_inode_map_serialize(BlueSkyFS *fs)
 {
+    gboolean updated = FALSE;
     GString *buf = g_string_new("");
     BlueSkyCloudLog *log = bluesky_cloudlog_new(fs, NULL);
     log->type = LOGTYPE_CHECKPOINT;
@@ -169,15 +171,40 @@ BlueSkyCloudLog *bluesky_inode_map_serialize(BlueSkyFS *fs)
         inum = GUINT64_TO_LE(range->end);
         g_string_append_len(buf, (const char *)&inum, sizeof(inum));
 
-        if (range->serialized == NULL)
+        if (range->serialized == NULL) {
             bluesky_inode_map_serialize_section(fs, range);
+            updated = TRUE;
+        }
         bluesky_cloudlog_ref(range->serialized);
         g_array_append_val(log->links, range->serialized);
         i = g_sequence_iter_next(i);
     }
 
     log->data = bluesky_string_new_from_gstring(buf);
-    return log;
+
+    if (updated) {
+        return log;
+    } else {
+        bluesky_cloudlog_unref(log);
+        return NULL;
+    }
+}
+
+/* Minimize resources consumed the inode map.  This should only be called once
+ * an updated inode map has been serialized to the cloud, and will replace
+ * cloud log objects with skeletal versions that just reference the data
+ * location in the cloud (rather than pinning all object data in memory). */
+void bluesky_inode_map_minimize(BlueSkyFS *fs)
+{
+    GSequenceIter *i = g_sequence_get_begin_iter(fs->inode_map);
+    while (!g_sequence_iter_is_end(i)) {
+        InodeMapRange *range = (InodeMapRange *)g_sequence_get(i);
+
+        if (range->serialized != NULL)
+            bluesky_cloudlog_erase(range->serialized);
+
+        i = g_sequence_iter_next(i);
+    }
 }
 
 /* Reconstruct the inode map from data stored in the cloud. */
@@ -205,8 +232,6 @@ static void bluesky_inode_map_deserialize(BlueSkyFS *fs, BlueSkyCloudLog *imap)
             entry->item = g_array_index(section->links,
                                         BlueSkyCloudLog *, j);
             bluesky_cloudlog_ref(entry->item);
-            entry->id = entry->item->id;
-            entry->location = entry->item->location;
             fs->next_inum = MAX(fs->next_inum, entry->inum + 1);
             inum++;
         }