All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
To: Eduardo Habkost <ehabkost@redhat.com>, qemu-devel@nongnu.org
Cc: "Igor Mammedov" <imammedo@redhat.com>,
	"Andreas Färber" <afaerber@suse.de>
Subject: [Qemu-devel] [RFC v4 3/3] i386: introduce cpu QOM hierarchy tree
Date: Wed, 12 Mar 2014 15:51:36 +0800	[thread overview]
Message-ID: <a8e62d3fe4c63e4436baefc85d66d6682aa87df3.1394609102.git.chen.fan.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <cover.1394609102.git.chen.fan.fnst@cn.fujitsu.com>

add cpu-topology.h cpu-topology.c files for prebuilding cpu qom tree
 "/machine/node[X]/socket[Y]/core[Z]->link cpu"

Signed-off-by: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
---
 hw/i386/pc.c               |   3 +
 target-i386/Makefile.objs  |   2 +-
 target-i386/cpu-topology.c | 199 +++++++++++++++++++++++++++++++++++++++++++++
 target-i386/cpu-topology.h |  71 ++++++++++++++++
 target-i386/cpu.c          |  13 +++
 target-i386/cpu.h          |   3 +
 6 files changed, 290 insertions(+), 1 deletion(-)
 create mode 100644 target-i386/cpu-topology.c
 create mode 100644 target-i386/cpu-topology.h

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index 765b634..22e81be 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -948,6 +948,7 @@ static X86CPU *pc_new_cpu(const char *cpu_model, X86CPUTopoInfo *topo,
 
     object_property_set_int(OBJECT(cpu), apic_id, "apic-id", &local_err);
     object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);
+    x86_topo_cpu_set_link(OBJECT(cpu), topo);
 
     if (local_err) {
         error_propagate(errp, local_err);
@@ -997,6 +998,8 @@ void pc_cpus_init(const char *cpu_model, DeviceState *icc_bridge)
     }
     current_cpu_model = cpu_model;
 
