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

Make var profile from session to global #2394

Merged
merged 4 commits into from
Dec 20, 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
8 changes: 4 additions & 4 deletions src/common/default_values.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,18 @@ export {
constexpr std::string_view CONNECTED_TS_VAR_NAME = "connected_timestamp"; // session
constexpr std::string_view CATALOG_VERSION_VAR_NAME = "catalog_version"; // global
constexpr std::string_view ACTIVE_WAL_FILENAME_VAR_NAME = "active_wal_filename"; // global
constexpr std::string_view ENABLE_PROFILE_VAR_NAME = "enable_profile"; // session
constexpr std::string_view PROFILE_RECORD_CAPACITY_VAR_NAME = "profile_record_capacity"; // session
constexpr std::string_view BG_TASK_COUNT_VAR_NAME = "bg_task_count"; // global
constexpr std::string_view RUNNING_BG_TASK_VAR_NAME = "running_bg_task"; // global
constexpr std::string_view RUNNING_COMPACT_TASK_VAR_NAME = "running_compact_task"; // global
constexpr std::string_view SYSTEM_MEMORY_USAGE_VAR_NAME = "system_memory_usage"; // global
constexpr std::string_view OPEN_FILE_COUNT_VAR_NAME = "open_file_count"; // global
constexpr std::string_view CPU_USAGE_VAR_NAME = "cpu_usage"; // global
constexpr std::string_view FOLLOWER_NUMBER_VAR_NAME = "follower_number"; // global
constexpr std::string_view FOLLOWER_NUMBER_VAR_NAME = "follower_number"; // global
constexpr std::string_view CACHE_RESULT_NUM_VAR_NAME = "cache_result_num"; // global
constexpr std::string_view MEMORY_CACHE_MISS_VAR_NAME = "memory_cache_miss"; // global
constexpr std::string_view DISK_CACHE_MISS_VAR_NAME = "disk_cache_miss"; // global
constexpr std::string_view MEMORY_CACHE_MISS_VAR_NAME = "memory_cache_miss"; // global
constexpr std::string_view DISK_CACHE_MISS_VAR_NAME = "disk_cache_miss"; // global
constexpr std::string_view ENABLE_PROFILE_VAR_NAME = "profile"; // global

// IO related
constexpr SizeT DEFAULT_READ_BUFFER_SIZE = 4096;
Expand Down
19 changes: 10 additions & 9 deletions src/executor/operator/physical_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ bool PhysicalCommand::Execute(QueryContext *query_context, OperatorState *operat
case SetScope::kSession: {
SessionVariable session_var = VarUtil::GetSessionVarByName(set_command->var_name());
switch(session_var) {
case SessionVariable::kEnableProfile: {
if (set_command->value_type() != SetVarType::kBool) {
Status status = Status::DataTypeMismatch("Boolean", set_command->value_type_str());
RecoverableError(status);
}
query_context->current_session()->SetProfile(set_command->value_bool());
return true;
}
case SessionVariable::kInvalid: {
Status status = Status::InvalidCommand(fmt::format("Unknown session variable: {}", set_command->var_name()));
RecoverableError(status);
Expand All @@ -90,6 +82,14 @@ bool PhysicalCommand::Execute(QueryContext *query_context, OperatorState *operat
case SetScope::kGlobal: {
GlobalVariable global_var = VarUtil::GetGlobalVarByName(set_command->var_name());
switch(global_var) {
case GlobalVariable::kEnableProfile: {
if (set_command->value_type() != SetVarType::kBool) {
Status status = Status::DataTypeMismatch("Boolean", set_command->value_type_str());
RecoverableError(status);
}
InfinityContext::instance().storage()->catalog()->SetProfile(set_command->value_bool());
return true;
}
case GlobalVariable::kProfileRecordCapacity: {
if (set_command->value_type() != SetVarType::kInteger) {
Status status = Status::DataTypeMismatch("Integer", set_command->value_type_str());
Expand Down Expand Up @@ -346,7 +346,8 @@ bool PhysicalCommand::Execute(QueryContext *query_context, OperatorState *operat
}
case CommandType::kExport: {
ExportCmd *export_command = (ExportCmd *)(command_info_.get());
auto profiler_record = query_context->current_session()->GetProfileRecord(export_command->file_no());

auto profiler_record = InfinityContext::instance().storage()->catalog()->GetProfileRecord(export_command->file_no());
if (profiler_record == nullptr) {
Status status = Status::DataNotExist(fmt::format("The record does not exist: {}", export_command->file_no()));
RecoverableError(status);
Expand Down
91 changes: 45 additions & 46 deletions src/executor/operator/physical_show.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,9 +1785,8 @@ void PhysicalShow::ExecuteShowViews(QueryContext *query_context, ShowOperatorSta
}

void PhysicalShow::ExecuteShowProfiles(QueryContext *query_context, ShowOperatorState *show_operator_state) {
auto txn = query_context->GetTxn();
auto varchar_type = MakeShared<DataType>(LogicalType::kVarchar);
auto catalog = txn->GetCatalog();
auto catalog = query_context->storage()->catalog();

// create data block for output state
UniquePtr<DataBlock> output_block_ptr = DataBlock::MakeUniquePtr();
Expand Down Expand Up @@ -3798,26 +3797,6 @@ void PhysicalShow::ExecuteShowSessionVariable(QueryContext *query_context, ShowO
value_expr.AppendToChunk(output_block_ptr->column_vectors[0]);
break;
}
case SessionVariable::kEnableProfile: {
Vector<SharedPtr<ColumnDef>> output_column_defs = {
MakeShared<ColumnDef>(0, integer_type, "value", std::set<ConstraintType>()),
};

SharedPtr<TableDef> table_def =
TableDef::Make(MakeShared<String>("default_db"), MakeShared<String>("variables"), nullptr, output_column_defs);
output_ = MakeShared<DataTable>(table_def, TableType::kResult);

Vector<SharedPtr<DataType>> output_column_types{
bool_type,
};

output_block_ptr->Init(output_column_types);

Value value = Value::MakeBool(query_context->is_enable_profiling());
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[0]);
break;
}
default: {
operator_state->status_ = Status::NoSysVar(*object_name_);
RecoverableError(operator_state->status_);
Expand Down Expand Up @@ -3932,29 +3911,6 @@ void PhysicalShow::ExecuteShowSessionVariables(QueryContext *query_context, Show
}
break;
}
case SessionVariable::kEnableProfile: {
{
// option name
Value value = Value::MakeVarchar(var_name);
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[0]);
}
{
// option value
bool enable_profile = query_context->is_enable_profiling();
String enable_profile_condition = enable_profile ? "true" : "false";
Value value = Value::MakeVarchar(enable_profile_condition);
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[1]);
}
{
// option description
Value value = Value::MakeVarchar("Enable profile");
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[2]);
}
break;
}
default: {
operator_state->status_ = Status::NoSysVar(var_name);
RecoverableError(operator_state->status_);
Expand Down Expand Up @@ -4558,6 +4514,26 @@ void PhysicalShow::ExecuteShowGlobalVariable(QueryContext *query_context, ShowOp
}
break;
}
case GlobalVariable::kEnableProfile: {
Vector<SharedPtr<ColumnDef>> output_column_defs = {
MakeShared<ColumnDef>(0, integer_type, "value", std::set<ConstraintType>()),
};

SharedPtr<TableDef> table_def =
TableDef::Make(MakeShared<String>("default_db"), MakeShared<String>("variables"), nullptr, output_column_defs);
output_ = MakeShared<DataTable>(table_def, TableType::kResult);

Vector<SharedPtr<DataType>> output_column_types{
bool_type,
};

output_block_ptr->Init(output_column_types);

Value value = Value::MakeBool(InfinityContext::instance().storage()->catalog()->GetProfile());
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[0]);
break;
}
case GlobalVariable::kCleanupTrace: {
CleanupInfoTracer *tracer = query_context->storage()->cleanup_info_tracer();
String error_msg = tracer->GetCleanupInfo();
Expand Down Expand Up @@ -5215,6 +5191,29 @@ void PhysicalShow::ExecuteShowGlobalVariables(QueryContext *query_context, ShowO
}
break;
}
case GlobalVariable::kEnableProfile: {
{
// option name
Value value = Value::MakeVarchar(var_name);
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[0]);
}
{
// option value
bool enable_profile = InfinityContext::instance().storage()->catalog()->GetProfile();
String enable_profile_condition = enable_profile ? "true" : "false";
Value value = Value::MakeVarchar(enable_profile_condition);
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[1]);
}
{
// option description
Value value = Value::MakeVarchar("Enable profile");
ValueExpression value_expr(value);
value_expr.AppendToChunk(output_block_ptr->column_vectors[2]);
}
break;
}
case GlobalVariable::kCleanupTrace: {
CleanupInfoTracer *tracer = query_context->storage()->cleanup_info_tracer();
String error_msg = tracer->GetCleanupInfo();
Expand Down Expand Up @@ -5882,7 +5881,7 @@ void PhysicalShow::ExecuteShowDeltaLogs(QueryContext *query_context, ShowOperato
output_block_ptr->Init(column_types);
SizeT row_count = 0;

auto catalog = query_context->GetTxn()->GetCatalog();
auto catalog = query_context->storage()->catalog();
Vector<CatalogDeltaOpBrief> delta_log_brief_array = catalog->GetDeltaLogBriefs();

for (const auto &delta_op_brief : delta_log_brief_array) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/query_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,36 @@ QueryResult QueryContext::QueryStatement(const BaseStatement *base_statement) {
return query_result;
}

void QueryContext::CreateQueryProfiler() {
if (InfinityContext::instance().storage()->catalog()->GetProfile()) {
query_profiler_ = MakeShared<QueryProfiler>(true);
}
}

void QueryContext::RecordQueryProfiler(const StatementType &type) {
if (type != StatementType::kCommand && type != StatementType::kExplain && type != StatementType::kShow) {
InfinityContext::instance().storage()->catalog()->AppendProfileRecord(query_profiler_);
}
}

void QueryContext::StartProfile(QueryPhase phase) {
if (query_profiler_) {
query_profiler_->StartPhase(phase);
}
}

void QueryContext::StopProfile(QueryPhase phase) {
if (query_profiler_) {
query_profiler_->StopPhase(phase);
}
}

void QueryContext::StopProfile() {
if (query_profiler_) {
query_profiler_->Stop();
}
}

bool QueryContext::ExecuteBGStatement(BaseStatement *base_statement, BGQueryState &state) {
QueryResult query_result;
try {
Expand Down
34 changes: 5 additions & 29 deletions src/main/query_context.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public:

[[nodiscard]] inline u64 cpu_number_limit() const { return cpu_number_limit_; }

[[nodiscard]] inline bool is_enable_profiling() const { return session_ptr_->GetProfile(); }

[[nodiscard]] inline u64 memory_size_limit() const { return memory_size_limit_; }

[[nodiscard]] inline u64 query_id() const { return query_id_; }
Expand Down Expand Up @@ -139,34 +137,12 @@ private:
QueryResult HandleAdminStatement(const AdminStatement* admin_statement);

private:
inline void CreateQueryProfiler() {
if (is_enable_profiling()) {
query_profiler_ = MakeShared<QueryProfiler>(true);
}
}

inline void RecordQueryProfiler(const StatementType &type) {
if (type != StatementType::kCommand && type != StatementType::kExplain && type != StatementType::kShow) {
GetTxn()->GetCatalog()->AppendProfileRecord(query_profiler_);
}
}

inline void StartProfile(QueryPhase phase) {
if(query_profiler_) {
query_profiler_->StartPhase(phase);
}
}
inline void StopProfile(QueryPhase phase) {
if(query_profiler_) {
query_profiler_->StopPhase(phase);
}
}

inline void StopProfile() {
if(query_profiler_) {
query_profiler_->Stop();
}
}
void CreateQueryProfiler();
void RecordQueryProfiler(const StatementType &type);
void StartProfile(QueryPhase phase);
void StopProfile(QueryPhase phase);
void StopProfile();

private:
// Parser
Expand Down
8 changes: 0 additions & 8 deletions src/main/session.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public:
[[nodiscard]] inline Txn *GetTxn() const { return txn_; }
inline void SetTxn(Txn *txn) { txn_ = txn; }

const QueryProfiler *GetProfileRecord(SizeT index) { return txn_->GetCatalog()->GetProfileRecord(index); }

void IncreaseQueryCount() { ++query_count_; }

[[nodiscard]] u64 query_count() const { return query_count_; }
Expand All @@ -60,10 +58,6 @@ public:

String ConnectedTimeToStr() const { return std::asctime(std::localtime(&connected_time_)); }

void SetProfile(bool flag) { enable_profile_ = flag; }

[[nodiscard]] bool GetProfile() const { return enable_profile_; }

protected:
std::time_t connected_time_;

Expand All @@ -81,8 +75,6 @@ protected:

u64 committed_txn_count_{0};
u64 rollbacked_txn_count_{0};

bool enable_profile_{false};
};

export class LocalSession : public BaseSession {
Expand Down
2 changes: 1 addition & 1 deletion src/main/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ void VarUtil::InitVariablesMap() {
global_name_map_[CACHE_RESULT_NUM_VAR_NAME.data()] = GlobalVariable::kCacheResultNum;
global_name_map_[MEMORY_CACHE_MISS_VAR_NAME.data()] = GlobalVariable::kMemoryCacheMiss;
global_name_map_[DISK_CACHE_MISS_VAR_NAME.data()] = GlobalVariable::kDiskCacheMiss;
global_name_map_[ENABLE_PROFILE_VAR_NAME.data()] = GlobalVariable::kEnableProfile;

session_name_map_[QUERY_COUNT_VAR_NAME.data()] = SessionVariable::kQueryCount;
session_name_map_[TOTAL_COMMIT_COUNT_VAR_NAME.data()] = SessionVariable::kTotalCommitCount;
session_name_map_[TOTAL_ROLLBACK_COUNT_VAR_NAME.data()] = SessionVariable::kTotalRollbackCount;
session_name_map_[CONNECTED_TS_VAR_NAME.data()] = SessionVariable::kConnectedTime;
session_name_map_["profile"] = SessionVariable::kEnableProfile;
}

HashMap<String, GlobalVariable> VarUtil::global_name_map_;
Expand Down
2 changes: 1 addition & 1 deletion src/main/variables.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export enum class GlobalVariable {
kCacheResultNum, // global
kMemoryCacheMiss, // global
kDiskCacheMiss, // global
kEnableProfile, // global
kInvalid,
};

Expand All @@ -59,7 +60,6 @@ export enum class SessionVariable {
kTotalCommitCount, // session
kTotalRollbackCount, // session
kConnectedTime, // session
kEnableProfile, // session

kInvalid,
};
Expand Down
3 changes: 2 additions & 1 deletion src/scheduler/fragment_context.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import create_index_data;
import logger;
import third_party;
import compact_state_data;
import infinity_context;

export module fragment_context;

Expand Down Expand Up @@ -110,7 +111,7 @@ public:
inline void IncreaseTask() { unfinished_task_n_.fetch_add(1); }

inline void FlushProfiler(TaskProfiler &profiler) {
if (!query_context_->is_enable_profiling()) {
if (!InfinityContext::instance().storage()->catalog()->GetProfile()) {
return;
}
query_context_->FlushProfiler(std::move(profiler));
Expand Down
3 changes: 2 additions & 1 deletion src/scheduler/fragment_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import defer_op;
import fragment_context;
import status;
import parser_assert;
import infinity_context;

namespace infinity {

Expand Down Expand Up @@ -71,7 +72,7 @@ void FragmentTask::OnExecute() {
// No source error
Vector<PhysicalOperator *> &operator_refs = fragment_context->GetOperators();

bool enable_profiler = query_context->is_enable_profiling();
bool enable_profiler = InfinityContext::instance().storage()->catalog()->GetProfile();
TaskProfiler profiler(TaskBinding(), enable_profiler, operator_count_);
HashMap<SizeT, SharedPtr<BaseTableRef>> table_refs;
profiler.Begin();
Expand Down
Loading