Created
November 11, 2025 18:33
-
-
Save w1redch4d/34a932f3974ecf2a0d2261a068b669e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <pthread.h> | |
| #include <unistd.h> | |
| #include <cstdint> | |
| #define BYTES_TO_MB(x) ((double)(x) / (1024.0 * 1024.0)) | |
| inline void printStackUsage() | |
| { | |
| volatile int localVar; | |
| void* currentStackPtr = (void*)&localVar; | |
| // Get stack info using pthread APIs | |
| pthread_attr_t attr; | |
| pthread_getattr_np(pthread_self(), &attr); | |
| void* stackAddr; | |
| size_t stackSize; | |
| pthread_attr_getstack(&attr, &stackAddr, &stackSize); | |
| pthread_attr_destroy(&attr); | |
| uintptr_t stackBase = reinterpret_cast<uintptr_t>(stackAddr); | |
| uintptr_t stackEnd = stackBase + stackSize; | |
| uintptr_t currentPtr = reinterpret_cast<uintptr_t>(currentStackPtr); | |
| size_t used = 0; | |
| if (currentPtr < stackEnd && currentPtr >= stackBase) | |
| used = stackEnd - currentPtr; | |
| size_t total = stackSize; | |
| std::cout << "Stack used: " << (100.0 * used / total) << "%\n"; | |
| std::cout << "Stack total: " << BYTES_TO_MB(total) << " MB\n\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment