Skip to content

Commit 63a2346

Browse files
spikehfacebook-github-bot
authored andcommitted
IoUringProvidedBufferRing: use folly::align_ceil
Summary: Use folly::align_ceil instead of calculating it by hand, which was buggy when huge_pages = false. Add a very basic test to validate both huge_pages = true and false. Reviewed By: yfeldblum Differential Revision: D72349393 fbshipit-source-id: 97b35e5f81e8975add5932440ddeef956562d1fd
1 parent 77a6924 commit 63a2346

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

folly/io/async/IoUringProvidedBufferRing.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <folly/Conv.h>
2020
#include <folly/ExceptionString.h>
2121
#include <folly/String.h>
22+
#include <folly/lang/Align.h>
2223

2324
#if FOLLY_HAS_LIBURING
2425

@@ -32,7 +33,7 @@ IoUringProvidedBufferRing::ProvidedBuffersBuffer::ProvidedBuffersBuffer(
3233
ringMask_ = ringCount - 1;
3334
ringMemSize_ = sizeof(struct io_uring_buf) * ringCount;
3435

35-
ringMemSize_ = (ringMemSize_ + kBufferAlignMask) & (~kBufferAlignMask);
36+
ringMemSize_ = align_ceil(ringMemSize_, kBufferAlignBytes);
3637

3738
if (bufferShift_ < 5) {
3839
bufferShift_ = 5; // for alignment
@@ -44,11 +45,11 @@ IoUringProvidedBufferRing::ProvidedBuffersBuffer::ProvidedBuffersBuffer(
4445

4546
int pages;
4647
if (huge_pages) {
47-
allSize_ = (allSize_ + kHugePageMask) & (~kHugePageMask);
48-
pages = allSize_ / (1 + kHugePageMask);
48+
allSize_ = align_ceil(allSize_, kHugePageSizeBytes);
49+
pages = allSize_ / kHugePageSizeBytes;
4950
} else {
50-
allSize_ = (kPageMask + kPageMask) & ~kPageMask;
51-
pages = allSize_ / (1 + kPageMask);
51+
allSize_ = align_ceil(allSize_, kPageSizeBytes);
52+
pages = allSize_ / kPageSizeBytes;
5253
}
5354

5455
buffer_ = ::mmap(

folly/io/async/IoUringProvidedBufferRing.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ class IoUringProvidedBufferRing : public IoUringBufferProviderBase {
106106
char* bufferBuffer_;
107107
uint32_t bufferCount_;
108108

109-
// static constexpr
110-
static constexpr size_t kHugePageMask = (1LLU << 21) - 1; // 2MB
111-
static constexpr size_t kPageMask = (1LLU << 12) - 1; // 4095
112-
static constexpr size_t kBufferAlignMask{31LLU};
109+
static constexpr size_t kHugePageSizeBytes = 1024 * 1024 * 2;
110+
static constexpr size_t kPageSizeBytes = 4096;
111+
static constexpr size_t kBufferAlignBytes = 32;
113112
};
114113

115114
io_uring* ioRingPtr_;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <folly/io/async/IoUringProvidedBufferRing.h>
18+
19+
#include <gtest/gtest.h>
20+
21+
#if FOLLY_HAS_LIBURING
22+
23+
using namespace ::testing;
24+
using namespace ::std;
25+
using namespace ::folly;
26+
27+
int get_shift(int x) {
28+
int shift = findLastSet(x) - 1;
29+
if (x != (1 << shift)) {
30+
shift++;
31+
}
32+
return shift;
33+
}
34+
35+
struct IoUringProvidedBufferRingTest : testing::Test {};
36+
37+
TEST_F(IoUringProvidedBufferRingTest, Create) {
38+
io_uring ring{};
39+
io_uring_queue_init(512, &ring, 0);
40+
int sizeShift = std::max<int>(get_shift(4096), 5);
41+
int ringShift = std::max<int>(get_shift(1000), 1);
42+
IoUringProvidedBufferRing bufRing{&ring, 1, 1000, sizeShift, ringShift};
43+
EXPECT_EQ(bufRing.count(), 1000);
44+
}
45+
46+
#endif

0 commit comments

Comments
 (0)