+    cpu_topo_init();
+
     for (i = 0; i < smp_cpus; i++) {
         X86CPUTopoInfo topo;
         x86_cpu_topo_ids_from_index(i, &topo);
diff --git a/target-i386/Makefile.objs b/target-i386/Makefile.objs
index 027b94e..239474d 100644
--- a/target-i386/Makefile.objs
+++ b/target-i386/Makefile.objs
@@ -1,4 +1,4 @@
-obj-y += translate.o helper.o cpu.o
+obj-y += translate.o helper.o cpu.o cpu-topology.o
 obj-y += excp_helper.o fpu_helper.o cc_helper.o int_helper.o svm_helper.o
 obj-y += smm_helper.o misc_helper.o mem_helper.o seg_helper.o
 obj-y += gdbstub.o
diff --git a/target-i386/cpu-topology.c b/target-i386/cpu-topology.c
new file mode 100644
index 0000000..707f080
--- /dev/null
+++ b/target-i386/cpu-topology.c
@@ -0,0 +1,199 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qom/object.h"
+#include "qemu/module.h"
+#include "hw/hw.h"
+#include "sysemu/cpus.h"
+#include "sysemu/sysemu.h"
+#include "cpu-topology.h"
+
+static QTAILQ_HEAD(, SocketState) sockets = QTAILQ_HEAD_INITIALIZER(sockets);
+static QTAILQ_HEAD(, NodeState) nodes = QTAILQ_HEAD_INITIALIZER(nodes);
+
+static NodeState *node_get(int node_id)
+{
+    NodeState *node;
+
+    QTAILQ_FOREACH(node, &nodes, next) {
+        if (node->node_id == node_id) {
+            return node;
+        }
+    }
+    return NULL;
+}
+
+static SocketState *cpu_socket_find(int pkg_id)
+{
+    SocketState *ss;
+
+    QTAILQ_FOREACH(ss, &sockets, next) {
+        if (ss->socket_id == pkg_id) {
+            return ss;
+        }
+    }
+    return NULL;
+}
+
+void cpu_topo_init(void)
+{
+    unsigned long *node_mask;
+    int i;
+
+    node_mask = bitmap_new(MAX_NODES);
+
+    for (i = 0; i < max_cpus; i++) {
+        NodeState *node;
+        SocketState *ss;
+        gchar *name;
+        int node_id = 0, socket_id;
+        int j;
+
+        for (j = 0; j < nb_numa_nodes; j++) {
+            if (test_bit(i, node_cpumask[j])) {
+                node_id = j;
+                break;
+            }
+        }
+
+        if (test_bit(node_id, node_mask)) {
+            node = node_get(node_id);
+        } else {
+            node = NODE(object_new(TYPE_NODE));
+            node->node_id = node_id;
+            name = g_strdup_printf("node[%" PRIu32 "]", node_id);
+            object_property_add_child(qdev_get_machine(), name,
+                                      OBJECT(node), NULL);
+            set_bit(node_id, node_mask);
+            g_free(name);
+            QTAILQ_INSERT_TAIL(&nodes, node, next);
+        }
+
+        socket_id = i / (smp_cores * smp_threads);
+        ss = cpu_socket_find(socket_id);
+        if (!ss) {
+            ss = SOCKET(object_new(TYPE_SOCKET));
+            ss->socket_id = socket_id;
+            name = g_strdup_printf("socket[%" PRIu32 "]", socket_id);
+            object_property_add_child(OBJECT(node), name,
+                                      OBJECT(ss), NULL);
+            g_free(name);
+            QTAILQ_INSERT_TAIL(&sockets, ss, next);
+        }
+    }
+
+    g_free(node_mask);
+}
+
+CoreState *cpu_topo_object_core_find(X86CPUTopoInfo *topo)
+{
+    SocketState *ss;
+    CoreState *core;
+
+    ss = cpu_socket_find(topo->pkg_id);
+    if (!ss) {
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(core, &ss->children, sibling) {
+        if (core->core_id == topo->core_id) {
+            return core;
+        }
+    }
+    return NULL;
+}
+
+static void socket_initfn(Object *obj)
+{
+    SocketState *ss = SOCKET(obj);
+    int i;
+
+    QTAILQ_INIT(&ss->children);
+
+    for (i = 0; i < smp_cores; i++) {
+        gchar *name;
+        CoreState *core;
+
+        core = CORE(object_new(TYPE_CORE));
+        core->core_id = i;
+        QTAILQ_INSERT_TAIL(&ss->children, core, sibling);
+
+        name = g_strdup_printf("core[%" PRIu32 "]", i);
+        object_property_add_child(obj, name, OBJECT(core), NULL);
+        g_free(name);
+    }
+}
+
+static void core_initfn(Object *obj)
+{
+    CoreState *cs = CORE(obj);
+    gchar *name;
+    int i;
+
+    cs->link_cpu = g_malloc0(sizeof(CPUState *) * smp_threads);
+    for (i = 0; i < smp_threads; i++) {
+        name = g_strdup_printf("cpu[%" PRIu32 "]", i);
+        object_property_add_link(obj, name, TYPE_CPU,
+                                 (Object **)&cs->link_cpu[i], NULL);
+        g_free(name);
+    }
+}
+
+static void core_fini(Object *obj)
+{
+    CoreState *cs = CORE(obj);
+
+    g_free(cs->link_cpu);
+}
+
+static const TypeInfo core_type_info = {
+    .name = TYPE_CORE,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(CoreState),
+    .instance_init = core_initfn,
+    .instance_finalize = core_fini,
+};
+
+static const TypeInfo socket_type_info = {
+    .name = TYPE_SOCKET,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(SocketState),
+    .instance_init = socket_initfn,
+};
+
+static const TypeInfo node_type_info = {
+    .name = TYPE_NODE,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(NodeState),
+};
+
+static void node_register_types(void)
+{
+    type_register_static(&node_type_info);
+    type_register_static(&socket_type_info);
+    type_register_static(&core_type_info);
+}
+
+type_init(node_register_types)
diff --git a/target-i386/cpu-topology.h b/target-i386/cpu-topology.h
new file mode 100644
index 0000000..6465d94
--- /dev/null
+++ b/target-i386/cpu-topology.h
@@ -0,0 +1,71 @@
+/*
+ * QEMU System Emulator
+ *
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef TARGET_I386_CPU_TOPOLOGY_H
+#define TARGET_I386_CPU_TOPOLOGY_H
+
+#include "hw/qdev-core.h"
+#include "cpu.h"
+#include "qom/cpu.h"
+
+#define TYPE_NODE   "node"
+#define TYPE_SOCKET "socket"
+#define TYPE_CORE   "core"
+
+#define NODE(obj) OBJECT_CHECK(NodeState, (obj), TYPE_NODE)
+
+#define SOCKET(obj) OBJECT_CHECK(SocketState, (obj), TYPE_SOCKET)
+
+#define CORE(obj) OBJECT_CHECK(CoreState, (obj), TYPE_CORE)
+
+typedef struct CoreState {
+    /*< private >*/
+    Object parent_obj;
+    /*< public >*/
+    int core_id;
+    QTAILQ_ENTRY(CoreState) sibling;
+    CPUState **link_cpu;
+} CoreState;
+
+typedef struct SocketState {
+    /*< private >*/
+    Object parent_obj;
+    /*< public >*/
+    unsigned socket_id;
+    QTAILQ_HEAD(ChildHread, CoreState) children;
+    QTAILQ_ENTRY(SocketState) next;
+} SocketState;
+
+typedef struct NodeState {
+    /*< private >*/
+    Object parent_obj;
+    /*< public >*/
+    int node_id;
+    QTAILQ_ENTRY(NodeState) next;
+} NodeState;
+
+CoreState *cpu_topo_object_core_find(X86CPUTopoInfo *topo);
+void cpu_topo_init(void);
+
+#endif /* TARGET_I386_CPU_TOPOLOGY_H */
diff --git a/target-i386/cpu.c b/target-i386/cpu.c
index d8ad484..de2994c 100644
--- a/target-i386/cpu.c
+++ b/target-i386/cpu.c
@@ -2713,6 +2713,19 @@ static void x86_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
     cpu->env.eip = tb->pc - tb->cs_base;
 }
 
