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