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