Blame SOURCES/kvm-job-Add-query-jobs-QMP-command.patch

1bdc94
From d32f4952587625fab92b42687273fbb9f89ae78c Mon Sep 17 00:00:00 2001
1bdc94
From: Kevin Wolf <kwolf@redhat.com>
1bdc94
Date: Tue, 26 Jun 2018 09:48:33 +0200
1bdc94
Subject: [PATCH 64/89] job: Add query-jobs QMP command
1bdc94
1bdc94
RH-Author: Kevin Wolf <kwolf@redhat.com>
1bdc94
Message-id: <20180626094856.6924-51-kwolf@redhat.com>
1bdc94
Patchwork-id: 81095
1bdc94
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 50/73] job: Add query-jobs QMP command
1bdc94
Bugzilla: 1513543
1bdc94
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Fam Zheng <famz@redhat.com>
1bdc94
1bdc94
This adds a minimal query-jobs implementation that shouldn't pose many
1bdc94
design questions. It can later be extended to expose more information,
1bdc94
and especially job-specific information.
1bdc94
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
(cherry picked from commit 456273b02474780537e2bb52a72213f63bb5227a)
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 include/qemu/job.h |  3 +++
1bdc94
 job-qmp.c          | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1bdc94
 job.c              |  2 +-
1bdc94
 qapi/job.json      | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1bdc94
 4 files changed, 104 insertions(+), 1 deletion(-)
1bdc94
1bdc94
diff --git a/include/qemu/job.h b/include/qemu/job.h
1bdc94
index 92d1d24..8c8badf 100644
1bdc94
--- a/include/qemu/job.h
1bdc94
+++ b/include/qemu/job.h
1bdc94
@@ -392,6 +392,9 @@ JobType job_type(const Job *job);
1bdc94
 /** Returns the enum string for the JobType of a given Job. */
1bdc94
 const char *job_type_str(const Job *job);
1bdc94
 
1bdc94
+/** Returns true if the job should not be visible to the management layer. */
1bdc94
+bool job_is_internal(Job *job);
1bdc94
+
1bdc94
 /** Returns whether the job is scheduled for cancellation. */
1bdc94
 bool job_is_cancelled(Job *job);
1bdc94
 
1bdc94
diff --git a/job-qmp.c b/job-qmp.c
1bdc94
index b2e18cf..7f38f63 100644
1bdc94
--- a/job-qmp.c
1bdc94
+++ b/job-qmp.c
1bdc94
@@ -132,3 +132,57 @@ void qmp_job_dismiss(const char *id, Error **errp)
1bdc94
     job_dismiss(&job, errp);
1bdc94
     aio_context_release(aio_context);
1bdc94
 }
1bdc94
+
1bdc94
+static JobInfo *job_query_single(Job *job, Error **errp)
1bdc94
+{
1bdc94
+    JobInfo *info;
1bdc94
+    const char *errmsg = NULL;
1bdc94
+
1bdc94
+    assert(!job_is_internal(job));
1bdc94
+
1bdc94
+    if (job->ret < 0) {
1bdc94
+        errmsg = strerror(-job->ret);
1bdc94
+    }
1bdc94
+
1bdc94
+    info = g_new(JobInfo, 1);
1bdc94
+    *info = (JobInfo) {
1bdc94
+        .id                 = g_strdup(job->id),
1bdc94
+        .type               = job_type(job),
1bdc94
+        .status             = job->status,
1bdc94
+        .current_progress   = job->progress_current,
1bdc94
+        .total_progress     = job->progress_total,
1bdc94
+        .has_error          = !!errmsg,
1bdc94
+        .error              = g_strdup(errmsg),
1bdc94
+    };
1bdc94
+
1bdc94
+    return info;
1bdc94
+}
1bdc94
+
1bdc94
+JobInfoList *qmp_query_jobs(Error **errp)
1bdc94
+{
1bdc94
+    JobInfoList *head = NULL, **p_next = &head;
1bdc94
+    Job *job;
1bdc94
+
1bdc94
+    for (job = job_next(NULL); job; job = job_next(job)) {
1bdc94
+        JobInfoList *elem;
1bdc94
+        AioContext *aio_context;
1bdc94
+
1bdc94
+        if (job_is_internal(job)) {
1bdc94
+            continue;
1bdc94
+        }
1bdc94
+        elem = g_new0(JobInfoList, 1);
1bdc94
+        aio_context = job->aio_context;
1bdc94
+        aio_context_acquire(aio_context);
1bdc94
+        elem->value = job_query_single(job, errp);
1bdc94
+        aio_context_release(aio_context);
1bdc94
+        if (!elem->value) {
1bdc94
+            g_free(elem);
1bdc94
+            qapi_free_JobInfoList(head);
1bdc94
+            return NULL;
1bdc94
+        }
1bdc94
+        *p_next = elem;
1bdc94
+        p_next = &elem->next;
1bdc94
+    }
1bdc94
+
1bdc94
+    return head;
1bdc94
+}
1bdc94
diff --git a/job.c b/job.c
1bdc94
index 599a104..f026661 100644
1bdc94
--- a/job.c
1bdc94
+++ b/job.c
1bdc94
@@ -158,7 +158,7 @@ static int job_txn_apply(JobTxn *txn, int fn(Job *), bool lock)
1bdc94
     return rc;
