Add locking, thread-ID tracking to profiling.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Tue, 23 Nov 2010 23:48:41 +0000 (15:48 -0800)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Tue, 23 Nov 2010 23:48:41 +0000 (15:48 -0800)
bluesky/bluesky.h
bluesky/util.c

index 6bbf4a2..653adc8 100644 (file)
@@ -368,6 +368,7 @@ void bluesky_debug_dump(BlueSkyFS *fs);
 
 /* Request response time tracking. */
 typedef struct BlueSkyProfile {
+    GMutex *lock;
     char *description;
     GList *events;
 } BlueSkyProfile;
index 90eda70..99e341f 100644 (file)
@@ -6,11 +6,15 @@
  * TODO: Licensing
  */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdint.h>
 #include <glib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
 
 #include "bluesky-private.h"
 
@@ -340,13 +344,16 @@ void bluesky_rangeset_get_extents(BlueSkyRangeset *rangeset,
 /**** Request response-time tracking. ****/
 /* TODO: Locking */
 typedef struct {
+    int tid;
     bluesky_time_hires timestamp;
     char *message;
 } RTEvent;
 
 BlueSkyProfile *bluesky_profile_new()
 {
-    return g_new0(BlueSkyProfile, 1);
+    BlueSkyProfile *profile = g_new0(BlueSkyProfile, 1);
+    profile->lock = g_mutex_new();
+    return profile;
 }
 
 void bluesky_profile_free(BlueSkyProfile *profile)
@@ -357,6 +364,7 @@ void bluesky_profile_free(BlueSkyProfile *profile)
         g_free(event);
         profile->events = g_list_delete_link(profile->events, profile->events);
     }
+    g_mutex_free(profile->lock);
     g_free(profile->description);
     g_free(profile);
 }
@@ -365,26 +373,32 @@ void bluesky_profile_add_event(BlueSkyProfile *profile, char *message)
 {
     g_return_if_fail(profile != NULL);
 
+    g_mutex_lock(profile->lock);
     RTEvent *event = g_new(RTEvent, 1);
     event->timestamp = bluesky_now_hires();
+    /* FIXME: Non-portable */
+    event->tid = syscall(SYS_gettid);
     event->message = message;
     profile->events = g_list_prepend(profile->events, event);
+    g_mutex_unlock(profile->lock);
 }
 
 void bluesky_profile_print(BlueSkyProfile *profile)
 {
     g_return_if_fail(profile != NULL);
 
+    g_mutex_lock(profile->lock);
     g_print("Event Timeline: %s\n", profile->description);
     GList *link = g_list_last(profile->events);
     bluesky_time_hires last_time = 0;
     while (link != NULL) {
         RTEvent *event = (RTEvent *)link->data;
-        g_print("  [%"PRIi64" ns]: %s\n",
-                event->timestamp - last_time, event->message);
+        g_print("  [%d] [%"PRIi64" ns]: %s\n",
+                event->tid, event->timestamp - last_time, event->message);
         last_time = event->timestamp;
         link = link->prev;
     }
+    g_mutex_unlock(profile->lock);
 }
 
 static GStaticPrivate per_thread_profile = G_STATIC_PRIVATE_INIT;