-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an automated cherry-pick of #8589
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
4b60531
commit 23f4447
Showing
5 changed files
with
170 additions
and
0 deletions.
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
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,69 @@ | ||
// Copyright 2023 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"math" | ||
"time" | ||
|
||
"github.com/KimMachineGun/automemlimit/memlimit" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb/util/memory" | ||
"github.com/pingcap/tiflow/pkg/errors" | ||
"github.com/shirou/gopsutil/v3/mem" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const memoryMax uint64 = math.MaxUint64 | ||
|
||
// GetMemoryLimit gets the memory limit of current process based on cgroup. | ||
// If the cgourp is not set or memory.max is set to max, returns the available | ||
// memory of host. | ||
func GetMemoryLimit() (uint64, error) { | ||
totalMemory, err := memlimit.FromCgroup() | ||
if err != nil || totalMemory == memoryMax { | ||
log.Info("no cgroup memory limit", zap.Error(err)) | ||
totalMemory, err = memory.MemTotal() | ||
if err != nil { | ||
return 0, errors.Trace(err) | ||
} | ||
} | ||
return totalMemory, nil | ||
} | ||
|
||
// CheckMemoryUsage checks if the memory usage is less than the limit. | ||
func CheckMemoryUsage(limit float64) (bool, error) { | ||
stat, err := mem.VirtualMemory() | ||
if err != nil { | ||
return false, err | ||
} | ||
return stat.UsedPercent < limit, nil | ||
} | ||
|
||
// WaitMemoryAvailable waits until the memory usage is less than the limit. | ||
func WaitMemoryAvailable(limit float64, timeout time.Duration) error { | ||
start := time.Now() | ||
for { | ||
hasFreeMemory, err := CheckMemoryUsage(limit) | ||
if err != nil { | ||
return err | ||
} | ||
if hasFreeMemory { | ||
return nil | ||
} | ||
if time.Since(start) > timeout { | ||
return errors.ErrWaitFreeMemoryTimeout.GenWithStackByArgs() | ||
} | ||
} | ||
} |
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,27 @@ | ||
// Copyright 2023 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetMemoryLimit(t *testing.T) { | ||
t.Parallel() | ||
limit, err := GetMemoryLimit() | ||
require.NoError(t, err) | ||
require.Less(t, limit, memoryMax) | ||
} |