debug_info.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "debug_info.h"
  2. #include <util/system/thread.h>
  3. #include <util/system/tls.h>
  4. #include <util/stream/file.h>
  5. #include <util/generic/string.h>
  6. #include <string.h>
  7. namespace NYql {
  8. static const size_t OPERATION_ID_MAX_LENGTH = 24;
  9. static const size_t THREAD_NAME_MAX_LENGTH = 16;
  10. struct TDebugInfo {
  11. char OperationId[OPERATION_ID_MAX_LENGTH + 1];
  12. };
  13. Y_POD_THREAD(TDebugInfo) TlsDebugInfo;
  14. void SetCurrentOperationId(const char* operationId) {
  15. size_t len = strlcpy(
  16. (&TlsDebugInfo)->OperationId,
  17. operationId,
  18. OPERATION_ID_MAX_LENGTH);
  19. const char* threadName = nullptr;
  20. if (len > THREAD_NAME_MAX_LENGTH) {
  21. threadName = operationId + (len - THREAD_NAME_MAX_LENGTH + 1);
  22. } else {
  23. threadName = operationId;
  24. }
  25. TThread::SetCurrentThreadName(threadName);
  26. }
  27. long GetRunnigThreadsCount() {
  28. TString procStat = TFileInput("/proc/self/stat").ReadAll();
  29. long num_threads = -2; // Number of threads in this process (since Linux 2.6)
  30. int n = sscanf(procStat.data(),
  31. "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld",
  32. &num_threads);
  33. return n == 1 ? num_threads : -2;
  34. }
  35. } // namespace NYql