Write profile data if and only if requested.
[bluesky.git] / bluesky / log.c
index d9ad11b..6d5c590 100644 (file)
@@ -62,6 +62,16 @@ static void log_commit(BlueSkyLog *log)
         return;
 
     fdatasync(log->fd);
+
+    /* Update disk-space usage statistics for the journal file. */
+    g_atomic_int_add(&log->disk_used, -log->current_log->disk_used);
+    struct stat statbuf;
+    if (fstat(log->fd, &statbuf) >= 0) {
+        /* Convert from 512-byte blocks to 1-kB units */
+        log->current_log->disk_used = (statbuf.st_blocks + 1) / 2;
+    }
+    g_atomic_int_add(&log->disk_used, log->current_log->disk_used);
+
     while (log->committed != NULL) {
         BlueSkyCloudLog *item = (BlueSkyCloudLog *)log->committed->data;
         g_mutex_lock(item->lock);
@@ -392,7 +402,8 @@ BlueSkyCacheFile *bluesky_cachefile_lookup(BlueSkyFS *fs,
         /* A stale reference to a journal file which doesn't exist any longer
          * because it was reclaimed.  Return NULL. */
     } else if (map == NULL) {
-        g_print("Adding cache file %s\n", logname);
+        if (bluesky_verbose)
+            g_print("Adding cache file %s\n", logname);
 
         map = g_new0(BlueSkyCacheFile, 1);
         map->fs = fs;
@@ -490,8 +501,10 @@ static void cloudlog_partial_fetch_complete(BlueSkyStoreAsync *async,
                 item = bluesky_rangeset_lookup_next(items, item_offset);
                 if (item == NULL)
                     break;
-                g_print("  item offset from range request: %d\n",
-                        (int)(item->start + async->start));
+                if (bluesky_verbose) {
+                    g_print("  item offset from range request: %d\n",
+                            (int)(item->start + async->start));
+                }
                 if (bluesky_rangeset_insert(cachefile->items,
                                             async->start + item->start,
                                             item->length, item->data))
@@ -514,6 +527,16 @@ static void cloudlog_partial_fetch_complete(BlueSkyStoreAsync *async,
         cloudlog_partial_fetch_start(cachefile, async->start, async->len);
     }
 
+    /* Update disk-space usage statistics, since the writes above may have
+     * consumed more space. */
+    g_atomic_int_add(&cachefile->log->disk_used, -cachefile->disk_used);
+    struct stat statbuf;
+    if (fstatat(cachefile->log->dirfd, cachefile->filename, &statbuf, 0) >= 0) {
+        /* Convert from 512-byte blocks to 1-kB units */
+        cachefile->disk_used = (statbuf.st_blocks + 1) / 2;
+    }
+    g_atomic_int_add(&cachefile->log->disk_used, cachefile->disk_used);
+
     bluesky_cachefile_unref(cachefile);
     g_cond_broadcast(cachefile->cond);
     g_mutex_unlock(cachefile->lock);
@@ -594,14 +617,29 @@ BlueSkyRCStr *bluesky_log_map_object(BlueSkyCloudLog *item, gboolean map_data)
                 goto exit2;
             }
             if (rangeitem == NULL) {
-                g_print("Item at offset 0x%zx not available, need to fetch.\n",
-                        file_offset);
-                if (range_request)
-                    cloudlog_partial_fetch_start(map, file_offset, file_size);
+                if (bluesky_verbose) {
+                    g_print("Item at offset 0x%zx not available, need to fetch.\n",
+                            file_offset);
+                }
+                if (range_request) {
+                    uint64_t start = 0, length = 0, end;
+                    if (map->prefetches != NULL)
+                        bluesky_rangeset_get_extents(map->prefetches,
+                                                     &start, &length);
+                    start = MIN(start, file_offset);
+                    end = MAX(start + length, file_offset + file_size);
+                    length = end - start;
+                    cloudlog_partial_fetch_start(map, start, length);
+                    if (map->prefetches != NULL) {
+                        bluesky_rangeset_free(map->prefetches);
+                        map->prefetches = NULL;
+                    }
+                }
                 g_cond_wait(map->cond, map->lock);
             } else if (rangeitem->start == file_offset
                        && rangeitem->length == file_size) {
-                g_print("Item now available.\n");
+                if (bluesky_verbose)
+                    g_print("Item now available.\n");
                 break;
             }
         }
@@ -618,9 +656,7 @@ BlueSkyRCStr *bluesky_log_map_object(BlueSkyCloudLog *item, gboolean map_data)
         off_t length = lseek(fd, 0, SEEK_END);
         map->addr = (const char *)mmap(NULL, length, PROT_READ, MAP_SHARED,
                                        fd, 0);
-        g_atomic_int_add(&log->disk_used, -(map->len / 1024));
         map->len = length;
-        g_atomic_int_add(&log->disk_used, map->len / 1024);
 
         g_atomic_int_inc(&map->refcount);
 
@@ -638,8 +674,6 @@ BlueSkyRCStr *bluesky_log_map_object(BlueSkyCloudLog *item, gboolean map_data)
     str = bluesky_string_new_from_mmap(map, file_offset, file_size);
     map->atime = bluesky_get_current_time();
 
-    g_print("Returning item at offset 0x%zx.\n", file_offset);
-
 exit2:
     bluesky_cachefile_unref(map);
     g_mutex_unlock(map->lock);
@@ -656,7 +690,8 @@ void bluesky_mmap_unref(BlueSkyCacheFile *mmap)
     if (g_atomic_int_dec_and_test(&mmap->mapcount)) {
         g_mutex_lock(mmap->lock);
         if (g_atomic_int_get(&mmap->mapcount) == 0) {
-            g_print("Unmapped log segment %d...\n", mmap->log_seq);
+            if (bluesky_verbose)
+                g_print("Unmapped log segment %d...\n", mmap->log_seq);
             munmap((void *)mmap->addr, mmap->len);
             mmap->addr = NULL;
             g_atomic_int_add(&mmap->refcount, -1);