Re: [ovs-dev] [PATCH 1/3] ovs-numa: Add dump based thread affinity functions.

2019-08-14 Thread William Tu
On Tue, Aug 13, 2019 at 8:30 AM Ilya Maximets  wrote:
>
> New functions to get and set CPU affinity using CPU dumps.
> This will abstract OS specific implementation details from the
> cross-platform code.
>
> Signed-off-by: Ilya Maximets 

Acked-by: William Tu 

One comment inline below.

> ---
>  lib/ovs-numa.c | 74 +-
>  lib/ovs-numa.h |  2 ++
>  2 files changed, 75 insertions(+), 1 deletion(-)
>
> diff --git a/lib/ovs-numa.c b/lib/ovs-numa.c
> index 24edeab2a..a417730b8 100644
> --- a/lib/ovs-numa.c
> +++ b/lib/ovs-numa.c
> @@ -532,6 +532,78 @@ ovs_numa_dump_destroy(struct ovs_numa_dump *dump)
>  free(dump);
>  }
>
> +struct ovs_numa_dump *
> +ovs_numa_thread_getaffinity_dump(void)
> +{
> +if (dummy_numa) {
> +/* Nothing to do. */
> +return NULL;
> +}
> +
> +#ifndef __linux__
> +return NULL;
> +#else
> +struct ovs_numa_dump *dump;
> +const struct numa_node *n;
> +cpu_set_t cpuset;
> +int err;
> +
> +CPU_ZERO();
> +err = pthread_getaffinity_np(pthread_self(), sizeof cpuset, );
> +if (err) {
> +VLOG_ERR("Thread getaffinity error: %s", ovs_strerror(err));
> +return NULL;
> +}
> +
> +dump = ovs_numa_dump_create();
> +
> +HMAP_FOR_EACH (n, hmap_node, _numa_nodes) {
> +const struct cpu_core *core;
> +
> +LIST_FOR_EACH (core, list_node, >cores) {
> +if (CPU_ISSET(core->core_id, )) {
> +ovs_numa_dump_add(dump, core->numa->numa_id, core->core_id);
> +}
> +}
> +}
> +
> +if (!ovs_numa_dump_count(dump)) {
> +ovs_numa_dump_destroy(dump);
> +return NULL;
> +}
> +return dump;
> +#endif /* __linux__ */
> +}
> +
> +int
> +ovs_numa_thread_setaffinity_dump(struct ovs_numa_dump *dump OVS_UNUSED)

'dump' is used.

> +{
> +if (!dump || dummy_numa) {
> +/* Nothing to do. */
> +return 0;
> +}
> +
> +#ifdef __linux__
> +struct ovs_numa_info_core *core;
> +cpu_set_t cpuset;
> +int err;
> +
> +CPU_ZERO();
> +FOR_EACH_CORE_ON_DUMP (core, dump) {
> +CPU_SET(core->core_id, );
> +}
> +err = pthread_setaffinity_np(pthread_self(), sizeof cpuset, );
> +if (err) {
> +VLOG_ERR("Thread setaffinity error: %s", ovs_strerror(err));
> +return err;
> +}
> +
> +return 0;
> +#else /* !__linux__ */
> +return EOPNOTSUPP;
> +#endif /* __linux__ */
> +}
> +
>  int ovs_numa_thread_setaffinity_core(unsigned core_id OVS_UNUSED)
>  {
>  if (dummy_numa) {
> @@ -547,7 +619,7 @@ int ovs_numa_thread_setaffinity_core(unsigned core_id 
> OVS_UNUSED)
>  CPU_SET(core_id, );
>  err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), );
>  if (err) {
> -VLOG_ERR("Thread affinity error %d",err);
> +VLOG_ERR("Thread setaffinity error: %s", ovs_strerror(err));
>  return err;
>  }
>
> diff --git a/lib/ovs-numa.h b/lib/ovs-numa.h
> index 088fcb8c3..88352a93e 100644
> --- a/lib/ovs-numa.h
> +++ b/lib/ovs-numa.h
> @@ -60,6 +60,8 @@ struct ovs_numa_dump *ovs_numa_dump_n_cores_per_numa(int n);
>  bool ovs_numa_dump_contains_core(const struct ovs_numa_dump *,
>   int numa_id, unsigned core_id);
>  size_t ovs_numa_dump_count(const struct ovs_numa_dump *);
> +struct ovs_numa_dump * ovs_numa_thread_getaffinity_dump(void);
> +int ovs_numa_thread_setaffinity_dump(struct ovs_numa_dump *);
>  void ovs_numa_dump_destroy(struct ovs_numa_dump *);
>  int ovs_numa_thread_setaffinity_core(unsigned core_id);
>
> --
> 2.17.1
>
___
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev


