Skip to content

Commit 7e87b34

Browse files
r-barnesmeta-codesync[bot]
authored andcommitted
folly::format -> fmt::format in cachelib
Summary: Mechanical migration of `folly::sformat(...)` call sites to `fmt::format(...)` (adding `#include <fmt/format.h>` where needed) in `cachelib`. `folly::sformat` and `fmt::format` share identical format-string syntax, so this is a semantically-equivalent, token-level rewrite produced by the `folly-to-fmt-format` clang codemod, run via the migration farm in `fbcode/scripts/rbarnes/folly_to_fmt` (D112466250). Part of the fbcode-wide `folly::format` -> `fmt::format` migration. This diff covers 27 file(s). Reviewed By: rlyerly Differential Revision: D113286692 fbshipit-source-id: 4e0ab9847908f3d0dbf827308b580b4aaba445ba
1 parent 94aa520 commit 7e87b34

17 files changed

Lines changed: 150 additions & 129 deletions

cachelib/allocator/nvmcache/NavyConfig.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "cachelib/allocator/nvmcache/NavyConfig.h"
1818

19+
#include <fmt/format.h>
20+
1921
#include <stdexcept>
2022
#include <string>
2123
#include <vector>
@@ -35,7 +37,7 @@ const std::vector<std::string>& NavyConfig::getRaidPaths() const {
3537
// admission policy settings
3638
RandomAPConfig& NavyConfig::enableRandomAdmPolicy() {
3739
if (!admissionPolicy_.empty()) {
38-
throw std::invalid_argument(folly::sformat(
40+
throw std::invalid_argument(fmt::format(
3941
"{} admission policy is already enabled", admissionPolicy_));
4042
}
4143
admissionPolicy_ = "random";
@@ -44,7 +46,7 @@ RandomAPConfig& NavyConfig::enableRandomAdmPolicy() {
4446

4547
DynamicRandomAPConfig& NavyConfig::enableDynamicRandomAdmPolicy() {
4648
if (!admissionPolicy_.empty()) {
47-
throw std::invalid_argument(folly::sformat(
49+
throw std::invalid_argument(fmt::format(
4850
"{} admission policy is already enabled", admissionPolicy_));
4951
}
5052
admissionPolicy_ = "dynamic_random";
@@ -53,7 +55,7 @@ DynamicRandomAPConfig& NavyConfig::enableDynamicRandomAdmPolicy() {
5355

5456
RandomAPConfig& RandomAPConfig::setAdmProbability(double admProbability) {
5557
if (admProbability < 0 || admProbability > 1) {
56-
throw std::invalid_argument(folly::sformat(
58+
throw std::invalid_argument(fmt::format(
5759
"admission probability should be in the range of [0, 1], but {} is set",
5860
admProbability));
5961
}
@@ -89,7 +91,7 @@ void NavyConfig::enableAsyncIo(uint32_t maxNumReads,
8991
}
9092

9193
if ((maxNumReads % readerThreads_) || (maxNumWrites % writerThreads_)) {
92-
throw std::invalid_argument(folly::sformat(
94+
throw std::invalid_argument(fmt::format(
9395
"reader threads ({}) and writer threads ({}) should divide evenly "
9496
"into maxNumReads ({}) or maxNumWrites ({})",
9597
readerThreads_, writerThreads_, maxNumReads, maxNumWrites));
@@ -130,7 +132,7 @@ void NavyConfig::setRaidFiles(std::vector<std::string> raidPaths,
130132
throw std::invalid_argument("already set a simple file");
131133
}
132134
if (raidPaths.size() <= 1) {
133-
throw std::invalid_argument(folly::sformat(
135+
throw std::invalid_argument(fmt::format(
134136
"RAID needs at least two paths, but {} path is set", raidPaths.size()));
135137
}
136138
raidPaths_ = std::move(raidPaths);
@@ -167,7 +169,7 @@ BlockCacheConfig& BlockCacheConfig::enableCustomReinsertion(
167169
BlockCacheConfig& BlockCacheConfig::setCleanRegions(
168170
uint32_t cleanRegions, uint32_t cleanRegionThreads) {
169171
if (!cleanRegionThreads || cleanRegionThreads > cleanRegions + 1) {
170-
throw std::invalid_argument(folly::sformat(
172+
throw std::invalid_argument(fmt::format(
171173
"number of clean region threads should be in the range of [1, {}]",
172174
cleanRegions + 1));
173175
}
@@ -219,7 +221,7 @@ BlockCacheConfig& BlockCacheConfig::enableFixedSizeIndex(
219221
BigHashConfig& BigHashConfig::setSizePctAndMaxItemSize(
220222
unsigned int sizePct, uint64_t smallItemMaxSize) {
221223
if (sizePct > 100) {
222-
throw std::invalid_argument(folly::sformat(
224+
throw std::invalid_argument(fmt::format(
223225
"to enable BigHash, BigHash size pct should be in the range of [0, 100]"
224226
", but {} is set",
225227
sizePct));
@@ -261,7 +263,7 @@ void NavyConfig::setReaderAndWriterThreads(unsigned int readerThreads,
261263

262264
if (maxNumReads > 0 || maxNumWrites > 0) {
263265
if ((maxNumReads % readerThreads_) || (maxNumWrites % writerThreads_)) {
264-
throw std::invalid_argument(folly::sformat(
266+
throw std::invalid_argument(fmt::format(
265267
"reader threads ({}) and writer threads ({}) should divide evenly "
266268
"into maxNumReads ({}) or maxNumWrites ({})",
267269
readerThreads_, writerThreads_, maxNumReads, maxNumWrites));

cachelib/allocator/nvmcache/NavySetup.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "cachelib/allocator/nvmcache/NavySetup.h"
1818

19+
#include <fmt/format.h>
1920
#include <folly/logging/xlog.h>
2021
#include <gmock/gmock.h>
2122

@@ -49,7 +50,7 @@ uint64_t getRegionSize(const navy::NavyConfig& config) {
4950
uint64_t regionSize = configs[0].blockCache().getRegionSize();
5051
for (size_t idx = 1; idx < configs.size(); idx++) {
5152
if (regionSize != configs[idx].blockCache().getRegionSize()) {
52-
throw std::invalid_argument(folly::sformat(
53+
throw std::invalid_argument(fmt::format(
5354
"Blockcache {} region size: {}, not equal to block cache 0: {}", idx,
5455
configs[idx].blockCache().getRegionSize(), regionSize));
5556
}
@@ -78,8 +79,8 @@ uint64_t setupBigHash(const navy::BigHashConfig& bigHashConfig,
7879
auto bucketSize = bigHashConfig.getBucketSize();
7980
if (bucketSize != alignUp(bucketSize, ioAlignSize)) {
8081
throw std::invalid_argument(
81-
folly::sformat("Bucket size: {} is not aligned to ioAlignSize: {}",
82-
bucketSize, ioAlignSize));
82+
fmt::format("Bucket size: {} is not aligned to ioAlignSize: {}",
83+
bucketSize, ioAlignSize));
8384
}
8485

8586
const uint64_t bigHashCacheOffset =
@@ -142,8 +143,8 @@ uint64_t setupBlockCache(const navy::BlockCacheConfig& blockCacheConfig,
142143
auto regionSize = blockCacheConfig.getRegionSize();
143144
if (regionSize != alignUp(regionSize, ioAlignSize)) {
144145
throw std::invalid_argument(
145-
folly::sformat("Region size: {} is not aligned to ioAlignSize: {}",
146-
regionSize, ioAlignSize));
146+
fmt::format("Region size: {} is not aligned to ioAlignSize: {}",
147+
regionSize, ioAlignSize));
147148
}
148149

149150
// Adjust starting size of block cache to ensure it is aligned to region
@@ -242,9 +243,9 @@ void setupCacheProtos(const navy::NavyConfig& config,
242243
metadataSize = alignUp(metadataSize, ioAlignSize);
243244
if (metadataSize >= totalCacheSize) {
244245
throw std::invalid_argument{
245-
folly::sformat("Invalid metadata size: {}. Cache size: {}",
246-
metadataSize,
247-
totalCacheSize)};
246+
fmt::format("Invalid metadata size: {}. Cache size: {}",
247+
metadataSize,
248+
totalCacheSize)};
248249
}
249250
proto.setMetadataSize(metadataSize);
250251

@@ -293,10 +294,10 @@ void setupCacheProtos(const navy::NavyConfig& config,
293294
config.getStackSize(), *enginePairProto);
294295
}
295296
if (blockCacheEndOffset > bigHashStartOffset) {
296-
throw std::invalid_argument(folly::sformat(
297-
"Invalid engine size configurations. block cache ends at "
298-
"{}, big hash starts at {}.",
299-
blockCacheEndOffset, bigHashStartOffset));
297+
throw std::invalid_argument(
298+
fmt::format("Invalid engine size configurations. block cache ends at "
299+
"{}, big hash starts at {}.",
300+
blockCacheEndOffset, bigHashStartOffset));
300301
}
301302
proto.addEnginePair(std::move(enginePairProto));
302303
bigHashEndOffset = bigHashStartOffset;
@@ -317,7 +318,7 @@ void setAdmissionPolicy(const cachelib::navy::NavyConfig& config,
317318
proto.setDynamicRandomAdmissionPolicy(config.dynamicRandomAdmPolicy());
318319
} else {
319320
throw std::invalid_argument{
320-
folly::sformat("invalid policy name {}", policyName)};
321+
fmt::format("invalid policy name {}", policyName)};
321322
}
322323
}
323324

cachelib/allocator/nvmcache/NvmItem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#pragma GCC diagnostic push
2020
#pragma GCC diagnostic ignored "-Wconversion"
21+
#include <fmt/format.h>
2122
#include <folly/Format.h>
2223
#pragma GCC diagnostic pop
2324

@@ -29,7 +30,7 @@ namespace cachelib {
2930
Blob NvmItem::getBlob(size_t index) const {
3031
if (index >= numBlobs_) {
3132
throw std::invalid_argument(
32-
folly::sformat("Index {} out of range {}", index, numBlobs_));
33+
fmt::format("Index {} out of range {}", index, numBlobs_));
3334
}
3435

3536
const auto& blobInfo = getBlobInfo(index);
@@ -55,7 +56,7 @@ NvmItem::NvmItem(PoolId id,
5556
auto& blobInfo = getBlobInfo(index++);
5657
if (offset + blob.data.size() >
5758
std::numeric_limits<decltype(blobInfo.endOffset)>::max()) {
58-
throw std::out_of_range(folly::sformat(
59+
throw std::out_of_range(fmt::format(
5960
"new offset {} is out of range. blob size {}, num blobs{}",
6061
offset,
6162
blob.data.size(),
@@ -79,7 +80,7 @@ NvmItem::NvmItem(PoolId id,
7980
if (blob.data.size() >
8081
std::numeric_limits<decltype(blobInfo.endOffset)>::max()) {
8182
throw std::out_of_range(
82-
folly::sformat("blob is too big. size {}", blob.data.size()));
83+
fmt::format("blob is too big. size {}", blob.data.size()));
8384
}
8485
std::memcpy(getDataBegin(), blob.data.data(), blob.data.size());
8586
blobInfo.origAllocSize = blob.origAllocSize;

cachelib/allocator/nvmcache/tests/NvmCacheTests.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <fmt/format.h>
1718
#include <folly/Random.h>
1819
#include <gtest/gtest.h>
1920

@@ -195,7 +196,7 @@ TEST_F(NvmCacheTest, EvictToNvmGet) {
195196
const int nKeys = 1024;
196197

197198
for (unsigned int i = 0; i < nKeys; i++) {
198-
auto key = folly::sformat("key{}", i);
199+
auto key = fmt::format("key{}", i);
199200
auto it = nvm.allocate(pid, key, 15 * 1024);
200201
ASSERT_NE(nullptr, it);
201202
nvm.insertOrReplace(it);
@@ -221,7 +222,7 @@ TEST_F(NvmCacheTest, EvictToNvmGet) {
221222
// reading items from navy.
222223
for (unsigned int i = nKeys + 100; i-- > 0;) {
223224
unsigned int index = i;
224-
auto key = folly::sformat("key{}", index);
225+
auto key = fmt::format("key{}", index);
225226
auto hdl = this->fetch(key, false /* ramOnly */);
226227
hdl.wait();
227228
if (index < nKeys) {
@@ -234,14 +235,14 @@ TEST_F(NvmCacheTest, EvictToNvmGet) {
234235
// associated with it on the local thread. It is adjusted at destruction
235236
// time to be net-zero for handle count (first an Inc, and then a Dec).
236237
EXPECT_EQ(0, nvm.getHandleCountForThread())
237-
<< folly::sformat("key: {} was read from Navy", key);
238+
<< fmt::format("key: {} was read from Navy", key);
238239
ASSERT_TRUE(isClean);
239240
ASSERT_TRUE(hdl.wentToNvm());
240241
} else {
241242
// A handle read from ram-cache will have incremented the thread-local
242243
// handle count when we have acquired the handle.
243244
EXPECT_EQ(1, nvm.getHandleCountForThread())
244-
<< folly::sformat("key: {} was read from RAM cache", key);
245+
<< fmt::format("key: {} was read from RAM cache", key);
245246
ASSERT_FALSE(isClean);
246247
}
247248
} else {
@@ -1922,7 +1923,7 @@ TEST_F(NvmCacheTest, NavyStats) {
19221923
TEST_F(NvmCacheTest, Raid0Basic) {
19231924
auto& config = getConfig();
19241925
auto& navyConfig = config.nvmConfig->navyConfig;
1925-
auto filePath = folly::sformat("/tmp/nvmcache-navy-raid0/{}", ::getpid());
1926+
auto filePath = fmt::format("/tmp/nvmcache-navy-raid0/{}", ::getpid());
19261927
util::makeDir(filePath);
19271928
SCOPE_EXIT { util::removePath(filePath); };
19281929

@@ -1988,7 +1989,7 @@ TEST_F(NvmCacheTest, Raid0Basic) {
19881989
TEST_F(NvmCacheTest, Raid0OrderChange) {
19891990
auto& config = getConfig();
19901991
auto& navyConfig = config.nvmConfig->navyConfig;
1991-
auto filePath = folly::sformat("/tmp/nvmcache-navy-raid0/{}", ::getpid());
1992+
auto filePath = fmt::format("/tmp/nvmcache-navy-raid0/{}", ::getpid());
19921993
util::makeDir(filePath);
19931994
SCOPE_EXIT { util::removePath(filePath); };
19941995

@@ -2001,7 +2002,7 @@ TEST_F(NvmCacheTest, Raid0OrderChange) {
20012002
// that everything is correct.
20022003
std::string val = "foobar";
20032004
int nKeys = 100;
2004-
auto makeKey = [&](int i) { return folly::sformat("blah-{}", i); };
2005+
auto makeKey = [&](int i) { return fmt::format("blah-{}", i); };
20052006

20062007
this->convertToShmCache();
20072008
{
@@ -2062,7 +2063,7 @@ TEST_F(NvmCacheTest, Raid0OrderChange) {
20622063
TEST_F(NvmCacheTest, Raid0NumFilesChange) {
20632064
auto& config = getConfig();
20642065
auto& navyConfig = config.nvmConfig->navyConfig;
2065-
auto filePath = folly::sformat("/tmp/nvmcache-navy-raid0/{}", ::getpid());
2066+
auto filePath = fmt::format("/tmp/nvmcache-navy-raid0/{}", ::getpid());
20662067
util::makeDir(filePath);
20672068
SCOPE_EXIT { util::removePath(filePath); };
20682069

@@ -2075,7 +2076,7 @@ TEST_F(NvmCacheTest, Raid0NumFilesChange) {
20752076
// that everything is correct.
20762077
std::string val = "foobar";
20772078
int nKeys = 100;
2078-
auto makeKey = [&](int i) { return folly::sformat("blah-{}", i); };
2079+
auto makeKey = [&](int i) { return fmt::format("blah-{}", i); };
20792080

20802081
this->convertToShmCache();
20812082
{
@@ -2135,7 +2136,7 @@ TEST_F(NvmCacheTest, Raid0NumFilesChange) {
21352136
TEST_F(NvmCacheTest, Raid0SizeChange) {
21362137
auto& config = getConfig();
21372138
auto& navyConfig = config.nvmConfig->navyConfig;
2138-
auto filePath = folly::sformat("/tmp/nvmcache-navy-raid0/{}", ::getpid());
2139+
auto filePath = fmt::format("/tmp/nvmcache-navy-raid0/{}", ::getpid());
21392140
util::makeDir(filePath);
21402141
SCOPE_EXIT { util::removePath(filePath); };
21412142
std::vector<std::string> vec = {filePath + "/CACHE0", filePath + "/CACHE1",
@@ -2147,7 +2148,7 @@ TEST_F(NvmCacheTest, Raid0SizeChange) {
21472148
// that everything is correct.
21482149
std::string val = "foobar";
21492150
int nKeys = 100;
2150-
auto makeKey = [&](int i) { return folly::sformat("blah-{}", i); };
2151+
auto makeKey = [&](int i) { return fmt::format("blah-{}", i); };
21512152

21522153
this->convertToShmCache();
21532154
{
@@ -2473,7 +2474,7 @@ TEST_F(NvmCacheTest, testSampleItem) {
24732474
// Insert items until either RAM or NVM cache is full
24742475
for (; numEvicted == 0 && nKeys < numMax; nKeys++) {
24752476
unsigned ttl = nKeys % 2 == 0 ? kEvenKeyTTL : 0;
2476-
auto key = folly::sformat("key{}", nKeys);
2477+
auto key = fmt::format("key{}", nKeys);
24772478
// the pool's allocsize is
24782479
auto it = cache.allocate(pid, key, 16 * 1024, ttl);
24792480
ASSERT_NE(nullptr, it);
@@ -3033,7 +3034,7 @@ TEST_F(NvmCacheTest, AccessTimeMapPopulatedOnDramEviction) {
30333034
auto now = util::getCurrentTimeSec();
30343035
int populated = 0;
30353036
for (int i = 0; i < nKeys; i++) {
3036-
auto key = folly::sformat("atm_multi_{}", i);
3037+
auto key = fmt::format("atm_multi_{}", i);
30373038
HashedKey hk{key};
30383039
auto ts = atm->get(hk.keyHash());
30393040
if (ts != std::nullopt) {
@@ -3067,7 +3068,7 @@ TEST_F(NvmCacheTest, AccessTimeMapNotUpdatedForBigHashItems) {
30673068
// bit is not set for BigHash items, preventing updateAccessTime().
30683069
auto* atm = this->getAccessTimeMap();
30693070
for (int i = 0; i < nKeys; i++) {
3070-
auto key = folly::sformat("bh_{}", i);
3071+
auto key = fmt::format("bh_{}", i);
30713072
HashedKey hk{key};
30723073
auto ts = atm->get(hk.keyHash());
30733074
EXPECT_EQ(std::nullopt, ts)
@@ -3090,7 +3091,7 @@ TEST_F(NvmCacheTest, AccessTimeMapNotUpdatedOnRegularEviction) {
30903091
// Evictions of these items go through the NVM put path, not
30913092
// the updateAccessTime path.
30923093
for (int i = 0; i < 1024; i++) {
3093-
auto key = folly::sformat("regular_{}", i);
3094+
auto key = fmt::format("regular_{}", i);
30943095
auto it = nvm.allocate(pid, key, allocSize);
30953096
ASSERT_NE(nullptr, it);
30963097
cache_->insertOrReplace(it);
@@ -3140,7 +3141,7 @@ TEST_F(NvmCacheTest, AccessTimeMapCleanupTest) {
31403141
constexpr int kNumGroups = 4;
31413142
std::array<std::vector<std::string>, kNumGroups> groups;
31423143
for (int i = 0; i < nKeys; i++) {
3143-
auto key = folly::sformat("atm_cl_{}", i);
3144+
auto key = fmt::format("atm_cl_{}", i);
31443145
HashedKey hk{key};
31453146
if (atm->get(hk.keyHash()) != std::nullopt) {
31463147
groups[i % kNumGroups].push_back(key);
@@ -3183,7 +3184,7 @@ TEST_F(NvmCacheTest, AccessTimeMapCleanupTest) {
31833184
const uint32_t numKeysPerRegion =
31843185
config_.blockCache().getRegionSize() / allocSize;
31853186
for (int i = 0; i < 2048; i++) {
3186-
auto key = folly::sformat("nvm_evictor_{}", i);
3187+
auto key = fmt::format("nvm_evictor_{}", i);
31873188
auto it = nvm.allocate(pid, key, allocSize);
31883189
ASSERT_NE(nullptr, it);
31893190
cache_->insertOrReplace(it);
@@ -3234,7 +3235,7 @@ TEST_F(NvmCacheTest, AccessTimeMapSurvivesWarmRoll) {
32343235
auto timeBefore = util::getCurrentTimeSec();
32353236
auto evictBefore = this->evictionCount();
32363237
for (int i = 0; i < 1024; i++) {
3237-
auto key = folly::sformat("atm_warm_roll_filler_{}", i);
3238+
auto key = fmt::format("atm_warm_roll_filler_{}", i);
32383239
auto it = nvm.allocate(pid, key, allocSize);
32393240
ASSERT_NE(nullptr, it);
32403241
cache_->insertOrReplace(it);

0 commit comments

Comments
 (0)