Re: [PATCH for-6.2 v2 11/11] tests/unit: Add a unit test for smp parsing

2021-07-22 Thread wangyanan (Y)

On 2021/7/22 21:12, Andrew Jones wrote:

On Thu, Jul 22, 2021 at 02:15:18PM +0800, wangyanan (Y) wrote:

On 2021/7/20 2:57, Andrew Jones wrote:

On Mon, Jul 19, 2021 at 11:20:43AM +0800, Yanan Wang wrote:

Add a QEMU unit test for the parsing of given SMP configuration.
Since all the parsing logic is in generic function smp_parse(),
this test passes diffenent SMP configurations to the function
and compare the parsing result with what is expected.

In the test, all possible collections of the topology parameters
and the corressponding expected results are listed, including the
valid and invalid ones.

The preference of sockets over cores and the preference of cores
over sockets, and the support of multi-dies are also considered.

Signed-off-by: Yanan Wang 
---
   MAINTAINERS |1 +
   tests/unit/meson.build  |1 +
   tests/unit/test-smp-parse.c | 1117 +++
   3 files changed, 1119 insertions(+)
   create mode 100644 tests/unit/test-smp-parse.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 70633e3bf4..160dba2e57 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1636,6 +1636,7 @@ F: include/hw/boards.h
   F: include/hw/core/cpu.h
   F: include/hw/cpu/cluster.h
   F: include/sysemu/numa.h
+F: tests/unit/test-smp-parse.c
   T: git https://gitlab.com/ehabkost/qemu.git machine-next
   Xtensa Machines
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 3e0504dd21..694a924627 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -44,6 +44,7 @@ tests = {
 'test-uuid': [],
 'ptimer-test': ['ptimer-test-stubs.c', meson.source_root() / 
'hw/core/ptimer.c'],
 'test-qapi-util': [],
+  'test-smp-parse': [qom, meson.source_root() / 'hw/core/machine-smp.c'],
   }
   if have_system or have_tools
diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c
new file mode 100644
index 00..bc1d324c3d
--- /dev/null
+++ b/tests/unit/test-smp-parse.c
@@ -0,0 +1,1117 @@
+/*
+ * SMP parsing unit-tests
+ *
+ * Copyright (C) 2021, Huawei, Inc.
+ *
+ * Authors:
+ *  Yanan Wang 
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qom/object.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+
+#include "hw/boards.h"
+
+#define T true
+#define F false
+
+/**
+ * SMPTestData:
+ * @config - the given SMP configuration for parsing
+ * @should_be_valid - whether the given configuration is supposed to be valid
+ * @expect - the CPU topology info expected to be parsed out
+ */
+typedef struct SMPTestData {
+SMPConfiguration config;
+bool should_be_valid;

Long way to say 'valid'.

Indeed..., "valid" should be enough.

+CpuTopology expect;
+} SMPTestData;
+
+/* the specific machine type info for this test */
+static const TypeInfo smp_machine_info = {
+.name = TYPE_MACHINE,
+.parent = TYPE_OBJECT,
+.class_size = sizeof(MachineClass),
+.instance_size = sizeof(MachineState),
+};
+
+/*
+ * prefer sockets over cores over threads before 6.2.
+ * all possible SMP configurations and the corressponding expected outputs

corresponding (please run spell check on your commit messages)


Ok, I missed the check.

+ * are listed for testing, including the valid and invalid ones.
+ */
+static struct SMPTestData prefer_sockets[] = {
+{
+/* config: no smp configuration provided
+ * expect: cpus=1,sockets=1,dies=1,cores=1,threads=1,maxcpus=1 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, F, 0 },

SMPConfiguration and CpuTopology have named fields so we could drop the
'expect: ...' comment line and instead do

   {
/* no configuration provided */
.config = { .has_cpus = F, .has_maxcpus = F, ... },
.valid = T,
.expect = { .sockets = 1, .cores = 1, ... },
   }, {
...
   }

which may be easier to maintain. OTOH, the concise form this approach has
is also nice.

I tried the structure initialization with explicit name fields in it like
above,
actually we are supposed to do in this way so that we don't have to worry
about the order change of the structure members.

But this would break the 80-char line limit or introduce more lines for

I wouldn't worry about 80-char (or even 90, which is when checkpatch
switches from a warning to an error) for something like this, but that's
just my opinion. You'd have to get agreement from whomever would decide /
not decide to merge this.


