(Mostly) merge local and cloud logging together.
[bluesky.git] / bluesky / debug.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 <stdlib.h>
11 #include <inttypes.h>
12 #include <glib.h>
13 #include <string.h>
14
15 #include "bluesky-private.h"
16
17 /* Debugging support for BlueSky. */
18
19 static void inode_dump(gpointer key, gpointer value, gpointer user_data)
20 {
21     BlueSkyInode *inode = (BlueSkyInode *)value;
22
23     g_print("Inode %"PRIu64":\n", inode->inum);
24
25     gboolean locked = TRUE;
26     if (g_mutex_trylock(inode->lock)) {
27         locked = FALSE;
28         g_mutex_unlock(inode->lock);
29     }
30     g_print("    Locked: %c   Refcount: %d\n",
31             locked ? 'T' : 'F', inode->refcount);
32
33     g_print("    Type: %d   Mode: %o\n", inode->type, inode->mode);
34     g_print("    change_count = %"PRIu64", change_commit = %"PRIu64"\n",
35             inode->change_count, inode->change_commit);
36 }
37
38 static void cloudlog_dump(gpointer key, gpointer value, gpointer user_data)
39 {
40     BlueSkyCloudLog *log = (BlueSkyCloudLog *)value;
41
42     for (int i = 0; i < sizeof(BlueSkyCloudID); i++) {
43         g_print("%02x", (uint8_t)(log->id.bytes[i]));
44     }
45     g_print(": ty=%d inode=%"PRIu64" locs=%x log@(%d,%d)\n",
46             log->type, log->inum, log->location_flags,
47             log->log_seq, log->log_offset);
48 }
49
50 /* Dump a summary of filesystem state as it is cached in memory. */
51 void bluesky_debug_dump(BlueSkyFS *fs)
52 {
53     g_print("*** DEBUG DUMP FOR FILESYSTEM %s ***\n", fs->name);
54     g_print("Cached blocks: %d\tDirty blocks: %d\n",
55             g_atomic_int_get(&fs->cache_total),
56             g_atomic_int_get(&fs->cache_dirty));
57     g_print("Cached inodes: %u\tNext inode: %"PRIu64"\n",
58             g_hash_table_size(fs->inodes), fs->next_inum);
59
60     GList *item;
61     g_print("Unsynced inode list:");
62     for (item = fs->unlogged_list.next; item != NULL; item = item->next) {
63         g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum);
64     }
65     g_print("\n");
66     g_print("Dirty inode LRU list:");
67     for (item = fs->dirty_list.next; item != NULL; item = item->next) {
68         g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum);
69     }
70     g_print("\n");
71     g_print("Accessed inode LRU list:");
72     for (item = fs->accessed_list.next; item != NULL; item = item->next) {
73         g_print(" %"PRIu64";", ((BlueSkyInode *)item->data)->inum);
74     }
75     g_print("\n");
76
77     g_hash_table_foreach(fs->inodes, inode_dump, fs);
78
79     g_print("\nLog Objects:\n");
80     g_hash_table_foreach(fs->locations, cloudlog_dump, fs);
81     g_print("\n");
82 }
83
84 /* Statistics counters: for operation counts, bytes transferred, etc. */
85 static GStaticMutex stats_lock = G_STATIC_MUTEX_INIT;
86 static GList *stats_list = NULL;
87
88 struct bluesky_stats *bluesky_stats_new(const char *name)
89 {
90     struct bluesky_stats *stats = g_new0(struct bluesky_stats, 1);
91     stats->name = name;
92     g_static_mutex_lock(&stats_lock);
93     stats_list = g_list_append(stats_list, stats);
94     g_static_mutex_unlock(&stats_lock);
95     return stats;
96 }
97
98 void bluesky_stats_add(struct bluesky_stats *stats, int64_t value)
99 {
100     __sync_fetch_and_add(&stats->count, (int64_t)1);
101     __sync_fetch_and_add(&stats->sum, value);
102 }
103
104 void bluesky_stats_dump_all()
105 {
106     g_static_mutex_lock(&stats_lock);
107     for (GList *item = stats_list; item != NULL; item = item->next) {
108         struct bluesky_stats *stats = (struct bluesky_stats *)item->data;
109         g_print("%s: count=%"PRIi64" sum=%"PRIi64"\n",
110                 stats->name, stats->count, stats->sum);
111     }
112     g_static_mutex_unlock(&stats_lock);
113 }