pmmp / PocketMine-MP

A server software for Minecraft: Bedrock Edition in PHP
https://pmmp.io
GNU Lesser General Public License v3.0
3.28k stars 1.56k forks source link

Difficult to identify specific threads when tracking down issues #6527

Open dadodasyra opened 1 week ago

dadodasyra commented 1 week ago

Problem description

I'm having trouble debugging who's using 10MBps, I've narrowed down to a SPID (thread pid) but I'm unable to do anything with it because there's no way to know which threads it corresponds in PM.

Proposed solution

As already asked, yes there's issues with the fact that there's no cross platform way (according to Dylan, though @cli_set_process_title may be an answer ?). I think at least there should be a way to get an instance of every threads used in pocketmine through a plugin so we can dump IDs with this. Or even better an in-game Pocketmine command to dump every process ids for admins.

Alternative solutions that don't require API changes

Doesn't require any BC breaks

dktapps commented 1 week ago

You can use ThreadManager to inspect threads created directly by the main thread, which include lots of useful functions to do what you want. Assuming that the thread you're looking for was started directly by the main thread, anyway.

dktapps commented 1 week ago

though @cli_set_process_title may be an answer

No, this doesn't work on individual threads.

pmmpthread could introduce a method to set thread name, but platforms like MacOS only allow a thread to change its own name, so it's a bit annoying to implement.

dktapps commented 1 week ago

Relates to https://github.com/pmmp/ext-pmmpthread/issues/139

dadodasyra commented 1 week ago

though @cli_set_process_title may be an answer

No, this doesn't work on individual threads.

Isn't @cli_set_process_title already used in PM by Console reader?

dadodasyra commented 1 week ago

You can use ThreadManager to inspect threads created directly by the main thread, which include lots of useful functions to do what you want. Assuming that the thread you're looking for was started directly by the main thread, anyway.

I've seen it after my issue, it seems like I'm unable to get a PID from it (currentThreadId seems a long 64bit id), and, weirdly the getcurrentthread returns null for each threads in the ThreadManager.

dktapps commented 1 week ago

though @cli_set_process_title may be an answer

No, this doesn't work on individual threads.

Isn't @cli_set_process_title already used in PM by Console reader?

ConsoleReader isn't a thread

getcurrentthread returns null for each threads in the ThreadManager.

Thread::getCurrentThreadId() returns the ID of the currently executing thread. It's static for a reason.

You should use this function: https://github.com/pmmp/ext-pmmpthread/blob/71cae13851a2ee9cc8558d6e5d0faf8c5bedb36d/stubs/Thread.stub.php#L159

dadodasyra commented 6 days ago

Thread::getCurrentThreadId() returns the ID of the currently executing thread. It's static for a reason.

You should use this function: https://github.com/pmmp/ext-pmmpthread/blob/71cae13851a2ee9cc8558d6e5d0faf8c5bedb36d/stubs/Thread.stub.php#L159

Yes that's my bad I didn't saw it was static, but that's what I was saying, it returns very long ID that I don't really know what they stands for:

        foreach (ThreadManager::getInstance()->getAll() as $thread) {
            var_dump($thread->getThreadName());
            var_dump($thread->getThreadId());
        }

string(13) "AsyncWorker#0"
int(128002130183936)
string(36) "poggit\libasynql\mysqli connector #0"
int(128002115503872)
string(36) "poggit\libasynql\mysqli connector #1"
int(128001985480448)
string(16) "ProxyChildThread"
int(128001819805440)
string(15) "LogsChildThread"
int(128001807222528)
string(4) "RCON"
int(128001792542464)
string(11) "TebexThread"
int(128001779959552)
string(11) "TebexThread"
int(128001631061760)
string(6) "RakLib"
int(128001618478848)
dktapps commented 6 days ago

After doing some digging it looks like pthread_self() isn't the same as the ID shown by htop. Still, this shouldn't be necessary anyway. What's needed is to implement pmmp/ext-pmmpthread#139.