From: Kefu Chai Date: Tue, 16 Mar 2021 01:38:41 +0000 (+0800) Subject: os/bluestore: use string_view in BlueFS X-Git-Tag: v15.2.14~45^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=087b4dd27f09b881cc76a5820974e3b541f11b33;p=ceph.git os/bluestore: use string_view in BlueFS this improves the performance of lookup. when it comes to mutation operations, std::map<> always require an instance of string, but it only require a single instance of string for each mutation operation in general, so this does not incurs performance regression due to multiple copies of the same string_view object. Signed-off-by: Kefu Chai (cherry picked from commit a90d63bc488b6f9c4f802b656222879130f725e3) Conflicts: primarily lacking std:: prefix src/os/bluestore/BlueFS.h src/os/bluestore/BlueStore.h src/os/bluestore/bluefs_types.h --- diff --git a/src/os/bluestore/BlueFS.cc b/src/os/bluestore/BlueFS.cc index 03cc9c5fe868..06a5cd714e49 100644 --- a/src/os/bluestore/BlueFS.cc +++ b/src/os/bluestore/BlueFS.cc @@ -3402,8 +3402,8 @@ void BlueFS::_maybe_compact_log(std::unique_lock& l) } int BlueFS::open_for_write( - const string& dirname, - const string& filename, + std::string_view dirname, + std::string_view filename, FileWriter **h, bool overwrite) { @@ -3434,7 +3434,7 @@ int BlueFS::open_for_write( file = ceph::make_ref(); file->fnode.ino = ++ino_last; file_map[ino_last] = file; - dir->file_map[filename] = file; + dir->file_map[string{filename}] = file; ++file->refs; create = true; } else { @@ -3519,8 +3519,8 @@ void BlueFS::_close_writer(FileWriter *h) } int BlueFS::open_for_read( - const string& dirname, - const string& filename, + std::string_view dirname, + std::string_view filename, FileReader **h, bool random) { @@ -3550,8 +3550,8 @@ int BlueFS::open_for_read( } int BlueFS::rename( - const string& old_dirname, const string& old_filename, - const string& new_dirname, const string& new_filename) + std::string_view old_dirname, std::string_view old_filename, + std::string_view new_dirname, std::string_view new_filename) { std::lock_guard l(lock); dout(10) << __func__ << " " << old_dirname << "/" << old_filename @@ -3590,15 +3590,15 @@ int BlueFS::rename( dout(10) << __func__ << " " << new_dirname << "/" << new_filename << " " << " " << file->fnode << dendl; - new_dir->file_map[new_filename] = file; - old_dir->file_map.erase(old_filename); + new_dir->file_map[string{new_filename}] = file; + old_dir->file_map.erase(string{old_filename}); log_t.op_dir_link(new_dirname, new_filename, file->fnode.ino); log_t.op_dir_unlink(old_dirname, old_filename); return 0; } -int BlueFS::mkdir(const string& dirname) +int BlueFS::mkdir(std::string_view dirname) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; @@ -3607,16 +3607,16 @@ int BlueFS::mkdir(const string& dirname) dout(20) << __func__ << " dir " << dirname << " exists" << dendl; return -EEXIST; } - dir_map[dirname] = ceph::make_ref(); + dir_map[string{dirname}] = ceph::make_ref(); log_t.op_dir_create(dirname); return 0; } -int BlueFS::rmdir(const string& dirname) +int BlueFS::rmdir(std::string_view dirname) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; - map::iterator p = dir_map.find(dirname); + auto p = dir_map.find(dirname); if (p == dir_map.end()) { dout(20) << __func__ << " dir " << dirname << " does not exist" << dendl; return -ENOENT; @@ -3626,12 +3626,12 @@ int BlueFS::rmdir(const string& dirname) dout(20) << __func__ << " dir " << dirname << " not empty" << dendl; return -ENOTEMPTY; } - dir_map.erase(dirname); + dir_map.erase(string{dirname}); log_t.op_dir_remove(dirname); return 0; } -bool BlueFS::dir_exists(const string& dirname) +bool BlueFS::dir_exists(std::string_view dirname) { std::lock_guard l(lock); map::iterator p = dir_map.find(dirname); @@ -3640,7 +3640,7 @@ bool BlueFS::dir_exists(const string& dirname) return exists; } -int BlueFS::stat(const string& dirname, const string& filename, +int BlueFS::stat(std::string_view dirname, std::string_view filename, uint64_t *size, utime_t *mtime) { std::lock_guard l(lock); @@ -3668,7 +3668,7 @@ int BlueFS::stat(const string& dirname, const string& filename, return 0; } -int BlueFS::lock_file(const string& dirname, const string& filename, +int BlueFS::lock_file(std::string_view dirname, std::string_view filename, FileLock **plock) { std::lock_guard l(lock); @@ -3679,7 +3679,7 @@ int BlueFS::lock_file(const string& dirname, const string& filename, return -ENOENT; } DirRef dir = p->second; - map::iterator q = dir->file_map.find(filename); + auto q = dir->file_map.find(filename); FileRef file; if (q == dir->file_map.end()) { dout(20) << __func__ << " dir " << dirname << " (" << dir @@ -3689,7 +3689,7 @@ int BlueFS::lock_file(const string& dirname, const string& filename, file->fnode.ino = ++ino_last; file->fnode.mtime = ceph_clock_now(); file_map[ino_last] = file; - dir->file_map[filename] = file; + dir->file_map[string{filename}] = file; ++file->refs; log_t.op_file_update(file->fnode); log_t.op_dir_link(dirname, filename, file->fnode.ino); @@ -3717,7 +3717,7 @@ int BlueFS::unlock_file(FileLock *fl) return 0; } -int BlueFS::readdir(const string& dirname, vector *ls) +int BlueFS::readdir(std::string_view dirname, vector *ls) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; @@ -3745,7 +3745,7 @@ int BlueFS::readdir(const string& dirname, vector *ls) return 0; } -int BlueFS::unlink(const string& dirname, const string& filename) +int BlueFS::unlink(std::string_view dirname, std::string_view filename) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << "/" << filename << dendl; @@ -3767,7 +3767,7 @@ int BlueFS::unlink(const string& dirname, const string& filename) << " is locked" << dendl; return -EBUSY; } - dir->file_map.erase(filename); + dir->file_map.erase(string{filename}); log_t.op_dir_unlink(dirname, filename); _drop_link(file); return 0; @@ -3984,7 +3984,7 @@ void BlueFS::debug_inject_duplicate_gift(unsigned id, void* OriginalVolumeSelector::get_hint_for_log() const { return reinterpret_cast(BlueFS::BDEV_WAL); } -void* OriginalVolumeSelector::get_hint_by_dir(const string& dirname) const { +void* OriginalVolumeSelector::get_hint_by_dir(std::string_view dirname) const { uint8_t res = BlueFS::BDEV_DB; if (dirname.length() > 5) { // the "db.slow" and "db.wal" directory names are hard-coded at diff --git a/src/os/bluestore/BlueFS.h b/src/os/bluestore/BlueFS.h index 6098ec30587e..524a25443481 100644 --- a/src/os/bluestore/BlueFS.h +++ b/src/os/bluestore/BlueFS.h @@ -84,7 +84,7 @@ public: virtual ~BlueFSVolumeSelector() { } virtual void* get_hint_for_log() const = 0; - virtual void* get_hint_by_dir(const std::string& dirname) const = 0; + virtual void* get_hint_by_dir(std::string_view dirname) const = 0; virtual void add_usage(void* file_hint, const bluefs_fnode_t& fnode) = 0; virtual void sub_usage(void* file_hint, const bluefs_fnode_t& fnode) = 0; @@ -489,14 +489,14 @@ public: int get_block_extents(unsigned id, interval_set *extents); int open_for_write( - const string& dir, - const string& file, + std::string_view dir, + std::string_view file, FileWriter **h, bool overwrite); int open_for_read( - const string& dir, - const string& file, + std::string_view dir, + std::string_view file, FileReader **h, bool random = false); @@ -505,21 +505,21 @@ public: _close_writer(h); } - int rename(const string& old_dir, const string& old_file, - const string& new_dir, const string& new_file); + int rename(std::string_view old_dir, std::string_view old_file, + std::string_view new_dir, std::string_view new_file); - int readdir(const string& dirname, vector *ls); + int readdir(std::string_view dirname, std::vector *ls); - int unlink(const string& dirname, const string& filename); - int mkdir(const string& dirname); - int rmdir(const string& dirname); + int unlink(std::string_view dirname, std::string_view filename); + int mkdir(std::string_view dirname); + int rmdir(std::string_view dirname); bool wal_is_rotational(); - bool dir_exists(const string& dirname); - int stat(const string& dirname, const string& filename, + bool dir_exists(std::string_view dirname); + int stat(std::string_view dirname, std::string_view filename, uint64_t *size, utime_t *mtime); - int lock_file(const string& dirname, const string& filename, FileLock **p); + int lock_file(std::string_view dirname, std::string_view filename, FileLock **p); int unlock_file(FileLock *l); void compact_log(); @@ -659,7 +659,7 @@ public: : wal_total(_wal_total), db_total(_db_total), slow_total(_slow_total) {} void* get_hint_for_log() const override; - void* get_hint_by_dir(const std::string& dirname) const override; + void* get_hint_by_dir(std::string_view dirname) const override; void add_usage(void* hint, const bluefs_fnode_t& fnode) override { // do nothing diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index 90fe81b62b66..c134e4773acc 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -16307,7 +16307,7 @@ void RocksDBBlueFSVolumeSelector::get_paths(const std::string& base, paths& res) res.emplace_back(base + ".slow", l_totals[LEVEL_SLOW - LEVEL_FIRST]); } -void* RocksDBBlueFSVolumeSelector::get_hint_by_dir(const string& dirname) const { +void* RocksDBBlueFSVolumeSelector::get_hint_by_dir(std::string_view dirname) const { uint8_t res = LEVEL_DB; if (dirname.length() > 5) { // the "db.slow" and "db.wal" directory names are hard-coded at diff --git a/src/os/bluestore/BlueStore.h b/src/os/bluestore/BlueStore.h index e3b982887f58..e1a530b22bbe 100644 --- a/src/os/bluestore/BlueStore.h +++ b/src/os/bluestore/BlueStore.h @@ -3674,7 +3674,7 @@ public: void* get_hint_for_log() const override { return reinterpret_cast(LEVEL_LOG); } - void* get_hint_by_dir(const string& dirname) const override; + void* get_hint_by_dir(std::string_view dirname) const override; void add_usage(void* hint, const bluefs_fnode_t& fnode) override { if (hint == nullptr) diff --git a/src/os/bluestore/bluefs_types.h b/src/os/bluestore/bluefs_types.h index 1b1a60c59146..b4d946696388 100644 --- a/src/os/bluestore/bluefs_types.h +++ b/src/os/bluestore/bluefs_types.h @@ -228,24 +228,24 @@ struct bluefs_transaction_t { encode(offset, op_bl); encode(length, op_bl); } - void op_dir_create(const string& dir) { + void op_dir_create(std::string_view dir) { using ceph::encode; encode((__u8)OP_DIR_CREATE, op_bl); encode(dir, op_bl); } - void op_dir_remove(const string& dir) { + void op_dir_remove(std::string_view dir) { using ceph::encode; encode((__u8)OP_DIR_REMOVE, op_bl); encode(dir, op_bl); } - void op_dir_link(const string& dir, const string& file, uint64_t ino) { + void op_dir_link(std::string_view dir, std::string_view file, uint64_t ino) { using ceph::encode; encode((__u8)OP_DIR_LINK, op_bl); encode(dir, op_bl); encode(file, op_bl); encode(ino, op_bl); } - void op_dir_unlink(const string& dir, const string& file) { + void op_dir_unlink(std::string_view dir, std::string_view file) { using ceph::encode; encode((__u8)OP_DIR_UNLINK, op_bl); encode(dir, op_bl);