| 373 | void SsdFile::write(std::vector<CachePin>& pins) { |
| 374 | // Sorts the pins by their file/offset. In this way what is adjacent in |
| 375 | // storage is likely adjacent on SSD. |
| 376 | std::sort(pins.begin(), pins.end()); |
| 377 | for (const auto& pin : pins) { |
| 378 | auto* entry = pin.checkedEntry(); |
| 379 | BOLT_CHECK_NULL(entry->ssdFile()); |
| 380 | } |
| 381 | |
| 382 | int32_t storeIndex = 0; |
| 383 | while (storeIndex < pins.size()) { |
| 384 | auto space = getSpace(pins, storeIndex); |
| 385 | if (!space.has_value()) { |
| 386 | // No space can be reclaimed. The pins are freed when the caller is freed. |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | auto [offset, available] = space.value(); |
| 391 | int32_t numWritten = 0; |
| 392 | int32_t bytes = 0; |
| 393 | std::vector<iovec> iovecs; |
| 394 | for (auto i = storeIndex; i < pins.size(); ++i) { |
| 395 | auto* entry = pins[i].checkedEntry(); |
| 396 | const auto entrySize = entry->size(); |
| 397 | if (bytes + entrySize > available) { |
| 398 | break; |
| 399 | } |
| 400 | addEntryToIovecs(*entry, iovecs); |
| 401 | bytes += entrySize; |
| 402 | ++numWritten; |
| 403 | } |
| 404 | BOLT_CHECK_GE(fileSize_, offset + bytes); |
| 405 | |
| 406 | const auto rc = folly::pwritev(fd_, iovecs.data(), iovecs.size(), offset); |
| 407 | if (rc != bytes) { |
| 408 | BOLT_SSD_CACHE_LOG(ERROR) |
| 409 | << "Failed to write to SSD, file name: " << fileName_ |
| 410 | << ", fd: " << fd_ << ", size: " << iovecs.size() |
| 411 | << ", offset: " << offset << ", error code: " << errno |
| 412 | << ", error string: " << folly::errnoStr(errno); |
| 413 | ++stats_.writeSsdErrors; |
| 414 | // If write fails, we return without adding the pins to the cache. The |
| 415 | // entries are unchanged. |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | { |
| 420 | std::lock_guard<std::shared_mutex> l(mutex_); |
| 421 | for (auto i = storeIndex; i < storeIndex + numWritten; ++i) { |
| 422 | auto* entry = pins[i].checkedEntry(); |
| 423 | entry->setSsdFile(this, offset); |
| 424 | const auto size = entry->size(); |
| 425 | FileCacheKey key = { |
| 426 | entry->key().fileNum, static_cast<uint64_t>(entry->offset())}; |
| 427 | entries_[std::move(key)] = SsdRun(offset, size); |
| 428 | if (FLAGS_bolt_ssd_verify_write) { |
| 429 | verifyWrite(*entry, SsdRun(offset, size)); |
| 430 | } |
| 431 | offset += size; |
| 432 | ++stats_.entriesWritten; |