[gnome-software] Use the thread-safe version of the profiling code automatically



commit f3b455ba81a4a7faafc988b14a6c40282799c1a8
Author: Richard Hughes <richard hughsie com>
Date:   Tue Oct 29 17:35:58 2013 +0000

    Use the thread-safe version of the profiling code automatically
    
    We can just compare the current thread with the 'main' thread and append the
    thread ID automatically in the case of a new thread. Relying on API users to
    know what thread they are coming from is just not required and is easy to get
    wrong.

 src/gs-plugin-loader.c                     |    4 +-
 src/gs-profile.c                           |   58 ++++++++++++++--------------
 src/gs-profile.h                           |    4 --
 src/plugins/gs-plugin-appstream.c          |   16 ++++----
 src/plugins/gs-plugin-packagekit-refine.c  |   24 ++++++------
 src/plugins/gs-plugin-packagekit-search.c  |    8 ++--
 src/plugins/gs-plugin-packagekit-updates.c |    8 ++--
 src/plugins/gs-plugin-packagekit.c         |    8 ++--
 8 files changed, 63 insertions(+), 67 deletions(-)
---
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index 15d421e..0ef5f60 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -212,7 +212,7 @@ gs_plugin_loader_run_refine_plugin (GsPluginLoader *plugin_loader,
                                              function_name_parent,
                                              function_name);
        }
-       gs_profile_start_full (plugin_loader->priv->profile, profile_id);
+       gs_profile_start (plugin_loader->priv->profile, profile_id);
        ret = plugin_func (plugin, list, flags, cancellable, error);
        if (!ret) {
                /* check the plugin is well behaved and sets error
@@ -237,7 +237,7 @@ gs_plugin_loader_run_refine_plugin (GsPluginLoader *plugin_loader,
        }
 out:
        if (profile_id != NULL) {
-               gs_profile_stop_full (plugin_loader->priv->profile, profile_id);
+               gs_profile_stop (plugin_loader->priv->profile, profile_id);
                gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_FINISHED);
        }
        g_free (profile_id);
diff --git a/src/gs-profile.c b/src/gs-profile.c
index 008aea0..c70bc99 100644
--- a/src/gs-profile.c
+++ b/src/gs-profile.c
@@ -34,6 +34,7 @@ struct GsProfilePrivate
        GPtrArray       *current;
        GPtrArray       *archived;
        GMutex           mutex;
+       GThread         *unthreaded;
 };
 
 typedef struct {
@@ -81,42 +82,41 @@ gs_profile_item_find (GPtrArray *array, const gchar *id)
 void
 gs_profile_start (GsProfile *profile, const gchar *id)
 {
+       GThread *self;
        GsProfileItem *item;
+       gchar *id_thr;
 
        g_return_if_fail (GS_IS_PROFILE (profile));
        g_return_if_fail (id != NULL);
 
+       /* only use the thread ID when not using the main thread */
+       self = g_thread_self ();
+       if (self != profile->priv->unthreaded) {
+               id_thr = g_strdup_printf ("%p~%s", self, id);
+       } else {
+               id_thr = g_strdup (id);
+       }
+
        /* lock */
        g_mutex_lock (&profile->priv->mutex);
 
        /* already started */
-       item = gs_profile_item_find (profile->priv->current, id);
+       item = gs_profile_item_find (profile->priv->current, id_thr);
        if (item != NULL) {
                gs_profile_dump (profile);
-               g_warning ("Already a started task for %s", id);
+               g_warning ("Already a started task for %s", id_thr);
                goto out;
        }
 
        /* add new item */
        item = g_new0 (GsProfileItem, 1);
-       item->id = g_strdup (id);
+       item->id = g_strdup (id_thr);
        item->time_start = g_get_real_time ();
        g_ptr_array_add (profile->priv->current, item);
-       g_debug ("run %s", id);
+       g_debug ("run %s", id_thr);
 out:
        /* unlock */
        g_mutex_unlock (&profile->priv->mutex);
-}
-
-/**
- * gs_profile_start_full:
- **/
-void
-gs_profile_start_full (GsProfile *profile, const gchar *id)
-{
-       gchar *id_thr;
-       id_thr = g_strdup_printf ("%p~%s", g_thread_self (), id);
-       gs_profile_start (profile, id_thr);
        g_free (id_thr);
 }
 
