struct/class that require cacheline alignment may not work when using new (before c++17).
- Dominant language
- C++
- Stars
- 17.6k
- Forks
- 4.1k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 69
Description
**Is your feature request related to a problem? (你需要的功能是否与某个问题有关?)**
当前 brpc 代码默认使用的 C++ 标准为 11,不支持 align new (since c++17 https://en.cppreference.com/w/cpp/memory/new/operator_new) .
如果某个 class 指定了 alignment 要求(例如 BAIDU_CACHELINE_ALIGNMENT),代码中使用 new 的方式来分配其对象时,地址有可能并不是严格按照其对齐方式的。
> 要让一个变量或结构体按cacheline对齐,可以include 后使用BAIDU_CACHELINE_ALIGNMENT宏,请自行grep brpc的代码了解用法。
https://github.com/apache/brpc/blob/master/docs/cn/atomic_instructions.md#cacheline
例如以下代码,使用当前的编译选项,就可能会出现错误。
```c++
class BAIDU_CACHELINE_ALIGNMENT A {
int i;
};
int main() {
for(size_t i =0 ;i<100;i++) {
A* a = new A();
// maybe fail.
assert((reinterpret_cast(a) & (63)) == 0);
}
```
**Describe the solution you'd like (描述你期望的解决方法)**
使用 new 来分配指定对齐要求的类时,内存地址应满足对齐的要求。
升级为 C++17 标准,或者开启 -faligned_new (gcc 7.4+,clang 7.1.0+ 都已经支持) .
目前来看是强行关闭了该警告信息(不知道具体原因).
https://github.com/apache/brpc/blob/f3fe5fc4ff315aeed1f1e4b6c43c2ebf470d4381/CMakeLists.txt#L70-L72
**Describe alternatives you've considered (描述你想到的折衷方案)**
对于需要对齐的类,在使用 new 分配内存时使用 aligned_alloc/posix_memalign 等函数申请 alignment 内存,再使用 Placement new 指定内存空间进行初始化.
**Additional context/screenshots (更多上下文/截图)**
os: 20.04.1-Ubuntu
compiler: clang version 10.0.0-4ubuntu1
cpu: x86_64, cache_alignment : 64 byte
例如对于 ```class BAIDU_CACHELINE_ALIGNMENT/*note*/ Socket ``` 类:
socket 的地址为 `0x00005555567f1530`, 并不是 64 byte 对齐,违反了要求,可能引起 false-sharing.
Contributor guide
Research direction
Start with CMakeLists.txt at lines 70-72 and grep for BAIDU_CACHELINE_ALIGNMENT usages, especially the Socket class. Compare the supported C++11 compiler modes and the proposed C++17 or -faligned_new options; done means aligned classes allocated with new meet their declared alignment without breaking supported builds.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- build-system, performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100