Skip to content

Commit a6b3a20

Browse files
committedApr 29, 2020
Add some std:: qualifiers to types and functions.
PiperOrigin-RevId: 309110431
1 parent 3f934e3 commit a6b3a20

34 files changed

+345
-314
lines changed
 

‎benchmarks/db_bench.cc

+72-64
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ class Stats {
221221
double micros = now - last_op_finish_;
222222
hist_.Add(micros);
223223
if (micros > 20000) {
224-
fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
225-
fflush(stderr);
224+
std::fprintf(stderr, "long op: %.1f micros%30s\r", micros, "");
225+
std::fflush(stderr);
226226
}
227227
last_op_finish_ = now;
228228
}
@@ -243,8 +243,8 @@ class Stats {
243243
next_report_ += 50000;
244244
else
245245
next_report_ += 100000;
246-
fprintf(stderr, "... finished %d ops%30s\r", done_, "");
247-
fflush(stderr);
246+
std::fprintf(stderr, "... finished %d ops%30s\r", done_, "");
247+
std::fflush(stderr);
248248
}
249249
}
250250

@@ -261,18 +261,20 @@ class Stats {
261261
// elapsed times.
262262
double elapsed = (finish_ - start_) * 1e-6;
263263
char rate[100];
264-
snprintf(rate, sizeof(rate), "%6.1f MB/s",
265-
(bytes_ / 1048576.0) / elapsed);
264+
std::snprintf(rate, sizeof(rate), "%6.1f MB/s",
265+
(bytes_ / 1048576.0) / elapsed);
266266
extra = rate;
267267
}
268268
AppendWithSpace(&extra, message_);
269269

270-
fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n", name.ToString().c_str(),
271-
seconds_ * 1e6 / done_, (extra.empty() ? "" : " "), extra.c_str());
270+
std::fprintf(stdout, "%-12s : %11.3f micros/op;%s%s\n",
271+
name.ToString().c_str(), seconds_ * 1e6 / done_,
272+
(extra.empty() ? "" : " "), extra.c_str());
272273
if (FLAGS_histogram) {
273-
fprintf(stdout, "Microseconds per op:\n%s\n", hist_.ToString().c_str());
274+
std::fprintf(stdout, "Microseconds per op:\n%s\n",
275+
hist_.ToString().c_str());
274276
}
275-
fflush(stdout);
277+
std::fflush(stdout);
276278
}
277279
};
278280

