Skip to content

[Enhancement] [Memory] [Vectorized] Stress test and optimize memory allocation - #9581

Merged
yiguolei merged 2 commits into
apache:masterfrom
xinyiZzz:fix_tracker_lru_cache_push
Jun 28, 2022
Merged

[Enhancement] [Memory] [Vectorized] Stress test and optimize memory allocation#9581
yiguolei merged 2 commits into
apache:masterfrom
xinyiZzz:fix_tracker_lru_cache_push

Conversation

@xinyiZzz

@xinyiZzzxinyiZzz commented May 15, 2022

Copy link
Copy Markdown
Contributor

Proposed changes

Issue Number: close#9540
#9580

Problem Summary:

  1. High concurrency stress test on SSB and wide table. Compare the performance of turning the vectorization engine on and off. Turning on the vectorization engine is slower for most SSB queries.

  2. Optimize the Allocator in the vectorization engine. In most queries, the performance is improved by about 10%.
    Memory allocation between 4KB and 64MB will be through ChunkAllocator, those less than 4KB will be through malloc, and those greater than 64MB will be through MMAP.

  3. Optimize Chunk Allocator, increase the limit that allows chunks to be stolen from other core's arena, and optimize reserved bytes conf.

Checklist(Required)

  1. Does it affect the original behavior: (Yes)
  2. Has unit tests been added: (No)
  3. Has document been added or modified: (Yes)
  4. Does it need to update dependencies: (No)
  5. Are there any changes that cannot be rolled back: (Yes)

Further comments

Stress testing the vectorization engine.

1. Env and Test Set

> Based on Doris V1.0
Env: 1 FE, 1 BE
Test Set: SSB, 100G, lineorder 60003w rows
Width table from online service, 419 columns, 1710549 rows
set global parallel_fragment_exec_instance_num=10
jmeter conf:
<stringProp name="ThreadGroup.num_threads">100</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<stringProp name="ThreadGroup.duration">30</stringProp>
<stringProp name="ThreadGroup.delay">0</stringProp>
actual concurrency = parallel_fragment_exec_instance_num * ThreadGroup.num_threads

2. Test

  • TO: Master, set global enable_vectorized_engine=false;
  • T1: Master, set global enable_vectorized_engine=true;
  • T2: Master, set global enable_vectorized_engine=true, tc_max_total_thread_cache_bytes=100G;
  • T3: This PR, set global enable_vectorized_engine=true, allocate 4k < size < 64M use ChunkAllocator;
  • T4: This PR, set global enable_vectorized_engine=true, Allocator 4k < size < 64M use chunkAllocator, and compile USE_MEM_TRACKER=0;
  • R1: (T0mid-T1mid)/T0mid, Compare the performance of turning the vectorization engine on and off.
  • R2: (T1mid-T3mid)/T1mid, Performance changes brought by allocating 4K < size < 64M memory through ChunkAllocator in the vectorization engine.
  • R3: (T1mid-T4mid)/T1mid, Same as above, close memtracker.

Form Notes: "xxx,xxx,xxx": Repeat 3 times, the AvgTime(ms) of each time.

querynum_threadsR1R2R3T0T1T2T3T4
Q1.11004.7%9.2%12.1%36252,36903,3680034297,35053,3608735757,34483,3382533314,31657,3183830801,31496,30445
Q1.2100-3.8%7%6.2%24017,24338,2547825273.25222,2591426647,24651,2540623453,23498,2360423771,23084,23704
Q1.3100-2.6%9%7.924349,23780,2284424073,24487,2440123842,23149,2419822614,22984,2305022678,22466,22225
Q2.120-11.2%0.6%19.3%89466,21528,2188926300,24345,2422289662,25042,2406924094,24542,2419720538,19651,19627
Q2.22012.7%4.9%0.8%16963,21435,1815415855,16936,1504716006,17251,1659315072,14407,1564815716,16347,15588
Q2.3201%3.2%8.9%15183,16194,1397715551,15033,1480114338,14605,1453114302,14548,1530114318,13601,13689
Q3.1205.4%19.4%23.6%32021,32176,3142731037,30283,3023138002,30272,3001625162,23187,2441127673,23147,22492
Q3.220-8%15.8%17.3%10379,10433,989311837,11184,1122311403,11481,97889296,9452,94559576,9172,9283
Q3.320-5%10.6%12.8%8559,8472,86398713,9390,89928367,8153,81337998,8618,80407952,7476,7845
Q4.120-35%27.9%31.8%32249,29965,2913647405,40357,4044341912,36981,3757131230,27683,2916631848,27585,27435
Q4.220-73.516.2%15.2%19979,18798,1716934066,32614,3084934560,34460,3519427117,29645,2733727651,29205,27149
Q4.320-46.2%-2.8%0.5%19357,20762,1999228256,29862,2923027647,29216,2909130523,30067,2926029092,26644,30017
Width table (419 rows)100100%17.6%17.9%no work4211,4546,47104089,4479,45513679,3745,38163664,3732,3829

