<div dir="ltr">Hi Daniel,<div><br></div><div>Thanks for thre review and reply, my first implementation was going to gather data from /proc/cpuinfo, but unlike X86, we can only get this kind of info:</div><div><br></div><div> processor : 0<br> BogoMIPS : 200.00<br> Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid<br> CPU implementer : 0x43<br> CPU architecture: 8<br> CPU variant : 0x1<br> CPU part : 0x0a1<br> CPU revision : 1<br></div><div><br></div><div>so we have to perform some translation to perform human readable information, and I mentioned that 'lscpu' has done that too. So Andrea Bolognani</div><div>suggested that maybe we can use it directly, to avoid re-implement the translation. Here is the discussion: <a href="https://www.redhat.com/archives/libvir-list/2020-March/msg00812.html">https://www.redhat.com/archives/libvir-list/2020-March/msg00812.html</a></div><div><br></div><div>BR,</div><div><br></div><div>Zhenyu</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 30, 2020 at 7:27 PM Daniel P. Berrangé <<a href="mailto:berrange@redhat.com">berrange@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Fri, Mar 27, 2020 at 04:52:25PM +0800, Zhenyu Zheng wrote:<br>
> Introduce getHost support for ARM, use data from 'lscpu' cmd<br>
> result. 'util-linux/lscpu' provided a very good translation of<br>
> ARM cpu data start from release v2.32, use it directly to avoid<br>
> re-implement the translation.<br>
<br>
I'm a bit wary of this approach, because parsing the output of<br>
command line tools is generally fragile / liable to break in<br>
future releases, since few tools guarantee that their output<br>
format is stable for machine parsing.<br>
<br>
How hard would it be to probe this directly in libvirt, as we<br>
do on x86 arch.<br>
<br>
> <br>
> Signed-of-by: Zhenyu Zheng <<a href="mailto:zhengzhenyulixi@gmail.com" target="_blank">zhengzhenyulixi@gmail.com</a>><br>
> ---<br>
> src/cpu/cpu_arm.c | 198 +++++++++++++++++++++++++++++++++++++++++++++-<br>
> 1 file changed, 197 insertions(+), 1 deletion(-)<br>
> <br>
> diff --git a/src/cpu/cpu_arm.c b/src/cpu/cpu_arm.c<br>
> index 969025b5cf..30d9c0ae2e 100644<br>
> --- a/src/cpu/cpu_arm.c<br>
> +++ b/src/cpu/cpu_arm.c<br>
> @@ -22,8 +22,11 @@<br>
> #include <config.h><br>
> <br>
> #include "viralloc.h"<br>
> +#include "virlog.h"<br>
> #include "cpu.h"<br>
> #include "cpu_map.h"<br>
> +#include "vircommand.h"<br>
> +#include "virfile.h"<br>
> #include "virstring.h"<br>
> #include "virxml.h"<br>
> #include "cpu_map.h"<br>
> @@ -31,6 +34,14 @@<br>
> <br>
> #define VIR_FROM_THIS VIR_FROM_CPU<br>
> <br>
> +VIR_LOG_INIT("cpu.cpu_arm");<br>
> +<br>
> +static const char *lsCpuPath = "/usr/bin/lscpu";<br>
> +<br>
> +#define LSCPU lsCpuPath<br>
> +#define MAX_LSCPU_SIZE = (1024*1024) /* 1MB limit for lscpu output */<br>
> +<br>
> +<br>
> static const virArch archs[] = {<br>
> VIR_ARCH_ARMV6L,<br>
> VIR_ARCH_ARMV7B,<br>
> @@ -464,13 +475,198 @@ virCPUarmValidateFeatures(virCPUDefPtr cpu)<br>
> return 0;<br>
> }<br>
> <br>
> +static int<br>
> +armCpuDataFromLsCpu(virCPUarmData *data)<br>
> +{<br>
> + int ret = -1;<br>
> + char *outbuf = NULL;<br>
> + char *eol = NULL;<br>
> + const char *cur;<br>
> + virCommandPtr cmd = NULL;<br>
> + g_autofree char *lscpu = NULL;<br>
> +<br>
> + if (!data)<br>
> + return ret;<br>
> +<br>
> + lscpu = virFindFileInPath("lscpu");<br>
> + cmd = virCommandNew(lscpu);<br>
> + virCommandSetOutputBuffer(cmd, &outbuf);<br>
> +<br>
> + if (virCommandRun(cmd, NULL) < 0) <br>
> + goto cleanup;<br>
> +<br>
> + if ((cur = strstr(outbuf, "Vendor ID")) == NULL) {<br>
> + virReportError(VIR_ERR_INTERNAL_ERROR,<br>
> + _("there is no \"Vendor ID\" info in %s command result"), LSCPU);<br>
> + goto cleanup;<br>
> + }<br>
> + cur = strchr(cur, ':') + 1;<br>
> + eol = strchr(cur, '\n');<br>
> + virSkipSpaces(&cur);<br>
> + if (!eol)<br>
> + goto cleanup;<br>
> +<br>
> + data->vendor_id = g_strndup(cur, eol - cur);<br>
> +<br>
> + if ((cur = strstr(outbuf, "Model name")) == NULL) {<br>
> + virReportError(VIR_ERR_INTERNAL_ERROR,<br>
> + _("there is no \"Model name\" info in %s command result"), LSCPU);<br>
> + goto cleanup;<br>
> + }<br>
> + cur = strchr(cur, ':') + 1;<br>
> + eol = strchr(cur, '\n');<br>
> + virSkipSpaces(&cur);<br>
> + if (!eol)<br>
> + goto cleanup;<br>
> +<br>
> + data->model_name = g_strndup(cur, eol - cur);<br>
> +<br>
> + if ((cur = strstr(outbuf, "Flags")) == NULL) {<br>
> + virReportError(VIR_ERR_INTERNAL_ERROR,<br>
> + _("there is no \"Flags\" info in %s command result"), LSCPU);<br>
> + goto cleanup;<br>
> + }<br>
> + cur = strchr(cur, ':') + 1;<br>
> + eol = strchr(cur, '\n');<br>
> + virSkipSpaces(&cur);<br>
> + if (!eol)<br>
> + goto cleanup;<br>
> +<br>
> + data->features = g_strndup(cur, eol - cur);<br>
> +<br>
> + ret = 0;<br>
> +<br>
> + cleanup:<br>
> + virCommandFree(cmd);<br>
> + VIR_FREE(outbuf);<br>
> + return ret;<br>
> +}<br>
> +<br>
> +static int<br>
> +armCpuDataParseFeatures(virCPUDefPtr cpu,<br>
> + const virCPUarmData *cpuData)<br>
> +{<br>
> + int ret = -1;<br>
> + size_t i;<br>
> + char **features;<br>
> +<br>
> + if (!cpu || !cpuData)<br>
> + return ret;<br>
> +<br>
> + if (!(features = virStringSplitCount(cpuData->features, " ",<br>
> + 0, &cpu->nfeatures)))<br>
> + return ret;<br>
> + if (cpu->nfeatures) {<br>
> + if (VIR_ALLOC_N(cpu->features, cpu->nfeatures) < 0)<br>
> + goto error;<br>
> +<br>
> + for (i = 0; i < cpu->nfeatures; i++) {<br>
> + cpu->features[i].policy = VIR_CPU_FEATURE_REQUIRE;<br>
> + cpu->features[i].name = g_strdup(features[i]);<br>
> + }<br>
> + }<br>
> +<br>
> + ret = 0;<br>
> +<br>
> + cleanup:<br>
> + virStringListFree(features);<br>
> + return ret;<br>
> +<br>
> + error:<br>
> + for (i = 0; i < cpu->nfeatures; i++)<br>
> + VIR_FREE(cpu->features[i].name);<br>
> + VIR_FREE(cpu->features);<br>
> + cpu->nfeatures = 0;<br>
> + goto cleanup;<br>
> +}<br>
> +<br>
> +static int<br>
> +armDecode(virCPUDefPtr cpu,<br>
> + const virCPUarmData *cpuData,<br>
> + virDomainCapsCPUModelsPtr models)<br>
> +{<br>
> + virCPUarmMapPtr map;<br>
> + virCPUarmModelPtr model;<br>
> + virCPUarmVendorPtr vendor = NULL;<br>
> +<br>
> + if (!cpuData || !(map = virCPUarmGetMap()))<br>
> + return -1;<br>
> +<br>
> + if (!(model = armModelFind(map, cpuData->model_name))) {<br>
> + virReportError(VIR_ERR_OPERATION_FAILED,<br>
> + _("Cannot find CPU model with name %s"),<br>
> + cpuData->model_name);<br>
> + return -1;<br>
> + }<br>
> +<br>
> + if (!virCPUModelIsAllowed(model->name, models)) {<br>
> + virReportError(VIR_ERR_CONFIG_UNSUPPORTED,<br>
> + _("CPU model %s is not supported by hypervisor"),<br>
> + model->name);<br>
> + return -1;<br>
> + }<br>
> +<br>
> + cpu->model = g_strdup(model->name);<br>
> +<br>
> + if (cpuData->vendor_id &&<br>
> + !(vendor = armVendorFindByName(map, cpuData->vendor_id))) {<br>
> + virReportError(VIR_ERR_OPERATION_FAILED,<br>
> + _("Cannot find CPU vendor with vendor id %s"),<br>
> + cpuData->vendor_id);<br>
> + return -1;<br>
> + }<br>
> +<br>
> +<br>
> + if (vendor)<br>
> + cpu->vendor = g_strdup(vendor->name);<br>
> +<br>
> + if (cpuData->features &&<br>
> + armCpuDataParseFeatures(cpu, cpuData) < 0)<br>
> + return -1;<br>
> +<br>
> + return 0;<br>
> +}<br>
> +<br>
> +static int<br>
> +armDecodeCPUData(virCPUDefPtr cpu,<br>
> + const virCPUData *data,<br>
> + virDomainCapsCPUModelsPtr models)<br>
> +{<br>
> + return armDecode(cpu, &data->data.arm, models);<br>
> +}<br>
> +<br>
> +static int<br>
> +virCPUarmGetHost(virCPUDefPtr cpu,<br>
> + virDomainCapsCPUModelsPtr models)<br>
> +{<br>
> + virCPUDataPtr cpuData = NULL;<br>
> + int ret = -1;<br>
> +<br>
> + if (virCPUarmDriverInitialize() < 0)<br>
> + goto cleanup;<br>
> +<br>
> + if (!(cpuData = virCPUDataNew(archs[0])))<br>
> + goto cleanup;<br>
> +<br>
> + if (armCpuDataFromLsCpu(&cpuData->data.arm) < 0)<br>
> + goto cleanup;<br>
> +<br>
> + ret = armDecodeCPUData(cpu, cpuData, models);<br>
> +<br>
> + cleanup:<br>
> + virCPUarmDataFree(cpuData);<br>
> + return ret;<br>
> +}<br>
> +<br>
> struct cpuArchDriver cpuDriverArm = {<br>
> .name = "arm",<br>
> .arch = archs,<br>
> .narch = G_N_ELEMENTS(archs),<br>
> .compare = virCPUarmCompare,<br>
> - .decode = NULL,<br>
> + .decode = armDecodeCPUData,<br>
> .encode = NULL,<br>
> + .dataFree = virCPUarmDataFree,<br>
> + .getHost = virCPUarmGetHost,<br>
> .baseline = virCPUarmBaseline,<br>
> .update = virCPUarmUpdate,<br>
> .validateFeatures = virCPUarmValidateFeatures,<br>
> -- <br>
> 2.26.0.windows.1<br>
> <br>
> <br>
<br>
Regards,<br>
Daniel<br>
-- <br>
|: <a href="https://berrange.com" rel="noreferrer" target="_blank">https://berrange.com</a> -o- <a href="https://www.flickr.com/photos/dberrange" rel="noreferrer" target="_blank">https://www.flickr.com/photos/dberrange</a> :|<br>
|: <a href="https://libvirt.org" rel="noreferrer" target="_blank">https://libvirt.org</a> -o- <a href="https://fstop138.berrange.com" rel="noreferrer" target="_blank">https://fstop138.berrange.com</a> :|<br>
|: <a href="https://entangle-photo.org" rel="noreferrer" target="_blank">https://entangle-photo.org</a> -o- <a href="https://www.instagram.com/dberrange" rel="noreferrer" target="_blank">https://www.instagram.com/dberrange</a> :|<br>
<br>
</blockquote></div>