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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
#include "ref_counted_tracker.h"
#include <util/system/type_name.h>
#include <yt/yt/core/concurrency/thread_affinity.h>
#include <library/cpp/yt/string/format.h>
#include <library/cpp/yt/misc/tls.h>
#include <algorithm>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
TRefCountedTrackerStatistics::TStatistics& TRefCountedTrackerStatistics::TStatistics::operator+= (
const TRefCountedTrackerStatistics::TStatistics& rhs)
{
ObjectsAllocated += rhs.ObjectsAllocated;
ObjectsFreed += rhs.ObjectsFreed;
ObjectsAlive += rhs.ObjectsAlive;
BytesAllocated += rhs.BytesAllocated;
BytesFreed += rhs.BytesFreed;
BytesAlive += rhs.BytesAlive;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
bool TRefCountedTracker::TKey::operator==(const TKey& other) const
{
return
TypeKey == other.TypeKey &&
Location == other.Location;
}
bool TRefCountedTracker::TKey::operator<(const TKey& other) const
{
if (TypeKey < other.TypeKey) {
return true;
}
if (other.TypeKey < TypeKey) {
return false;
}
if (Location < other.Location) {
return true;
}
if (other.Location < Location) {
return false;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
TRefCountedTracker::TNamedSlot::TNamedSlot(const TKey& key, size_t objectSize)
: Key_(key)
, ObjectSize_(objectSize)
{ }
TRefCountedTypeKey TRefCountedTracker::TNamedSlot::GetTypeKey() const
{
return Key_.TypeKey;
}
const TSourceLocation& TRefCountedTracker::TNamedSlot::GetLocation() const
{
return Key_.Location;
}
TString TRefCountedTracker::TNamedSlot::GetTypeName() const
{
return TypeName(*GetTypeKey().Underlying());
}
TString TRefCountedTracker::TNamedSlot::GetFullName() const
{
const auto& location = Key_.Location;
return location.IsValid()
? Format("%v at %v:%v", GetTypeName(), location.GetFileName(), location.GetLine())
: GetTypeName();
}
size_t TRefCountedTracker::TNamedSlot::GetObjectsAllocated() const
{
return ObjectsAllocated_ + TagObjectsAllocated_;
}
size_t TRefCountedTracker::TNamedSlot::GetObjectsFreed() const
{
return ObjectsFreed_ + TagObjectsFreed_;
}
size_t TRefCountedTracker::TNamedSlot::GetObjectsAlive() const
{
return
ClampNonnegative(ObjectsAllocated_, ObjectsFreed_) +
ClampNonnegative(TagObjectsAllocated_, TagObjectsFreed_);
}
size_t TRefCountedTracker::TNamedSlot::GetBytesAllocated() const
{
return
ObjectsAllocated_ * ObjectSize_ +
SpaceSizeAllocated_;
}
size_t TRefCountedTracker::TNamedSlot::GetBytesFreed() const
{
return
ObjectsFreed_ * ObjectSize_ +
SpaceSizeFreed_;
}
size_t TRefCountedTracker::TNamedSlot::GetBytesAlive() const
{
return
ClampNonnegative(ObjectsAllocated_, ObjectsFreed_) * ObjectSize_ +
ClampNonnegative(SpaceSizeAllocated_, SpaceSizeFreed_);
}
TRefCountedTrackerStatistics::TNamedSlotStatistics TRefCountedTracker::TNamedSlot::GetStatistics() const
{
TRefCountedTrackerStatistics::TNamedSlotStatistics result;
result.FullName = GetFullName();
result.ObjectsAllocated = GetObjectsAllocated();
result.ObjectsFreed = GetObjectsFreed();
result.ObjectsAlive = GetObjectsAlive();
result.BytesAllocated = GetBytesAllocated();
result.BytesFreed = GetBytesFreed();
result.BytesAlive = GetBytesAlive();
return result;
}
size_t TRefCountedTracker::TNamedSlot::ClampNonnegative(size_t allocated, size_t freed)
{
return allocated >= freed ? allocated - freed : 0;
}
////////////////////////////////////////////////////////////////////////////////
// nullptr if not initialized or already destroyed
YT_DEFINE_THREAD_LOCAL(TRefCountedTracker::TLocalSlots*, RefCountedTrackerLocalSlots);
// nullptr if not initialized or already destroyed
YT_DEFINE_THREAD_LOCAL(TRefCountedTracker::TLocalSlot*, RefCountedTrackerLocalSlotsBegin);
// 0 if not initialized
// -1 if already destroyed
YT_DEFINE_THREAD_LOCAL(int, RefCountedTrackerLocalSlotsSize);
int TRefCountedTracker::GetTrackedThreadCount() const
{
auto guard = Guard(SpinLock_);
return std::ssize(AllLocalSlots_);
}
TRefCountedTypeCookie TRefCountedTracker::GetCookie(
TRefCountedTypeKey typeKey,
size_t objectSize,
const TSourceLocation& location)
{
auto guard = Guard(SpinLock_);
TypeKeyToObjectSize_.emplace(typeKey, objectSize);
TKey key{typeKey, location};
auto it = KeyToCookie_.find(key);
if (it != KeyToCookie_.end()) {
return it->second;
}
auto cookie = TRefCountedTypeCookie(std::ssize(CookieToKey_));
KeyToCookie_.emplace(key, cookie);
CookieToKey_.push_back(key);
return cookie;
}
TRefCountedTracker::TNamedStatistics TRefCountedTracker::GetSnapshot() const
{
auto guard = Guard(SpinLock_);
TNamedStatistics result;
for (const auto& key : CookieToKey_) {
result.emplace_back(key, GetObjectSize(key.TypeKey));
}
auto accumulateResult = [&] (const auto& slots) {
for (auto index = 0; index < std::ssize(result) && index < std::ssize(slots); ++index) {
result[index] += slots[index];
}
};
accumulateResult(GlobalSlots_);
for (const auto* slots : AllLocalSlots_) {
accumulateResult(*slots);
}
return result;
}
void TRefCountedTracker::SortSnapshot(TNamedStatistics* snapshot, int sortByColumn)
{
std::function<bool(const TNamedSlot& lhs, const TNamedSlot& rhs)> predicate;
switch (sortByColumn) {
case 0:
predicate = [] (const TNamedSlot& lhs, const TNamedSlot& rhs) {
return lhs.GetObjectsAlive() > rhs.GetObjectsAlive();
};
break;
case 1:
predicate = [] (const TNamedSlot& lhs, const TNamedSlot& rhs) {
return lhs.GetObjectsAllocated() > rhs.GetObjectsAllocated();
};
break;
case 2:
default:
predicate = [] (const TNamedSlot& lhs, const TNamedSlot& rhs) {
return lhs.GetBytesAlive() > rhs.GetBytesAlive();
};
break;
case 3:
predicate = [] (const TNamedSlot& lhs, const TNamedSlot& rhs) {
return lhs.GetBytesAllocated() > rhs.GetBytesAllocated();
};
break;
case 4:
predicate = [] (const TNamedSlot& lhs, const TNamedSlot& rhs) {
return lhs.GetTypeName() < rhs.GetTypeName();
};
break;
}
std::sort(snapshot->begin(), snapshot->end(), predicate);
}
TString TRefCountedTracker::GetDebugInfo(int sortByColumn) const
{
auto snapshot = GetSnapshot();
SortSnapshot(&snapshot, sortByColumn);
TStringBuilder builder;
size_t totalObjectsAlive = 0;
size_t totalObjectsAllocated = 0;
size_t totalBytesAlive = 0;
size_t totalBytesAllocated = 0;
builder.AppendFormat(
"%10s %10s %15s %15s %s\n",
"ObjAlive",
"ObjAllocated",
"BytesAlive",
"BytesAllocated",
"Name");
builder.AppendString("-------------------------------------------------------------------------------------------------------------\n");
for (const auto& slot : snapshot) {
totalObjectsAlive += slot.GetObjectsAlive();
totalObjectsAllocated += slot.GetObjectsAllocated();
totalBytesAlive += slot.GetBytesAlive();
totalBytesAllocated += slot.GetBytesAllocated();
builder.AppendFormat(
"%10" PRISZT " %10" PRISZT " %15" PRISZT " %15" PRISZT " %s\n",
slot.GetObjectsAlive(),
slot.GetObjectsAllocated(),
slot.GetBytesAlive(),
slot.GetBytesAllocated(),
slot.GetFullName().data());
}
builder.AppendString("-------------------------------------------------------------------------------------------------------------\n");
builder.AppendFormat(
"%10" PRISZT " %10" PRISZT " %15" PRISZT " %15" PRISZT " %s\n",
totalObjectsAlive,
totalObjectsAllocated,
totalBytesAlive,
totalBytesAllocated,
"Total");
return builder.Flush();
}
TRefCountedTrackerStatistics TRefCountedTracker::GetStatistics() const
{
auto slots = GetSnapshot();
SortSnapshot(&slots, -1);
TRefCountedTrackerStatistics result;
result.NamedStatistics.reserve(slots.size());
for (const auto& slot : slots) {
auto statistics = slot.GetStatistics();
result.NamedStatistics.push_back(statistics);
result.TotalStatistics += statistics;
}
return result;
}
size_t TRefCountedTracker::GetObjectsAllocated(TRefCountedTypeKey typeKey) const
{
return GetSlot(typeKey).GetObjectsAllocated();
}
size_t TRefCountedTracker::GetObjectsAlive(TRefCountedTypeKey typeKey) const
{
return GetSlot(typeKey).GetObjectsAlive();
}
size_t TRefCountedTracker::GetBytesAllocated(TRefCountedTypeKey typeKey) const
{
return GetSlot(typeKey).GetBytesAllocated();
}
size_t TRefCountedTracker::GetBytesAlive(TRefCountedTypeKey typeKey) const
{
return GetSlot(typeKey).GetBytesAlive();
}
size_t TRefCountedTracker::GetObjectSize(TRefCountedTypeKey typeKey) const
{
auto it = TypeKeyToObjectSize_.find(typeKey);
return it == TypeKeyToObjectSize_.end() ? 0 : it->second;
}
TRefCountedTracker::TNamedSlot TRefCountedTracker::GetSlot(TRefCountedTypeKey typeKey) const
{
auto guard = Guard(SpinLock_);
TKey key{typeKey, TSourceLocation()};
TNamedSlot result(key, GetObjectSize(typeKey));
auto it = KeyToCookie_.lower_bound(key);
while (it != KeyToCookie_.end() && it->first.TypeKey == typeKey) {
auto cookie = it->second;
auto index = cookie.Underlying();
auto accumulateResult = [&] (const auto& slots) {
if (index < std::ssize(slots)) {
result += slots[index];
}
};
accumulateResult(GlobalSlots_);
for (auto* slots : AllLocalSlots_) {
accumulateResult(*slots);
}
++it;
}
return result;
}
#define INCREMENT_COUNTER_SLOW(name, delta) \
if (RefCountedTrackerLocalSlotsSize() < 0) { \
auto guard = Guard(SpinLock_); \
GetGlobalSlot(cookie)->name += delta; \
} else { \
GetLocalSlot(cookie)->name += delta; \
}
void TRefCountedTracker::AllocateInstanceSlow(TRefCountedTypeCookie cookie)
{
INCREMENT_COUNTER_SLOW(ObjectsAllocated, 1)
}
void TRefCountedTracker::FreeInstanceSlow(TRefCountedTypeCookie cookie)
{
INCREMENT_COUNTER_SLOW(ObjectsFreed, 1)
}
void TRefCountedTracker::AllocateTagInstanceSlow(TRefCountedTypeCookie cookie)
{
INCREMENT_COUNTER_SLOW(TagObjectsAllocated, 1)
}
void TRefCountedTracker::FreeTagInstanceSlow(TRefCountedTypeCookie cookie)
{
INCREMENT_COUNTER_SLOW(TagObjectsFreed, 1)
}
void TRefCountedTracker::AllocateSpaceSlow(TRefCountedTypeCookie cookie, size_t space)
{
INCREMENT_COUNTER_SLOW(SpaceSizeAllocated, space)
}
void TRefCountedTracker::FreeSpaceSlow(TRefCountedTypeCookie cookie, size_t space)
{
INCREMENT_COUNTER_SLOW(SpaceSizeFreed, space)
}
#undef INCREMENT_COUNTER_SLOW
TRefCountedTracker::TLocalSlot* TRefCountedTracker::GetLocalSlot(TRefCountedTypeCookie cookie)
{
struct TReclaimer
{
~TReclaimer()
{
auto* this_ = TRefCountedTracker::Get();
auto guard = Guard(this_->SpinLock_);
auto& localSlots = RefCountedTrackerLocalSlots();
if (this_->GlobalSlots_.size() < localSlots->size()) {
this_->GlobalSlots_.resize(std::max(localSlots->size(), this_->GlobalSlots_.size()));
}
for (auto index = 0; index < std::ssize(*localSlots); ++index) {
this_->GlobalSlots_[index] += (*localSlots)[index];
}
YT_VERIFY(this_->AllLocalSlots_.erase(localSlots) == 1);
delete localSlots;
localSlots = nullptr;
RefCountedTrackerLocalSlotsBegin() = nullptr;
RefCountedTrackerLocalSlotsSize() = -1;
}
};
thread_local TReclaimer Reclaimer;
auto& refCountedTrackerLocalSlotsSize = RefCountedTrackerLocalSlotsSize();
YT_VERIFY(refCountedTrackerLocalSlotsSize >= 0);
auto guard = Guard(SpinLock_);
auto& localSlotsBegin = RefCountedTrackerLocalSlotsBegin();
auto& localSlots = RefCountedTrackerLocalSlots();
if (!localSlots) {
localSlots = new TLocalSlots();
YT_VERIFY(AllLocalSlots_.insert(localSlots).second);
}
auto index = cookie.Underlying();
if (index >= std::ssize(*localSlots)) {
localSlots->resize(2 * static_cast<size_t>(index) + 1);
}
localSlotsBegin = localSlots->data();
refCountedTrackerLocalSlotsSize = std::ssize(*localSlots);
return localSlotsBegin + index;
}
TRefCountedTracker::TGlobalSlot* TRefCountedTracker::GetGlobalSlot(TRefCountedTypeCookie cookie)
{
VERIFY_SPINLOCK_AFFINITY(SpinLock_);
auto index = cookie.Underlying();
if (index >= std::ssize(GlobalSlots_)) {
GlobalSlots_.resize(static_cast<size_t>(index) + 1);
}
return &GlobalSlots_[index];
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|