forked from Ved-vampir/ceph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlueFS.cc
4369 lines (3996 loc) · 134 KB
/
BlueFS.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "boost/algorithm/string.hpp"
#include "bluestore_common.h"
#include "BlueFS.h"
#include "common/debug.h"
#include "common/errno.h"
#include "common/perf_counters.h"
#include "Allocator.h"
#include "include/ceph_assert.h"
#include "common/admin_socket.h"
#define dout_context cct
#define dout_subsys ceph_subsys_bluefs
#undef dout_prefix
#define dout_prefix *_dout << "bluefs "
using TOPNSPC::common::cmd_getval;
using std::byte;
using std::list;
using std::make_pair;
using std::map;
using std::ostream;
using std::pair;
using std::set;
using std::string;
using std::to_string;
using std::vector;
using ceph::bufferlist;
using ceph::decode;
using ceph::encode;
using ceph::Formatter;
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::File, bluefs_file, bluefs);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::Dir, bluefs_dir, bluefs);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileWriter, bluefs_file_writer, bluefs_file_writer);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileReaderBuffer,
bluefs_file_reader_buffer, bluefs_file_reader);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileReader, bluefs_file_reader, bluefs_file_reader);
MEMPOOL_DEFINE_OBJECT_FACTORY(BlueFS::FileLock, bluefs_file_lock, bluefs);
static void wal_discard_cb(void *priv, void* priv2) {
BlueFS *bluefs = static_cast<BlueFS*>(priv);
interval_set<uint64_t> *tmp = static_cast<interval_set<uint64_t>*>(priv2);
bluefs->handle_discard(BlueFS::BDEV_WAL, *tmp);
}
static void db_discard_cb(void *priv, void* priv2) {
BlueFS *bluefs = static_cast<BlueFS*>(priv);
interval_set<uint64_t> *tmp = static_cast<interval_set<uint64_t>*>(priv2);
bluefs->handle_discard(BlueFS::BDEV_DB, *tmp);
}
static void slow_discard_cb(void *priv, void* priv2) {
BlueFS *bluefs = static_cast<BlueFS*>(priv);
interval_set<uint64_t> *tmp = static_cast<interval_set<uint64_t>*>(priv2);
bluefs->handle_discard(BlueFS::BDEV_SLOW, *tmp);
}
class BlueFS::SocketHook : public AdminSocketHook {
BlueFS* bluefs;
public:
static BlueFS::SocketHook* create(BlueFS* bluefs)
{
BlueFS::SocketHook* hook = nullptr;
AdminSocket* admin_socket = bluefs->cct->get_admin_socket();
if (admin_socket) {
hook = new BlueFS::SocketHook(bluefs);
int r = admin_socket->register_command("bluestore bluefs device info "
"name=alloc_size,type=CephInt,req=false",
hook,
"Shows space report for bluefs devices. "
"This also includes an estimation for space "
"available to bluefs at main device. "
"alloc_size, if set, specifies the custom bluefs "
"allocation unit size for the estimation above.");
if (r != 0) {
ldout(bluefs->cct, 1) << __func__ << " cannot register SocketHook" << dendl;
delete hook;
hook = nullptr;
} else {
r = admin_socket->register_command("bluefs stats",
hook,
"Dump internal statistics for bluefs."
"");
ceph_assert(r == 0);
r = admin_socket->register_command("bluefs files list", hook,
"print files in bluefs");
ceph_assert(r == 0);
r = admin_socket->register_command("bluefs debug_inject_read_zeros", hook,
"Injects 8K zeros into next BlueFS read. Debug only.");
ceph_assert(r == 0);
}
}
return hook;
}
~SocketHook() {
AdminSocket* admin_socket = bluefs->cct->get_admin_socket();
admin_socket->unregister_commands(this);
}
private:
SocketHook(BlueFS* bluefs) :
bluefs(bluefs) {}
int call(std::string_view command, const cmdmap_t& cmdmap,
Formatter *f,
std::ostream& errss,
bufferlist& out) override {
if (command == "bluestore bluefs device info") {
int64_t alloc_size = 0;
cmd_getval(cmdmap, "alloc_size", alloc_size);
if ((alloc_size & (alloc_size - 1)) != 0) {
errss << "Invalid allocation size:'" << alloc_size << std::endl;
return -EINVAL;
}
if (alloc_size == 0)
alloc_size = bluefs->cct->_conf->bluefs_shared_alloc_size;
f->open_object_section("bluefs_device_info");
for (unsigned dev = BDEV_WAL; dev <= BDEV_SLOW; dev++) {
if (bluefs->bdev[dev]) {
f->open_object_section("dev");
f->dump_string("device", bluefs->get_device_name(dev));
ceph_assert(bluefs->alloc[dev]);
auto total = bluefs->get_total(dev);
auto free = bluefs->get_free(dev);
auto used = bluefs->get_used(dev);
f->dump_int("total", total);
f->dump_int("free", free);
f->dump_int("bluefs_used", used);
if (bluefs->is_shared_alloc(dev)) {
size_t avail = bluefs->probe_alloc_avail(dev, alloc_size);
f->dump_int("bluefs max available", avail);
}
f->close_section();
}
}
f->close_section();
} else if (command == "bluefs stats") {
std::stringstream ss;
bluefs->dump_block_extents(ss);
bluefs->dump_volume_selector(ss);
out.append(ss);
} else if (command == "bluefs files list") {
const char* devnames[3] = {"wal","db","slow"};
std::lock_guard l(bluefs->nodes.lock);
f->open_array_section("files");
for (auto &d : bluefs->nodes.dir_map) {
std::string dir = d.first;
for (auto &r : d.second->file_map) {
f->open_object_section("file");
f->dump_string("name", (dir + "/" + r.first).c_str());
std::vector<size_t> sizes;
sizes.resize(bluefs->bdev.size());
for(auto& i : r.second->fnode.extents) {
sizes[i.bdev] += i.length;
}
for (size_t i = 0; i < sizes.size(); i++) {
if (sizes[i]>0) {
if (i < sizeof(devnames) / sizeof(*devnames))
f->dump_int(devnames[i], sizes[i]);
else
f->dump_int(("dev-"+to_string(i)).c_str(), sizes[i]);
}
}
f->close_section();
}
}
f->close_section();
f->flush(out);
} else if (command == "bluefs debug_inject_read_zeros") {
bluefs->inject_read_zeros++;
} else {
errss << "Invalid command" << std::endl;
return -ENOSYS;
}
return 0;
}
};
BlueFS::BlueFS(CephContext* cct)
: cct(cct),
bdev(MAX_BDEV),
ioc(MAX_BDEV),
block_reserved(MAX_BDEV),
alloc(MAX_BDEV),
alloc_size(MAX_BDEV, 0)
{
dirty.pending_release.resize(MAX_BDEV);
discard_cb[BDEV_WAL] = wal_discard_cb;
discard_cb[BDEV_DB] = db_discard_cb;
discard_cb[BDEV_SLOW] = slow_discard_cb;
asok_hook = SocketHook::create(this);
}
BlueFS::~BlueFS()
{
delete asok_hook;
for (auto p : ioc) {
if (p)
p->aio_wait();
}
for (auto p : bdev) {
if (p) {
p->close();
delete p;
}
}
for (auto p : ioc) {
delete p;
}
}
void BlueFS::_init_logger()
{
PerfCountersBuilder b(cct, "bluefs",
l_bluefs_first, l_bluefs_last);
b.add_u64(l_bluefs_db_total_bytes, "db_total_bytes",
"Total bytes (main db device)",
"b", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_db_used_bytes, "db_used_bytes",
"Used bytes (main db device)",
"u", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_wal_total_bytes, "wal_total_bytes",
"Total bytes (wal device)",
"walb", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_wal_used_bytes, "wal_used_bytes",
"Used bytes (wal device)",
"walu", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_slow_total_bytes, "slow_total_bytes",
"Total bytes (slow device)",
"slob", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_slow_used_bytes, "slow_used_bytes",
"Used bytes (slow device)",
"slou", PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_num_files, "num_files", "File count",
"f", PerfCountersBuilder::PRIO_USEFUL);
b.add_u64(l_bluefs_log_bytes, "log_bytes", "Size of the metadata log",
"jlen", PerfCountersBuilder::PRIO_INTERESTING, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_log_compactions, "log_compactions",
"Compactions of the metadata log");
b.add_u64_counter(l_bluefs_logged_bytes, "logged_bytes",
"Bytes written to the metadata log",
"j",
PerfCountersBuilder::PRIO_CRITICAL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_files_written_wal, "files_written_wal",
"Files written to WAL");
b.add_u64_counter(l_bluefs_files_written_sst, "files_written_sst",
"Files written to SSTs");
b.add_u64_counter(l_bluefs_bytes_written_wal, "bytes_written_wal",
"Bytes written to WAL",
"walb",
PerfCountersBuilder::PRIO_CRITICAL);
b.add_u64_counter(l_bluefs_bytes_written_sst, "bytes_written_sst",
"Bytes written to SSTs",
"sstb",
PerfCountersBuilder::PRIO_CRITICAL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_bytes_written_slow, "bytes_written_slow",
"Bytes written to WAL/SSTs at slow device",
"slwb",
PerfCountersBuilder::PRIO_CRITICAL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_max_bytes_wal, "max_bytes_wal",
"Maximum bytes allocated from WAL",
"mxwb",
PerfCountersBuilder::PRIO_INTERESTING,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_max_bytes_db, "max_bytes_db",
"Maximum bytes allocated from DB",
"mxdb",
PerfCountersBuilder::PRIO_INTERESTING,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_max_bytes_slow, "max_bytes_slow",
"Maximum bytes allocated from SLOW",
"mxwb",
PerfCountersBuilder::PRIO_INTERESTING,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_main_alloc_unit, "alloc_unit_main",
"Allocation unit size (in bytes) for primary/shared device",
"aumb",
PerfCountersBuilder::PRIO_CRITICAL,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_db_alloc_unit, "alloc_unit_db",
"Allocation unit size (in bytes) for standalone DB device",
"audb",
PerfCountersBuilder::PRIO_CRITICAL,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_wal_alloc_unit, "alloc_unit_wal",
"Allocation unit size (in bytes) for standalone WAL device",
"auwb",
PerfCountersBuilder::PRIO_CRITICAL,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_count, "read_random_count",
"random read requests processed",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_random_bytes, "read_random_bytes",
"Bytes requested in random read mode",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_disk_count, "read_random_disk_count",
"random reads requests going to disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_random_disk_bytes, "read_random_disk_bytes",
"Bytes read from disk in random read mode",
"rrb",
PerfCountersBuilder::PRIO_INTERESTING,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_disk_bytes_wal, "read_random_disk_bytes_wal",
"random reads requests going to WAL disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_disk_bytes_db, "read_random_disk_bytes_db",
"random reads requests going to DB disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_disk_bytes_slow, "read_random_disk_bytes_slow",
"random reads requests going to main disk",
"rrsb",
PerfCountersBuilder::PRIO_INTERESTING,
unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_random_buffer_count, "read_random_buffer_count",
"random read requests processed using prefetch buffer",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_random_buffer_bytes, "read_random_buffer_bytes",
"Bytes read from prefetch buffer in random read mode",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_count, "read_count",
"buffered read requests processed",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_bytes, "read_bytes",
"Bytes requested in buffered read mode",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_disk_count, "read_disk_count",
"buffered reads requests going to disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_disk_bytes, "read_disk_bytes",
"Bytes read in buffered mode from disk",
"rb",
PerfCountersBuilder::PRIO_INTERESTING, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_disk_bytes_wal, "read_disk_bytes_wal",
"reads requests going to WAL disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_disk_bytes_db, "read_disk_bytes_db",
"reads requests going to DB disk",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_disk_bytes_slow, "read_disk_bytes_slow",
"reads requests going to main disk",
"rsb",
PerfCountersBuilder::PRIO_INTERESTING, unit_t(UNIT_BYTES));
b.add_u64_counter(l_bluefs_read_prefetch_count, "read_prefetch_count",
"prefetch read requests processed",
NULL,
PerfCountersBuilder::PRIO_USEFUL);
b.add_u64_counter(l_bluefs_read_prefetch_bytes, "read_prefetch_bytes",
"Bytes requested in prefetch read mode",
NULL,
PerfCountersBuilder::PRIO_USEFUL, unit_t(UNIT_BYTES));
b.add_u64(l_bluefs_read_zeros_candidate, "read_zeros_candidate",
"How many times bluefs read found page with all 0s");
b.add_u64(l_bluefs_read_zeros_errors, "read_zeros_errors",
"How many times bluefs read found transient page with all 0s");
logger = b.create_perf_counters();
cct->get_perfcounters_collection()->add(logger);
}
void BlueFS::_shutdown_logger()
{
cct->get_perfcounters_collection()->remove(logger);
delete logger;
}
void BlueFS::_update_logger_stats()
{
if (alloc[BDEV_WAL]) {
logger->set(l_bluefs_wal_total_bytes, _get_total(BDEV_WAL));
logger->set(l_bluefs_wal_used_bytes, _get_used(BDEV_WAL));
}
if (alloc[BDEV_DB]) {
logger->set(l_bluefs_db_total_bytes, _get_total(BDEV_DB));
logger->set(l_bluefs_db_used_bytes, _get_used(BDEV_DB));
}
if (alloc[BDEV_SLOW]) {
logger->set(l_bluefs_slow_total_bytes, _get_total(BDEV_SLOW));
logger->set(l_bluefs_slow_used_bytes, _get_used(BDEV_SLOW));
}
}
int BlueFS::add_block_device(unsigned id, const string& path, bool trim,
uint64_t reserved,
bluefs_shared_alloc_context_t* _shared_alloc)
{
dout(10) << __func__ << " bdev " << id << " path " << path << " "
<< reserved << dendl;
ceph_assert(id < bdev.size());
ceph_assert(bdev[id] == NULL);
BlockDevice *b = BlockDevice::create(cct, path, NULL, NULL,
discard_cb[id], static_cast<void*>(this));
block_reserved[id] = reserved;
if (_shared_alloc) {
b->set_no_exclusive_lock();
}
int r = b->open(path);
if (r < 0) {
delete b;
return r;
}
if (trim) {
b->discard(0, b->get_size());
}
dout(1) << __func__ << " bdev " << id << " path " << path
<< " size " << byte_u_t(b->get_size()) << dendl;
bdev[id] = b;
ioc[id] = new IOContext(cct, NULL);
if (_shared_alloc) {
ceph_assert(!shared_alloc);
shared_alloc = _shared_alloc;
alloc[id] = shared_alloc->a;
shared_alloc_id = id;
}
return 0;
}
bool BlueFS::bdev_support_label(unsigned id)
{
ceph_assert(id < bdev.size());
ceph_assert(bdev[id]);
return bdev[id]->supported_bdev_label();
}
uint64_t BlueFS::get_block_device_size(unsigned id) const
{
if (id < bdev.size() && bdev[id])
return bdev[id]->get_size();
return 0;
}
void BlueFS::handle_discard(unsigned id, interval_set<uint64_t>& to_release)
{
dout(10) << __func__ << " bdev " << id << dendl;
ceph_assert(alloc[id]);
alloc[id]->release(to_release);
if (is_shared_alloc(id)) {
shared_alloc->bluefs_used -= to_release.size();
}
}
uint64_t BlueFS::get_used()
{
uint64_t used = 0;
for (unsigned id = 0; id < MAX_BDEV; ++id) {
used += _get_used(id);
}
return used;
}
uint64_t BlueFS::_get_used(unsigned id) const
{
uint64_t used = 0;
if (!alloc[id])
return 0;
if (is_shared_alloc(id)) {
used = shared_alloc->bluefs_used;
} else {
used = _get_total(id) - alloc[id]->get_free();
}
return used;
}
uint64_t BlueFS::get_used(unsigned id)
{
ceph_assert(id < alloc.size());
ceph_assert(alloc[id]);
return _get_used(id);
}
uint64_t BlueFS::_get_total(unsigned id) const
{
ceph_assert(id < bdev.size());
ceph_assert(id < block_reserved.size());
return get_block_device_size(id) - block_reserved[id];
}
uint64_t BlueFS::get_total(unsigned id)
{
return _get_total(id);
}
uint64_t BlueFS::get_free(unsigned id)
{
ceph_assert(id < alloc.size());
return alloc[id]->get_free();
}
void BlueFS::dump_perf_counters(Formatter *f)
{
f->open_object_section("bluefs_perf_counters");
logger->dump_formatted(f,0);
f->close_section();
}
void BlueFS::dump_block_extents(ostream& out)
{
for (unsigned i = 0; i < MAX_BDEV; ++i) {
if (!bdev[i]) {
continue;
}
auto total = get_total(i);
auto free = get_free(i);
out << i << " : device size 0x" << std::hex << total
<< " : using 0x" << total - free
<< std::dec << "(" << byte_u_t(total - free) << ")";
out << "\n";
}
}
int BlueFS::get_block_extents(unsigned id, interval_set<uint64_t> *extents)
{
std::lock_guard nl(nodes.lock);
dout(10) << __func__ << " bdev " << id << dendl;
ceph_assert(id < alloc.size());
for (auto& p : nodes.file_map) {
for (auto& q : p.second->fnode.extents) {
if (q.bdev == id) {
extents->insert(q.offset, q.length);
}
}
}
return 0;
}
int BlueFS::mkfs(uuid_d osd_uuid, const bluefs_layout_t& layout)
{
dout(1) << __func__
<< " osd_uuid " << osd_uuid
<< dendl;
// set volume selector if not provided before/outside
if (vselector == nullptr) {
vselector.reset(
new OriginalVolumeSelector(
get_block_device_size(BlueFS::BDEV_WAL) * 95 / 100,
get_block_device_size(BlueFS::BDEV_DB) * 95 / 100,
get_block_device_size(BlueFS::BDEV_SLOW) * 95 / 100));
}
_init_logger();
_init_alloc();
super.version = 1;
super.block_size = bdev[BDEV_DB]->get_block_size();
super.osd_uuid = osd_uuid;
super.uuid.generate_random();
dout(1) << __func__ << " uuid " << super.uuid << dendl;
// init log
FileRef log_file = ceph::make_ref<File>();
log_file->fnode.ino = 1;
log_file->vselector_hint = vselector->get_hint_for_log();
int r = _allocate(
vselector->select_prefer_bdev(log_file->vselector_hint),
cct->_conf->bluefs_max_log_runway,
&log_file->fnode);
vselector->add_usage(log_file->vselector_hint, log_file->fnode);
ceph_assert(r == 0);
log.writer = _create_writer(log_file);
// initial txn
ceph_assert(log.seq_live == 1);
log.t.seq = 1;
log.t.op_init();
_flush_and_sync_log_LD();
// write supers
super.log_fnode = log_file->fnode;
super.memorized_layout = layout;
_write_super(BDEV_DB);
_flush_bdev();
// clean up
super = bluefs_super_t();
_close_writer(log.writer);
log.writer = NULL;
vselector.reset(nullptr);
_stop_alloc();
_shutdown_logger();
if (shared_alloc) {
ceph_assert(shared_alloc->need_init);
shared_alloc->need_init = false;
}
dout(10) << __func__ << " success" << dendl;
return 0;
}
void BlueFS::_init_alloc()
{
dout(20) << __func__ << dendl;
size_t wal_alloc_size = 0;
if (bdev[BDEV_WAL]) {
wal_alloc_size = cct->_conf->bluefs_alloc_size;
alloc_size[BDEV_WAL] = wal_alloc_size;
}
logger->set(l_bluefs_wal_alloc_unit, wal_alloc_size);
if (bdev[BDEV_SLOW]) {
alloc_size[BDEV_DB] = cct->_conf->bluefs_alloc_size;
alloc_size[BDEV_SLOW] = cct->_conf->bluefs_shared_alloc_size;
logger->set(l_bluefs_db_alloc_unit, cct->_conf->bluefs_alloc_size);
logger->set(l_bluefs_main_alloc_unit, cct->_conf->bluefs_shared_alloc_size);
} else {
alloc_size[BDEV_DB] = cct->_conf->bluefs_shared_alloc_size;
logger->set(l_bluefs_main_alloc_unit, 0);
logger->set(l_bluefs_db_alloc_unit, cct->_conf->bluefs_shared_alloc_size);
}
// new wal and db devices are never shared
if (bdev[BDEV_NEWWAL]) {
alloc_size[BDEV_NEWWAL] = cct->_conf->bluefs_alloc_size;
}
if (bdev[BDEV_NEWDB]) {
alloc_size[BDEV_NEWDB] = cct->_conf->bluefs_alloc_size;
}
for (unsigned id = 0; id < bdev.size(); ++id) {
if (!bdev[id]) {
continue;
}
ceph_assert(bdev[id]->get_size());
ceph_assert(alloc_size[id]);
if (is_shared_alloc(id)) {
dout(1) << __func__ << " shared, id " << id << std::hex
<< ", capacity 0x" << bdev[id]->get_size()
<< ", block size 0x" << alloc_size[id]
<< std::dec << dendl;
} else {
std::string name = "bluefs-";
const char* devnames[] = { "wal","db","slow" };
if (id <= BDEV_SLOW)
name += devnames[id];
else
name += to_string(uintptr_t(this));
dout(1) << __func__ << " new, id " << id << std::hex
<< ", allocator name " << name
<< ", allocator type " << cct->_conf->bluefs_allocator
<< ", capacity 0x" << bdev[id]->get_size()
<< ", block size 0x" << alloc_size[id]
<< std::dec << dendl;
alloc[id] = Allocator::create(cct, cct->_conf->bluefs_allocator,
bdev[id]->get_size(),
alloc_size[id],
0, 0,
name);
alloc[id]->init_add_free(
block_reserved[id],
_get_total(id));
}
}
}
void BlueFS::_stop_alloc()
{
dout(20) << __func__ << dendl;
for (auto p : bdev) {
if (p)
p->discard_drain();
}
for (size_t i = 0; i < alloc.size(); ++i) {
if (alloc[i] && !is_shared_alloc(i)) {
alloc[i]->shutdown();
delete alloc[i];
alloc[i] = nullptr;
}
}
}
int BlueFS::_read_and_check(uint8_t ndev, uint64_t off, uint64_t len,
ceph::buffer::list *pbl, IOContext *ioc, bool buffered)
{
dout(10) << __func__ << " dev " << int(ndev)
<< ": 0x" << std::hex << off << "~" << len << std::dec
<< (buffered ? " buffered" : "")
<< dendl;
int r;
bufferlist bl;
r = _bdev_read(ndev, off, len, &bl, ioc, buffered);
if (r != 0) {
return r;
}
uint64_t block_size = bdev[ndev]->get_block_size();
if (inject_read_zeros) {
if (len >= block_size * 2) {
derr << __func__ << " injecting error, zeros at "
<< int(ndev) << ": 0x" << std::hex << (off + len / 2)
<< "~" << (block_size * 2) << std::dec << dendl;
//use beginning, replace 8K in the middle with zeros, use tail
bufferlist temp;
bl.splice(0, len / 2 - block_size, &temp);
temp.append(buffer::create(block_size * 2, 0));
bl.splice(block_size * 2, len / 2 - block_size, &temp);
bl = temp;
inject_read_zeros--;
}
}
//make a check if there is a block with all 0
uint64_t to_check_len = len;
uint64_t skip = p2nphase(off, block_size);
if (skip >= to_check_len) {
return r;
}
auto it = bl.begin(skip);
to_check_len -= skip;
bool all_zeros = false;
while (all_zeros == false && to_check_len >= block_size) {
// checking 0s step
unsigned block_left = block_size;
unsigned avail;
const char* data;
all_zeros = true;
while (all_zeros && block_left > 0) {
avail = it.get_ptr_and_advance(block_left, &data);
block_left -= avail;
all_zeros = mem_is_zero(data, avail);
}
// skipping step
while (block_left > 0) {
avail = it.get_ptr_and_advance(block_left, &data);
block_left -= avail;
}
to_check_len -= block_size;
}
if (all_zeros) {
logger->inc(l_bluefs_read_zeros_candidate, 1);
bufferlist bl_reread;
r = _bdev_read(ndev, off, len, &bl_reread, ioc, buffered);
if (r != 0) {
return r;
}
// check if both read gave the same
if (!bl.contents_equal(bl_reread)) {
// report problems to log, but continue, maybe it will be good now...
derr << __func__ << " initial read of " << int(ndev)
<< ": 0x" << std::hex << off << "~" << len
<< std::dec << ": different then re-read " << dendl;
logger->inc(l_bluefs_read_zeros_errors, 1);
}
// use second read will be better if is different
pbl->append(bl_reread);
} else {
pbl->append(bl);
}
return r;
}
int BlueFS::_read_random_and_check(
uint8_t ndev, uint64_t off, uint64_t len, char *buf, bool buffered)
{
dout(10) << __func__ << " dev " << int(ndev)
<< ": 0x" << std::hex << off << "~" << len << std::dec
<< (buffered ? " buffered" : "")
<< dendl;
int r;
r = _bdev_read_random(ndev, off, len, buf, buffered);
if (r != 0) {
return r;
}
uint64_t block_size = bdev[ndev]->get_block_size();
if (inject_read_zeros) {
if (len >= block_size * 2) {
derr << __func__ << " injecting error, zeros at "
<< int(ndev) << ": 0x" << std::hex << (off + len / 2)
<< "~" << (block_size * 2) << std::dec << dendl;
//zero middle 8K
memset(buf + len / 2 - block_size, 0, block_size * 2);
inject_read_zeros--;
}
}
//make a check if there is a block with all 0
uint64_t to_check_len = len;
const char* data = buf;
uint64_t skip = p2nphase(off, block_size);
if (skip >= to_check_len) {
return r;
}
to_check_len -= skip;
data += skip;
bool all_zeros = false;
while (all_zeros == false && to_check_len >= block_size) {
if (mem_is_zero(data, block_size)) {
// at least one block is all zeros
all_zeros = true;
break;
}
data += block_size;
to_check_len -= block_size;
}
if (all_zeros) {
logger->inc(l_bluefs_read_zeros_candidate, 1);
std::unique_ptr<char[]> data_reread(new char[len]);
r = _bdev_read_random(ndev, off, len, &data_reread[0], buffered);
if (r != 0) {
return r;
}
// check if both read gave the same
if (memcmp(buf, &data_reread[0], len) != 0) {
derr << __func__ << " initial read of " << int(ndev)
<< ": 0x" << std::hex << off << "~" << len
<< std::dec << ": different then re-read " << dendl;
logger->inc(l_bluefs_read_zeros_errors, 1);
// second read is probably better
memcpy(buf, &data_reread[0], len);
}
}
return r;
}
int BlueFS::_bdev_read(uint8_t ndev, uint64_t off, uint64_t len,
ceph::buffer::list* pbl, IOContext* ioc, bool buffered)
{
int cnt = 0;
switch (ndev) {
case BDEV_WAL: cnt = l_bluefs_read_disk_bytes_wal; break;
case BDEV_DB: cnt = l_bluefs_read_disk_bytes_db; break;
case BDEV_SLOW: cnt = l_bluefs_read_disk_bytes_slow; break;
}
if (cnt) {
logger->inc(cnt, len);
}
return bdev[ndev]->read(off, len, pbl, ioc, buffered);
}
int BlueFS::_bdev_read_random(uint8_t ndev, uint64_t off, uint64_t len,
char* buf, bool buffered)
{
int cnt = 0;
switch (ndev) {
case BDEV_WAL: cnt = l_bluefs_read_random_disk_bytes_wal; break;
case BDEV_DB: cnt = l_bluefs_read_random_disk_bytes_db; break;
case BDEV_SLOW: cnt = l_bluefs_read_random_disk_bytes_slow; break;
}
if (cnt) {
logger->inc(cnt, len);
}
return bdev[ndev]->read_random(off, len, buf, buffered);
}
int BlueFS::mount()
{
dout(1) << __func__ << dendl;
_init_logger();
int r = _open_super();
if (r < 0) {
derr << __func__ << " failed to open super: " << cpp_strerror(r) << dendl;
goto out;
}
// set volume selector if not provided before/outside
if (vselector == nullptr) {
vselector.reset(
new OriginalVolumeSelector(
get_block_device_size(BlueFS::BDEV_WAL) * 95 / 100,
get_block_device_size(BlueFS::BDEV_DB) * 95 / 100,
get_block_device_size(BlueFS::BDEV_SLOW) * 95 / 100));
}
_init_alloc();
r = _replay(false, false);
if (r < 0) {
derr << __func__ << " failed to replay log: " << cpp_strerror(r) << dendl;
_stop_alloc();
goto out;
}
// init freelist
for (auto& p : nodes.file_map) {
dout(30) << __func__ << " noting alloc for " << p.second->fnode << dendl;
for (auto& q : p.second->fnode.extents) {
bool is_shared = is_shared_alloc(q.bdev);
ceph_assert(!is_shared || (is_shared && shared_alloc));
if (is_shared && shared_alloc->need_init && shared_alloc->a) {
shared_alloc->bluefs_used += q.length;
alloc[q.bdev]->init_rm_free(q.offset, q.length);
} else if (!is_shared) {
alloc[q.bdev]->init_rm_free(q.offset, q.length);
}
}
}
if (shared_alloc) {
shared_alloc->need_init = false;
dout(1) << __func__ << " shared_bdev_used = "
<< shared_alloc->bluefs_used << dendl;
} else {
dout(1) << __func__ << " shared bdev not used"
<< dendl;
}
// set up the log for future writes
log.writer = _create_writer(_get_file(1));
ceph_assert(log.writer->file->fnode.ino == 1);
log.writer->pos = log.writer->file->fnode.size;
log.writer->file->fnode.reset_delta();
dout(10) << __func__ << " log write pos set to 0x"
<< std::hex << log.writer->pos << std::dec
<< dendl;
// update log size
logger->set(l_bluefs_log_bytes, log.writer->file->fnode.size);
return 0;
out:
super = bluefs_super_t();
return r;
}
int BlueFS::maybe_verify_layout(const bluefs_layout_t& layout) const
{
if (super.memorized_layout) {
if (layout == *super.memorized_layout) {
dout(10) << __func__ << " bluefs layout verified positively" << dendl;
} else {
derr << __func__ << " memorized layout doesn't fit current one" << dendl;
return -EIO;
}
} else {
dout(10) << __func__ << " no memorized_layout in bluefs superblock"
<< dendl;
}
return 0;
}
void BlueFS::umount(bool avoid_compact)
{
dout(1) << __func__ << dendl;
sync_metadata(avoid_compact);
if (cct->_conf->bluefs_check_volume_selector_on_umount) {
_check_vselector_LNF();
}
_close_writer(log.writer);
log.writer = NULL;
log.t.clear();
vselector.reset(nullptr);
_stop_alloc();
nodes.file_map.clear();
nodes.dir_map.clear();
super = bluefs_super_t();
_shutdown_logger();
}
int BlueFS::prepare_new_device(int id, const bluefs_layout_t& layout)
{
dout(1) << __func__ << dendl;
if(id == BDEV_NEWDB) {
int new_log_dev_cur = BDEV_WAL;
int new_log_dev_next = BDEV_WAL;
if (!bdev[BDEV_WAL]) {
new_log_dev_cur = BDEV_NEWDB;
new_log_dev_next = BDEV_DB;
}
_rewrite_log_and_layout_sync_LNF_LD(false,
BDEV_NEWDB,
new_log_dev_cur,
new_log_dev_next,
RENAME_DB2SLOW,
layout);
//}
} else if(id == BDEV_NEWWAL) {
_rewrite_log_and_layout_sync_LNF_LD(false,
BDEV_DB,
BDEV_NEWWAL,
BDEV_WAL,
REMOVE_WAL,
layout);
} else {
assert(false);
}
return 0;