+void x86_topo_cpu_set_link(Object *obj, X86CPUTopoInfo *topo)
+{
+    gchar *name;
+    CoreState *core;
+
+    core = cpu_topo_object_core_find(topo);
+    if (core) {
+        name = g_strdup_printf("cpu[%" PRIu32 "]", topo->smt_id);
+        object_property_set_link(OBJECT(core), obj, name, NULL);
+        g_free(name);
+    }
+}
+
 static Property x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("pmu", X86CPU, enable_pmu, false),
     { .name  = "hv-spinlocks", .info  = &qdev_prop_spinlocks },
diff --git a/target-i386/cpu.h b/target-i386/cpu.h
index a410b16..d7f74e2 100644
--- a/target-i386/cpu.h
+++ b/target-i386/cpu.h
@@ -24,6 +24,7 @@
 
 #include "sysemu/cpus.h"
 #include "topology.h"
+#include "cpu-topology.h"
 
 #ifdef TARGET_X86_64
 #define TARGET_LONG_BITS 64
@@ -1293,6 +1294,8 @@ void x86_cpu_topo_ids_from_index(unsigned int cpu_index,
                                  X86CPUTopoInfo *topo);
 void enable_compat_apic_id_mode(void);
 
+void x86_topo_cpu_set_link(Object *obj, X86CPUTopoInfo *topo);
+
 #define APIC_DEFAULT_ADDRESS 0xfee00000
 #define APIC_SPACE_SIZE      0x100000
 
