aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lfalloc/alloc_profiler/stackcollect.cpp
blob: d608803e8473cd9b8552e4e7a1c3967c64f4c532 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "stackcollect.h"

#include "profiler.h"

#include <util/generic/algorithm.h>
#include <util/generic/vector.h>
#include <util/stream/format.h>
#include <util/stream/str.h> 
#include <util/string/cast.h> 
#include <util/string/printf.h> 
#include <util/system/backtrace.h>
#include <util/system/spinlock.h>
#include <util/system/yassert.h>


namespace NAllocProfiler { 
 
////////////////////////////////////////////////////////////////////////////////

template <typename T>
class TStackCollector: private TNonCopyable {
public:
    struct TFrameInfo {
        int PrevInd;
        void* Addr;
        int Tag;
        T Stats;

        void Clear()
        {
            PrevInd = 0;
            Addr = nullptr;
            Tag = 0;
            Stats.Clear();
        }
    };

private:
    static const size_t STACKS_HASH_MAP_SIZE = 256 * 1024;
    TFrameInfo Frames[STACKS_HASH_MAP_SIZE];

    ui64 Samples;            // Saved samples count
    ui64 UniqueSamples;      // Number of unique addresses
    ui64 UsedSlots;          // Number of occupied slots in the hashtable
    ui64 DroppedSamples;     // Number of unsaved addresses
    ui64 SearchSkipCount;    // Total number of linear hash table probes due to collisions

    TAdaptiveLock Lock;

public:
    TStackCollector()
    {
        Clear();
    }

    int AddStack(void** stack, size_t frameCount, int tag)
    {
        Y_ASSERT(frameCount > 0);

        int prevInd = -1;
        with_lock (Lock) {
            for (int i = frameCount - 1; i >= 0; --i) {
                prevInd = AddFrame(stack[i], prevInd, ((i == 0) ? tag : 0), (i == 0));
                if (prevInd == -1) {
                    break;
                }
            }
        }
        return prevInd;
    }

    T& GetStats(int stackId)
    {
        Y_ASSERT(stackId >= 0 && (size_t)stackId < Y_ARRAY_SIZE(Frames));
        Y_ASSERT(!IsSlotEmpty(stackId));

        return Frames[stackId].Stats;
    }

    const TFrameInfo* GetFrames() const
    {
        return Frames;
    }

    size_t GetFramesCount() const
    {
        return Y_ARRAY_SIZE(Frames);
    }

    void BackTrace(const TFrameInfo* stack, TStackVec<void*, 64>& frames) const 
    {
        frames.clear(); 
        for (size_t i = 0; i < 100; ++i) {
            frames.push_back(stack->Addr); 
            int prevInd = stack->PrevInd;
            if (prevInd == -1) {
                break;
            }
            stack = &Frames[prevInd];
        }
    }

    void Clear()
    {
        for (auto& frame: Frames) {
            frame.Clear();
        }

        Samples = 0;
        DroppedSamples = 0;
        UniqueSamples = 0;
        UsedSlots = 0;
        SearchSkipCount = 0;
    }

private:
    // Hash function applied to the addresses
    static ui32 Hash(void* addr, int prevInd, int tag)
    {
        return (((size_t)addr + ((size_t)addr / STACKS_HASH_MAP_SIZE)) + prevInd + tag) % STACKS_HASH_MAP_SIZE;
    }

    static bool EqualFrame(const TFrameInfo& frame, void* addr, int prevInd, int tag)
    {
        return (frame.Addr == addr && frame.PrevInd == prevInd && frame.Tag == tag);
    }

    bool IsSlotEmpty(ui32 slot) const
    {
        return Frames[slot].Addr == 0;
    }

    bool InsertsAllowed() const
    {
        return UsedSlots < STACKS_HASH_MAP_SIZE / 2;
    }

    // returns the index in the hashmap
    int AddFrame(void* addr, int prevFrameIndex, int tag, bool last)
    {
        ui32 slot = Hash(addr, prevFrameIndex, tag);
        ui32 prevSlot = (slot - 1) % STACKS_HASH_MAP_SIZE;

        while (!EqualFrame(Frames[slot], addr, prevFrameIndex, tag) && !IsSlotEmpty(slot) && slot != prevSlot) {
            slot = (slot + 1) % STACKS_HASH_MAP_SIZE;
            SearchSkipCount++;
        }

        if (EqualFrame(Frames[slot], addr, prevFrameIndex, tag)) {
            if (last) {
                ++Samples;
            }
        } else if (InsertsAllowed() && IsSlotEmpty(slot)) {
            // add new sample
            Frames[slot].Clear();
            Frames[slot].Addr = addr;
            Frames[slot].PrevInd = prevFrameIndex;
            Frames[slot].Tag = tag;
            ++UsedSlots;
            if (last) {
                ++UniqueSamples;
                ++Samples;
            }
        } else {
            // don't insert new sample if the search is becoming too slow
            ++DroppedSamples;
            return -1;
        }

        return slot;
    }
};


////////////////////////////////////////////////////////////////////////////////

class TAllocationStackCollector::TImpl: public TStackCollector<TStats> { 
    using TBase = TStackCollector<TStats>; 

private:
    TStats Total; 

public:
    int Alloc(void** stack, size_t frameCount, int tag, size_t size)
    {
        int stackId = TBase::AddStack(stack, frameCount, tag);
        if (stackId >= 0) {
            TBase::GetStats(stackId).Alloc(size);
            Total.Alloc(size);
        }
        return stackId;
    }

    void Free(int stackId, size_t size)
    {
        TBase::GetStats(stackId).Free(size);
        Total.Free(size);
    }

    void Clear()
    {
        TBase::Clear();
        Total.Clear();
    }

    void Dump(int count, IAllocationStatsDumper& out) const 
    {
        const TFrameInfo* frames = TBase::GetFrames();
        size_t framesCount = TBase::GetFramesCount();

        TVector<const TFrameInfo*> stacks;
        for (size_t i = 0; i < framesCount; ++i) {
            if (frames[i].Stats.Allocs) {
                stacks.push_back(&frames[i]);
            }
        }

        Sort(stacks, [] (const TFrameInfo* l, const TFrameInfo* r) {
            const auto& ls = l->Stats;
            const auto& rs = r->Stats;
            return ls.CurrentSize != rs.CurrentSize
                ? ls.CurrentSize > rs.CurrentSize
                : ls.Allocs != rs.Allocs
                    ? ls.Allocs > rs.Allocs
                    : ls.Frees > rs.Frees;
        });

        out.DumpTotal(Total); 

        TAllocationInfo allocInfo; 
        int printedCount = 0;
        for (const TFrameInfo* stack: stacks) {
            allocInfo.Clear(); 
            allocInfo.Tag = stack->Tag; 
            allocInfo.Stats = stack->Stats; 
            TBase::BackTrace(stack, allocInfo.Stack); 

            out.DumpEntry(allocInfo); 
 
            if (++printedCount >= count) {
                break;
            }
        }
    }
};

////////////////////////////////////////////////////////////////////////////////

TAllocationStackCollector::TAllocationStackCollector()
    : Impl(new TImpl())
{}

TAllocationStackCollector::~TAllocationStackCollector()
{}

int TAllocationStackCollector::Alloc(void** stack, size_t frameCount, int tag, size_t size)
{
    return Impl->Alloc(stack, frameCount, tag, size);
}

void TAllocationStackCollector::Free(int stackId, size_t size)
{
    Impl->Free(stackId, size);
}

void TAllocationStackCollector::Clear()
{
    Impl->Clear();
}

void TAllocationStackCollector::Dump(int count, IAllocationStatsDumper &out) const 
{
    Impl->Dump(count, out);
}

 
TString IAllocationStatsDumper::FormatTag(int tag) { 
    return ToString(tag); 
} 
 
TString IAllocationStatsDumper::FormatSize(intptr_t sz) { 
    return ToString(sz); 
} 
 
 
TAllocationStatsDumper::TAllocationStatsDumper(IOutputStream& out) 
    : PrintedCount(0) 
    , Out(out) 
    , SymbolCache(2048) 
{} 
 
void TAllocationStatsDumper::DumpTotal(const TStats& total) { 
    Out << "TOTAL" 
        << "\tAllocs: " << total.Allocs 
        << "\tFrees: " << total.Frees 
        << "\tCurrentSize: " << FormatSize(total.CurrentSize) 
        << Endl; 
} 
 
void TAllocationStatsDumper::DumpEntry(const TAllocationInfo& allocInfo) { 
    Out << Endl 
        << "STACK #" << PrintedCount+1 << ": " << FormatTag(allocInfo.Tag) 
        << "\tAllocs: " << allocInfo.Stats.Allocs 
        << "\tFrees: " << allocInfo.Stats.Frees 
        << "\tCurrentSize: " << FormatSize(allocInfo.Stats.CurrentSize) 
        << Endl; 
    FormatBackTrace(allocInfo.Stack.data(), allocInfo.Stack.size()); 
    PrintedCount++; 
} 
 
void TAllocationStatsDumper::FormatBackTrace(void* const* stack, size_t sz) { 
    char name[1024]; 
    for (size_t i = 0; i < sz; ++i) { 
        TSymbol symbol; 
        auto it = SymbolCache.Find(stack[i]); 
        if (it != SymbolCache.End()) { 
            symbol = it.Value(); 
        } else { 
            TResolvedSymbol rs = ResolveSymbol(stack[i], name, sizeof(name)); 
            symbol = {rs.NearestSymbol, rs.Name}; 
            SymbolCache.Insert(stack[i], symbol); 
        } 
 
        Out << Hex((intptr_t)stack[i], HF_FULL) << "\t" << symbol.Name; 
        intptr_t offset = (intptr_t)stack[i] - (intptr_t)symbol.Address; 
        if (offset) 
            Out << " +" << offset; 
        Out << Endl; 
    } 
} 
 
}   // namespace NAllocProfiler