forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hanqing Wu <[email protected]>
- Loading branch information
1 parent
17070cf
commit 5b6ce69
Showing
10 changed files
with
191 additions
and
9 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
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,61 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase 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. | ||
*/ | ||
|
||
#include "src/client/utils.h" | ||
|
||
#include <glog/logging.h> | ||
#include <sys/resource.h> | ||
|
||
#include <cerrno> | ||
|
||
namespace curve { | ||
namespace client { | ||
|
||
bool AdjustOpenFileSoftLimitToHardLimit(uint64_t limit) { | ||
if (limit == 0) { | ||
return true; | ||
} | ||
|
||
struct rlimit rlim; | ||
int rc = getrlimit(RLIMIT_NOFILE, &rlim); | ||
|
||
if (rc != 0) { | ||
LOG(WARNING) << "getrlimit failed, error: " << strerror(errno); | ||
return false; | ||
} | ||
|
||
if (rlim.rlim_max < limit) { | ||
LOG(WARNING) << "hard limit: " << rlim.rlim_max | ||
<< " is less than min limit: " << limit; | ||
return false; | ||
} | ||
|
||
if (rlim.rlim_cur != rlim.rlim_max) { | ||
rlim.rlim_cur = rlim.rlim_max; | ||
rc = setrlimit(RLIMIT_NOFILE, &rlim); | ||
if (rc != 0) { | ||
LOG(WARNING) << "setrlimit failed, error: " << strerror(errno); | ||
return false; | ||
} | ||
} | ||
|
||
LOG(INFO) << "set hard limit: " << rlim.rlim_max | ||
<< " soft limit: " << rlim.rlim_cur; | ||
return true; | ||
} | ||
|
||
} // namespace client | ||
} // namespace curve |
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,33 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase 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. | ||
*/ | ||
|
||
#ifndef SRC_CLIENT_UTILS_H_ | ||
#define SRC_CLIENT_UTILS_H_ | ||
|
||
#include <cstdint> | ||
|
||
namespace curve { | ||
namespace client { | ||
|
||
// Adjust the soft limit of open files to hard limit. | ||
// If |limit| equals 0, then directly return true. | ||
// If hard limit is less than |limit| than return false. | ||
bool AdjustOpenFileSoftLimitToHardLimit(uint64_t limit); | ||
|
||
} // namespace client | ||
} // namespace curve | ||
|
||
#endif // SRC_CLIENT_UTILS_H_ |
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,48 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase 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. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
#include <sys/resource.h> | ||
|
||
#include "src/client/utils.h" | ||
|
||
namespace curve { | ||
namespace client { | ||
|
||
TEST(AdjustOpenFileSoftLimitToHardLimitTest, TestAdjustToZero) { | ||
ASSERT_TRUE(AdjustOpenFileSoftLimitToHardLimit(0)); | ||
} | ||
|
||
TEST(AdjustOpenFileSoftLimitToHardLimitTest, TestExceedHardLimit) { | ||
struct rlimit rlim; | ||
ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlim)); | ||
ASSERT_FALSE(AdjustOpenFileSoftLimitToHardLimit(rlim.rlim_max + 1)); | ||
} | ||
|
||
TEST(AdjustOpenFileSoftLimitToHardLimitTest, TestAdjustToHardLimit) { | ||
struct rlimit rlim; | ||
ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlim)); | ||
|
||
rlim.rlim_cur = rlim.rlim_max / 2; | ||
ASSERT_EQ(0, setrlimit(RLIMIT_NOFILE, &rlim)); | ||
|
||
ASSERT_TRUE(AdjustOpenFileSoftLimitToHardLimit(rlim.rlim_max - 1)); | ||
ASSERT_EQ(0, getrlimit(RLIMIT_NOFILE, &rlim)); | ||
ASSERT_EQ(rlim.rlim_cur, rlim.rlim_max); | ||
} | ||
|
||
} // namespace client | ||
} // namespace curve |