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

🐌 [Performance] Don't spawn new threads in every game loop, just offload it into a ThreadPool #2747

Open
0blu opened this issue Aug 31, 2024 · 4 comments
Labels
feature New feature or request

Comments

@0blu
Copy link
Collaborator

0blu commented Aug 31, 2024

🐌 Performance

Currently a new thread is created when MapManager::Update invokes CreateNewInstancesForPlayers.

std::thread instanceCreationThread = std::thread(&MapManager::CreateNewInstancesForPlayers, this);

Thread creation should be avoided. Especially that often and for such a small workload.
Its better to off load it into a ThreadPool which accepts small tasks like this and processes them with a pre-existing thread.

@0blu 0blu added the feature New feature or request label Aug 31, 2024
@Wall-core
Copy link
Contributor

good change, as I've had (very rare) cases where TBB malloc fails when creating a new thread for this purpose and crashes (without crashing mangos)

@0blu
Copy link
Collaborator Author

0blu commented Aug 31, 2024

Interesting that no one catched this earlier.
Creating native system thread is super workload heavy.

I think this if one of the smaller things that I will tackle after native branch.
Right now there is no ThreadPool for small tasks in mangos.
The class that is called ThreadPool is a more like a BroadcastThreadPool which executes the same workload on different threads.

Edit: Okay tests have shown, its not that slow (~0.07ms creation time, ~0.20ms until thread actually starts) but we should keep this in mind for the future. (every microsecond counts when dealing with the game loop)

@Stoabrogga
Copy link
Contributor

I run Vmangos in GDB and it spammed my log file with thousands of "New Thread" messages:

log entries
Restoring deleted items to players ...

>> Restored 0 previously deleted items.
[New Thread 0x7fffc7ee4640 (LWP 4643)]
[New Thread 0x7fffc76e3640 (LWP 4644)]

==========================================================
Current content is set to Patch 1.12: Drums of War.
Supported client build is set to 5875.
==========================================================

World initialized.
SERVER STARTUP TIME: 0 minutes 4 seconds
[New Thread 0x7fffc6ee2640 (LWP 4645)]
[New Thread 0x7fffc66e1640 (LWP 4646)]

>> Only expired mails (need to be return or delete) or DB table `mail` is empty.
[New Thread 0x7fffc5ee0640 (LWP 4647)]
[New Thread 0x7fffc56df640 (LWP 4648)]

mangos>[New Thread 0x7fffc4ede640 (LWP 4649)]
Max allowed socket connections 1048576
[New Thread 0x7fffbcffd640 (LWP 4650)]
[New Thread 0x7fffabfff640 (LWP 4651)]
[New Thread 0x7fffab7fe640 (LWP 4652)]
[New Thread 0x7fffaaffd640 (LWP 4653)]
[Thread 0x7fffabfff640 (LWP 4651) exited]
[New Thread 0x7fffabfff640 (LWP 4654)]
[Thread 0x7fffabfff640 (LWP 4654) exited]
[New Thread 0x7fffabfff640 (LWP 4655)]
[Thread 0x7fffabfff640 (LWP 4655) exited]
[New Thread 0x7fffabfff640 (LWP 4656)]
[Thread 0x7fffabfff640 (LWP 4656) exited]
[New Thread 0x7fffabfff640 (LWP 4657)]
[Thread 0x7fffabfff640 (LWP 4657) exited]
[New Thread 0x7fffabfff640 (LWP 4658)]
[Thread 0x7fffabfff640 (LWP 4658) exited]
[New Thread 0x7fffabfff640 (LWP 4659)]
[Thread 0x7fffabfff640 (LWP 4659) exited]
[New Thread 0x7fffabfff640 (LWP 4660)]
[Thread 0x7fffabfff640 (LWP 4660) exited]
[New Thread 0x7fffabfff640 (LWP 4661)]
[Thread 0x7fffabfff640 (LWP 4661) exited]
[New Thread 0x7fffabfff640 (LWP 4662)]
[Thread 0x7fffabfff640 (LWP 4662) exited]
[New Thread 0x7fffabfff640 (LWP 4663)]
[Thread 0x7fffabfff640 (LWP 4663) exited]

Is this the same issue? I had to disable thread-event logging by adding this GDB parameter:

-ex "set print thread-events off"

@0blu
Copy link
Collaborator Author

0blu commented Sep 1, 2024

Is this the same issue? I had to disable thread-event logging by adding this GDB parameter:

Yes, this is also the way how I discovered it.
I was working with thread_static and realized that the constructor is called for every game tick.

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

No branches or pull requests

3 participants