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