storage/nvm/pet_kv/persistence.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | #include "base/base.h" | ||
| 3 | #include "base/log.h" | ||
| 4 | |||
| 5 | static const int kCACHELINE_SIZE = 64; | ||
| 6 | |||
| 7 | #define IF_Persistence(something) \ | ||
| 8 | if (Persistence) { \ | ||
| 9 | something \ | ||
| 10 | } | ||
| 11 | |||
| 12 | namespace base { | ||
| 13 | class FENCE { | ||
| 14 | public: | ||
| 15 | static void mfence() { asm volatile("mfence" ::: "memory"); } | ||
| 16 | static void lfence() { asm volatile("lfence" ::: "memory"); } | ||
| 17 | 112446 | static void sfence() { asm volatile("sfence" ::: "memory"); } | |
| 18 | }; | ||
| 19 | |||
| 20 | 11804978 | inline void clflushopt(void* addr) { | |
| 21 | 11804978 | return; | |
| 22 | static int isClflushoptEnabled = !system("lscpu |grep clflushopt >/dev/null"); | ||
| 23 | static int isClwbEnabled = !system("lscpu |grep clfw >/dev/null"); | ||
| 24 | static int isClflushEnabled = !system("lscpu |grep clflush >/dev/null"); | ||
| 25 | if (isClflushoptEnabled) { | ||
| 26 | asm volatile(".byte 0x66; clflush %0" : "+m"(*(volatile char*)(addr))); | ||
| 27 | } else if (isClwbEnabled) { | ||
| 28 | asm volatile(".byte 0x66; xsaveopt %0" : "+m"(*(volatile char*)(addr))); | ||
| 29 | } else if (isClflushEnabled) { | ||
| 30 | asm volatile("clflush %0" : "+m"(*(volatile char*)addr)); | ||
| 31 | } else { | ||
| 32 | LOG(FATAL) << "system does't support clflush-like instrs"; | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | 112446 | inline void clflushopt_range(void* des, size_t size) { | |
| 38 | 112446 | char* addr = reinterpret_cast<char*>(des); | |
| 39 | 112446 | size = size + ((uint64)(addr) & (kCACHELINE_SIZE - 1)); | |
| 40 |
2/2✓ Branch 0 taken 11804978 times.
✓ Branch 1 taken 112446 times.
|
11917424 | for (size_t i = 0; i < size; i += kCACHELINE_SIZE) { |
| 41 | 11804978 | clflushopt(addr + i); | |
| 42 | } | ||
| 43 | 112446 | FENCE::sfence(); | |
| 44 | 112446 | } | |
| 45 | }; // namespace base | ||
| 46 |