Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@deweerdt
Copy link
Member

@deweerdt deweerdt commented Mar 26, 2019

Modify num-threads to accept a sequence. If passed a sequence, num-threads will treat it as a list of CPUs to bind to, instantiation one thread per CPU.

@kazuho
Copy link
Member

kazuho commented Mar 26, 2019

Thank you for the PR. I think this is a nice enhancement.

That said, I think we might want to consider extending num_threads. Because I'd assume that what you'd actually want to do is to pin each of our worker threads to each of the specified CPU core. Or do you have a different use-case in mind?

Regarding listing the CPU cores, I think we might want to use a YAML sequence here. Just naming all the CPU core IDs in numbers could be fine, or if we want to support range expressions, we could support YAML expression like [0, 3-6, 9] (in which we parse "3-6" as a single range).

@deweerdt
Copy link
Member Author

That's a good idea, thanks for suggesting it. IIUC, num-threads would take two kinds of values:

  • a single integer, in which case it designates a the number of threads to spawn
  • a sequence, in which case the number of threads will be specified by the number of CPUs in the sequence, and each thread will be assigned to one CPU

Is that correct?

`num-threads` will treat it as a list of CPUs to bind to, instantiation
one thread per CPU.
@deweerdt
Copy link
Member Author

@kazuho how does 2147a98 look?

Copy link
Member

@kazuho kazuho left a comment

Choose a reason for hiding this comment

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

Thank you for the changes. I've read the code, I think the PR would be ready for merge once we resolve the issues below.

src/main.c Outdated
}
return 0;
InvalidSeq:
h2o_configurator_errprintf(cmd, node, "cpus must be specified as sequence of positive integers or a ranges low-high");
Copy link
Member

Choose a reason for hiding this comment

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

I would appreciate it if you could generate more specific errors, by calling h2o_configurator_err_printf(cmd, node->data.sequence.elements + i, ...) within the loop.

That would provide users the actual location (i.e. line number and column number) where the error exists.

src/main.c Outdated
}
}
#else /* H2O_HAS_PTHREAD_SETAFFINITY_NP */
fprintf(stderr, "[warning] ignoring CPU list, this platform doesn't support `pthread_setaffinity_np`\n");
Copy link
Member

Choose a reason for hiding this comment

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

Can we generate the error in on_config_num_threads? Then we can guide users how to address the issue.

src/main.c Outdated
*num_threads += 1;
(*cpu_list) = realloc(*cpu_list, sizeof(**cpu_list) * (*num_threads + 1));
(*cpu_list)[*num_threads - 1] = cpu;
(*cpu_list)[*num_threads] = -1;
Copy link
Member

Choose a reason for hiding this comment

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

Are you concerned about having overruns here?

I ask this because, if you are concerned about that, then I might suggest doing a refactor. Replace conf.num_threads and conf.cpu_list with a H2O_VECTOR(int). The size of the vector indicates the number of threads, and each value of the vector indicates the CPU id (if non-negative) to be pinned to, or if it's not pinned (if -1). By taking that approach, there would be no fear of losing synchronization between num_threads and cpu_list, and you'd also be able to use things like h2o_vector_reserve.

I am not saying that we should make such change, but it's generally the case that I prefer avoiding having extra complexity for making the code "fail-safe."

array that maps threads to CPUs.
- Have specific error messages
- Error out in num-threads if pthread_setaffinity_np is not available
@deweerdt
Copy link
Member Author

Thanks for your review @kazuho ! I believe 71a3b71 addresses all your comments so far.

Copy link
Member

@kazuho kazuho left a comment

Choose a reason for hiding this comment

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

Thank you very much for the changes, I like how isolated yet integrated the code that deal with the feature is.

Below are my refinement suggestions. Please let me know what you think.

@deweerdt
Copy link
Member Author

Thanks for the review, @kazuho I believe ab3d087 addresses all the comments you've made so far. I've also used an intermediate variable instead of accessing node->data.sequence.elements[i] directly.

@kazuho kazuho merged commit ab3d087 into h2o:master Apr 15, 2019
kazuho added a commit that referenced this pull request Apr 15, 2019
[configuration] Allow to specify a list of CPUs to bind H2O to
@kazuho
Copy link
Member

kazuho commented Apr 15, 2019

Thank you for the changes. I've merged the PR with some tweaks. I believe that the changes are not disputable (mostly refactor and editorial changes), but please let me know if you have concerns.

@deweerdt
Copy link
Member Author

Thank you for the changes. I've merged the PR with some tweaks. I believe that the changes are not disputable (mostly refactor and editorial changes), but please let me know if you have concerns.

Changes look great to me, thank you.

@deweerdt deweerdt deleted the cpu-list branch April 16, 2019 04:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants