Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: Fix incorrect use of cpu_set_t #122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/rt-app.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define FORKS_LIMIT 1024


/*
* Maximum number of CPUs rt-app will be able to detect.
*/
#define MAX_CPUS 1024

static volatile sig_atomic_t continue_running;
static pthread_data_t *threads;
static int nthreads;
Expand Down Expand Up @@ -733,6 +739,12 @@ static void __shutdown(bool force_terminate)
{
/* clean up tdata if this was a forked thread */
free(threads[i].data->name);
if (threads[i].data->def_cpu_data.cpuset)
CPU_FREE(threads[i].data->def_cpu_data.cpuset);

if (threads[i].data->def_cpu_data.cpuset_str)
free(threads[i].data->def_cpu_data.cpuset_str);

free(threads[i].data);
}

Expand Down Expand Up @@ -793,7 +805,7 @@ static int create_cpuset_str(cpuset_data_t *cpu_data)
for (i = 0; i < 10000 && cpu_count; ++i) {
unsigned int n;

if (CPU_ISSET(i, cpu_data->cpuset)) {
if (CPU_ISSET_S(cpu_data->cpusetsize, i, cpu_data->cpuset)) {
--cpu_count;
if (size_needed <= (idx + 1)) {
log_error("Not enough memory for array");
Expand Down Expand Up @@ -830,21 +842,20 @@ static void set_thread_affinity(thread_data_t *data, cpuset_data_t *cpu_data)

if (data->def_cpu_data.cpuset == NULL) {
/* Get default affinity */
cpu_set_t cpuset;
unsigned int cpu_count;
data->def_cpu_data.cpusetsize = CPU_ALLOC_SIZE(MAX_CPUS);
data->def_cpu_data.cpuset = CPU_ALLOC(MAX_CPUS);
if (!data->def_cpu_data.cpuset) {
perror("cpu_set_t malloc");
exit(EXIT_FAILURE);
}

ret = pthread_getaffinity_np(pthread_self(),
sizeof(cpu_set_t), &cpuset);
data->def_cpu_data.cpusetsize, data->def_cpu_data.cpuset);
if (ret != 0) {
errno = ret;
perror("pthread_get_affinity");
exit(EXIT_FAILURE);
}
cpu_count = CPU_COUNT(&cpuset);
data->def_cpu_data.cpusetsize = CPU_ALLOC_SIZE(cpu_count);
data->def_cpu_data.cpuset = CPU_ALLOC(cpu_count);
memcpy(data->def_cpu_data.cpuset, &cpuset,
data->def_cpu_data.cpusetsize);
create_cpuset_str(&data->def_cpu_data);
data->curr_cpu_data = &data->def_cpu_data;
}
Expand All @@ -861,8 +872,12 @@ static void set_thread_affinity(thread_data_t *data, cpuset_data_t *cpu_data)
if (actual_cpu_data->cpuset == NULL)
actual_cpu_data = &data->def_cpu_data;

if (!CPU_EQUAL(actual_cpu_data->cpuset, data->curr_cpu_data->cpuset))
{
if (
actual_cpu_data->cpusetsize != data->curr_cpu_data->cpusetsize ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Witht this PR, cpusetsize is always set to sizeof(cpu_set_t) so this test is useless

!CPU_EQUAL_S(
actual_cpu_data->cpusetsize,
actual_cpu_data->cpuset, data->curr_cpu_data->cpuset))
{
log_debug("[%d] setting cpu affinity to CPU(s) %s", data->ind,
actual_cpu_data->cpuset_str);
ret = pthread_setaffinity_np(pthread_self(),
Expand Down
6 changes: 3 additions & 3 deletions src/rt-app_parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,17 @@ static void parse_cpuset_data(struct json_object *obj, cpuset_data_t *data)
data->cpuset_str = strdup(json_object_to_json_string(cpuset_obj));
data->cpusetsize = sizeof(cpu_set_t);
data->cpuset = malloc(data->cpusetsize);
CPU_ZERO(data->cpuset);
CPU_ZERO_S(data->cpusetsize, data->cpuset);
for (i = 0; i < json_object_array_length(cpuset_obj); i++) {
cpu = json_object_array_get_idx(cpuset_obj, i);
cpu_idx = json_object_get_int(cpu);
if (cpu_idx > max_cpu) {
log_critical(PIN2 "Invalid cpu %u in cpuset %s", cpu_idx, data->cpuset_str);
free(data->cpuset);
CPU_FREE(data->cpuset);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have data->cpuset = malloc(data->cpusetsize); few lines above so why replacing free() by CPU_FREE() ?

we should use the same alloc function everywhere: CPU_ALLOC(MAX_CPUS); and CPU_ALLOC_SIZE(MAX_CPUS);

free(data->cpuset_str);
exit(EXIT_INV_CONFIG);
}
CPU_SET(cpu_idx, data->cpuset);
CPU_SET_S(cpu_idx, data->cpusetsize, data->cpuset);
}
} else {
data->cpuset_str = strdup("-");
Expand Down