mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
src: set the OS level thread name
Setting the thread name makes it easier to debug libvirtd when many threads are running. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
eab55b2534
commit
c85256b31b
@ -3279,6 +3279,7 @@ virThreadCreateFull;
|
|||||||
virThreadID;
|
virThreadID;
|
||||||
virThreadIsSelf;
|
virThreadIsSelf;
|
||||||
virThreadJoin;
|
virThreadJoin;
|
||||||
|
virThreadMaxName;
|
||||||
virThreadSelf;
|
virThreadSelf;
|
||||||
virThreadSelfID;
|
virThreadSelfID;
|
||||||
|
|
||||||
|
@ -175,23 +175,57 @@ void virCondBroadcast(virCondPtr c)
|
|||||||
|
|
||||||
struct virThreadArgs {
|
struct virThreadArgs {
|
||||||
virThreadFunc func;
|
virThreadFunc func;
|
||||||
const char *funcName;
|
char *name;
|
||||||
bool worker;
|
bool worker;
|
||||||
void *opaque;
|
void *opaque;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
size_t virThreadMaxName(void)
|
||||||
|
{
|
||||||
|
#if defined(__FreeBSD__) || defined(__APPLE__)
|
||||||
|
return 63;
|
||||||
|
#else
|
||||||
|
# ifdef __linux__
|
||||||
|
return 15;
|
||||||
|
# else
|
||||||
|
return 0; /* unlimited */
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static void *virThreadHelper(void *data)
|
static void *virThreadHelper(void *data)
|
||||||
{
|
{
|
||||||
struct virThreadArgs *args = data;
|
struct virThreadArgs *args = data;
|
||||||
struct virThreadArgs local = *args;
|
struct virThreadArgs local = *args;
|
||||||
|
g_autofree char *thname = NULL;
|
||||||
|
size_t maxname = virThreadMaxName();
|
||||||
|
|
||||||
/* Free args early, rather than tying it up during the entire thread. */
|
/* Free args early, rather than tying it up during the entire thread. */
|
||||||
VIR_FREE(args);
|
g_free(args);
|
||||||
|
|
||||||
if (local.worker)
|
if (local.worker)
|
||||||
virThreadJobSetWorker(local.funcName);
|
virThreadJobSetWorker(local.name);
|
||||||
else
|
else
|
||||||
virThreadJobSet(local.funcName);
|
virThreadJobSet(local.name);
|
||||||
|
|
||||||
|
if (maxname) {
|
||||||
|
thname = g_strndup(local.name, maxname);
|
||||||
|
} else {
|
||||||
|
thname = g_strdup(local.name);
|
||||||
|
}
|
||||||
|
g_free(local.name);
|
||||||
|
|
||||||
|
#if defined(__linux__) || defined(WIN32)
|
||||||
|
pthread_setname_np(pthread_self(), thname);
|
||||||
|
#else
|
||||||
|
# ifdef __FreeBSD__
|
||||||
|
pthread_set_name_np(pthread_self(), thname);
|
||||||
|
# else
|
||||||
|
# ifdef __APPLE__
|
||||||
|
pthread_setname_np(thname);
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
local.func(local.opaque);
|
local.func(local.opaque);
|
||||||
|
|
||||||
@ -204,7 +238,7 @@ static void *virThreadHelper(void *data)
|
|||||||
int virThreadCreateFull(virThreadPtr thread,
|
int virThreadCreateFull(virThreadPtr thread,
|
||||||
bool joinable,
|
bool joinable,
|
||||||
virThreadFunc func,
|
virThreadFunc func,
|
||||||
const char *funcName,
|
const char *name,
|
||||||
bool worker,
|
bool worker,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
@ -221,7 +255,7 @@ int virThreadCreateFull(virThreadPtr thread,
|
|||||||
}
|
}
|
||||||
|
|
||||||
args->func = func;
|
args->func = func;
|
||||||
args->funcName = funcName;
|
args->name = g_strdup(name);
|
||||||
args->worker = worker;
|
args->worker = worker;
|
||||||
args->opaque = opaque;
|
args->opaque = opaque;
|
||||||
|
|
||||||
@ -230,7 +264,8 @@ int virThreadCreateFull(virThreadPtr thread,
|
|||||||
|
|
||||||
err = pthread_create(&thread->thread, &attr, virThreadHelper, args);
|
err = pthread_create(&thread->thread, &attr, virThreadHelper, args);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
VIR_FREE(args);
|
g_free(args->name);
|
||||||
|
g_free(args);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
/* New thread owns 'args' in success case, so don't free */
|
/* New thread owns 'args' in success case, so don't free */
|
||||||
|
@ -90,13 +90,15 @@ typedef void (*virThreadFunc)(void *opaque);
|
|||||||
int virThreadCreateFull(virThreadPtr thread,
|
int virThreadCreateFull(virThreadPtr thread,
|
||||||
bool joinable,
|
bool joinable,
|
||||||
virThreadFunc func,
|
virThreadFunc func,
|
||||||
const char *funcName,
|
const char *name,
|
||||||
bool worker,
|
bool worker,
|
||||||
void *opaque) G_GNUC_WARN_UNUSED_RESULT;
|
void *opaque) G_GNUC_WARN_UNUSED_RESULT;
|
||||||
void virThreadSelf(virThreadPtr thread);
|
void virThreadSelf(virThreadPtr thread);
|
||||||
bool virThreadIsSelf(virThreadPtr thread);
|
bool virThreadIsSelf(virThreadPtr thread);
|
||||||
void virThreadJoin(virThreadPtr thread);
|
void virThreadJoin(virThreadPtr thread);
|
||||||
|
|
||||||
|
size_t virThreadMaxName(void);
|
||||||
|
|
||||||
/* This API is *NOT* for general use. It exists solely as a stub
|
/* This API is *NOT* for general use. It exists solely as a stub
|
||||||
* for integration with libselinux AVC callbacks */
|
* for integration with libselinux AVC callbacks */
|
||||||
void virThreadCancel(virThreadPtr thread);
|
void virThreadCancel(virThreadPtr thread);
|
||||||
|
Loading…
Reference in New Issue
Block a user