-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
import: fix memory leak #39332
Merged
Merged
import: fix memory leak #39332
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b8426bc
import: fix memory leak
dsdashun a71021c
address comments
dsdashun 7eb6b5d
Merge remote-tracking branch 'origin/master' into fix_mem_leak
dsdashun 0174d56
add missing bazel file
dsdashun c246754
Merge branch 'master' into fix-39331
ti-chi-bot 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 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 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 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,126 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kv | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/docker/go-units" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKVMemBufInterweaveAllocAndRecycle(t *testing.T) { | ||
type testCase struct { | ||
AllocSizes []int | ||
FinalAvailableByteBufCaps []int | ||
} | ||
for _, tc := range []testCase{ | ||
{ | ||
AllocSizes: []int{ | ||
1 * units.MiB, | ||
2 * units.MiB, | ||
3 * units.MiB, | ||
4 * units.MiB, | ||
5 * units.MiB, | ||
}, | ||
// [2] => [2,4] => [2,4,8] => [4,2,8] => [4,2,8,16] | ||
FinalAvailableByteBufCaps: []int{ | ||
4 * units.MiB, | ||
2 * units.MiB, | ||
8 * units.MiB, | ||
16 * units.MiB, | ||
}, | ||
}, | ||
{ | ||
AllocSizes: []int{ | ||
5 * units.MiB, | ||
4 * units.MiB, | ||
3 * units.MiB, | ||
2 * units.MiB, | ||
1 * units.MiB, | ||
}, | ||
// [16] => [16] => [16] => [16] => [16] | ||
FinalAvailableByteBufCaps: []int{16 * units.MiB}, | ||
}, | ||
{ | ||
AllocSizes: []int{5, 4, 3, 2, 1}, | ||
// [1] => [1] => [1] => [1] => [1] | ||
FinalAvailableByteBufCaps: []int{1 * units.MiB}, | ||
}, | ||
{ | ||
AllocSizes: []int{ | ||
1 * units.MiB, | ||
2 * units.MiB, | ||
3 * units.MiB, | ||
2 * units.MiB, | ||
1 * units.MiB, | ||
5 * units.MiB, | ||
}, | ||
// [2] => [2,4] => [2,4,8] => [2,8,4] => [8,4,2] => [8,4,2,16] | ||
FinalAvailableByteBufCaps: []int{ | ||
8 * units.MiB, | ||
4 * units.MiB, | ||
2 * units.MiB, | ||
16 * units.MiB, | ||
}, | ||
}, | ||
} { | ||
testKVMemBuf := &kvMemBuf{} | ||
for _, allocSize := range tc.AllocSizes { | ||
testKVMemBuf.AllocateBuf(allocSize) | ||
testKVMemBuf.Recycle(testKVMemBuf.buf) | ||
} | ||
require.Equal(t, len(tc.FinalAvailableByteBufCaps), len(testKVMemBuf.availableBufs)) | ||
for i, bb := range testKVMemBuf.availableBufs { | ||
require.Equal(t, tc.FinalAvailableByteBufCaps[i], bb.cap) | ||
} | ||
} | ||
} | ||
|
||
func TestKVMemBufBatchAllocAndRecycle(t *testing.T) { | ||
type testCase struct { | ||
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. this type is not used? |
||
AllocSizes []int | ||
FinalAvailableByteBufCaps []int | ||
} | ||
testKVMemBuf := &kvMemBuf{} | ||
bBufs := []*bytesBuf{} | ||
for i := 0; i < maxAvailableBufSize; i++ { | ||
testKVMemBuf.AllocateBuf(1 * units.MiB) | ||
bBufs = append(bBufs, testKVMemBuf.buf) | ||
} | ||
for i := 0; i < maxAvailableBufSize; i++ { | ||
testKVMemBuf.AllocateBuf(2 * units.MiB) | ||
bBufs = append(bBufs, testKVMemBuf.buf) | ||
} | ||
for _, bb := range bBufs { | ||
testKVMemBuf.Recycle(bb) | ||
} | ||
require.Equal(t, maxAvailableBufSize, len(testKVMemBuf.availableBufs)) | ||
for _, bb := range testKVMemBuf.availableBufs { | ||
require.Equal(t, 4*units.MiB, bb.cap) | ||
} | ||
bBufs = bBufs[:0] | ||
for i := 0; i < maxAvailableBufSize; i++ { | ||
testKVMemBuf.AllocateBuf(1 * units.MiB) | ||
bb := testKVMemBuf.buf | ||
require.Equal(t, 4*units.MiB, bb.cap) | ||
bBufs = append(bBufs, bb) | ||
require.Equal(t, maxAvailableBufSize-i-1, len(testKVMemBuf.availableBufs)) | ||
} | ||
for _, bb := range bBufs { | ||
testKVMemBuf.Recycle(bb) | ||
} | ||
require.Equal(t, maxAvailableBufSize, len(testKVMemBuf.availableBufs)) | ||
} |
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.
should we limit max length of
mb.availableBufs
? in caseencodeLoop
is slow down for we might loop through all availableBufs on everyAllocateBuf
in worst caseThere 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.
Maybe no need to limit the max length. There are two reasons:
availableBufs
size is controllable.But it is OK to add a defending mechanism for the performance degration here.
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.
Fixed in a71021c