image

3. Detailed description

  • T2: Theoretically, when the capacity of the tcmalloc thread cache is sufficient, the spin lock in the central free list will be avoided to a great extent, but in practice, the spin lock cost is still large in high concurrency queries, I will test this matter in more detail below.
  • T3: Because tcmalloc thread cache cannot avoid spin lock, the introduction of ChunkAllocator is equivalent to adding a layer of cache in User Mode.
    In allocator.h, Memory allocation between 4KB and 64MB will be through ChunkAllocator, those less than 4KB will be through malloc (for example, tcmalloc), and those greater than 64MB will be through MMAP.
    In the actual test, chunkallocator allocates less than 4KB of memory slower than malloc, and chunkallocator allocates more than 64MB of memory slower than MMAP, but the 4KB threshold is an empirical value, which needs to be determined by more detailed test later.
  • T4: Close memtracker at compile time can be selected during POC. Memtracker records the consumption value through an atomic variable. In high query concurrency, the atomic variable spin lock has a high cost. I'll optimize memtracker. After that

Comment threadbe/src/common/config.h
@yiguoleiyiguolei added this to the v1.2 milestone May 16, 2022
} while (!_reserved_bytes.compare_exchange_weak(old_reserved_bytes, new_reserved_bytes));

// Reduce set metric frequency
if (_reserved_bytes % 100 == 32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to make sure the correctness?

At the first look, (_reserved_bytes % 100 < 32) or (_reserved_bytes % 100 > 32) both will not update the metric.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the first look, ChunkAllocator will allocate/free many times, the memory size of each allocate/free is a multiple of 2, so _reserved_bytes% 100 == 32 will definitely happen, and the latest _reserved_bytes value will be set each time .

The real-time and accurate _reserved_bytes value is not required. Usually, the value of _reserved_bytes is equal to ChunkAllocator MemTracker. The _reserved_bytes metric is only concerned when verifying the accuracy of MemTracker.

Therefore, reduce the number of sets and reduce the performance impact.

@xinyiZzz
xinyiZzzforce-pushed the fix_tracker_lru_cache_push branch from 9b9b6bb to f97096dCompareMay 16, 2022 17:58
@github-actionsgithub-actionsBot added the kind/docs Categorizes issue or PR as related to documentation. label May 16, 2022
Comment threadbuild.sh Outdated
Comment threadbe/src/gutil/strings/numbers.cc
@xinyiZzz
xinyiZzzforce-pushed the fix_tracker_lru_cache_push branch 2 times, most recently from db7b85a to 345679cCompareJune 28, 2022 03:52
void* buf;

if (size >= MMAP_THRESHOLD) {
if (alignment > MMAP_MIN_ALIGNMENT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call populate to populate the memory to avoid too many page fault during usage?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for zero-fill, because mmap guarantees it.

Comment threadbe/src/runtime/memory/chunk_allocator.h Outdated
@xinyiZzz
xinyiZzzforce-pushed the fix_tracker_lru_cache_push branch from 345679c to c20832cCompareJune 28, 2022 08:53
@xinyiZzz

Copy link
Copy Markdown
ContributorAuthor

Based on the latest master (commit id: 7898c81)
The test results are the same as above.

@yiguoleiyiguolei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@yiguolei
yiguolei merged commit deeb302 into apache:masterJun 28, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/vectorizationkind/docsCategorizes issue or PR as related to documentation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DCHECK failed caused by tls_ctx()->type() == ThreadContext::TaskType::UNKNOWN

4 participants

@xinyiZzz@yangzhg@yiguolei@cambyzju