@@ -126,26 +126,36 @@ gs_profile_start_full (GsProfile *profile, const gchar *id)
 void
 gs_profile_stop (GsProfile *profile, const gchar *id)
 {
+       GThread *self;
        GsProfileItem *item;
+       gchar *id_thr;
        gdouble elapsed_ms;
 
        g_return_if_fail (GS_IS_PROFILE (profile));
        g_return_if_fail (id != NULL);
 
+       /* only use the thread ID when not using the main thread */
+       self = g_thread_self ();
+       if (self != profile->priv->unthreaded) {
+               id_thr = g_strdup_printf ("%p~%s", self, id);
+       } else {
+               id_thr = g_strdup (id);
+       }
+
        /* lock */
        g_mutex_lock (&profile->priv->mutex);
 
        /* already started */
-       item = gs_profile_item_find (profile->priv->current, id);
+       item = gs_profile_item_find (profile->priv->current, id_thr);
        if (item == NULL) {
-               g_warning ("Not already a started task for %s", id);
+               g_warning ("Not already a started task for %s", id_thr);
                goto out;
        }
 
        /* debug */
        elapsed_ms = (item->time_stop - item->time_start) / 1000;
        if (elapsed_ms > 5)
-               g_debug ("%s took %.0fms", id, elapsed_ms);
+               g_debug ("%s took %.0fms", id_thr, elapsed_ms);
 
        /* update */
        item->time_stop = g_get_real_time ();
@@ -156,17 +166,6 @@ gs_profile_stop (GsProfile *profile, const gchar *id)
 out:
        /* unlock */
        g_mutex_unlock (&profile->priv->mutex);
-}
-
-/**
- * gs_profile_stop_full:
- **/
-void
-gs_profile_stop_full (GsProfile *profile, const gchar *id)
-{
-       gchar *id_thr;
-       id_thr = g_strdup_printf ("%p~%s", g_thread_self (), id);
-       gs_profile_stop (profile, id_thr);
        g_free (id_thr);
 }
 
@@ -275,6 +274,7 @@ gs_profile_init (GsProfile *profile)
 {
        profile->priv = GS_PROFILE_GET_PRIVATE (profile);
        profile->priv->current = g_ptr_array_new ();
+       profile->priv->unthreaded = g_thread_self ();
        profile->priv->archived = g_ptr_array_new_with_free_func ((GDestroyNotify) gs_profile_item_free);
        g_mutex_init (&profile->priv->mutex);
 }
diff --git a/src/gs-profile.h b/src/gs-profile.h
index ff106be..13f3fa5 100644
--- a/src/gs-profile.h
+++ b/src/gs-profile.h
@@ -53,10 +53,6 @@ void          gs_profile_start               (GsProfile      *profile,
                                                 const gchar    *id);
 void            gs_profile_stop                (GsProfile      *profile,
                                                 const gchar    *id);
-void            gs_profile_start_full          (GsProfile      *profile,
-                                                const gchar    *id);
-void            gs_profile_stop_full           (GsProfile      *profile,
-                                                const gchar    *id);
 void            gs_profile_dump                (GsProfile      *profile);
 
 G_END_DECLS
diff --git a/src/plugins/gs-plugin-appstream.c b/src/plugins/gs-plugin-appstream.c
index 359a653..a3bd35a 100644
--- a/src/plugins/gs-plugin-appstream.c
+++ b/src/plugins/gs-plugin-appstream.c
@@ -623,21 +623,21 @@ gs_plugin_refine (GsPlugin *plugin,
                        goto out;
        }
 
-       gs_profile_start_full (plugin->profile, "appstream::refine");
+       gs_profile_start (plugin->profile, "appstream::refine");
        for (l = list; l != NULL; l = l->next) {
                app = GS_APP (l->data);
                ret = gs_plugin_refine_from_id (plugin, app, error);
                if (!ret) {
-                       gs_profile_stop_full (plugin->profile, "appstream::refine");
+                       gs_profile_stop (plugin->profile, "appstream::refine");
                        goto out;
                }
                ret = gs_plugin_refine_from_pkgname (plugin, app, error);
                if (!ret) {
-                       gs_profile_stop_full (plugin->profile, "appstream::refine");
+                       gs_profile_stop (plugin->profile, "appstream::refine");
                        goto out;
                }
        }
-       gs_profile_stop_full (plugin->profile, "appstream::refine");
+       gs_profile_stop (plugin->profile, "appstream::refine");
 
        /* sucess */
        ret = TRUE;
