aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/debug_info.cpp
blob: ff56166695ec796ef12990ee0c85ce33e8637f60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "debug_info.h"

#include <util/system/thread.h>
#include <util/system/tls.h>
#include <util/stream/file.h>
#include <util/generic/string.h>

#include <string.h>


namespace NYql {

static const size_t OPERATION_ID_MAX_LENGTH = 24;
static const size_t THREAD_NAME_MAX_LENGTH = 16;


struct TDebugInfo {
    char OperationId[OPERATION_ID_MAX_LENGTH + 1];
};

Y_POD_THREAD(TDebugInfo) TlsDebugInfo;


void SetCurrentOperationId(const char* operationId) {
    size_t len = strlcpy(
            (&TlsDebugInfo)->OperationId,
            operationId,
            OPERATION_ID_MAX_LENGTH);

    const char* threadName = nullptr;
    if (len > THREAD_NAME_MAX_LENGTH) {
        threadName = operationId + (len - THREAD_NAME_MAX_LENGTH + 1);
    } else {
        threadName = operationId;
    }
    TThread::SetCurrentThreadName(threadName);
}

long GetRunnigThreadsCount() {
    TString procStat = TFileInput("/proc/self/stat").ReadAll();
    long num_threads = -2;         // Number of threads in this process (since Linux 2.6)

    int n = sscanf(procStat.data(),
        "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld",
        &num_threads);

    return n == 1 ? num_threads : -2;
}

} // namespace NYql