Ok, I think I will try the format with explicit name fields.

Thanks,
Yanan



for each SMP configuration. If this is not a real problem, I also prefer
above format.

I don't think you should need the casts in the assignments
though.

Yes, the casts may be unnecessary, will remove them.

+.should_be_valid = true,
+.expect = (CpuTopology) { 1, 1, 1, 1, 1, 1 },
+}, {
+/* config: -smp 8
+ * expect: cpus=8,sockets=8,dies=1,cores=1

Re: [PATCH for-6.2 v2 11/11] tests/unit: Add a unit test for smp parsing

2021-07-22 Thread Andrew Jones
On Thu, Jul 22, 2021 at 02:15:18PM +0800, wangyanan (Y) wrote:
> On 2021/7/20 2:57, Andrew Jones wrote:
> > On Mon, Jul 19, 2021 at 11:20:43AM +0800, Yanan Wang wrote:
> > > Add a QEMU unit test for the parsing of given SMP configuration.
> > > Since all the parsing logic is in generic function smp_parse(),
> > > this test passes diffenent SMP configurations to the function
> > > and compare the parsing result with what is expected.
> > > 
> > > In the test, all possible collections of the topology parameters
> > > and the corressponding expected results are listed, including the
> > > valid and invalid ones.
> > > 
> > > The preference of sockets over cores and the preference of cores
> > > over sockets, and the support of multi-dies are also considered.
> > > 
> > > Signed-off-by: Yanan Wang 
> > > ---
> > >   MAINTAINERS |1 +
> > >   tests/unit/meson.build  |1 +
> > >   tests/unit/test-smp-parse.c | 1117 +++
> > >   3 files changed, 1119 insertions(+)
> > >   create mode 100644 tests/unit/test-smp-parse.c
> > > 
> > > diff --git a/MAINTAINERS b/MAINTAINERS
> > > index 70633e3bf4..160dba2e57 100644
> > > --- a/MAINTAINERS
> > > +++ b/MAINTAINERS
> > > @@ -1636,6 +1636,7 @@ F: include/hw/boards.h
> > >   F: include/hw/core/cpu.h
> > >   F: include/hw/cpu/cluster.h
> > >   F: include/sysemu/numa.h
> > > +F: tests/unit/test-smp-parse.c
> > >   T: git https://gitlab.com/ehabkost/qemu.git machine-next
> > >   Xtensa Machines
> > > diff --git a/tests/unit/meson.build b/tests/unit/meson.build
> > > index 3e0504dd21..694a924627 100644
> > > --- a/tests/unit/meson.build
> > > +++ b/tests/unit/meson.build
> > > @@ -44,6 +44,7 @@ tests = {
> > > 'test-uuid': [],
> > > 'ptimer-test': ['ptimer-test-stubs.c', meson.source_root() / 
> > > 'hw/core/ptimer.c'],
> > > 'test-qapi-util': [],
> > > +  'test-smp-parse': [qom, meson.source_root() / 'hw/core/machine-smp.c'],
> > >   }
> > >   if have_system or have_tools
> > > diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c
> > > new file mode 100644
> > > index 00..bc1d324c3d
> > > --- /dev/null
> > > +++ b/tests/unit/test-smp-parse.c
> > > @@ -0,0 +1,1117 @@
> > > +/*
> > > + * SMP parsing unit-tests
> > > + *
> > > + * Copyright (C) 2021, Huawei, Inc.
> > > + *
> > > + * Authors:
> > > + *  Yanan Wang 
> > > + *
> > > + * This work is licensed under the terms of the GNU LGPL, version 2.1 or 
> > > later.
> > > + * See the COPYING.LIB file in the top-level directory.
> > > + */
> > > +
> > > +#include "qemu/osdep.h"
> > > +#include "qom/object.h"
> > > +#include "qemu/module.h"
> > > +#include "qapi/error.h"
> > > +
> > > +#include "hw/boards.h"
> > > +
> > > +#define T true
> > > +#define F false
> > > +
> > > +/**
> > > + * SMPTestData:
> > > + * @config - the given SMP configuration for parsing
> > > + * @should_be_valid - whether the given configuration is supposed to be 
> > > valid
> > > + * @expect - the CPU topology info expected to be parsed out
> > > + */
> > > +typedef struct SMPTestData {
> > > +SMPConfiguration config;
> > > +bool should_be_valid;
> > Long way to say 'valid'.
> Indeed..., "valid" should be enough.
> > > +CpuTopology expect;
> > > +} SMPTestData;
> > > +
> > > +/* the specific machine type info for this test */
> > > +static const TypeInfo smp_machine_info = {
> > > +.name = TYPE_MACHINE,
> > > +.parent = TYPE_OBJECT,
> > > +.class_size = sizeof(MachineClass),
> > > +.instance_size = sizeof(MachineState),
> > > +};
> > > +
> > > +/*
> > > + * prefer sockets over cores over threads before 6.2.
> > > + * all possible SMP configurations and the corressponding expected 
> > > outputs
> > corresponding (please run spell check on your commit messages)
> > 
> Ok, I missed the check.
> > > + * are listed for testing, including the valid and invalid ones.
> > > + */
> > > +static struct SMPTestData prefer_sockets[] = {
> > > +{
> > > +/* config: no smp configuration provided
> > > + * expect: cpus=1,sockets=1,dies=1,cores=1,threads=1,maxcpus=1 */
> > > +.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, F, 
> > > 0 },
> > SMPConfiguration and CpuTopology have named fields so we could drop the
> > 'expect: ...' comment line and instead do
> > 
> >   {
> >/* no configuration provided */
> >.config = { .has_cpus = F, .has_maxcpus = F, ... },
> >.valid = T,
> >.expect = { .sockets = 1, .cores = 1, ... },
> >   }, {
> >...
> >   }
> > 
> > which may be easier to maintain. OTOH, the concise form this approach has
> > is also nice.
> I tried the structure initialization with explicit name fields in it like
> above,
> actually we are supposed to do in this way so that we don't have to worry
> about the order change of the structure members.
> 
> But this would break the 80-char line limit or introduce more lines for

I wouldn't worry about 80-char (or even 90, which is when chec

Re: [PATCH for-6.2 v2 11/11] tests/unit: Add a unit test for smp parsing

2021-07-21 Thread wangyanan (Y)

On 2021/7/20 2:57, Andrew Jones wrote:

On Mon, Jul 19, 2021 at 11:20:43AM +0800, Yanan Wang wrote:

Add a QEMU unit test for the parsing of given SMP configuration.
Since all the parsing logic is in generic function smp_parse(),
this test passes diffenent SMP configurations to the function
and compare the parsing result with what is expected.

In the test, all possible collections of the topology parameters
and the corressponding expected results are listed, including the
valid and invalid ones.

The preference of sockets over cores and the preference of cores
over sockets, and the support of multi-dies are also considered.

Signed-off-by: Yanan Wang 
---
  MAINTAINERS |1 +
  tests/unit/meson.build  |1 +
  tests/unit/test-smp-parse.c | 1117 +++
  3 files changed, 1119 insertions(+)
  create mode 100644 tests/unit/test-smp-parse.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 70633e3bf4..160dba2e57 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1636,6 +1636,7 @@ F: include/hw/boards.h
  F: include/hw/core/cpu.h
  F: include/hw/cpu/cluster.h
  F: include/sysemu/numa.h
+F: tests/unit/test-smp-parse.c
  T: git https://gitlab.com/ehabkost/qemu.git machine-next
  
  Xtensa Machines

diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 3e0504dd21..694a924627 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -44,6 +44,7 @@ tests = {
'test-uuid': [],
'ptimer-test': ['ptimer-test-stubs.c', meson.source_root() / 
'hw/core/ptimer.c'],
'test-qapi-util': [],
+  'test-smp-parse': [qom, meson.source_root() / 'hw/core/machine-smp.c'],
  }
  
  if have_system or have_tools

diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c
new file mode 100644
index 00..bc1d324c3d
--- /dev/null
+++ b/tests/unit/test-smp-parse.c
@@ -0,0 +1,1117 @@
+/*
+ * SMP parsing unit-tests
+ *
+ * Copyright (C) 2021, Huawei, Inc.
+ *
+ * Authors:
+ *  Yanan Wang 
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qom/object.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+
+#include "hw/boards.h"
+
+#define T true
+#define F false
+
+/**
+ * SMPTestData:
+ * @config - the given SMP configuration for parsing
+ * @should_be_valid - whether the given configuration is supposed to be valid
+ * @expect - the CPU topology info expected to be parsed out
+ */
+typedef struct SMPTestData {
+SMPConfiguration config;
+bool should_be_valid;

Long way to say 'valid'.

Indeed..., "valid" should be enough.

+CpuTopology expect;
+} SMPTestData;
+
+/* the specific machine type info for this test */
+static const TypeInfo smp_machine_info = {
+.name = TYPE_MACHINE,
+.parent = TYPE_OBJECT,
+.class_size = sizeof(MachineClass),
+.instance_size = sizeof(MachineState),
+};
+
+/*
+ * prefer sockets over cores over threads before 6.2.
+ * all possible SMP configurations and the corressponding expected outputs

corresponding (please run spell check on your commit messages)


Ok, I missed the check.

+ * are listed for testing, including the valid and invalid ones.
+ */
+static struct SMPTestData prefer_sockets[] = {
+{
+/* config: no smp configuration provided
+ * expect: cpus=1,sockets=1,dies=1,cores=1,threads=1,maxcpus=1 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, F, 0 },

SMPConfiguration and CpuTopology have named fields so we could drop the
'expect: ...' comment line and instead do

  {
   /* no configuration provided */
   .config = { .has_cpus = F, .has_maxcpus = F, ... },
   .valid = T,
   .expect = { .sockets = 1, .cores = 1, ... },
  }, {
   ...
  }

which may be easier to maintain. OTOH, the concise form this approach has
is also nice.
I tried the structure initialization with explicit name fields in it 
like above,

actually we are supposed to do in this way so that we don't have to worry
about the order change of the structure members.

But this would break the 80-char line limit or introduce more lines for
for each SMP configuration. If this is not a real problem, I also prefer
above format.

I don't think you should need the casts in the assignments
though.

Yes, the casts may be unnecessary, will remove them.



+.should_be_valid = true,
+.expect = (CpuTopology) { 1, 1, 1, 1, 1, 1 },
+}, {
+/* config: -smp 8
+ * expect: cpus=8,sockets=8,dies=1,cores=1,threads=1,maxcpus=8 */
+.config = (SMPConfiguration) { T, 8, F, 0, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 8, 8, 1, 1, 1, 8 },
+}, {
+/* config: -smp sockets=2
+ * expect: cpus=2,sockets=2,dies=1,cores=1,threads=1,maxcpus=2 */
+.config = (SMPConfiguration) { F, 0, T, 2, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.expect =

Re: [PATCH for-6.2 v2 11/11] tests/unit: Add a unit test for smp parsing

2021-07-19 Thread Andrew Jones
On Mon, Jul 19, 2021 at 11:20:43AM +0800, Yanan Wang wrote:
> Add a QEMU unit test for the parsing of given SMP configuration.
> Since all the parsing logic is in generic function smp_parse(),
> this test passes diffenent SMP configurations to the function
> and compare the parsing result with what is expected.
> 
> In the test, all possible collections of the topology parameters
> and the corressponding expected results are listed, including the
> valid and invalid ones.
> 
> The preference of sockets over cores and the preference of cores
> over sockets, and the support of multi-dies are also considered.
> 
> Signed-off-by: Yanan Wang 
> ---
>  MAINTAINERS |1 +
>  tests/unit/meson.build  |1 +
>  tests/unit/test-smp-parse.c | 1117 +++
>  3 files changed, 1119 insertions(+)
>  create mode 100644 tests/unit/test-smp-parse.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 70633e3bf4..160dba2e57 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -1636,6 +1636,7 @@ F: include/hw/boards.h
>  F: include/hw/core/cpu.h
>  F: include/hw/cpu/cluster.h
>  F: include/sysemu/numa.h
> +F: tests/unit/test-smp-parse.c
>  T: git https://gitlab.com/ehabkost/qemu.git machine-next
>  
>  Xtensa Machines
> diff --git a/tests/unit/meson.build b/tests/unit/meson.build
> index 3e0504dd21..694a924627 100644
> --- a/tests/unit/meson.build
> +++ b/tests/unit/meson.build
> @@ -44,6 +44,7 @@ tests = {
>'test-uuid': [],
>'ptimer-test': ['ptimer-test-stubs.c', meson.source_root() / 
> 'hw/core/ptimer.c'],
>'test-qapi-util': [],
> +  'test-smp-parse': [qom, meson.source_root() / 'hw/core/machine-smp.c'],
>  }
>  
>  if have_system or have_tools
> diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c
> new file mode 100644
> index 00..bc1d324c3d
> --- /dev/null
> +++ b/tests/unit/test-smp-parse.c
> @@ -0,0 +1,1117 @@
> +/*
> + * SMP parsing unit-tests
> + *
> + * Copyright (C) 2021, Huawei, Inc.
> + *
> + * Authors:
> + *  Yanan Wang 
> + *
> + * This work is licensed under the terms of the GNU LGPL, version 2.1 or 
> later.
> + * See the COPYING.LIB file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qom/object.h"
> +#include "qemu/module.h"
> +#include "qapi/error.h"
> +
> +#include "hw/boards.h"
> +
> +#define T true
> +#define F false
> +
> +/**
> + * SMPTestData:
> + * @config - the given SMP configuration for parsing
> + * @should_be_valid - whether the given configuration is supposed to be valid
> + * @expect - the CPU topology info expected to be parsed out
> + */
> +typedef struct SMPTestData {
> +SMPConfiguration config;
> +bool should_be_valid;

Long way to say 'valid'.

> +CpuTopology expect;
> +} SMPTestData;
> +
> +/* the specific machine type info for this test */
> +static const TypeInfo smp_machine_info = {
> +.name = TYPE_MACHINE,
> +.parent = TYPE_OBJECT,
> +.class_size = sizeof(MachineClass),
> +.instance_size = sizeof(MachineState),
> +};
> +
> +/*
> + * prefer sockets over cores over threads before 6.2.
> + * all possible SMP configurations and the corressponding expected outputs

corresponding (please run spell check on your commit messages)


> + * are listed for testing, including the valid and invalid ones.
> + */
> +static struct SMPTestData prefer_sockets[] = {
> +{
> +/* config: no smp configuration provided
> + * expect: cpus=1,sockets=1,dies=1,cores=1,threads=1,maxcpus=1 */
> +.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, F, 0 },

SMPConfiguration and CpuTopology have named fields so we could drop the
'expect: ...' comment line and instead do

 {
  /* no configuration provided */ 
  .config = { .has_cpus = F, .has_maxcpus = F, ... },
  .valid = T,
  .expect = { .sockets = 1, .cores = 1, ... },
 }, {
  ...
 }

which may be easier to maintain. OTOH, the concise form this approach has
is also nice. I don't think you should need the casts in the assignments
though.

> +.should_be_valid = true,
> +.expect = (CpuTopology) { 1, 1, 1, 1, 1, 1 },
> +}, {
> +/* config: -smp 8
> + * expect: cpus=8,sockets=8,dies=1,cores=1,threads=1,maxcpus=8 */
> +.config = (SMPConfiguration) { T, 8, F, 0, F, 0, F, 0, F, 0, F, 0 },
> +.should_be_valid = true,
> +.expect = (CpuTopology) { 8, 8, 1, 1, 1, 8 },
> +}, {
> +/* config: -smp sockets=2
> + * expect: cpus=2,sockets=2,dies=1,cores=1,threads=1,maxcpus=2 */
> +.config = (SMPConfiguration) { F, 0, T, 2, F, 0, F, 0, F, 0, F, 0 },
> +.should_be_valid = true,
> +.expect = (CpuTopology) { 2, 2, 1, 1, 1, 2 },
> +}, {
> +/* config: -smp cores=4
> + * expect: cpus=4,sockets=1,dies=1,cores=4,threads=1,maxcpus=4 */
> +.config = (SMPConfiguration) { F, 0, F, 0, F, 0, T, 4, F, 0, F, 0 },
> +.should_be_valid = true,
> +.expect = (CpuTopology) { 4, 

[PATCH for-6.2 v2 11/11] tests/unit: Add a unit test for smp parsing

2021-07-18 Thread Yanan Wang
Add a QEMU unit test for the parsing of given SMP configuration.
Since all the parsing logic is in generic function smp_parse(),
this test passes diffenent SMP configurations to the function
and compare the parsing result with what is expected.

In the test, all possible collections of the topology parameters
and the corressponding expected results are listed, including the
valid and invalid ones.

The preference of sockets over cores and the preference of cores
over sockets, and the support of multi-dies are also considered.

Signed-off-by: Yanan Wang 
---
 MAINTAINERS |1 +
 tests/unit/meson.build  |1 +
 tests/unit/test-smp-parse.c | 1117 +++
 3 files changed, 1119 insertions(+)
 create mode 100644 tests/unit/test-smp-parse.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 70633e3bf4..160dba2e57 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1636,6 +1636,7 @@ F: include/hw/boards.h
 F: include/hw/core/cpu.h
 F: include/hw/cpu/cluster.h
 F: include/sysemu/numa.h
+F: tests/unit/test-smp-parse.c
 T: git https://gitlab.com/ehabkost/qemu.git machine-next
 
 Xtensa Machines
diff --git a/tests/unit/meson.build b/tests/unit/meson.build
index 3e0504dd21..694a924627 100644
--- a/tests/unit/meson.build
+++ b/tests/unit/meson.build
@@ -44,6 +44,7 @@ tests = {
   'test-uuid': [],
   'ptimer-test': ['ptimer-test-stubs.c', meson.source_root() / 
'hw/core/ptimer.c'],
   'test-qapi-util': [],
+  'test-smp-parse': [qom, meson.source_root() / 'hw/core/machine-smp.c'],
 }
 
 if have_system or have_tools
diff --git a/tests/unit/test-smp-parse.c b/tests/unit/test-smp-parse.c
new file mode 100644
index 00..bc1d324c3d
--- /dev/null
+++ b/tests/unit/test-smp-parse.c
@@ -0,0 +1,1117 @@
+/*
+ * SMP parsing unit-tests
+ *
+ * Copyright (C) 2021, Huawei, Inc.
+ *
+ * Authors:
+ *  Yanan Wang 
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qom/object.h"
+#include "qemu/module.h"
+#include "qapi/error.h"
+
+#include "hw/boards.h"
+
+#define T true
+#define F false
+
+/**
+ * SMPTestData:
+ * @config - the given SMP configuration for parsing
+ * @should_be_valid - whether the given configuration is supposed to be valid
+ * @expect - the CPU topology info expected to be parsed out
+ */
+typedef struct SMPTestData {
+SMPConfiguration config;
+bool should_be_valid;
+CpuTopology expect;
+} SMPTestData;
+
+/* the specific machine type info for this test */
+static const TypeInfo smp_machine_info = {
+.name = TYPE_MACHINE,
+.parent = TYPE_OBJECT,
+.class_size = sizeof(MachineClass),
+.instance_size = sizeof(MachineState),
+};
+
+/*
+ * prefer sockets over cores over threads before 6.2.
+ * all possible SMP configurations and the corressponding expected outputs
+ * are listed for testing, including the valid and invalid ones.
+ */
+static struct SMPTestData prefer_sockets[] = {
+{
+/* config: no smp configuration provided
+ * expect: cpus=1,sockets=1,dies=1,cores=1,threads=1,maxcpus=1 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 1, 1, 1, 1, 1, 1 },
+}, {
+/* config: -smp 8
+ * expect: cpus=8,sockets=8,dies=1,cores=1,threads=1,maxcpus=8 */
+.config = (SMPConfiguration) { T, 8, F, 0, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 8, 8, 1, 1, 1, 8 },
+}, {
+/* config: -smp sockets=2
+ * expect: cpus=2,sockets=2,dies=1,cores=1,threads=1,maxcpus=2 */
+.config = (SMPConfiguration) { F, 0, T, 2, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 2, 2, 1, 1, 1, 2 },
+}, {
+/* config: -smp cores=4
+ * expect: cpus=4,sockets=1,dies=1,cores=4,threads=1,maxcpus=4 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, T, 4, F, 0, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 4, 1, 1, 4, 1, 4 },
+}, {
+/* config: -smp threads=2
+ * expect: cpus=2,sockets=1,dies=1,cores=1,threads=2,maxcpus=2 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, T, 2, F, 0 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 2, 1, 1, 1, 2, 2 },
+}, {
+/* config: -smp maxcpus=16
+ * expect: cpus=16,sockets=16,dies=1,cores=1,threads=1,maxcpus=16 */
+.config = (SMPConfiguration) { F, 0, F, 0, F, 0, F, 0, F, 0, T, 16 },
+.should_be_valid = true,
+.expect = (CpuTopology) { 16, 16, 1, 1, 1, 16 },
+}, {
+/* config: -smp 8,sockets=2
+ * expect: cpus=8,sockets=2,dies=1,cores=4,threads=1,maxcpus=8 */
+.config = (SMPConfiguration) { T, 8, T, 2, F, 0, F, 0, F, 0, F, 0 },
+.should_be_valid = true,
+.