@@ -323,51 +325,55 @@ class Benchmark {
323325
void PrintHeader() {
324326
const int kKeySize = 16;
325327
PrintEnvironment();
326-
fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
327-
fprintf(stdout, "Values: %d bytes each (%d bytes after compression)\n",
328-
FLAGS_value_size,
329-
static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
330-
fprintf(stdout, "Entries: %d\n", num_);
331-
fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
332-
((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /
333-
1048576.0));
334-
fprintf(stdout, "FileSize: %.1f MB (estimated)\n",
335-
(((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_) /
336-
1048576.0));
328+
std::fprintf(stdout, "Keys: %d bytes each\n", kKeySize);
329+
std::fprintf(
330+
stdout, "Values: %d bytes each (%d bytes after compression)\n",
331+
FLAGS_value_size,
332+
static_cast<int>(FLAGS_value_size * FLAGS_compression_ratio + 0.5));
333+
std::fprintf(stdout, "Entries: %d\n", num_);
334+
std::fprintf(stdout, "RawSize: %.1f MB (estimated)\n",
335+
((static_cast<int64_t>(kKeySize + FLAGS_value_size) * num_) /
336+
1048576.0));
337+
std::fprintf(
338+
stdout, "FileSize: %.1f MB (estimated)\n",
339+
(((kKeySize + FLAGS_value_size * FLAGS_compression_ratio) * num_) /
340+
1048576.0));
337341
PrintWarnings();
338-
fprintf(stdout, "------------------------------------------------\n");
342+
std::fprintf(stdout, "------------------------------------------------\n");
339343
}
340344

341345
void PrintWarnings() {
342346
#if defined(__GNUC__) && !defined(__OPTIMIZE__)
343-
fprintf(
347+
std::fprintf(
344348
stdout,
345349
"WARNING: Optimization is disabled: benchmarks unnecessarily slow\n");
346350
#endif
347351
#ifndef NDEBUG
348-
fprintf(stdout,
349-
"WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
352+
std::fprintf(
353+
stdout,
354+
"WARNING: Assertions are enabled; benchmarks unnecessarily slow\n");
350355
#endif
351356

352357
// See if snappy is working by attempting to compress a compressible string
353358
const char text[] = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy";
354359
std::string compressed;
355360
if (!port::Snappy_Compress(text, sizeof(text), &compressed)) {
356-
fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
361+
std::fprintf(stdout, "WARNING: Snappy compression is not enabled\n");
357362
} else if (compressed.size() >= sizeof(text)) {
358-
fprintf(stdout, "WARNING: Snappy compression is not effective\n");
363+
std::fprintf(stdout, "WARNING: Snappy compression is not effective\n");
359364
}
360365
}
361366

362367
void PrintEnvironment() {
363-
fprintf(stderr, "LevelDB: version %d.%d\n", kMajorVersion,
364-
kMinorVersion);
368+
std::fprintf(stderr, "LevelDB: version %d.%d\n", kMajorVersion,
369+
kMinorVersion);
365370

366371
#if defined(__linux)
367372
time_t now = time(nullptr);
368-
fprintf(stderr, "Date: %s", ctime(&now)); // ctime() adds newline
373+
std::fprintf(stderr, "Date: %s",
374+
ctime(&now)); // ctime() adds newline
369375

370-
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
376+
FILE* cpuinfo = std::fopen("/proc/cpuinfo", "r");
371377
if (cpuinfo != nullptr) {
372378
char line[1000];
373379
int num_cpus = 0;
@@ -387,9 +393,9 @@ class Benchmark {
387393
cache_size = val.ToString();
388394
}
389395
}
390-
fclose(cpuinfo);
391-
fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
392-
fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
396+
std::fclose(cpuinfo);
397+
std::fprintf(stderr, "CPU: %d * %s\n", num_cpus, cpu_type.c_str());
398+
std::fprintf(stderr, "CPUCache: %s\n", cache_size.c_str());
393399
}
394400
#endif
395401
}
@@ -516,14 +522,15 @@ class Benchmark {
516522
PrintStats("leveldb.sstables");
517523
} else {
518524
if (!name.empty()) { // No error message for empty name
519-
fprintf(stderr, "unknown benchmark '%s'\n", name.ToString().c_str());
525+
std::fprintf(stderr, "unknown benchmark '%s'\n",
526+
name.ToString().c_str());
520527
}
521528
}
522529

523530
if (fresh_db) {
524531
if (FLAGS_use_existing_db) {
525-
fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
526-
name.ToString().c_str());
532+
std::fprintf(stdout, "%-12s : skipped (--use_existing_db is true)\n",
533+
name.ToString().c_str());
527534
method = nullptr;
528535
} else {
529536
delete db_;
@@ -625,7 +632,7 @@ class Benchmark {
625632
bytes += size;
626633
}
627634
// Print so result is not dead
628-
fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
635+
std::fprintf(stderr, "... crc=0x%x\r", static_cast<unsigned int>(crc));
629636

630637
thread->stats.AddBytes(bytes);
631638
thread->stats.AddMessage(label);
@@ -649,8 +656,8 @@ class Benchmark {
649656
thread->stats.AddMessage("(snappy failure)");
650657
} else {
651658
char buf[100];
652-
snprintf(buf, sizeof(buf), "(output: %.1f%%)",
653-
(produced * 100.0) / bytes);
659+
std::snprintf(buf, sizeof(buf), "(output: %.1f%%)",
660+
(produced * 100.0) / bytes);
654661
thread->stats.AddMessage(buf);
655662
thread->stats.AddBytes(bytes);
656663
}
@@ -692,8 +699,8 @@ class Benchmark {
692699
options.reuse_logs = FLAGS_reuse_logs;
693700
Status s = DB::Open(options, FLAGS_db, &db_);
694701
if (!s.ok()) {
695-
fprintf(stderr, "open error: %s\n", s.ToString().c_str());
696-
exit(1);
702+
std::fprintf(stderr, "open error: %s\n", s.ToString().c_str());
703+
std::exit(1);
697704
}
698705
}
699706

@@ -712,7 +719,7 @@ class Benchmark {
712719
void DoWrite(ThreadState* thread, bool seq) {
713720
if (num_ != FLAGS_num) {
714721
char msg[100];
715-
snprintf(msg, sizeof(msg), "(%d ops)", num_);
722+
std::snprintf(msg, sizeof(msg), "(%d ops)", num_);
716723
thread->stats.AddMessage(msg);
717724
}
718725

@@ -725,15 +732,15 @@ class Benchmark {
725732
for (int j = 0; j < entries_per_batch_; j++) {
726733
const int k = seq ? i + j : (thread->rand.Next() % FLAGS_num);
727734
char key[100];
728-
snprintf(key, sizeof(key), "%016d", k);
735+
std::snprintf(key, sizeof(key), "%016d", k);
729736
batch.Put(key, gen.Generate(value_size_));
730737
bytes += value_size_ + strlen(key);
731738
thread->stats.FinishedSingleOp();
732739
}
733740
s = db_->Write(write_options_, &batch);
734741
if (!s.ok()) {
735-
fprintf(stderr, "put error: %s\n", s.ToString().c_str());
736-
exit(1);
742+
std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
743+
std::exit(1);
737744
}
738745
}
739746
thread->stats.AddBytes(bytes);
@@ -772,14 +779,14 @@ class Benchmark {
772779
for (int i = 0; i < reads_; i++) {
773780
char key[100];
774781
const int k = thread->rand.Next() % FLAGS_num;
775-
snprintf(key, sizeof(key), "%016d", k);
782+
std::snprintf(key, sizeof(key), "%016d", k);
776783
if (db_->Get(options, key, &value).ok()) {
777784
found++;
778785
}
779786
thread->stats.FinishedSingleOp();
780787
}
781788
char msg[100];
782-
snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
789+
std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
783790
thread->stats.AddMessage(msg);
784791
}
785792

@@ -789,7 +796,7 @@ class Benchmark {
789796
for (int i = 0; i < reads_; i++) {
790797
char key[100];
791798
const int k = thread->rand.Next() % FLAGS_num;
792-
snprintf(key, sizeof(key), "%016d.", k);
799+
std::snprintf(key, sizeof(key), "%016d.", k);
793800
db_->Get(options, key, &value);
794801
thread->stats.FinishedSingleOp();
795802
}
@@ -802,7 +809,7 @@ class Benchmark {
802809
for (int i = 0; i < reads_; i++) {
803810
char key[100];
804811
const int k = thread->rand.Next() % range;
805-
snprintf(key, sizeof(key), "%016d", k);
812+
std::snprintf(key, sizeof(key), "%016d", k);
806813
db_->Get(options, key, &value);
807814
thread->stats.FinishedSingleOp();
808815
}
@@ -815,14 +822,14 @@ class Benchmark {
815822
Iterator* iter = db_->NewIterator(options);
816823
char key[100];
817824
const int k = thread->rand.Next() % FLAGS_num;
818-
snprintf(key, sizeof(key), "%016d", k);
825+
std::snprintf(key, sizeof(key), "%016d", k);
819826
iter->Seek(key);
820827
if (iter->Valid() && iter->key() == key) found++;
821828
delete iter;
822829
thread->stats.FinishedSingleOp();
823830
}
824831
char msg[100];
825-
snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
832+
std::snprintf(msg, sizeof(msg), "(%d of %d found)", found, num_);
826833
thread->stats.AddMessage(msg);
827834
}
828835

@@ -835,14 +842,14 @@ class Benchmark {
835842
for (int j = 0; j < entries_per_batch_; j++) {
836843
const int k = seq ? i + j : (thread->rand.Next() % FLAGS_num);
837844
char key[100];
838-
snprintf(key, sizeof(key), "%016d", k);
845+
std::snprintf(key, sizeof(key), "%016d", k);
839846
batch.Delete(key);
840847
thread->stats.FinishedSingleOp();
841848
}
842849
s = db_->Write(write_options_, &batch);
843850
if (!s.ok()) {
844-
fprintf(stderr, "del error: %s\n", s.ToString().c_str());
845-
exit(1);
851+
std::fprintf(stderr, "del error: %s\n", s.ToString().c_str());
852+
std::exit(1);
846853
}
847854
}
848855
}
@@ -868,11 +875,11 @@ class Benchmark {
868875

869876
const int k = thread->rand.Next() % FLAGS_num;
870877
char key[100];
871-
snprintf(key, sizeof(key), "%016d", k);
878+
std::snprintf(key, sizeof(key), "%016d", k);
872879
Status s = db_->Put(write_options_, key, gen.Generate(value_size_));
873880
if (!s.ok()) {
874-
fprintf(stderr, "put error: %s\n", s.ToString().c_str());
875-
exit(1);
881+
std::fprintf(stderr, "put error: %s\n", s.ToString().c_str());
882+
std::exit(1);
876883
}
877884
}
878885

@@ -888,7 +895,7 @@ class Benchmark {
888895
if (!db_->GetProperty(key, &stats)) {
889896
stats = "(failed)";
890897
}
891-
fprintf(stdout, "\n%s\n", stats.c_str());
898+
std::fprintf(stdout, "\n%s\n", stats.c_str());
892899
}
893900

894901
static void WriteToFile(void* arg, const char* buf, int n) {
@@ -897,17 +904,18 @@ class Benchmark {
897904

898905
void HeapProfile() {
899906
char fname[100];
900-
snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db, ++heap_counter_);
907+
std::snprintf(fname, sizeof(fname), "%s/heap-%04d", FLAGS_db,
908+
++heap_counter_);
901909
WritableFile* file;
902910
Status s = g_env->NewWritableFile(fname, &file);
903911
if (!s.ok()) {
904-
fprintf(stderr, "%s\n", s.ToString().c_str());
912+
std::fprintf(stderr, "%s\n", s.ToString().c_str());
905913
return;
906914
}
907915
bool ok = port::GetHeapProfile(WriteToFile, file);
908916
delete file;
909917
if (!ok) {
910-
fprintf(stderr, "heap profiling not supported\n");
918+
std::fprintf(stderr, "heap profiling not supported\n");
911919
g_env->RemoveFile(fname);
912920
}
913921
}
@@ -962,8 +970,8 @@ int main(int argc, char** argv) {
962970
} else if (strncmp(argv[i], "--db=", 5) == 0) {
963971
FLAGS_db = argv[i] + 5;
964972
} else {
965-
fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
966-
exit(1);
973+
std::fprintf(stderr, "Invalid flag '%s'\n", argv[i]);
974+
std::exit(1);
967975
}
968976
}
969977

0 commit comments

Comments
 (0)