-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Shrink NET::buff to reduce memory usage #714
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
Open
SongLibing
wants to merge
1
commit into
mysql:trunk
Choose a base branch
from
SongLibing:net_buffer_shrink
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+338
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| SHOW VARIABLES LIKE 'net_buffer_length'; | ||
| Variable_name Value | ||
| net_buffer_length 16384 | ||
| CREATE USER 'shrink_test_user'@'localhost'; | ||
| SET GLOBAL net_buffer_shrink_threshold = 5; | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff after a large query; capacity should have grown." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 36864 | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff after four small queries; capacity should not be shrunk." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 36864 | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff after five small queries; capacity should be about half." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 20480 | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff after ten small queries; capacity should reach its minimum." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 16384 | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff remains at its minimum after five more small queries." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 16384 | ||
| [connection con1] | ||
| [connection default] | ||
| "NET::buff after another large query; capacity should grow again." | ||
| CURRENT_ALLOCATIONS CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| 1 36864 | ||
| DROP USER 'shrink_test_user'@'localhost'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| # Test NET::buff shrinking for classic protocol connections. | ||
| # | ||
| # The test verifies that: | ||
| # 1. A query larger than net_buffer_length grows NET::buff. | ||
| # 2. Five consecutive small queries shrink the buffer by approximately half. | ||
| # 3. Further small queries shrink it to, but never below, net_buffer_length. | ||
| # 4. A later large query can grow the buffer again after it was shrunk. | ||
| # | ||
| # Expected result: PFS allocation statistics show grow, gradual shrink, a | ||
| # stable lower bound, and successful regrowth in that order. | ||
|
|
||
| --source include/have_nodebug.inc | ||
|
|
||
| # ============================================================================= | ||
| # Prerequisites | ||
| # ============================================================================= | ||
|
|
||
| if (`SELECT @@GLOBAL.performance_schema = FALSE`) { | ||
| skip Test requires @@GLOBAL.performance_schema = TRUE.; | ||
| } | ||
|
|
||
| let $instrument = `SELECT ENABLED FROM performance_schema.setup_instruments | ||
| WHERE NAME = 'memory/sql/NET::buff'`; | ||
| if ($instrument != YES) { | ||
| skip Test requires memory/sql/NET::buff instrumentation.; | ||
| } | ||
|
|
||
| # ============================================================================= | ||
| # Setup | ||
| # ============================================================================= | ||
|
|
||
| let $old_net_buffer_shrink_threshold = query_get_value( | ||
| SHOW VARIABLES LIKE "net_buffer_shrink_threshold", Value, 1); | ||
| SHOW VARIABLES LIKE 'net_buffer_length'; | ||
|
|
||
| # Generate padding for a query larger than net_buffer_length. The padding is | ||
| # produced on the default connection so setup cannot affect con1's shrink count. | ||
| let $query_padding = `SELECT REPEAT('x', 32768)`; | ||
|
|
||
| CREATE USER 'shrink_test_user'@'localhost'; | ||
| SET GLOBAL net_buffer_shrink_threshold = 5; | ||
| --connect(con1, localhost, shrink_test_user, , ) | ||
| let $con1_processlist_id = `SELECT CONNECTION_ID()`; | ||
| --connection default | ||
| --disable_query_log | ||
| --eval SET @con1_processlist_id = $con1_processlist_id | ||
|
|
||
| # Extra 39 bytes are allocated for | ||
| # PSI header (32 B), net header (4 B), compression header (4 B) | ||
| # They are not counted. | ||
|
|
||
| let $pfs_net_buff_stmt = | ||
| SELECT COUNT_ALLOC - COUNT_FREE AS CURRENT_ALLOCATIONS, | ||
| CURRENT_NUMBER_OF_BYTES_USED - 39 | ||
| FROM performance_schema.memory_summary_by_thread_by_event_name | ||
| WHERE EVENT_NAME = 'memory/sql/NET::buff' | ||
| AND THREAD_ID = (SELECT THREAD_ID | ||
| FROM performance_schema.threads | ||
| WHERE PROCESSLIST_ID = @con1_processlist_id); | ||
|
|
||
| # ============================================================================= | ||
| # Test 1: A large incoming query grows NET::buff | ||
| # ============================================================================= | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| --eval SELECT 1 /* $query_padding */ | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff after a large query; capacity should have grown." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| # ============================================================================= | ||
| # Test 2: Five small queries shrink the grown buffer by about half | ||
| # ============================================================================= | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff after four small queries; capacity should not be shrunk." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| SELECT 1; | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff after five small queries; capacity should be about half." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| # ============================================================================= | ||
| # Test 3: Five more small queries reach net_buffer_length | ||
| # ============================================================================= | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff after ten small queries; capacity should reach its minimum." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| let $net_buffer_length = query_get_value( | ||
| SHOW VARIABLES LIKE 'net_buffer_length', Value, 1); | ||
| let $current_buffer_capacity = `$pfs_net_buff_stmt`; | ||
|
|
||
| if ($current_buffer_capacity == $net_buffer_length) { | ||
| --echo NET::buff capacity is equal to net_buffer_length. | ||
| } | ||
|
|
||
| # ============================================================================= | ||
| # Test 4: Further small queries do not shrink below net_buffer_length | ||
| # ============================================================================= | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| SELECT 1; | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff remains at its minimum after five more small queries." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| # ============================================================================= | ||
| # Test 5: A later large query grows the buffer again | ||
| # ============================================================================= | ||
|
|
||
| --connection con1 | ||
| --echo [connection con1] | ||
| --disable_result_log | ||
| --eval SELECT 1 /* $query_padding */ | ||
| --enable_result_log | ||
|
|
||
| --connection default | ||
| --echo [connection default] | ||
| --echo "NET::buff after another large query; capacity should grow again." | ||
| --eval $pfs_net_buff_stmt | ||
|
|
||
| # ============================================================================= | ||
| # Cleanup | ||
| # ============================================================================= | ||
|
|
||
| --disconnect con1 | ||
| --eval SET GLOBAL net_buffer_shrink_threshold = $old_net_buffer_shrink_threshold | ||
| --enable_query_log | ||
| DROP USER 'shrink_test_user'@'localhost'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -258,6 +258,36 @@ bool net_realloc(NET *net, size_t length) { | |
| return false; | ||
| } | ||
|
|
||
| #ifdef MYSQL_SERVER | ||
| /** | ||
| Shrink net buffer. | ||
|
|
||
| @param net NET handler | ||
| @param length shrink net buffer to length. length should smaller | ||
| than the original buffer size | ||
|
|
||
| @retval true failed to shrink | ||
| @retval false Shrunk to length successfully | ||
| */ | ||
| bool net_shrink(NET *net, size_t length) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fold it into try_shrink_net_buffer() or define a separate function within server layer itself.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added #ifdef MYSQL_SERVER to make it server only. |
||
| uchar *buff; | ||
| size_t pkt_length; | ||
|
|
||
| assert(length < net->max_packet); | ||
| pkt_length = (length + IO_SIZE - 1) & ~(IO_SIZE - 1); | ||
|
|
||
| if (!(buff = (uchar *)my_realloc( | ||
| key_memory_NET_buff, (char *)net->buff, | ||
| pkt_length + NET_HEADER_SIZE + COMP_HEADER_SIZE, MYF(0)))) | ||
| return true; | ||
|
|
||
| net->buff = net->write_pos = buff; | ||
| net->buff_end = buff + pkt_length; | ||
| net->max_packet = ulong(pkt_length); | ||
| return false; | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| Clear (reinitialize) the NET structure for a new command. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kindly add test to verify behavior with small queries less than net_buffer_shrink_threshold.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For test2 and test3, I added query to show the allocated buffer, just after the forth SELECT 1. the allocated buffer should no change.