Add a connection pool to the simplesever client
[bluesky.git] / bluesky / file.c
index 977618d..5c4b157 100644 (file)
@@ -44,9 +44,6 @@ void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
         break;
     }
 
-    /*if (block->type != BLUESKY_BLOCK_CACHED
-            && block->type != BLUESKY_BLOCK_DIRTY)
-        g_atomic_int_add(&inode->fs->cache_total, 1);   //FIXME */
     if (block->type != BLUESKY_BLOCK_DIRTY)
         g_atomic_int_add(&inode->fs->cache_dirty, 1);
 
@@ -96,11 +93,8 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
         g_array_set_size(inode->blocks, blocks);
     } else if (blocks < inode->blocks->len) {
         /* Delete blocks from a file.  Must reclaim memory. */
-        for (guint i = inode->blocks->len; i < blocks; i++) {
+        for (guint i = blocks; i < inode->blocks->len; i++) {
             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
-            /* if (b->type == BLUESKY_BLOCK_CACHED
-                    || b->type == BLUESKY_BLOCK_DIRTY)
-                g_atomic_int_add(&inode->fs->cache_total, -1); FIXME */
             if (b->type == BLUESKY_BLOCK_DIRTY)
                 g_atomic_int_add(&inode->fs->cache_dirty, -1);
             bluesky_string_unref(b->dirty);
@@ -172,31 +166,22 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
     g_return_if_fail(offset < inode->size);
     g_return_if_fail(len <= inode->size - offset);
 
-#if 0
-    /* Start fetches on any data blocks that we will need for this read. */
-    BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
-    barrier->op = STORE_OP_BARRIER;
+    BlueSkyProfile *profile = bluesky_profile_get();
+
+    bluesky_profile_add_event(profile,
+                              g_strdup_printf("Start file read prefetch"));
     uint64_t start_block, end_block;
     start_block = offset / BLUESKY_BLOCK_SIZE;
     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
-    if (bluesky_verbose) {
-        g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
-              "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
-              start_block, end_block);
-    }
     for (uint64_t i = start_block; i <= end_block; i++) {
         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
                                          i);
         if (b->type == BLUESKY_BLOCK_REF)
-            bluesky_block_fetch(inode, b, barrier);
+            bluesky_cloudlog_prefetch(b->ref);
     }
-    bluesky_store_async_submit(barrier);
-    bluesky_store_async_wait(barrier);
-    bluesky_store_async_unref(barrier);
-    if (bluesky_verbose) {
-        g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
-    }
-#endif
+
+    bluesky_profile_add_event(profile,
+                              g_strdup_printf("End file read prefetch"));
 
     while (len > 0) {
         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
@@ -222,6 +207,9 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
         buf += bytes;
         len -= bytes;
     }
+
+    bluesky_profile_add_event(profile,
+                              g_strdup_printf("BlueSky read complete"));
 }
 
 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
@@ -234,7 +222,6 @@ void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
     bluesky_cloudlog_fetch(block->ref);
     g_mutex_unlock(block->ref->lock);
     block->type = BLUESKY_BLOCK_REF;
-    g_atomic_int_add(&inode->fs->cache_total, 1);  //FIXME
 }
 
 /* Write the given block to cloud-backed storage and mark it clean. */
@@ -248,10 +235,11 @@ void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
 
     g_assert(block->ref == NULL);
 
-    BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
+    BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs, NULL);
     cloudlog->type = LOGTYPE_DATA;
     cloudlog->inum = inode->inum;
     cloudlog->data = block->dirty;      // String ownership is transferred
+    bluesky_cloudlog_stats_update(cloudlog, 1);
     bluesky_cloudlog_sync(cloudlog);
     bluesky_cloudlog_ref(cloudlog);     // Reference for log_items list
     *log_items = g_list_prepend(*log_items, cloudlog);
@@ -288,11 +276,12 @@ void bluesky_file_drop_cached(BlueSkyInode *inode)
                 && g_atomic_int_get(&b->ref->data_lock_count) == 0
                 && (b->ref->location_flags != 0))
             {
+                bluesky_cloudlog_stats_update(b->ref, -1);
                 bluesky_string_unref(b->ref->data);
                 b->ref->data = NULL;
+                bluesky_cloudlog_stats_update(b->ref, 1);
             }
             g_mutex_unlock(b->ref->lock);
-            g_atomic_int_add(&inode->fs->cache_total, -1);
         }
     }
 }