-- 
1.8.1.4

  parent reply	other threads:[~2014-03-12  7:54 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-14  9:27 [Qemu-devel] [RFC 0/3] fix migration issues after hotplug a discontinuous cpuid Chen Fan
2014-01-14  9:27 ` [Qemu-devel] [RFC 1/3] target-i386: moving registers of vmstate from cpu_exec_init() to x86_cpu_realizefn() Chen Fan
2014-01-14 10:40   ` Igor Mammedov
2014-01-15 12:24     ` Chen Fan
2014-01-15 14:37       ` Igor Mammedov
2014-01-17 19:13         ` [Qemu-devel] Exposing and calculating CPU APIC IDs (was Re: [RFC 1/3] target-i386: moving registers of vmstate from cpu_exec_init() to x86_cpu_realizefn()) Eduardo Habkost
2014-01-20 12:29           ` Igor Mammedov
2014-01-21  7:12             ` Chen Fan
2014-01-21  9:31               ` Igor Mammedov
2014-01-21  9:51                 ` Chen Fan
2014-01-21 10:10                   ` Andreas Färber
2014-02-13  6:14                     ` Chen Fan
2014-02-13  9:44                       ` Igor Mammedov
2014-02-17 10:24                         ` Chen Fan
2014-02-17 10:43                           ` Igor Mammedov
2014-02-25  9:07                             ` [Qemu-devel] [PATCH 0/2][RFC] prebuild cpu QOM tree /machine/node/socket/core/thread/ Chen Fan
2014-02-25  9:07                               ` [Qemu-devel] [PATCH 1/2][RFC] qom: introduce cpu QOM hierarchy tree /machine/node/socket/core/thread/cpu Chen Fan
2014-02-25 13:35                                 ` Eric Blake
2014-02-26  1:05                                   ` Chen Fan
2014-02-26 18:52                                 ` Eduardo Habkost
2014-02-28  2:01                                   ` Chen Fan
2014-03-04 10:50                                     ` [Qemu-devel] [RFC v2 0/2] prebuild cpu QOM tree /machine/node/socket/core/thread/ Chen Fan
2014-03-04 10:50                                       ` [Qemu-devel] [RFC v2 1/2] i386: introduce "struct X86TopoInfo" for saving cpu topology information Chen Fan
2014-03-04 19:35                                         ` Eduardo Habkost
2014-03-05  1:33                                           ` Chen Fan
2014-03-11 10:58                                             ` [Qemu-devel] [RFC v3 0/3] prebuild cpu QOM tree /machine/node/socket/core ->link-cpu chen.fan.fnst
2014-03-11 10:58                                               ` [Qemu-devel] [RFC v3 1/3] cpu: introduce CpuTopoInfo structure for argument simplification chen.fan.fnst
2014-03-11 17:10                                                 ` Eduardo Habkost
2014-03-11 10:58                                               ` [Qemu-devel] [RFC v3 2/3] i386: use CpuTopoInfo instead apic_id as argument for pc_new_cpu() chen.fan.fnst
2014-03-11 18:00                                                 ` Eduardo Habkost
2014-03-12  5:53                                                   ` Chen Fan
2014-03-12  7:51                                                   ` [Qemu-devel] [RFC v4 0/3] prebuild cpu QOM tree /machine/node/socket/core ->link-cpu Chen Fan
2014-03-12  7:51                                                     ` [Qemu-devel] [RFC v4 1/3] cpu: introduce CpuTopoInfo structure for argument simplification Chen Fan
2014-03-12 15:36                                                       ` Eduardo Habkost
2014-03-19  8:53                                                         ` [Qemu-devel] [PATCH v1 0/4] prebuild cpu QOM tree /machine/node/socket/core ->link-cpu Chen Fan
2014-03-19  8:53                                                           ` [Qemu-devel] [PATCH v1 1/4] cpu: introduce CpuTopoInfo structure for argument simplification Chen Fan
2014-03-19  8:53                                                           ` [Qemu-devel] [PATCH v1 2/4] i386: use CpuTopoInfo instead apic_id as argument for pc_new_cpu() Chen Fan
2014-03-19 19:27                                                             ` Eduardo Habkost
2014-03-20  6:25                                                               ` Chen Fan
2014-03-19  8:53                                                           ` [Qemu-devel] [PATCH v1 3/4] topo unit-test: update Unit tests to test-x86-cpuid.c Chen Fan
2014-03-19  8:53                                                           ` [Qemu-devel] [PATCH v1 4/4] i386: introduce cpu QOM hierarchy tree Chen Fan
2014-03-19 12:00                                                           ` [Qemu-devel] [PATCH v1 0/4] prebuild cpu QOM tree /machine/node/socket/core ->link-cpu Eric Blake
2014-03-20  0:55                                                             ` Chen Fan
2014-03-12  7:51                                                     ` [Qemu-devel] [RFC v4 2/3] i386: use CpuTopoInfo instead apic_id as argument for pc_new_cpu() Chen Fan
2014-03-12 15:39                                                       ` Eduardo Habkost
2014-03-12  7:51                                                     ` Chen Fan [this message]
2014-03-11 10:58                                               ` [Qemu-devel] [RFC v3 3/3] i386: introduce cpu QOM hierarchy tree chen.fan.fnst
2014-03-04 10:50                                       ` [Qemu-devel] [RFC v2 2/2] " Chen Fan
2014-02-25  9:07                               ` [Qemu-devel] [PATCH 2/2][RFC] cpu: link each new cpu to QOM tree /machine/node/socket/core/thread/cpu respectively Chen Fan
2014-02-26 19:11                                 ` Eduardo Habkost
2014-02-13 10:00                     ` [Qemu-devel] Exposing and calculating CPU APIC IDs (was Re: [RFC 1/3] target-i386: moving registers of vmstate from cpu_exec_init() to x86_cpu_realizefn()) Igor Mammedov
2014-01-14  9:27 ` [Qemu-devel] [RFC 2/3] target-i386: add -smp X,apics=0x option Chen Fan
2014-02-17 18:37   ` Eric Blake
2014-02-18  1:49     ` Chen Fan
2014-01-14  9:27 ` [Qemu-devel] [RFC 3/3] target-i386: add qmp command 'query-cpus' to display apic_id Chen Fan
2014-02-17 18:37   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a8e62d3fe4c63e4436baefc85d66d6682aa87df3.1394609102.git.chen.fan.fnst@cn.fujitsu.com \
    --to=chen.fan.fnst@cn.fujitsu.com \
    --cc=afaerber@suse.de \
    --cc=ehabkost@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.