Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract common/port.h and optimize dbstats #2145

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/common/port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#pragma once

#if defined(__sparc__) || defined(__arm__)
#define USE_ALIGNED_ACCESS
#endif

#if defined(__s390__)
#if defined(__GNUC__) && __GNUC__ < 7
constexpr size_t CACHE_LINE_SIZE = 64U;
#else
constexpr size_t CACHE_LINE_SIZE = 256U;
#endif
Comment on lines +28 to +32
Copy link
Member

@PragmaTwice PragmaTwice Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why it will be changed for different GCC versions?

Copy link
Member Author

@mapleFU mapleFU Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is borrow from rocksdb, seems it's a bug in gcc, we can ingoring it?
facebook/rocksdb@65996dd

#elif defined(__powerpc__) || defined(__aarch64__)
constexpr size_t CACHE_LINE_SIZE = 128U;
#else
constexpr size_t CACHE_LINE_SIZE = 64U;
#endif
14 changes: 9 additions & 5 deletions src/storage/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ const int64_t kIORateLimitMaxMb = 1024000;
using rocksdb::Slice;

Storage::Storage(Config *config)
: backup_creating_time_(util::GetTimeStamp()), env_(rocksdb::Env::Default()), config_(config), lock_mgr_(16) {
: backup_creating_time_(util::GetTimeStamp()),
env_(rocksdb::Env::Default()),
config_(config),
lock_mgr_(16),
db_stats_(std::make_unique<DBStats>()) {
Metadata::InitVersionCounter();
SetWriteOptions(config->rocks_db.write_options);
}
Expand Down Expand Up @@ -707,16 +711,16 @@ Status Storage::ApplyWriteBatch(const rocksdb::WriteOptions &options, std::strin
void Storage::RecordStat(StatType type, uint64_t v) {
switch (type) {
case StatType::FlushCount:
db_stats_.flush_count += v;
db_stats_->flush_count.fetch_add(v, std::memory_order_relaxed);
break;
case StatType::CompactionCount:
db_stats_.compaction_count += v;
db_stats_->compaction_count.fetch_add(v, std::memory_order_relaxed);
break;
case StatType::KeyspaceHits:
db_stats_.keyspace_hits += v;
db_stats_->keyspace_hits.fetch_add(v, std::memory_order_relaxed);
break;
case StatType::KeyspaceMisses:
db_stats_.keyspace_misses += v;
db_stats_->keyspace_misses.fetch_add(v, std::memory_order_relaxed);
break;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <utility>
#include <vector>

#include "common/port.h"
#include "config/config.h"
#include "lock_manager.h"
#include "observer_or_unique.h"
Expand Down Expand Up @@ -107,18 +108,18 @@ inline const std::vector<CacheOption> CacheOptions = {
{BlockCacheType::kCacheTypeHCC, "hcc", "kCacheTypeHCC"},
};

enum class StatType {
enum class StatType : uint_fast8_t {
CompactionCount,
FlushCount,
KeyspaceHits,
KeyspaceMisses,
};

struct DBStats {
std::atomic<uint64_t> compaction_count = 0;
std::atomic<uint64_t> flush_count = 0;
std::atomic<uint64_t> keyspace_hits = 0;
std::atomic<uint64_t> keyspace_misses = 0;
alignas(CACHE_LINE_SIZE) std::atomic<uint_fast64_t> compaction_count = 0;
alignas(CACHE_LINE_SIZE) std::atomic<uint_fast64_t> flush_count = 0;
alignas(CACHE_LINE_SIZE) std::atomic<uint_fast64_t> keyspace_hits = 0;
alignas(CACHE_LINE_SIZE) std::atomic<uint_fast64_t> keyspace_misses = 0;
};

class Storage {
Expand Down Expand Up @@ -195,7 +196,7 @@ class Storage {
bool IsSlotIdEncoded() const { return config_->slot_id_encoded; }
Config *GetConfig() const { return config_; }

const DBStats *GetDBStats() const { return &db_stats_; }
const DBStats *GetDBStats() const { return db_stats_.get(); }
void RecordStat(StatType type, uint64_t v);

Status BeginTxn();
Expand Down Expand Up @@ -261,7 +262,7 @@ class Storage {
LockManager lock_mgr_;
std::atomic<bool> db_size_limit_reached_{false};

DBStats db_stats_;
std::unique_ptr<DBStats> db_stats_;

std::shared_mutex db_rw_lock_;
bool db_closing_ = true;
Expand Down
5 changes: 1 addition & 4 deletions src/types/redis_bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
#include <vector>

#include "common/bitfield_util.h"
#include "common/port.h"
#include "storage/redis_db.h"
#include "storage/redis_metadata.h"

#if defined(__sparc__) || defined(__arm__)
#define USE_ALIGNED_ACCESS
#endif

enum BitOpFlags {
kBitOpAnd,
kBitOpOr,
Expand Down
Loading