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

Spawn io_service threads according to number cpu cores #878

Closed
windycrypto opened this issue Apr 26, 2018 · 7 comments
Closed

Spawn io_service threads according to number cpu cores #878

windycrypto opened this issue Apr 26, 2018 · 7 comments
Assignees

Comments

@windycrypto
Copy link
Member

windycrypto commented Apr 26, 2018

The default_io_service_scope fixedly spawns 8 threads for io_servcie, while with cpu less than 8 cores, performance may discount due to context switches.

So the good practice maybe detecting host's cpu core number at building stage, and spawning the corresponding threads, i.e. io_service-per-core.

btw, seems the threads of io_service mainly used for serve websocket client now, actually it serves a natural thread pool, maybe we can use it doing p2p stuff, but this is another issue, I will open another for talking

@abitmore
Copy link
Member

detecting host's cpu core number at building stage

No, because not everyone is building on production server.

Better if we can detect it at runtime, or alternatively, add a configurable parameter.

@jmjatlanta
Copy link
Contributor

std::thread::hardware_concurrency() gives you a guess, although it is not accurate on all platforms. Most implementations I've seen rely on this method. And if it returns 0, 2 is used.

IMO: use the logic above, and let configuration parameters (config file or command line) override it.

@marcialvieira
Copy link
Contributor

@jmjatlanta boost has boost::thread::hardware_concurrency too, but I don't know if it has the same problem.

@windycrypto
Copy link
Member Author

Better if we can detect it at runtime, or alternatively, add a configurable parameter.

Yeah, I mean through compiler flag, macro defined, etc.. pass the cpu cores to code, substitute the constant 8

@jmjatlanta
Copy link
Contributor

What about: In absence of a configurable parameter,
std::max(std::thread::hardware_concurrency() - 1, 2)

That will almost always give 1 free thread for other things, and will scale with number of cores.

@marcialvieira
Copy link
Contributor

marcialvieira commented May 11, 2018

I'm still thinking it's best to use the boost implementation instead of std.

https://github.com/steinwurf/boost/blob/master/libs/thread/src/pthread/thread.cpp#L549
https://gcc.gnu.org/onlinedocs/gcc-4.6.2/libstdc++/api/a01083_source.html

Now I see, boost::thread::physical_concurrency() too.

@jmjatlanta jmjatlanta self-assigned this May 16, 2018
@abitmore abitmore added this to the 201805 - Non-Consensus-Changing Release milestone May 20, 2018
abitmore added a commit that referenced this issue May 20, 2018
Issue #878 permit configuration of io threads
@abitmore
Copy link
Member

Done with #927.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants