* [PATCH v2 2/3] tests/qtest/libqtest: add qtest_has_cpu() api
@ 2024-06-10 13:21 Ani Sinha
2024-06-10 14:38 ` Philippe Mathieu-Daudé
0 siblings, 1 reply; 2+ messages in thread
From: Ani Sinha @ 2024-06-10 13:21 UTC (permalink / raw)
To: Thomas Huth, Laurent Vivier, Paolo Bonzini
Cc: Ani Sinha, Reviewed-by : Daniel P . Berrangé, qemu-devel
Added a new test api qtest_has_cpu() in order to check availability of some
cpu models in the current QEMU binary. The specific architecture of the QEMU
binary is selected using the QTEST_QEMU_BINARY environment variable. This api
would be useful to run tests against some older cpu models after checking if
QEMU actually supported these models.
CC: thuth@redhat.com
Signed-off-by: Ani Sinha <anisinha@redhat.com>
Reviewed-by: Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
---
tests/qtest/libqtest.c | 83 ++++++++++++++++++++++++++++++++++++++++++
tests/qtest/libqtest.h | 8 ++++
2 files changed, 91 insertions(+)
changelist:
v2: changes related to suggestions made by danpb. added tags.
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index d8f80d335e..7647818c66 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -37,6 +37,7 @@
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qstring.h"
+#include "qapi/qmp/qbool.h"
#define MAX_IRQ 256
@@ -1471,6 +1472,12 @@ struct MachInfo {
char *alias;
};
+struct CpuInfo {
+ char *name;
+ char *alias_of;
+ bool deprecated;
+};
+
static void qtest_free_machine_list(struct MachInfo *machines)
{
if (machines) {
@@ -1550,6 +1557,82 @@ static struct MachInfo *qtest_get_machines(const char *var)
return machines;
}
+static struct CpuInfo *qtest_get_cpus(void)
+{
+ static struct CpuInfo *cpus;
+ QDict *response, *minfo;
+ QList *list;
+ const QListEntry *p;
+ QObject *qobj;
+ QString *qstr;
+ QBool *qbool;
+ QTestState *qts;
+ int idx;
+
+ if (cpus) {
+ return cpus;
+ }
+
+ silence_spawn_log = !g_test_verbose();
+
+ qts = qtest_init_with_env(NULL, "-machine none");
+ response = qtest_qmp(qts, "{ 'execute': 'query-cpu-definitions' }");
+ g_assert(response);
+ list = qdict_get_qlist(response, "return");
+ g_assert(list);
+
+ cpus = g_new0(struct CpuInfo, qlist_size(list) + 1);
+
+ for (p = qlist_first(list), idx = 0; p; p = qlist_next(p), idx++) {
+ minfo = qobject_to(QDict, qlist_entry_obj(p));
+ g_assert(minfo);
+
+ qobj = qdict_get(minfo, "name");
+ g_assert(qobj);
+ qstr = qobject_to(QString, qobj);
+ g_assert(qstr);
+ cpus[idx].name = g_strdup(qstring_get_str(qstr));
+
+ qobj = qdict_get(minfo, "alias_of");
+ if (qobj) { /* old machines do not report aliases */
+ qstr = qobject_to(QString, qobj);
+ g_assert(qstr);
+ cpus[idx].alias_of = g_strdup(qstring_get_str(qstr));
+ } else {
+ cpus[idx].alias_of = NULL;
+ }
+
+ qobj = qdict_get(minfo, "deprecated");
+ qbool = qobject_to(QBool, qobj);
+ g_assert(qbool);
+ cpus[idx].deprecated = qbool_get_bool(qbool);
+ }
+
+ qtest_quit(qts);
+ qobject_unref(response);
+
+ silence_spawn_log = false;
+
+ return cpus;
+}
+
+bool qtest_has_cpu(const char *cpu)
+{
+ struct CpuInfo *cpus;
+ int i;
+
+ cpus = qtest_get_cpus();
+
+ for (i = 0; cpus[i].name != NULL; i++) {
+ if (g_str_equal(cpu, cpus[i].name) ||
+ (cpus[i].alias_of && g_str_equal(cpu, cpus[i].alias_of))) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
void qtest_cb_for_every_machine(void (*cb)(const char *machine),
bool skip_old_versioned)
{
diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h
index 6e3d3525bf..c94b64f960 100644
--- a/tests/qtest/libqtest.h
+++ b/tests/qtest/libqtest.h
@@ -949,6 +949,14 @@ bool qtest_has_machine(const char *machine);
*/
bool qtest_has_machine_with_env(const char *var, const char *machine);
+/**
+ * qtest_has_cpu:
+ * @cpu: The cpu to look for
+ *
+ * Returns: true if the cpu is available in the target binary.
+ */
+bool qtest_has_cpu(const char *cpu);
+
/**
* qtest_has_device:
* @device: The device to look for
--
2.42.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2 2/3] tests/qtest/libqtest: add qtest_has_cpu() api
2024-06-10 13:21 [PATCH v2 2/3] tests/qtest/libqtest: add qtest_has_cpu() api Ani Sinha
@ 2024-06-10 14:38 ` Philippe Mathieu-Daudé
0 siblings, 0 replies; 2+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-10 14:38 UTC (permalink / raw)
To: Ani Sinha, Thomas Huth, Laurent Vivier, Paolo Bonzini
Cc: Reviewed-by : Daniel P . Berrangé, qemu-devel
Hi Ani,
On 10/6/24 15:21, Ani Sinha wrote:
> Added a new test api qtest_has_cpu() in order to check availability of some
> cpu models in the current QEMU binary. The specific architecture of the QEMU
> binary is selected using the QTEST_QEMU_BINARY environment variable. This api
> would be useful to run tests against some older cpu models after checking if
> QEMU actually supported these models.
>
> CC: thuth@redhat.com
> Signed-off-by: Ani Sinha <anisinha@redhat.com>
> Reviewed-by: Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
> tests/qtest/libqtest.c | 83 ++++++++++++++++++++++++++++++++++++++++++
> tests/qtest/libqtest.h | 8 ++++
> 2 files changed, 91 insertions(+)
Since "CPU" commonly refers to a CPU state, I suggest renaming as:
> +struct CpuInfo {
CpuModel
> +static struct CpuInfo *qtest_get_cpus(void)
qtest_get_cpu_models()
> +bool qtest_has_cpu(const char *cpu)
qtest_has_cpu_model()
Regards,
Phil.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-06-10 14:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-10 13:21 [PATCH v2 2/3] tests/qtest/libqtest: add qtest_has_cpu() api Ani Sinha
2024-06-10 14:38 ` Philippe Mathieu-Daudé
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).