Skip to content

Commit

Permalink
linux-gen: cpu: make possible to read cpu id during every odp_cpu_id(…
Browse files Browse the repository at this point in the history
…) call

Add new config file option 'system:cpu_id_static' for selecting whether
the implementation reads CPU identifier value from OS during every
odp_cpu_id() call or only once  during thread initialization (default). If
thread(s) may be migrated between cores during the application lifetime
this option should be set to 0.

Signed-off-by: Matias Elo <[email protected]>
Reviewed-by: Tuomas Taipale <[email protected]>
  • Loading branch information
MatiasElo authored and TuomasTaipale committed Jul 22, 2024
1 parent 1bab696 commit 6037b52
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 25 deletions.
10 changes: 9 additions & 1 deletion config/odp-linux-generic.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

# System options
system: {
Expand All @@ -38,6 +38,14 @@ system: {
# scaling is disabled.
cpu_hz_static = 0

# When enabled (1), implementation reads the CPU identifier values from
# OS only once during ODP initialization. Enabling this option removes
# a system call from odp_cpu_id() implementation.
#
# This option should only be used when ODP threads are not migrated
# during application lifetime.
cpu_id_static = 1

# Maximum number of ODP threads that can be created.
# odp_thread_count_max() returns this value or the build time
# maximum ODP_THREAD_COUNT_MAX, whichever is lower. This setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {

typedef struct {
odp_log_func_t log_fn;
int (*cpu_id_fn)(void);
odp_thread_type_t type;
int thr;
int cpu;
Expand Down
6 changes: 5 additions & 1 deletion platform/linux-generic/include/odp/api/plat/thread_inlines.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2018 Linaro Limited
* Copyright (c) 2024 Nokia
*/

#ifndef ODP_PLAT_THREAD_INLINES_H_
Expand Down Expand Up @@ -37,7 +38,10 @@ _ODP_INLINE odp_thread_type_t odp_thread_type(void)

_ODP_INLINE int odp_cpu_id(void)
{
return _odp_this_thread->cpu;
if (_odp_this_thread->cpu >= 0)
return _odp_this_thread->cpu;

return _odp_this_thread->cpu_id_fn();
}

/** @endcond */
Expand Down
1 change: 1 addition & 0 deletions platform/linux-generic/include/odp_global_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct {
uint64_t page_size;
int cache_line_size;
uint8_t cpu_hz_static;
uint8_t cpu_id_static;
uint8_t cpu_constant_tsc;
odp_cpu_arch_t cpu_arch;
odp_cpu_arch_isa_t cpu_isa_sw;
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/m4/odp_libconfig.m4
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
##########################################################################
m4_define([_odp_config_version_generation], [0])
m4_define([_odp_config_version_major], [1])
m4_define([_odp_config_version_minor], [28])
m4_define([_odp_config_version_minor], [29])

m4_define([_odp_config_version],
[_odp_config_version_generation._odp_config_version_major._odp_config_version_minor])
Expand Down
19 changes: 18 additions & 1 deletion platform/linux-generic/odp_system_info.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2013-2018 Linaro Limited
* Copyright (c) 2020-2022 Nokia
* Copyright (c) 2020-2024 Nokia
*
* Copyright(c) 2010-2014 Intel Corporation
* - lib/eal/common/eal_common_string_fns.c
Expand Down Expand Up @@ -337,6 +337,23 @@ static int read_config_file(void)
}
odp_global_ro.system_info.cpu_hz_static = !!val;

str = "system.cpu_id_static";
if (!_odp_libconfig_lookup_int(str, &val)) {
_ODP_ERR("Config option '%s' not found.\n", str);
return -1;
}
odp_global_ro.system_info.cpu_id_static = !!val;

_ODP_PRINT("System config:\n");
_ODP_PRINT(" system.cpu_mhz: %" PRIu64 "\n",
odp_global_ro.system_info.default_cpu_hz);
_ODP_PRINT(" system.cpu_mhz_max: %" PRIu64 "\n",
odp_global_ro.system_info.default_cpu_hz_max);
_ODP_PRINT(" system.cpu_hz_static: %" PRIu8 "\n",
odp_global_ro.system_info.cpu_hz_static);
_ODP_PRINT(" system.cpu_id_static: %" PRIu8 "\n\n",
odp_global_ro.system_info.cpu_id_static);

return 0;
}

Expand Down
45 changes: 29 additions & 16 deletions platform/linux-generic/odp_thread.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2013-2018 Linaro Limited
* Copyright (c) 2021 Nokia
* Copyright (c) 2021-2024 Nokia
*/

#include <odp_posix_extensions.h>

#include <sched.h>
#include <odp/api/align.h>
#include <odp/api/atomic.h>
#include <odp/api/hints.h>
#include <odp/api/shared_memory.h>
#include <odp/api/spinlock.h>
#include <odp/api/thread.h>
#include <odp/api/thrmask.h>
#include <odp/api/spinlock.h>
#include <odp_init_internal.h>

#include <odp/api/plat/thread_inlines.h>

#include <odp_config_internal.h>
#include <odp_debug_internal.h>
#include <odp/api/shared_memory.h>
#include <odp/api/align.h>
#include <odp/api/cpu.h>
#include <odp_schedule_if.h>
#include <odp/api/plat/thread_inlines.h>
#include <odp_global_data.h>
#include <odp_init_internal.h>
#include <odp_libconfig_internal.h>
#include <odp_schedule_if.h>

#include <string.h>
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
_odp_thread_state_t thr[ODP_THREAD_COUNT_MAX];
Expand Down Expand Up @@ -154,6 +158,17 @@ static int free_id(int thr)
return odp_atomic_fetch_dec_u32(&thread_globals->num) - 1;
}

static int cpu_id(void)
{
int cpu = sched_getcpu();

if (odp_unlikely(cpu < 0)) {
_ODP_ERR("sched_getcpu() failed: %s\n", strerror(errno));
return -1;
}
return cpu;
}

int _odp_thread_init_local(odp_thread_type_t type)
{
int id;
Expand Down Expand Up @@ -182,15 +197,13 @@ int _odp_thread_init_local(odp_thread_type_t type)
return -1;
}

cpu = sched_getcpu();

if (cpu < 0) {
_ODP_ERR("getcpu failed\n");
cpu = cpu_id();
if (cpu < 0)
return -1;
}

thread_globals->thr[id].thr = id;
thread_globals->thr[id].cpu = cpu;
thread_globals->thr[id].cpu = odp_global_ro.system_info.cpu_id_static ? cpu : -1;
thread_globals->thr[id].cpu_id_fn = cpu_id;
thread_globals->thr[id].type = type;

_odp_this_thread = &thread_globals->thr[id];
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/test/inline-timer.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

timer: {
# Enable inline timer implementation
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/test/packet_align.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

pool: {
pkt: {
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/test/process-mode.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

# Shared memory options
shm: {
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/test/sched-basic.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

# Test scheduler with an odd spread value and without dynamic load balance
sched_basic: {
Expand Down
2 changes: 1 addition & 1 deletion platform/linux-generic/test/stash-custom.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mandatory fields
odp_implementation = "linux-generic"
config_file_version = "0.1.28"
config_file_version = "0.1.29"

# Test overflow safe stash variant
stash: {
Expand Down

0 comments on commit 6037b52

Please sign in to comment.