]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Add support for tracking thread jobs
authorJiri Denemark <jdenemar@redhat.com>
Mon, 8 Jul 2013 10:27:34 +0000 (12:27 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Wed, 25 Mar 2015 09:00:53 +0000 (10:00 +0100)
Each thread can use a thread local variable to keep the name of a job
which is currently running in the job.

The virThreadJobSetWorker API is supposed to be called once by any
thread which is used as a worker, i.e., it is waiting in a pool, woken
up to do a job, and returned back to the pool.

The virThreadJobSet/virThreadJobClear APIs are to be called at the
beginning/end of each job.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
include/libvirt/virterror.h
po/POTFILES.in
src/Makefile.am
src/libvirt_private.syms
src/util/virerror.c
src/util/virthreadjob.c [new file with mode: 0644]
src/util/virthreadjob.h [new file with mode: 0644]

index 5dc99dc1bda3cd84b6333c458d40d6bee3045f67..9c5b0692820ebe07bbb53950899e07c46651bd35 100644 (file)
@@ -125,6 +125,7 @@ typedef enum {
     VIR_FROM_FIREWALL = 59,     /* Error from firewall */
 
     VIR_FROM_POLKIT = 60,       /* Error from polkit code */
+    VIR_FROM_THREAD = 61,       /* Error from thread utils */
 
 # ifdef VIR_ENUM_SENTINELS
     VIR_ERR_DOMAIN_LAST
index ab6ae3bf2319a73a67febb4425137c260d145a37..dd06ab3c524b520c1a490c0a25e85e9ca4d55d98 100644 (file)
@@ -220,6 +220,7 @@ src/util/virstorageencryption.c
 src/util/virstoragefile.c
 src/util/virstring.c
 src/util/virsysinfo.c
+src/util/virthreadjob.c
 src/util/virtime.c
 src/util/virtpm.c
 src/util/virtypedparam.c
index 00b47e78a7ce90c77eb581dea4296850b90d31b9..ec62ab4607f58ad46b2e644d591f07ae0269a482 100644 (file)
@@ -160,6 +160,7 @@ UTIL_SOURCES =                                                      \
                util/virsysinfo.c util/virsysinfo.h             \
                util/virsystemd.c util/virsystemd.h             \
                util/virthread.c util/virthread.h               \
+               util/virthreadjob.c utils/virthreadjob.h        \
                util/virthreadpool.c util/virthreadpool.h       \
                util/virtime.h util/virtime.c                   \
                util/virtpm.h util/virtpm.c                     \
@@ -2147,6 +2148,7 @@ libvirt_setuid_rpc_client_la_SOURCES =            \
                util/virstring.c                \
                util/virtime.c                  \
                util/virthread.c                \
+               util/virthreadjob.c             \
                util/virtypedparam.c            \
                util/viruri.c                   \
                util/virutil.c                  \
index 33222f075e491a16d1a621acbc7857254569e12a..a8dced5cdcf1c24b7f5835783170ce2b8d471466 100644 (file)
@@ -2190,6 +2190,13 @@ virThreadSelf;
 virThreadSelfID;
 
 
+# util/virthreadjob.h
+virThreadJobClear;
+virThreadJobGet;
+virThreadJobSet;
+virThreadJobSetWorker;
+
+
 # util/virthreadpool.h
 virThreadPoolFree;
 virThreadPoolGetMaxWorkers;
index 9635c8212fdf60cd24556a8f87890c73e8913202..c4e84e73cd4cf6d6aa34ee2b0963c23e6be4434e 100644 (file)
@@ -132,6 +132,7 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST,
               "Firewall",
 
               "Polkit", /* 60 */
+              "Thread jobs",
     )
 
 
diff --git a/src/util/virthreadjob.c b/src/util/virthreadjob.c
new file mode 100644 (file)
index 0000000..2f483f9
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * virthreadjob.c: code for tracking job associated with current thread
+ *
+ * Copyright (C) 2013-2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Jiri Denemark <jdenemar@redhat.com>
+ */
+
+#include <config.h>
+
+#include "internal.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virthread.h"
+#include "virthreadjob.h"
+
+#define VIR_FROM_THIS VIR_FROM_THREAD
+VIR_LOG_INIT("util.threadjob");
+
+virThreadLocal virThreadJobWorker;
+virThreadLocal virThreadJobName;
+
+
+static int
+virThreadJobOnceInit(void)
+{
+    if (virThreadLocalInit(&virThreadJobWorker, NULL) < 0 ||
+        virThreadLocalInit(&virThreadJobName, NULL) < 0)
+        return -1;
+    return 0;
+}
+
+VIR_ONCE_GLOBAL_INIT(virThreadJob)
+
+
+const char *
+virThreadJobGet(void)
+{
+    const char *job;
+
+    if (virThreadJobInitialize() < 0)
+        return NULL;
+
+    job = virThreadLocalGet(&virThreadJobName);
+    if (!job)
+        job = virThreadLocalGet(&virThreadJobWorker);
+
+    return job;
+}
+
+
+void
+virThreadJobSetWorker(const char *worker)
+{
+    if (!worker || virThreadJobInitialize() < 0)
+        return;
+
+    if (virThreadLocalSet(&virThreadJobWorker, (void *) worker) < 0)
+        virReportSystemError(errno,
+                             _("cannot set worker name to %s"),
+                             worker);
+
+    VIR_DEBUG("Thread %llu is running worker %s", virThreadSelfID(), worker);
+}
+
+
+void
+virThreadJobSet(const char *caller)
+{
+    const char *worker;
+
+    if (!caller || virThreadJobInitialize() < 0)
+        return;
+
+    if (virThreadLocalSet(&virThreadJobName, (void *) caller) < 0)
+        virReportSystemError(errno,
+                             _("cannot set current job to %s"),
+                             caller);
+
+    if ((worker = virThreadLocalGet(&virThreadJobWorker))) {
+        VIR_DEBUG("Thread %llu (%s) is now running job %s",
+                  virThreadSelfID(), worker, caller);
+    } else {
+        VIR_DEBUG("Thread %llu is now running job %s",
+                  virThreadSelfID(), caller);
+    }
+}
+
+
+void
+virThreadJobClear(int rv)
+{
+    const char *old;
+    const char *worker;
+
+    if (virThreadJobInitialize() < 0)
+        return;
+
+    if (!(old = virThreadLocalGet(&virThreadJobName)))
+        return;
+
+    if (virThreadLocalSet(&virThreadJobName, NULL) < 0)
+        virReportSystemError(errno, "%s", _("cannot reset current job"));
+
+    if ((worker = virThreadLocalGet(&virThreadJobWorker))) {
+        VIR_DEBUG("Thread %llu (%s) finished job %s with ret=%d",
+                  virThreadSelfID(), worker, old, rv);
+    } else {
+        VIR_DEBUG("Thread %llu finished job %s with ret=%d",
+                  virThreadSelfID(), old, rv);
+    }
+}
diff --git a/src/util/virthreadjob.h b/src/util/virthreadjob.h
new file mode 100644 (file)
index 0000000..659f91d
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * virthreadjob.h: APIs for tracking job associated with current thread
+ *
+ * Copyright (C) 2013-2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Jiri Denemark <jdenemar@redhat.com>
+ */
+
+#ifndef __VIR_THREAD_JOB_H__
+# define __VIR_THREAD_JOB_H__
+
+
+const char *virThreadJobGet(void);
+
+void virThreadJobSetWorker(const char *caller);
+void virThreadJobSet(const char *caller);
+void virThreadJobClear(int rv);
+
+#endif