[ovs-dev] [PATCH 1/3] ovs-numa: Add dump based thread affinity functions.

2019-08-13 Thread Ilya Maximets
New functions to get and set CPU affinity using CPU dumps.
This will abstract OS specific implementation details from the
cross-platform code.

Signed-off-by: Ilya Maximets 
---
 lib/ovs-numa.c | 74 +-
 lib/ovs-numa.h |  2 ++
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/lib/ovs-numa.c b/lib/ovs-numa.c
index 24edeab2a..a417730b8 100644
--- a/lib/ovs-numa.c
+++ b/lib/ovs-numa.c
@@ -532,6 +532,78 @@ ovs_numa_dump_destroy(struct ovs_numa_dump *dump)
 free(dump);
 }
 
+struct ovs_numa_dump *
+ovs_numa_thread_getaffinity_dump(void)
+{
+if (dummy_numa) {
+/* Nothing to do. */
+return NULL;
+}
+
+#ifndef __linux__
+return NULL;
+#else
+struct ovs_numa_dump *dump;
+const struct numa_node *n;
+cpu_set_t cpuset;
+int err;
+
+CPU_ZERO();
+err = pthread_getaffinity_np(pthread_self(), sizeof cpuset, );
+if (err) {
+VLOG_ERR("Thread getaffinity error: %s", ovs_strerror(err));
+return NULL;
+}
+
+dump = ovs_numa_dump_create();
+
+HMAP_FOR_EACH (n, hmap_node, _numa_nodes) {
+const struct cpu_core *core;
+
+LIST_FOR_EACH (core, list_node, >cores) {
+if (CPU_ISSET(core->core_id, )) {
+ovs_numa_dump_add(dump, core->numa->numa_id, core->core_id);
+}
+}
+}
+
+if (!ovs_numa_dump_count(dump)) {
+ovs_numa_dump_destroy(dump);
+return NULL;
+}
+return dump;
+#endif /* __linux__ */
+}
+
+int
+ovs_numa_thread_setaffinity_dump(struct ovs_numa_dump *dump OVS_UNUSED)
+{
+if (!dump || dummy_numa) {
+/* Nothing to do. */
+return 0;
+}
+
+#ifdef __linux__
+struct ovs_numa_info_core *core;
+cpu_set_t cpuset;
+int err;
+
+CPU_ZERO();
+FOR_EACH_CORE_ON_DUMP (core, dump) {
+CPU_SET(core->core_id, );
+}
+err = pthread_setaffinity_np(pthread_self(), sizeof cpuset, );
+if (err) {
+VLOG_ERR("Thread setaffinity error: %s", ovs_strerror(err));
+return err;
+}
+
+return 0;
+#else /* !__linux__ */
+return EOPNOTSUPP;
+#endif /* __linux__ */
+}
+
 int ovs_numa_thread_setaffinity_core(unsigned core_id OVS_UNUSED)
 {
 if (dummy_numa) {
@@ -547,7 +619,7 @@ int ovs_numa_thread_setaffinity_core(unsigned core_id 
OVS_UNUSED)
 CPU_SET(core_id, );
 err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), );
 if (err) {
-VLOG_ERR("Thread affinity error %d",err);
+VLOG_ERR("Thread setaffinity error: %s", ovs_strerror(err));
 return err;
 }
 
diff --git a/lib/ovs-numa.h b/lib/ovs-numa.h
index 088fcb8c3..88352a93e 100644
--- a/lib/ovs-numa.h
+++ b/lib/ovs-numa.h
@@ -60,6 +60,8 @@ struct ovs_numa_dump *ovs_numa_dump_n_cores_per_numa(int n);
 bool ovs_numa_dump_contains_core(const struct ovs_numa_dump *,
  int numa_id, unsigned core_id);
 size_t ovs_numa_dump_count(const struct ovs_numa_dump *);
+struct ovs_numa_dump * ovs_numa_thread_getaffinity_dump(void);
+int ovs_numa_thread_setaffinity_dump(struct ovs_numa_dump *);
 void ovs_numa_dump_destroy(struct ovs_numa_dump *);
 int ovs_numa_thread_setaffinity_core(unsigned core_id);
 
-- 
2.17.1

___
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev