aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/malloc/threadpool.hpp
blob: 7e86c3f691a1c171f7b7a0b1cf320d2a92de2c24 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespamalloc/malloc/threadpool.h>

namespace vespamalloc {

template <typename MemBlockPtrT, typename ThreadStatT>
size_t ThreadPoolT<MemBlockPtrT, ThreadStatT>::_threadCacheLimit __attribute__((visibility("hidden"))) = 0x10000;

template <typename MemBlockPtrT, typename ThreadStatT>
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::info(FILE * os, size_t level, const DataSegment<MemBlockPtrT> & ds) const {
    if (level > 0) {
        for (size_t i=0; i < NELEMS(_stat); i++) {
            const ThreadStatT & s = _stat[i];
            const AllocFree & af = _memList[i];
            if (s.isUsed()) {
                size_t localAvailCount((af._freeTo ? af._freeTo->count() : 0)
                                       + (af._allocFrom ? af._allocFrom->count() : 0));
                fprintf(os, "SC %2ld(%10ld) Local(%3ld) Alloc(%10ld), "
                        "Free(%10ld) ExchangeAlloc(%8ld), ExChangeFree(%8ld) "
                        "Returned(%8ld) ExactAlloc(%8ld)\n",
                        i, MemBlockPtrT::classSize(i), localAvailCount,
                        s.alloc(), s.free(), s.exchangeAlloc(),
                        s.exchangeFree(), s.returnFree(), s.exactAlloc());
            }
        }
    }
    if (level > 2) {
        fprintf(os, "BlockList:%ld,%ld,%ld\n", NELEMS(_stat), sizeof(_stat), sizeof(_stat[0]));
        size_t sum(0), sumLocal(0);
        for (size_t i=0; i < NELEMS(_stat); i++) {
            const ThreadStatT & s = _stat[i];
            if (s.isUsed()) {
                fprintf(os, "Allocated Blocks SC %2ld(%10ld): ", i, MemBlockPtrT::classSize(i));
                size_t allocCount = ds.infoThread(os, level, threadId(), i);
                const AllocFree & af = _memList[i];
                size_t localAvailCount((af._freeTo ? af._freeTo->count() : 0)
                                       + (af._allocFrom ? af._allocFrom->count() : 0));
                sum += allocCount*MemBlockPtrT::classSize(i);
                sumLocal += localAvailCount*MemBlockPtrT::classSize(i);
                fprintf(os, " Total used(%ld + %ld = %ld(%ld)).\n",
                        allocCount, localAvailCount, localAvailCount+allocCount,
                        (localAvailCount+allocCount)*MemBlockPtrT::classSize(i));
            }
        }
        fprintf(os, "Sum = (%ld + %ld) = %ld\n", sum, sumLocal, sum+sumLocal);
    }
}

template <typename MemBlockPtrT, typename ThreadStatT >
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::
mallocHelper(size_t exactSize,
             SizeClassT sc,
             typename ThreadPoolT<MemBlockPtrT, ThreadStatT>::AllocFree & af,
             MemBlockPtrT & mem)
{
    if (!af._freeTo->empty()) {
        af.swap();
        af._allocFrom->sub(mem);
        PARANOID_CHECK2( if (!mem.ptr()) { *(int *)0 = 0; } );
    } else {
        if ( ! alwaysReuse(sc) ) {
            af._allocFrom = _allocPool->exchangeAlloc(sc, af._allocFrom);
            _stat[sc].incExchangeAlloc();
            if (af._allocFrom) {
                af._allocFrom->sub(mem);
                PARANOID_CHECK2( if (!mem.ptr()) { *(int *)1 = 1; } );
            } else {
                PARANOID_CHECK2( *(int *)2 = 2; );
            }
        } else {
            af._allocFrom = _allocPool->exactAlloc(exactSize, sc, af._allocFrom);
            _stat[sc].incExactAlloc();
            if (af._allocFrom) {
                af._allocFrom->sub(mem);
                PARANOID_CHECK2( if (!mem.ptr()) { *(int *)3 = 3; } );
            } else {
                PARANOID_CHECK2( *(int *)4 = 4; );
            }
        }
    }
}

template <typename MemBlockPtrT, typename ThreadStatT >
ThreadPoolT<MemBlockPtrT, ThreadStatT>::ThreadPoolT() :
    _allocPool(nullptr),
    _threadId(0),
    _osThreadId(0)
{
}

template <typename MemBlockPtrT, typename ThreadStatT >
ThreadPoolT<MemBlockPtrT, ThreadStatT>::~ThreadPoolT() = default;

template <typename MemBlockPtrT, typename ThreadStatT >
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::malloc(size_t sz, MemBlockPtrT & mem)
{
    SizeClassT sc = MemBlockPtrT::sizeClass(sz);
    AllocFree & af = _memList[sc];
    af._allocFrom->sub(mem);
    if ( !mem.ptr()) {
        mallocHelper(sz, sc, af, mem);
    }
    PARANOID_CHECK2(if (!mem.validFree()) { *(int *)1 = 1; } );
    _stat[sc].incAlloc();
    mem.setThreadId(_threadId);
    PARANOID_CHECK2(if (af._allocFrom->count() > ChunkSList::NumBlocks) { *(int *)1 = 1; } );
    PARANOID_CHECK2(if (af._freeTo->count() > ChunkSList::NumBlocks) { *(int *)1 = 1; } );
    PARANOID_CHECK2(if (af._freeTo->full()) { *(int *)1 = 1; } );
    PARANOID_CHECK2(if (af._allocFrom->full()) { *(int *)1 = 1; } );
}

template <typename MemBlockPtrT, typename ThreadStatT >
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::free(MemBlockPtrT mem, SizeClassT sc)
{
    PARANOID_CHECK2(if (!mem.validFree()) { *(int *)1 = 1; } );
    AllocFree & af = _memList[sc];
    const size_t cs(MemBlockPtrT::classSize(sc));
    if ((af._allocFrom->count()+1)*cs < _threadCacheLimit) {
        if ( ! af._allocFrom->full() ) {
            af._allocFrom->add(mem);
        } else {
            af._freeTo->add(mem);
            if (af._freeTo->full()) {
                af._freeTo = _allocPool->exchangeFree(sc, af._freeTo);
                _stat[sc].incExchangeFree();
            }
        }
    } else if (cs < _threadCacheLimit) {
        af._freeTo->add(mem);
        if (af._freeTo->count()*cs > _threadCacheLimit) {
            af._freeTo = _allocPool->exchangeFree(sc, af._freeTo);
            _stat[sc].incExchangeFree();
        }
    } else if ( !alwaysReuse(sc) ) {
        af._freeTo->add(mem);
        af._freeTo = _allocPool->exchangeFree(sc, af._freeTo);
        _stat[sc].incExchangeFree();
    } else {
        af._freeTo->add(mem);
        af._freeTo = _allocPool->returnMemory(sc, af._freeTo);
        _stat[sc].incReturnFree();
    }

    _stat[sc].incFree();
    PARANOID_CHECK2(if (af._allocFrom->count() > ChunkSList::NumBlocks) { *(int *)1 = 1; } );
    PARANOID_CHECK2(if (af._freeTo->count() > ChunkSList::NumBlocks) { *(int *)1 = 1; } );
    PARANOID_CHECK2(if (af._freeTo->full()) { *(int *)1 = 1; } );
}

template <typename MemBlockPtrT, typename ThreadStatT >
bool ThreadPoolT<MemBlockPtrT, ThreadStatT>::isActive() const
{
    return (_osThreadId != 0);
}

template <typename MemBlockPtrT, typename ThreadStatT >
bool ThreadPoolT<MemBlockPtrT, ThreadStatT>::isUsed() const
{
    return isActive() && hasActuallyBeenUsed();
}

template <typename MemBlockPtrT, typename ThreadStatT >
bool ThreadPoolT<MemBlockPtrT, ThreadStatT>::hasActuallyBeenUsed() const
{
    bool used(false);
    for (size_t i=0; !used && (i < NELEMS(_memList)); i++) {
        used = (_memList[i]._allocFrom != nullptr
                && !_memList[i]._allocFrom->empty()
                && !_memList[i]._freeTo->full());
    }
    return used;
}

template <typename MemBlockPtrT, typename ThreadStatT >
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::init(int thrId)
{
    setThreadId(thrId);
    assert(_osThreadId.load(std::memory_order_relaxed) == -1);
    _osThreadId = pthread_self();
    for (size_t i=0; (i < NELEMS(_memList)); i++) {
        _memList[i].init(*_allocPool, i);
    }
    // printf("OsThreadId = %lx, threadId = %x\n", _osThreadId, _threadId);
}

template <typename MemBlockPtrT, typename ThreadStatT >
void ThreadPoolT<MemBlockPtrT, ThreadStatT>::setParams(size_t threadCacheLimit)
{
    _threadCacheLimit = threadCacheLimit;
}

template <typename MemBlockPtrT, typename ThreadStatT >
bool ThreadPoolT<MemBlockPtrT, ThreadStatT>::grabAvailable()
{
    if (_osThreadId.load(std::memory_order_relaxed) == 0) {
        ssize_t expected = 0;
        if (_osThreadId.compare_exchange_strong(expected, -1)) {
            return true;
        }
    }
    return false;
}

}