| 156 | std::atomic<int> buffer_test_finished_cores = 0; |
| 157 | |
| 158 | auto spinlock_smp_buffer_test() -> bool { |
| 159 | // 每个核心尝试写入一定次数 |
| 160 | int writes_per_core = 500; |
| 161 | |
| 162 | for (int i = 0; i < writes_per_core; ++i) { |
| 163 | (void)buffer_lock.Lock(); |
| 164 | if (buffer_index < BUFFER_SIZE) { |
| 165 | // 写入 Core ID |
| 166 | shared_buffer[buffer_index++] = cpu_io::GetCurrentCoreId(); |
| 167 | } |
| 168 | (void)buffer_lock.UnLock(); |
| 169 | } |
| 170 | |
| 171 | int finished = buffer_test_finished_cores.fetch_add(1) + 1; |
| 172 | int total_cores = BasicInfoSingleton::instance().core_count; |
| 173 | |
| 174 | if (finished == total_cores) { |
| 175 | // 最后一个完成的核心进行检查 |
| 176 | klog::Info("All cores finished buffer writes. Checking buffer..."); |
| 177 | |
| 178 | // 检查写入总数 |
| 179 | int expected_writes = writes_per_core * total_cores; |
| 180 | if (expected_writes > BUFFER_SIZE) expected_writes = BUFFER_SIZE; |
| 181 | |
| 182 | if (buffer_index != expected_writes) { |
| 183 | klog::Err("FAIL: Buffer index {}, expected {}", buffer_index, |
| 184 | expected_writes); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | klog::Info("Buffer test passed. Final index: {}", buffer_index); |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | constexpr int STR_BUFFER_SIZE = 512 * 1024; |
| 196 | // 使用更大的缓冲区以容纳所有字符串 |
no test coverage detected