Coverage Report

Created: 2026-09-28 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/db/db_info_dumper.cc
Line
Count
Source
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
6
#include "db/db_info_dumper.h"
7
8
#include <algorithm>
9
#include <cinttypes>
10
#include <cstdio>
11
#include <string>
12
#include <vector>
13
14
#include "file/filename.h"
15
#include "rocksdb/env.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
19
void DumpDBFileSummary(const ImmutableDBOptions& options,
20
                       const std::string& dbname,
21
53.3k
                       const std::string& session_id) {
22
53.3k
  if (options.info_log == nullptr) {
23
0
    return;
24
0
  }
25
26
53.3k
  auto* env = options.env;
27
53.3k
  uint64_t number = 0;
28
53.3k
  FileType type = kInfoLogFile;
29
30
53.3k
  std::vector<std::string> files;
31
53.3k
  uint64_t file_num = 0;
32
53.3k
  uint64_t file_size;
33
53.3k
  std::string file_info, wal_info;
34
35
53.3k
  Header(options.info_log, "DB SUMMARY\n");
36
53.3k
  {
37
53.3k
    std::string hostname;
38
53.3k
    if (env->GetHostNameString(&hostname).ok()) {
39
53.3k
      Header(options.info_log, "Host name (Env):  %s\n", hostname.c_str());
40
53.3k
    }
41
53.3k
  }
42
53.3k
  Header(options.info_log, "DB Session ID:  %s\n", session_id.c_str());
43
44
53.3k
  Status s;
45
  // Get files in dbname dir
46
53.3k
  s = env->GetChildren(dbname, &files);
47
53.3k
  if (!s.ok()) {
48
0
    Error(options.info_log, "Error when reading %s dir %s\n", dbname.c_str(),
49
0
          s.ToString().c_str());
50
0
  }
51
53.3k
  std::sort(files.begin(), files.end());
52
946k
  for (const std::string& file : files) {
53
946k
    if (!ParseFileName(file, &number, &type)) {
54
538k
      continue;
55
538k
    }
56
408k
    switch (type) {
57
44.9k
      case kCurrentFile:
58
44.9k
        Header(options.info_log, "CURRENT file:  %s\n", file.c_str());
59
44.9k
        break;
60
44.9k
      case kIdentityFile:
61
44.9k
        Header(options.info_log, "IDENTITY file:  %s\n", file.c_str());
62
44.9k
        break;
63
44.9k
      case kDescriptorFile:
64
44.9k
        s = env->GetFileSize(dbname + "/" + file, &file_size);
65
44.9k
        if (s.ok()) {
66
44.9k
          Header(options.info_log,
67
44.9k
                 "MANIFEST file:  %s size: %" PRIu64 " Bytes\n", file.c_str(),
68
44.9k
                 file_size);
69
44.9k
        } else {
70
0
          Error(options.info_log,
71
0
                "Error when reading MANIFEST file: %s/%s %s\n", dbname.c_str(),
72
0
                file.c_str(), s.ToString().c_str());
73
0
        }
74
44.9k
        break;
75
44.9k
      case kWalFile:
76
44.9k
        s = env->GetFileSize(dbname + "/" + file, &file_size);
77
44.9k
        if (s.ok()) {
78
44.9k
          wal_info.append(file)
79
44.9k
              .append(" size: ")
80
44.9k
              .append(std::to_string(file_size))
81
44.9k
              .append(" ; ");
82
44.9k
        } else {
83
0
          Error(options.info_log, "Error when reading LOG file: %s/%s %s\n",
84
0
                dbname.c_str(), file.c_str(), s.ToString().c_str());
85
0
        }
86
44.9k
        break;
87
100k
      case kTableFile:
88
100k
        if (++file_num < 10) {
89
96.7k
          file_info.append(file).append(" ");
90
96.7k
        }
91
100k
        break;
92
128k
      default:
93
128k
        break;
94
408k
    }
95
408k
  }
96
97
  // Get sst files in db_path dir
98
53.3k
  for (auto& db_path : options.db_paths) {
99
53.3k
    if (dbname.compare(db_path.path) != 0) {
100
0
      s = env->GetChildren(db_path.path, &files);
101
0
      if (s.IsNotFound() || s.IsPathNotFound()) {
102
0
        Header(options.info_log,
103
0
               "Directory from db_paths/cf_paths does not yet exist: %s\n",
104
0
               db_path.path.c_str());
105
0
        continue;
106
0
      } else if (!s.ok()) {
107
0
        Error(options.info_log, "Error when reading %s dir %s\n",
108
0
              db_path.path.c_str(), s.ToString().c_str());
109
0
        continue;
110
0
      }
111
0
      std::sort(files.begin(), files.end());
112
0
      for (const std::string& file : files) {
113
0
        if (ParseFileName(file, &number, &type)) {
114
0
          if (type == kTableFile && ++file_num < 10) {
115
0
            file_info.append(file).append(" ");
116
0
          }
117
0
        }
118
0
      }
119
0
    }
120
53.3k
    Header(options.info_log,
121
53.3k
           "SST files in %s dir, Total Num: %" PRIu64 ", files: %s\n",
122
53.3k
           db_path.path.c_str(), file_num, file_info.c_str());
123
53.3k
    file_num = 0;
124
53.3k
    file_info.clear();
125
53.3k
  }
126
127
  // Get wal file in wal_dir
128
53.3k
  const auto& wal_dir = options.GetWalDir(dbname);
129
53.3k
  bool log_wal_info = true;
130
53.3k
  if (!options.IsWalDirSameAsDBPath(dbname)) {
131
0
    s = env->GetChildren(wal_dir, &files);
132
0
    if (s.IsNotFound() || s.IsPathNotFound()) {
133
0
      Header(options.info_log,
134
0
             "Write Ahead Log directory does not yet exist: %s\n",
135
0
             wal_dir.c_str());
136
0
      log_wal_info = false;
137
0
    } else if (!s.ok()) {
138
0
      Error(options.info_log, "Error when reading wal dir %s: %s\n",
139
0
            wal_dir.c_str(), s.ToString().c_str());
140
0
      log_wal_info = false;
141
0
    }
142
0
    wal_info.clear();
143
0
    for (const std::string& file : files) {
144
0
      if (ParseFileName(file, &number, &type)) {
145
0
        if (type == kWalFile) {
146
0
          s = env->GetFileSize(wal_dir + "/" + file, &file_size);
147
0
          if (s.ok()) {
148
0
            wal_info.append(file)
149
0
                .append(" size: ")
150
0
                .append(std::to_string(file_size))
151
0
                .append(" ; ");
152
0
          } else {
153
0
            Error(options.info_log, "Error when reading LOG file %s/%s %s\n",
154
0
                  wal_dir.c_str(), file.c_str(), s.ToString().c_str());
155
0
          }
156
0
        }
157
0
      }
158
0
    }
159
0
  }
160
53.3k
  if (log_wal_info) {
161
53.3k
    Header(options.info_log, "Write Ahead Log file in %s: %s\n",
162
53.3k
           wal_dir.c_str(), wal_info.c_str());
163
53.3k
  }
164
53.3k
}
165
}  // namespace ROCKSDB_NAMESPACE