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

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