1bdc94
 }
1bdc94
 
1bdc94
-static bool job_is_internal(Job *job)
1bdc94
+bool job_is_internal(Job *job)
1bdc94
 {
1bdc94
     return (job->id == NULL);
1bdc94
 }
1bdc94
diff --git a/qapi/job.json b/qapi/job.json
1bdc94
index b84dc6c..970124d 100644
1bdc94
--- a/qapi/job.json
1bdc94
+++ b/qapi/job.json
1bdc94
@@ -205,3 +205,49 @@
1bdc94
 # Since: 2.13
1bdc94
 ##
1bdc94
 { 'command': 'job-finalize', 'data': { 'id': 'str' } }
1bdc94
+
1bdc94
+##
1bdc94
+# @JobInfo:
1bdc94
+#
1bdc94
+# Information about a job.
1bdc94
+#
1bdc94
+# @id:                  The job identifier
1bdc94
+#
1bdc94
+# @type:                The kind of job that is being performed
1bdc94
+#
1bdc94
+# @status:              Current job state/status
1bdc94
+#
1bdc94
+# @current-progress:    Progress made until now. The unit is arbitrary and the
1bdc94
+#                       value can only meaningfully be used for the ratio of
1bdc94
+#                       @current-progress to @total-progress. The value is
1bdc94
+#                       monotonically increasing.
1bdc94
+#
1bdc94
+# @total-progress:      Estimated @current-progress value at the completion of
1bdc94
+#                       the job. This value can arbitrarily change while the
1bdc94
+#                       job is running, in both directions.
1bdc94
+#
1bdc94
+# @error:               If this field is present, the job failed; if it is
1bdc94
+#                       still missing in the CONCLUDED state, this indicates
1bdc94
+#                       successful completion.
1bdc94
+#
1bdc94
+#                       The value is a human-readable error message to describe
1bdc94
+#                       the reason for the job failure. It should not be parsed
1bdc94
+#                       by applications.
1bdc94
+#
1bdc94
+# Since: 2.13
1bdc94
+##
1bdc94
+{ 'struct': 'JobInfo',
1bdc94
+  'data': { 'id': 'str', 'type': 'JobType', 'status': 'JobStatus',
1bdc94
+            'current-progress': 'int', 'total-progress': 'int',
1bdc94
+            '*error': 'str' } }
1bdc94
+
1bdc94
+##
1bdc94
+# @query-jobs:
1bdc94
+#
1bdc94
+# Return information about jobs.
1bdc94
+#
1bdc94
+# Returns: a list with a @JobInfo for each active job
1bdc94
+#
1bdc94
+# Since: 2.13
1bdc94
+##
1bdc94
+{ 'command': 'query-jobs', 'returns': ['JobInfo'] }
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94