aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lfalloc/dbg_info/dbg_info.h
blob: 071562a81abcf9af1370452f82dfe6264e4b62c4 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once

#include <util/generic/ptr.h>
#include <util/system/types.h>

namespace NAllocDbg {
    ////////////////////////////////////////////////////////////////////////////////
    // Allocation statistics

    enum ELFAllocCounter {
        CT_USER_ALLOC,     // accumulated size requested by user code
        CT_MMAP,           // accumulated mmapped size
        CT_MMAP_CNT,       // number of mmapped regions
        CT_MUNMAP,         // accumulated unmmapped size
        CT_MUNMAP_CNT,     // number of munmaped regions
        CT_SYSTEM_ALLOC,   // accumulated allocated size for internal lfalloc needs
        CT_SYSTEM_FREE,    // accumulated deallocated size for internal lfalloc needs
        CT_SMALL_ALLOC,    // accumulated allocated size for fixed-size blocks
        CT_SMALL_FREE,     // accumulated deallocated size for fixed-size blocks
        CT_LARGE_ALLOC,    // accumulated allocated size for large blocks
        CT_LARGE_FREE,     // accumulated deallocated size for large blocks
        CT_SLOW_ALLOC_CNT, // number of slow (not LF) allocations
        CT_DEGRAGMENT_CNT, // number of memory defragmentations
        CT_MAX
    };

    i64 GetAllocationCounterFast(ELFAllocCounter counter);
    i64 GetAllocationCounterFull(ELFAllocCounter counter);

    ////////////////////////////////////////////////////////////////////////////////
    // Allocation statistics could be tracked on per-tag basis

    int SetThreadAllocTag(int tag);

    class TScopedTag {
    private:
        int PrevTag;

    public:
        explicit TScopedTag(int tag) {
            PrevTag = SetThreadAllocTag(tag);
        }

        ~TScopedTag() {
            SetThreadAllocTag(PrevTag);
        }
    };

    struct TPerTagAllocInfo {
        ssize_t Count;
        ssize_t Size;
    };

    TArrayPtr<TPerTagAllocInfo> GetPerTagAllocInfo(
        bool flushPerThreadCounters,
        int& maxTag,
        int& numSizes);

    ////////////////////////////////////////////////////////////////////////////////
    // Allocation sampling could be used to collect detailed information

    bool SetProfileCurrentThread(bool newVal);
    bool SetProfileAllThreads(bool newVal);
    bool SetAllocationSamplingEnabled(bool newVal);

    size_t SetAllocationSampleRate(size_t newVal);
    size_t SetAllocationSampleMaxSize(size_t newVal);

#define DBG_ALLOC_INVALID_COOKIE (-1)

    using TAllocationCallback = int(int tag, size_t size, int sizeIdx);
    using TDeallocationCallback = void(int cookie, int tag, size_t size, int sizeIdx);

    TAllocationCallback* SetAllocationCallback(TAllocationCallback* newVal);
    TDeallocationCallback* SetDeallocationCallback(TDeallocationCallback* newVal);

}