Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: track the memory usage of aggregate more accurate #34732

Merged
merged 7 commits into from May 23, 2022

Conversation

wshwsh12
Copy link
Contributor

@wshwsh12 wshwsh12 commented May 17, 2022

What problem does this PR solve?

Issue Number: ref #33877
close #35635

Problem Summary:

  1. At present, tidb's memory estimation for Golang Map only track the new buckets after expansion, and the old buckets will not be GCed immediately. Considering the worst case, the memory error will be 1.5 times. And GolangMap will definitely reach its peak value after the expansion, so we will expand the current memory statistics to 1.5 times, and reserve this part of the space for GolangMap

  2. For StringSet, we shouldn't track the len(string), because map only save the pointer. We should track it when allocated.

  3. The magic number in getGroupKeyMemUsage is wrong, and the unsafe.sizeof(slice) is 24.

  4. We should track the memory usage of the pointer point to the PartialResult.

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
    In the workload tpch-50g, try to run the aggregate function in q18: We can see that tidb tracks 39.0 GB.
root@127.0.0.1:test> desc analyze  select sum(l_quantity), l_orderkey from lineitem group by l_orderkey;
+----------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------+-------------------------+------+
| id                         | estRows      | actRows   | task      | access object  | execution info                                                                                                                                                                                                                                                                                                                                                                                                 | operator info                                                                                                                          | memory                  | disk |
+----------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------+-------------------------+------+
| HashAgg_11                 | 73908224.00  | 75000000  | root      |                | time:2m34.1s, loops:73247, partial_worker:{wall_time:49.929152551s, concurrency:5, task_num:568, tot_wait:22.124151295s, tot_exec:3m18.668402668s, tot_time:4m5.159963839s, max:49.929101236s, p95:49.929101236s}, final_worker:{wall_time:2m34.147709325s, concurrency:5, task_num:25, tot_wait:4m0.420835575s, tot_exec:8m42.60698766s, tot_time:12m43.027854689s, max:2m34.147671304s, p95:2m34.147671304s} | group by:test.lineitem.l_orderkey, funcs:sum(Column#19)->Column#18, funcs:firstrow(test.lineitem.l_orderkey)->test.lineitem.l_orderkey | 39.0 GB(Total: 39.0 GB) | N/A  |
| └─TableReader_12           | 73908224.00  | 75000433  | root      |                | time:15.1s, loops:569, cop_task: {num: 568, max: 1.25s, min: 468.5ms, avg: 616.8ms, p95: 827ms, max_proc_keys: 618925, p95_proc_keys: 528090, tot_proc: 5m25.5s, tot_wait: 410ms, rpc_num: 568, rpc_time: 5m50.3s, copr_cache: disabled}                                                                                                                                                                       | data:HashAgg_5                                                                                                                         | 90.7 MB                 | N/A  |
|   └─HashAgg_5              | 73908224.00  | 75000433  | cop[tikv] |                | tikv_task:{proc max:828ms, min:446ms, p80:595ms, p95:638ms, iters:293177, tasks:568}, scan_detail: {total_process_keys: 300005811, total_process_keys_size: 59595430182, total_keys: 300006379, rocksdb: {delete_skipped_count: 0, key_skipped_count: 300005811, block: {cache_hit_count: 977687, read_count: 0, read_byte: 0 Bytes}}}                                                                         | group by:test.lineitem.l_orderkey, funcs:sum(test.lineitem.l_quantity)->Column#19                                                      | N/A                     | N/A  |
|     └─TableFullScan_10     | 300005811.00 | 300005811 | cop[tikv] | table:lineitem | tikv_task:{proc max:696ms, min:335ms, p80:466ms, p95:503ms, iters:293177, tasks:568}                                                                                                                                                                                                                                                                                                                           | keep order:false                                                                                                                       | N/A                     | N/A  |
+----------------------------+--------------+-----------+-----------+----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------+-------------------------+------+
4 rows in set (3 min 25.79 sec)

See the heap profile: about 40495 MB(39.5 GB) memory inuse.
image

See the graph

  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented May 17, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • XuHuaiyu
  • guo-shaoge

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2022
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2022
@sre-bot
Copy link
Contributor

sre-bot commented May 17, 2022

// Golang Map needs to acquire double the memory when expanding, and the old buckets will be released after the data is migrated.
// Considering the worst case, the data in the old bucket cannot be migrated in time, and the old bucket cannot
// be GCed, we expand the bucket size to 1.5 times to estimate the memory usage of Golang Map.
DefBucketMemoryUsageForMapStrToSlice = (8*(1+16+24) + 16) / 2 * 3
Copy link
Contributor

Choose a reason for hiding this comment

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

ToPtr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because type aggPartialResultMapper map[string][]aggfuncs.PartialResult

@wshwsh12 wshwsh12 requested a review from XuHuaiyu May 18, 2022 06:28
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label May 18, 2022
@XuHuaiyu XuHuaiyu added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 18, 2022
@wshwsh12 wshwsh12 removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 23, 2022
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels May 23, 2022
@wshwsh12
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 59d4e60

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label May 23, 2022
@ti-chi-bot ti-chi-bot merged commit 515de80 into pingcap:master May 23, 2022
@sre-bot
Copy link
Contributor

sre-bot commented May 23, 2022

TiDB MergeCI notify

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci/integration-cdc-test 🟢 all 34 tests passed 27 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 11 min Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 7 min 45 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 7 min 29 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 7 min 5 sec Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 12 tests passed 6 min 56 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 6 min 14 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 4 min 4 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 54 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Track aggregate memory usage more accurate
5 participants