@@ -673,7 +673,7 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
        }
 
        /* get the two search terms */
-       gs_profile_start_full (plugin->profile, "appstream::add-category-apps");
+       gs_profile_start (plugin->profile, "appstream::add-category-apps");
        search_id1 = gs_category_get_id (category);
        parent = gs_category_get_parent (category);
        if (parent != NULL)
@@ -703,7 +703,7 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
                        goto out;
                gs_plugin_add_app (list, app);
        }
-       gs_profile_stop_full (plugin->profile, "appstream::add-category-apps");
+       gs_profile_stop (plugin->profile, "appstream::add-category-apps");
 out:
        return ret;
 }
@@ -778,7 +778,7 @@ gs_plugin_add_categories (GsPlugin *plugin,
        }
 
        /* find out how many packages are in each category */
-       gs_profile_start_full (plugin->profile, "appstream::add-categories");
+       gs_profile_start (plugin->profile, "appstream::add-categories");
        for (l = *list; l != NULL; l = l->next) {
                parent = GS_CATEGORY (l->data);
                children = gs_category_get_subcategories (parent);
@@ -813,7 +813,7 @@ gs_plugin_add_categories (GsPlugin *plugin,
                }
                g_list_free (children);
        }
-       gs_profile_stop_full (plugin->profile, "appstream::add-categories");
+       gs_profile_stop (plugin->profile, "appstream::add-categories");
 out:
        return ret;
 }
