db_info_dumper.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "db/db_info_dumper.h"
  6. #include <stdio.h>
  7. #include <algorithm>
  8. #include <cinttypes>
  9. #include <string>
  10. #include <vector>
  11. #include "file/filename.h"
  12. #include "rocksdb/env.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. void DumpDBFileSummary(const ImmutableDBOptions& options,
  15. const std::string& dbname) {
  16. if (options.info_log == nullptr) {
  17. return;
  18. }
  19. auto* env = options.env;
  20. uint64_t number = 0;
  21. FileType type = kInfoLogFile;
  22. std::vector<std::string> files;
  23. uint64_t file_num = 0;
  24. uint64_t file_size;
  25. std::string file_info, wal_info;
  26. Header(options.info_log, "DB SUMMARY\n");
  27. // Get files in dbname dir
  28. if (!env->GetChildren(dbname, &files).ok()) {
  29. Error(options.info_log,
  30. "Error when reading %s dir\n", dbname.c_str());
  31. }
  32. std::sort(files.begin(), files.end());
  33. for (const std::string& file : files) {
  34. if (!ParseFileName(file, &number, &type)) {
  35. continue;
  36. }
  37. switch (type) {
  38. case kCurrentFile:
  39. Header(options.info_log, "CURRENT file: %s\n", file.c_str());
  40. break;
  41. case kIdentityFile:
  42. Header(options.info_log, "IDENTITY file: %s\n", file.c_str());
  43. break;
  44. case kDescriptorFile:
  45. env->GetFileSize(dbname + "/" + file, &file_size);
  46. Header(options.info_log, "MANIFEST file: %s size: %" PRIu64 " Bytes\n",
  47. file.c_str(), file_size);
  48. break;
  49. case kLogFile:
  50. env->GetFileSize(dbname + "/" + file, &file_size);
  51. char str[16];
  52. snprintf(str, sizeof(str), "%" PRIu64, file_size);
  53. wal_info.append(file).append(" size: ").
  54. append(str).append(" ; ");
  55. break;
  56. case kTableFile:
  57. if (++file_num < 10) {
  58. file_info.append(file).append(" ");
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. // Get sst files in db_path dir
  66. for (auto& db_path : options.db_paths) {
  67. if (dbname.compare(db_path.path) != 0) {
  68. if (!env->GetChildren(db_path.path, &files).ok()) {
  69. Error(options.info_log,
  70. "Error when reading %s dir\n",
  71. db_path.path.c_str());
  72. continue;
  73. }
  74. std::sort(files.begin(), files.end());
  75. for (const std::string& file : files) {
  76. if (ParseFileName(file, &number, &type)) {
  77. if (type == kTableFile && ++file_num < 10) {
  78. file_info.append(file).append(" ");
  79. }
  80. }
  81. }
  82. }
  83. Header(options.info_log,
  84. "SST files in %s dir, Total Num: %" PRIu64 ", files: %s\n",
  85. db_path.path.c_str(), file_num, file_info.c_str());
  86. file_num = 0;
  87. file_info.clear();
  88. }
  89. // Get wal file in wal_dir
  90. if (dbname.compare(options.wal_dir) != 0) {
  91. if (!env->GetChildren(options.wal_dir, &files).ok()) {
  92. Error(options.info_log,
  93. "Error when reading %s dir\n",
  94. options.wal_dir.c_str());
  95. return;
  96. }
  97. wal_info.clear();
  98. for (const std::string& file : files) {
  99. if (ParseFileName(file, &number, &type)) {
  100. if (type == kLogFile) {
  101. env->GetFileSize(options.wal_dir + "/" + file, &file_size);
  102. char str[16];
  103. snprintf(str, sizeof(str), "%" PRIu64, file_size);
  104. wal_info.append(file).append(" size: ").
  105. append(str).append(" ; ");
  106. }
  107. }
  108. }
  109. }
  110. Header(options.info_log, "Write Ahead Log file in %s: %s\n",
  111. options.wal_dir.c_str(), wal_info.c_str());
  112. }
  113. } // namespace ROCKSDB_NAMESPACE