The VIR_DOMAIN_EVENT_ID_JOB_COMPLETED event will be triggered once a job
(such as migration) finishes and it will contain statistics for the job
as one would get by calling virDomainGetJobStats. Thanks to this event
it is now possible to get statistics of a completed migration of a
transient domain on the source host.
Signed-off-by: Jiri Denemark <jdenemar(a)redhat.com>
---
daemon/remote.c | 40 ++++++++++++++++++
include/libvirt/libvirt-domain.h | 26 ++++++++++++
src/conf/domain_event.c | 91 +++++++++++++++++++++++++++++++++++++++-
src/conf/domain_event.h | 12 ++++++
src/libvirt-domain.c | 4 +-
src/libvirt_private.syms | 2 +
src/remote/remote_driver.c | 43 +++++++++++++++++++
src/remote/remote_protocol.x | 15 ++++++-
src/remote_protocol-structs | 10 +++++
tools/virsh-domain.c | 28 +++++++++++++
10 files changed, 268 insertions(+), 3 deletions(-)
diff --git a/daemon/remote.c b/daemon/remote.c
index ca692a9..b9dda72 100644
--- a/daemon/remote.c
+++ b/daemon/remote.c
@@ -1099,6 +1099,45 @@ remoteRelayDomainEventMigrationIteration(virConnectPtr conn,
}
+static int
+remoteRelayDomainEventJobCompleted(virConnectPtr conn,
+ virDomainPtr dom,
+ int type,
+ virTypedParameterPtr params,
+ int nparams,
+ void *opaque)
+{
+ daemonClientEventCallbackPtr callback = opaque;
+ remote_domain_event_callback_job_completed_msg data;
+
+ if (callback->callbackID < 0 ||
+ !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
+ return -1;
+
+ VIR_DEBUG("Relaying domain migration completed event %s %d, "
+ "callback %d, type %d, params %p %d",
+ dom->name, dom->id, callback->callbackID, type, params, nparams);
+
+ /* build return data */
+ memset(&data, 0, sizeof(data));
+ data.callbackID = callback->callbackID;
+ make_nonnull_domain(&data.dom, dom);
+
+ data.type = type;
+ if (virTypedParamsSerialize(params, nparams,
+ (virTypedParameterRemotePtr *)
&data.params.params_val,
+ &data.params.params_len,
+ VIR_TYPED_PARAM_STRING_OKAY) < 0)
+ return -1;
+
+ remoteDispatchObjectEventSend(callback->client, remoteProgram,
+ REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED,
+
(xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg,
+ &data);
+ return 0;
+}
+
+
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
@@ -1121,6 +1160,7 @@ static virConnectDomainEventGenericCallback domainEventCallbacks[] =
{
VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
+ VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventJobCompleted),
};
verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);
diff --git a/include/libvirt/libvirt-domain.h b/include/libvirt/libvirt-domain.h
index 65f1618..5e6cb0c 100644
--- a/include/libvirt/libvirt-domain.h
+++ b/include/libvirt/libvirt-domain.h
@@ -3305,6 +3305,31 @@ typedef void
(*virConnectDomainEventMigrationIterationCallback)(virConnectPtr co
void *opaque);
/**
+ * virConnectDomainEventJobCompletedCallback:
+ * @conn: connection object
+ * @dom: domain on which the event occurred
+ * @type: job type (one of virDomainJobType)
+ * @params: job statistics stored as an array of virTypedParameter
+ * @nparams: size of the params array
+ * @opaque: application specific data
+ *
+ * This callback occurs when a job (such as migration) running on the domain
+ * is completed. The params array will contain statistics of the just completed
+ * job as virDomainGetJobStats would return. The callback must not free @params
+ * (the array will be freed once the callback finishes).
+ *
+ * The callback signature to use when registering for an event of type
+ * VIR_DOMAIN_EVENT_ID_JOB_COMPLETED with
+ * virConnectDomainEventRegisterAny().
+ */
+typedef void (*virConnectDomainEventJobCompletedCallback)(virConnectPtr conn,
+ virDomainPtr dom,
+ int type,
+ virTypedParameterPtr params,
+ int nparams,
+ void *opaque);
+
+/**
* VIR_DOMAIN_TUNABLE_CPU_VCPUPIN:
*
* Macro represents formatted pinning for one vcpu specified by id which is
@@ -3588,6 +3613,7 @@ typedef enum {
VIR_DOMAIN_EVENT_ID_AGENT_LIFECYCLE = 18,/*
virConnectDomainEventAgentLifecycleCallback */
VIR_DOMAIN_EVENT_ID_DEVICE_ADDED = 19, /* virConnectDomainEventDeviceAddedCallback
*/
VIR_DOMAIN_EVENT_ID_MIGRATION_ITERATION = 20, /*
virConnectDomainEventMigrationIterationCallback */
+ VIR_DOMAIN_EVENT_ID_JOB_COMPLETED = 21, /* virConnectDomainEventJobCompletedCallback
*/
# ifdef VIR_ENUM_SENTINELS
VIR_DOMAIN_EVENT_ID_LAST
diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c
index 5cb3ccd..fea058c 100644
--- a/src/conf/domain_event.c
+++ b/src/conf/domain_event.c
@@ -57,7 +57,7 @@ static virClassPtr virDomainEventTunableClass;
static virClassPtr virDomainEventAgentLifecycleClass;
static virClassPtr virDomainEventDeviceAddedClass;
static virClassPtr virDomainEventMigrationIterationClass;
-
+static virClassPtr virDomainEventJobCompletedClass;
static void virDomainEventDispose(void *obj);
static void virDomainEventLifecycleDispose(void *obj);
@@ -76,6 +76,7 @@ static void virDomainEventTunableDispose(void *obj);
static void virDomainEventAgentLifecycleDispose(void *obj);
static void virDomainEventDeviceAddedDispose(void *obj);
static void virDomainEventMigrationIterationDispose(void *obj);
+static void virDomainEventJobCompletedDispose(void *obj);
static void
virDomainEventDispatchDefaultFunc(virConnectPtr conn,
@@ -246,6 +247,15 @@ struct _virDomainEventMigrationIteration {
typedef struct _virDomainEventMigrationIteration virDomainEventMigrationIteration;
typedef virDomainEventMigrationIteration *virDomainEventMigrationIterationPtr;
+struct _virDomainEventJobCompleted {
+ virDomainEvent parent;
+
+ int type;
+ virTypedParameterPtr params;
+ int nparams;
+};
+typedef struct _virDomainEventJobCompleted virDomainEventJobCompleted;
+typedef virDomainEventJobCompleted *virDomainEventJobCompletedPtr;
static int
virDomainEventsOnceInit(void)
@@ -352,6 +362,12 @@ virDomainEventsOnceInit(void)
sizeof(virDomainEventMigrationIteration),
virDomainEventMigrationIterationDispose)))
return -1;
+ if (!(virDomainEventJobCompletedClass =
+ virClassNew(virDomainEventClass,
+ "virDomainEventJobCompleted",
+ sizeof(virDomainEventJobCompleted),
+ virDomainEventJobCompletedDispose)))
+ return -1;
return 0;
}
@@ -519,6 +535,15 @@ virDomainEventMigrationIterationDispose(void *obj)
VIR_DEBUG("obj=%p", event);
};
+static void
+virDomainEventJobCompletedDispose(void *obj)
+{
+ virDomainEventJobCompletedPtr event = obj;
+ VIR_DEBUG("obj=%p", event);
+
+ virTypedParamsFree(event->params, event->nparams);
+}
+
static void *
virDomainEventNew(virClassPtr klass,
@@ -1394,6 +1419,57 @@ virDomainEventMigrationIterationNewFromDom(virDomainPtr dom,
iteration);
}
+/* This function consumes @params, the caller must not free it.
+ */
+static virObjectEventPtr
+virDomainEventJobCompletedNew(int id,
+ const char *name,
+ const unsigned char *uuid,
+ int type,
+ virTypedParameterPtr params,
+ int nparams)
+{
+ virDomainEventJobCompletedPtr ev;
+
+ if (virDomainEventsInitialize() < 0)
+ goto error;
+
+ if (!(ev = virDomainEventNew(virDomainEventJobCompletedClass,
+ VIR_DOMAIN_EVENT_ID_JOB_COMPLETED,
+ id, name, uuid)))
+ goto error;
+
+ ev->type = type;
+ ev->params = params;
+ ev->nparams = nparams;
+
+ return (virObjectEventPtr) ev;
+
+ error:
+ virTypedParamsFree(params, nparams);
+ return NULL;
+}
+
+virObjectEventPtr
+virDomainEventJobCompletedNewFromObj(virDomainObjPtr obj,
+ int type,
+ virTypedParameterPtr params,
+ int nparams)
+{
+ return virDomainEventJobCompletedNew(obj->def->id, obj->def->name,
+ obj->def->uuid, type, params, nparams);
+}
+
+virObjectEventPtr
+virDomainEventJobCompletedNewFromDom(virDomainPtr dom,
+ int type,
+ virTypedParameterPtr params,
+ int nparams)
+{
+ return virDomainEventJobCompletedNew(dom->id, dom->name, dom->uuid,
+ type, params, nparams);
+}
+
/* This function consumes the params so caller don't have to care about
* freeing it even if error occurs. The reason is to not have to do deep
@@ -1685,6 +1761,19 @@ virDomainEventDispatchDefaultFunc(virConnectPtr conn,
goto cleanup;
}
+ case VIR_DOMAIN_EVENT_ID_JOB_COMPLETED:
+ {
+ virDomainEventJobCompletedPtr ev;
+
+ ev = (virDomainEventJobCompletedPtr) event;
+ ((virConnectDomainEventJobCompletedCallback) cb)(conn, dom,
+ ev->type,
+ ev->params,
+ ev->nparams,
+ cbopaque);
+ goto cleanup;
+ }
+
case VIR_DOMAIN_EVENT_ID_LAST:
break;
}
diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h
index b7cddb5..3bdd49a 100644
--- a/src/conf/domain_event.h
+++ b/src/conf/domain_event.h
@@ -217,6 +217,18 @@ virObjectEventPtr
virDomainEventMigrationIterationNewFromDom(virDomainPtr dom,
int iteration);
+virObjectEventPtr
+virDomainEventJobCompletedNewFromObj(virDomainObjPtr obj,
+ int type,
+ virTypedParameterPtr params,
+ int nparams);
+
+virObjectEventPtr
+virDomainEventJobCompletedNewFromDom(virDomainPtr dom,
+ int type,
+ virTypedParameterPtr params,
+ int nparams);
+
int
virDomainEventStateRegister(virConnectPtr conn,
virObjectEventStatePtr state,
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 9491845..ca32dc1 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -8873,7 +8873,9 @@ virDomainGetJobInfo(virDomainPtr domain, virDomainJobInfoPtr info)
* when libvirtd is restarted. Note that time information returned for
* completed migrations may be completely irrelevant unless both source and
* destination hosts have synchronized time (i.e., NTP daemon is running on
- * both of them).
+ * both of them). The statistics of a completed job can also be obtained by
+ * listening to a VIR_DOMAIN_EVENT_ID_JOB_COMPLETED event (on the source host
+ * in case of a migration job).
*
* Returns 0 in case of success and -1 in case of failure.
*/
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4b40612..858ec06 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -508,6 +508,8 @@ virDomainEventIOErrorNewFromDom;
virDomainEventIOErrorNewFromObj;
virDomainEventIOErrorReasonNewFromDom;
virDomainEventIOErrorReasonNewFromObj;
+virDomainEventJobCompletedNewFromDom;
+virDomainEventJobCompletedNewFromObj;
virDomainEventLifecycleNew;
virDomainEventLifecycleNewFromDef;
virDomainEventLifecycleNewFromDom;
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 7cf61cf..17ffb20 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -342,6 +342,11 @@
remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog,
void *evdata, void *opaque);
static void
+remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog,
+ virNetClientPtr client,
+ void *evdata, void *opaque);
+
+static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
virNetClientPtr client ATTRIBUTE_UNUSED,
void *evdata, void *opaque);
@@ -508,6 +513,10 @@ static virNetClientProgramEvent remoteEvents[] = {
remoteDomainBuildEventCallbackMigrationIteration,
sizeof(remote_domain_event_callback_migration_iteration_msg),
(xdrproc_t)xdr_remote_domain_event_callback_migration_iteration_msg },
+ { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED,
+ remoteDomainBuildEventCallbackJobCompleted,
+ sizeof(remote_domain_event_callback_job_completed_msg),
+ (xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg },
};
@@ -5365,6 +5374,40 @@
remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog ATT
static void
+remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
+ virNetClientPtr client ATTRIBUTE_UNUSED,
+ void *evdata,
+ void *opaque)
+{
+ virConnectPtr conn = opaque;
+ remote_domain_event_callback_job_completed_msg *msg = evdata;
+ struct private_data *priv = conn->privateData;
+ virDomainPtr dom;
+ virObjectEventPtr event = NULL;
+ virTypedParameterPtr params = NULL;
+ int nparams = 0;
+
+ if (virTypedParamsDeserialize((virTypedParameterRemotePtr)
msg->params.params_val,
+ msg->params.params_len,
+ REMOTE_DOMAIN_JOB_STATS_MAX,
+ ¶ms, &nparams) < 0)
+ return;
+
+ if (!(dom = get_nonnull_domain(conn, msg->dom))) {
+ virTypedParamsFree(params, nparams);
+ return;
+ }
+
+ event = virDomainEventJobCompletedNewFromDom(dom, msg->type,
+ params, nparams);
+
+ virObjectUnref(dom);
+
+ remoteEventQueue(priv, event, msg->callbackID);
+}
+
+
+static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
virNetClientPtr client ATTRIBUTE_UNUSED,
void *evdata, void *opaque)
diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
index bfdbce7..2317ea8 100644
--- a/src/remote/remote_protocol.x
+++ b/src/remote/remote_protocol.x
@@ -3228,6 +3228,13 @@ struct remote_domain_event_callback_migration_iteration_msg {
int iteration;
};
+struct remote_domain_event_callback_job_completed_msg {
+ int callbackID;
+ remote_nonnull_domain dom;
+ int type;
+ remote_typed_param params<REMOTE_DOMAIN_JOB_STATS_MAX>;
+};
+
/*----- Protocol. -----*/
/* Define the program number, protocol version and procedure numbers here. */
@@ -5706,5 +5713,11 @@ enum remote_procedure {
* @generate: both
* @acl: none
*/
- REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION = 359
+ REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION = 359,
+
+ /**
+ * @generate: both
+ * @acl: none
+ */
+ REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED = 360
};
diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
index dff54e8..efd8bd1 100644
--- a/src/remote_protocol-structs
+++ b/src/remote_protocol-structs
@@ -2697,6 +2697,15 @@ struct remote_domain_event_callback_migration_iteration_msg {
remote_nonnull_domain dom;
int iteration;
};
+struct remote_domain_event_callback_job_completed_msg {
+ int callbackID;
+ remote_nonnull_domain dom;
+ int type;
+ struct {
+ u_int params_len;
+ remote_typed_param * params_val;
+ } params;
+};
enum remote_procedure {
REMOTE_PROC_CONNECT_OPEN = 1,
REMOTE_PROC_CONNECT_CLOSE = 2,
@@ -3057,4 +3066,5 @@ enum remote_procedure {
REMOTE_PROC_DOMAIN_SET_USER_PASSWORD = 357,
REMOTE_PROC_DOMAIN_RENAME = 358,
REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION = 359,
+ REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED = 360,
};
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index bfcc0b3..b7c59a8 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -11973,6 +11973,32 @@ virshEventMigrationIterationPrint(virConnectPtr conn
ATTRIBUTE_UNUSED,
virshEventPrint(opaque, &buf);
}
+static void
+virshEventJobCompletedPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
+ virDomainPtr dom,
+ int type,
+ virTypedParameterPtr params,
+ int nparams,
+ void *opaque)
+{
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+ size_t i;
+ char *value;
+
+ virBufferAsprintf(&buf, _("event 'job-completed' for domain %s:
"
+ "job type %s:\n"),
+ virDomainGetName(dom),
+ virshDomainJobToString(type));
+ for (i = 0; i < nparams; i++) {
+ value = virTypedParameterToString(¶ms[i]);
+ if (value) {
+ virBufferAsprintf(&buf, "\t%s: %s\n", params[i].field, value);
+ VIR_FREE(value);
+ }
+ }
+ virshEventPrint(opaque, &buf);
+}
+
static vshEventCallback vshEventCallbacks[] = {
{ "lifecycle",
VIR_DOMAIN_EVENT_CALLBACK(virshEventLifecyclePrint), },
@@ -12014,6 +12040,8 @@ static vshEventCallback vshEventCallbacks[] = {
VIR_DOMAIN_EVENT_CALLBACK(virshEventDeviceAddedPrint), },
{ "migration-iteration",
VIR_DOMAIN_EVENT_CALLBACK(virshEventMigrationIterationPrint), },
+ { "job-completed",
+ VIR_DOMAIN_EVENT_CALLBACK(virshEventJobCompletedPrint), },
};
verify(VIR_DOMAIN_EVENT_ID_LAST == ARRAY_CARDINALITY(vshEventCallbacks));
--
2.7.2