diff --git a/src/plugins/gs-plugin-packagekit-refine.c b/src/plugins/gs-plugin-packagekit-refine.c
index e09d324..82e8f81 100644
--- a/src/plugins/gs-plugin-packagekit-refine.c
+++ b/src/plugins/gs-plugin-packagekit-refine.c
@@ -95,11 +95,11 @@ gs_plugin_packagekit_progress_cb (PkProgress *progress,
 
        /* profile */
        if (status == PK_STATUS_ENUM_SETUP) {
-               gs_profile_start_full (plugin->profile,
-                                      "packagekit-refine::transaction");
+               gs_profile_start (plugin->profile,
+                                 "packagekit-refine::transaction");
        } else if (status == PK_STATUS_ENUM_FINISHED) {
-               gs_profile_stop_full (plugin->profile,
-                                     "packagekit-refine::transaction");
+               gs_profile_stop (plugin->profile,
+                                "packagekit-refine::transaction");
        }
 
        plugin_status = packagekit_status_enum_to_plugin_status (status);
@@ -539,7 +539,7 @@ gs_plugin_refine_require_details (GsPlugin *plugin,
        GsApp *app;
        gboolean ret = TRUE;
 
-       gs_profile_start_full (plugin->profile, "packagekit-refine[source->licence]");
+       gs_profile_start (plugin->profile, "packagekit-refine[source->licence]");
        for (l = list; l != NULL; l = l->next) {
                app = GS_APP (l->data);
                if (gs_app_get_licence (app) != NULL &&
@@ -561,7 +561,7 @@ gs_plugin_refine_require_details (GsPlugin *plugin,
        if (!ret)
                goto out;
 out:
-       gs_profile_stop_full (plugin->profile, "packagekit-refine[source->licence]");
+       gs_profile_stop (plugin->profile, "packagekit-refine[source->licence]");
        g_list_free (list_tmp);
        return ret;
 }
@@ -598,7 +598,7 @@ gs_plugin_refine (GsPlugin *plugin,
        gboolean ret = TRUE;
 
        /* can we resolve in one go? */
-       gs_profile_start_full (plugin->profile, "packagekit-refine[name->id]");
+       gs_profile_start (plugin->profile, "packagekit-refine[name->id]");
        for (l = list; l != NULL; l = l->next) {
                app = GS_APP (l->data);
                if (gs_app_get_source_id_default (app) != NULL)
@@ -621,10 +621,10 @@ gs_plugin_refine (GsPlugin *plugin,
                if (!ret)
                        goto out;
        }
-       gs_profile_stop_full (plugin->profile, "packagekit-refine[name->id]");
+       gs_profile_stop (plugin->profile, "packagekit-refine[name->id]");
 
        /* set the package-id for an installed desktop file */
-       gs_profile_start_full (plugin->profile, "packagekit-refine[desktop-filename->id]");
+       gs_profile_start (plugin->profile, "packagekit-refine[desktop-filename->id]");
        for (l = list; l != NULL; l = l->next) {
                if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION) == 0)
                        continue;
@@ -642,10 +642,10 @@ gs_plugin_refine (GsPlugin *plugin,
                if (!ret)
                        goto out;
        }
-       gs_profile_stop_full (plugin->profile, "packagekit-refine[desktop-filename->id]");
+       gs_profile_stop (plugin->profile, "packagekit-refine[desktop-filename->id]");
 
        /* any update details missing? */
-       gs_profile_start_full (plugin->profile, "packagekit-refine[id->update-details]");
+       gs_profile_start (plugin->profile, "packagekit-refine[id->update-details]");
        for (l = list; l != NULL; l = l->next) {
                app = GS_APP (l->data);
                if (gs_app_get_state (app) != GS_APP_STATE_UPDATABLE)
@@ -664,7 +664,7 @@ gs_plugin_refine (GsPlugin *plugin,
                if (!ret)
                        goto out;
        }
-       gs_profile_stop_full (plugin->profile, "packagekit-refine[id->update-details]");
+       gs_profile_stop (plugin->profile, "packagekit-refine[id->update-details]");
 
        /* any important details missing? */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_LICENCE) > 0 ||
diff --git a/src/plugins/gs-plugin-packagekit-search.c b/src/plugins/gs-plugin-packagekit-search.c
index 3cd93b0..77ff5ec 100644
--- a/src/plugins/gs-plugin-packagekit-search.c
+++ b/src/plugins/gs-plugin-packagekit-search.c
@@ -98,11 +98,11 @@ gs_plugin_packagekit_progress_cb (PkProgress *progress,
 
        /* profile */
        if (status == PK_STATUS_ENUM_SETUP) {
-               gs_profile_start_full (plugin->profile,
-                                      "packagekit-search::transaction");
+               gs_profile_start (plugin->profile,
+                                 "packagekit-search::transaction");
        } else if (status == PK_STATUS_ENUM_FINISHED) {
-               gs_profile_stop_full (plugin->profile,
-                                     "packagekit-search::transaction");
+               gs_profile_stop (plugin->profile,
+                                "packagekit-search::transaction");
        }
 
        plugin_status = packagekit_status_enum_to_plugin_status (status);
diff --git a/src/plugins/gs-plugin-packagekit-updates.c b/src/plugins/gs-plugin-packagekit-updates.c
index 6533c8c..3730458 100644
--- a/src/plugins/gs-plugin-packagekit-updates.c
+++ b/src/plugins/gs-plugin-packagekit-updates.c
@@ -94,11 +94,11 @@ gs_plugin_packagekit_progress_cb (PkProgress *progress,
 
        /* profile */
        if (status == PK_STATUS_ENUM_SETUP) {
-               gs_profile_start_full (plugin->profile,
-                                      "packagekit-refine::transaction");
+               gs_profile_start (plugin->profile,
+                                 "packagekit-refine::transaction");
        } else if (status == PK_STATUS_ENUM_FINISHED) {
-               gs_profile_stop_full (plugin->profile,
-                                     "packagekit-refine::transaction");
+               gs_profile_stop (plugin->profile,
+                                "packagekit-refine::transaction");
        }
 
        plugin_status = packagekit_status_enum_to_plugin_status (status);
diff --git a/src/plugins/gs-plugin-packagekit.c b/src/plugins/gs-plugin-packagekit.c
index c860662..c1db3cb 100644
--- a/src/plugins/gs-plugin-packagekit.c
+++ b/src/plugins/gs-plugin-packagekit.c
@@ -94,11 +94,11 @@ gs_plugin_packagekit_progress_cb (PkProgress *progress,
 
        /* profile */
        if (status == PK_STATUS_ENUM_SETUP) {
-               gs_profile_start_full (plugin->profile,
-                                      "packagekit-refine::transaction");
+               gs_profile_start (plugin->profile,
+                                 "packagekit-refine::transaction");
        } else if (status == PK_STATUS_ENUM_FINISHED) {
-               gs_profile_stop_full (plugin->profile,
-                                     "packagekit-refine::transaction");
+               gs_profile_stop (plugin->profile,
+                                "packagekit-refine::transaction");
        }
 
        plugin_status = packagekit_status_enum_to_plugin_status (status);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]