diff --git a/runtime-common/stdlib/tracing/tracing-context.h b/runtime-common/stdlib/tracing/tracing-context.h new file mode 100644 index 0000000000..119cf152c9 --- /dev/null +++ b/runtime-common/stdlib/tracing/tracing-context.h @@ -0,0 +1,27 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include + +#include "common/mixin/not_copyable.h" + +namespace kphp_tracing { + +struct TracingContext final : private vk::not_copyable { + // cur_trace_level is level of tracing for current php script + // -1 - not inited, disabled (on php script finish, it's reset to -1) + // 0 - inited, but turned off + // (binlog is not written, on_rpc_query_send() and other "callbacks" are not called) + // 1 - turned on, this php script is traced in a regular mode + // (binlog is written, and when the script finishes, it's either flushed to a file or dismissed) + // 2 - turned on in advanced mode + // (same as 1, but more events with more details are written to binlog, significant overhead) + int32_t cur_trace_level{-1}; + + static TracingContext &get() noexcept; +}; + +} // namespace kphp_tracing diff --git a/runtime-common/stdlib/tracing/tracing-functions.h b/runtime-common/stdlib/tracing/tracing-functions.h new file mode 100644 index 0000000000..9e56f6fba7 --- /dev/null +++ b/runtime-common/stdlib/tracing/tracing-functions.h @@ -0,0 +1,15 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include "runtime-common/stdlib/tracing/tracing-context.h" + +namespace kphp_tracing { + +inline bool is_turned_on() { + return TracingContext::get().cur_trace_level >= 1; +} + +} // namespace kphp_tracing diff --git a/runtime-common/stdlib/tracing/tracing.h b/runtime-common/stdlib/tracing/tracing.h new file mode 100644 index 0000000000..9c5aa65938 --- /dev/null +++ b/runtime-common/stdlib/tracing/tracing.h @@ -0,0 +1,80 @@ +// Compiler for PHP (aka KPHP) +// Copyright (c) 2024 LLC «V Kontakte» +// Distributed under the GPL v3 License, see LICENSE.notice.txt + +#pragma once + +#include + +#include "common/wrappers/likely.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" + +namespace kphp_tracing { + +// see comments about `@kphp-tracing aggregate` +enum class BuiltinFuncID : int { + _max_user_defined_functions = 64000, // and 1.5k for built-in functions (16 bits total) + _max_user_defined_aggregate = 24, // and 7 for built-in aggregates (5 bits total) + _shift_for_branch = 16, + _shift_for_aggregate = 18, + _shift_for_reserved = 23, + + preg_match = _max_user_defined_functions + 1, + preg_match_all, + preg_split, + preg_replace, + preg_replace_callback, + exec, + system, + passthru, + + branch_regex_needs_compilation = 1, + + aggregate_regexp_functions = (_max_user_defined_aggregate + 1) << _shift_for_aggregate, +}; + +} // namespace kphp_tracing + +// function calls: autogen from `@kphp-tracing aggregate` + +class KphpTracingAggregateGuard { + int32_t func_call_mask{}; + + void on_started(int32_t func_call_mask) noexcept; + void on_enter_branch(int32_t branch_num) const noexcept; + void on_finished() const noexcept; + +public: + KphpTracingAggregateGuard() noexcept = default; + KphpTracingAggregateGuard(const KphpTracingAggregateGuard &) = delete; + KphpTracingAggregateGuard &operator=(const KphpTracingAggregateGuard &) = delete; + + void start(int32_t call_mask) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { + on_started(call_mask); + } + } + explicit KphpTracingAggregateGuard(kphp_tracing::BuiltinFuncID func_id, kphp_tracing::BuiltinFuncID aggregate_bits) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { + on_started(static_cast(func_id) | static_cast(aggregate_bits)); + } + } + + void enter_branch(int32_t branch_num) { + // codegenerated instead of kphp_tracing_func_enter_branch(N) + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { + on_enter_branch(branch_num); + } + } + void enter_branch(kphp_tracing::BuiltinFuncID branch_num) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { + on_enter_branch(static_cast(branch_num)); + } + } + + ~KphpTracingAggregateGuard() { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { + on_finished(); + } + } +}; diff --git a/runtime/curl.cpp b/runtime/curl.cpp index c22ef194e6..de52cdc490 100644 --- a/runtime/curl.cpp +++ b/runtime/curl.cpp @@ -10,6 +10,7 @@ #include #include +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/interface.h" diff --git a/runtime/exec.cpp b/runtime/exec.cpp index 1cfcd85e87..67c9d883e4 100644 --- a/runtime/exec.cpp +++ b/runtime/exec.cpp @@ -7,6 +7,7 @@ #include #include "common/smart_ptrs/unique_ptr_with_delete_function.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/kphp_tracing.h" namespace { diff --git a/runtime/files.cpp b/runtime/files.cpp index 7a4fac7d86..543933ec13 100644 --- a/runtime/files.cpp +++ b/runtime/files.cpp @@ -16,6 +16,7 @@ #include "common/kernel-version.h" #include "common/macos-ports.h" #include "common/wrappers/mkdir_recursive.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime-common/stdlib/string/string-context.h" #include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" diff --git a/runtime/interface.cpp b/runtime/interface.cpp index cabdd79187..0548322774 100644 --- a/runtime/interface.cpp +++ b/runtime/interface.cpp @@ -23,6 +23,7 @@ #include "net/net-connections.h" #include "runtime-common/stdlib/string/string-context.h" #include "runtime-common/stdlib/string/string-functions.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/array_functions.h" #include "runtime/bcmath.h" #include "runtime/confdata-functions.h" diff --git a/runtime/job-workers/client-functions.cpp b/runtime/job-workers/client-functions.cpp index 64b75f4323..8e50939abd 100644 --- a/runtime/job-workers/client-functions.cpp +++ b/runtime/job-workers/client-functions.cpp @@ -5,6 +5,7 @@ #include #include +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/critical_section.h" #include "runtime/instance-copy-processor.h" #include "runtime/job-workers/job-interface.h" diff --git a/runtime/job-workers/processing-jobs.cpp b/runtime/job-workers/processing-jobs.cpp index b43ff45b7c..7516bd43c1 100644 --- a/runtime/job-workers/processing-jobs.cpp +++ b/runtime/job-workers/processing-jobs.cpp @@ -4,9 +4,10 @@ #include "runtime/job-workers/processing-jobs.h" -#include "runtime/net_events.h" -#include "runtime/kphp_tracing.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/instance-copy-processor.h" +#include "runtime/kphp_tracing.h" +#include "runtime/net_events.h" #include "server/job-workers/job-message.h" #include "server/job-workers/shared-memory-manager.h" diff --git a/runtime/kphp_tracing.cpp b/runtime/kphp_tracing.cpp index 9bd528dd40..301d10c35c 100644 --- a/runtime/kphp_tracing.cpp +++ b/runtime/kphp_tracing.cpp @@ -7,6 +7,8 @@ #include #include "runtime-common/stdlib/string/string-functions.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/critical_section.h" #include "runtime/interface.h" #include "runtime/job-workers/job-interface.h" @@ -99,7 +101,6 @@ constexpr int SPECIAL_ENUM_TL_MAGICS = 65001; static bool worker_process_has_sent_enums = false; tracing_binary_buffer cur_binlog; -int cur_trace_level; static int cur_last_span_id; static double cur_start_timestamp; static tracing_binary_buffer flush_strings_binlog; @@ -109,12 +110,18 @@ static on_trace_enums_callback_t cur_on_enums_callback; static on_rpc_provide_details_typed_t cur_on_rpc_details_typed_callback; static on_rpc_provide_details_untyped_t cur_on_rpc_details_untyped_callback; +static TracingContext tracing_ctx{}; + +TracingContext &TracingContext::get() noexcept { + return tracing_ctx; +} + int generate_uniq_id() { return f$mt_rand(); } void init_tracing_lib() { - cur_trace_level = -1; + TracingContext::get().cur_trace_level = -1; cur_last_span_id = 0; cur_start_timestamp = 0.0; flush_strings_binlog.set_use_heap_memory(); @@ -183,7 +190,7 @@ static inline int calc_coroutine_id(int64_t fork_id) noexcept { [[gnu::always_inline]] static inline void provide_mem_info() { BinlogWriter::provideMemUsed(dl::get_script_memory_stats().memory_used); - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { provide_advanced_mem_details(); } } @@ -357,7 +364,7 @@ void on_php_script_finish_ok(double net_time, double script_time) { double now_timestamp = calc_now_timestamp(); bool trace_needs_flush = false; - if (!cur_trace.is_null() && cur_on_finish_callback && cur_trace_level >= 1) { + if (!cur_trace.is_null() && cur_on_finish_callback && TracingContext::get().cur_trace_level >= 1) { cur_trace->end_timestamp = now_timestamp; trace_needs_flush = cur_on_finish_callback(now_timestamp); } @@ -412,7 +419,7 @@ void on_php_script_finish_ok(double net_time, double script_time) { cur_trace->to_json(json_buf, 1024); cur_binlog.output_to_json_log(json_buf); - } else if (cur_trace_level != -1) { + } else if (TracingContext::get().cur_trace_level != -1) { // trace is dropped, but if it added new strings to binlog table, save that strings // see kphp_tracing_binlog.cpp dl::CriticalSectionGuard lock; @@ -425,7 +432,7 @@ void on_php_script_finish_ok(double net_time, double script_time) { // so, the trace is dropped, do the same as above // todo think about OOM, we want to flush traces on soft OOM void on_php_script_finish_terminated() { - if (cur_trace_level != -1) { + if (TracingContext::get().cur_trace_level != -1) { dl::CriticalSectionGuard lock; flush_strings_binlog.append_current_php_script_strings(); } @@ -616,7 +623,7 @@ class_instance f$kphp_tracing_init(const string &root_span_title) { } kphp_tracing::cur_start_timestamp = now_timestamp; - kphp_tracing::cur_trace_level = 1; + kphp_tracing::TracingContext::get().cur_trace_level = 1; kphp_tracing::cur_last_span_id = kphp_tracing::SPECIAL_SPAN_ID_ROOT; cur_trace.alloc(); @@ -633,16 +640,17 @@ class_instance f$kphp_tracing_init(const string &root_span_title) { } void f$kphp_tracing_set_level(int64_t trace_level) { - if (kphp_tracing::cur_trace_level == -1 || kphp_tracing::cur_trace_level == trace_level || trace_level < 0 || trace_level > 2) { + auto &cur_trace_level = kphp_tracing::TracingContext::get().cur_trace_level; + if (cur_trace_level == -1 || cur_trace_level == trace_level || trace_level < 0 || trace_level > 2) { return; } - kphp_tracing::cur_trace_level = trace_level; // 0 may be used to disable tracing in the middle + cur_trace_level = trace_level; // 0 may be used to disable tracing in the middle BinlogWriter::onSpanAddedAttributeInt32(kphp_tracing::SPECIAL_SPAN_ID_ROOT, string("trace_level", 11), trace_level); } int64_t f$kphp_tracing_get_level() { - return kphp_tracing::cur_trace_level; + return kphp_tracing::TracingContext::get().cur_trace_level; } void kphp_tracing_register_on_finish_impl(kphp_tracing::on_trace_finish_callback_t &&cb_should_be_flushed) { @@ -661,7 +669,7 @@ void kphp_tracing_register_rpc_details_provider_impl(kphp_tracing::on_rpc_provid class_instance f$kphp_tracing_start_span(const string &title, const string &short_desc, double start_timestamp) { class_instance span; span.alloc(++kphp_tracing::cur_last_span_id); - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { kphp_tracing::provide_mem_info(); if (short_desc.empty()) { BinlogWriter::onSpanCreatedTitleOnly(span->span_id, title, calc_time_offset(start_timestamp)); @@ -722,18 +730,18 @@ void KphpTracingFuncCallGuard::on_finished() const { BinlogWriter::onFuncCallFinished(span_id, calc_time_offset(now_timestamp)); } -void KphpTracingAggregateGuard::on_started(int call_mask) { +void KphpTracingAggregateGuard::on_started(int call_mask) noexcept { func_call_mask = call_mask; double now_timestamp = calc_now_timestamp(); BinlogWriter::onFuncAggregateStarted(func_call_mask, calc_time_offset(now_timestamp)); } -void KphpTracingAggregateGuard::on_enter_branch(int branch_num) const { +void KphpTracingAggregateGuard::on_enter_branch(int branch_num) const noexcept { int mask = (func_call_mask & 0xFFFF) | (branch_num << static_cast(kphp_tracing::BuiltinFuncID::_shift_for_branch)); BinlogWriter::onFuncAggregateBranch(mask); } -void KphpTracingAggregateGuard::on_finished() const { +void KphpTracingAggregateGuard::on_finished() const noexcept { double now_timestamp = calc_now_timestamp(); BinlogWriter::onFuncAggregateFinished(func_call_mask, calc_time_offset(now_timestamp)); } diff --git a/runtime/kphp_tracing.h b/runtime/kphp_tracing.h index 21beb3b687..4d9338be4e 100644 --- a/runtime/kphp_tracing.h +++ b/runtime/kphp_tracing.h @@ -8,6 +8,8 @@ #include "runtime-common/core/class-instance/refcountable-php-classes.h" #include "runtime-common/core/runtime-core.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" +#include "runtime-common/stdlib/tracing/tracing.h" #include "runtime/critical_section.h" #include "runtime/dummy-visitor-methods.h" @@ -20,42 +22,6 @@ struct C$VK$TL$RpcFunction; namespace kphp_tracing { -// see comments about `@kphp-tracing aggregate` -enum class BuiltinFuncID : int { - _max_user_defined_functions = 64000, // and 1.5k for built-in functions (16 bits total) - _max_user_defined_aggregate = 24, // and 7 for built-in aggregates (5 bits total) - _shift_for_branch = 16, - _shift_for_aggregate = 18, - _shift_for_reserved = 23, - - preg_match = _max_user_defined_functions + 1, - preg_match_all, - preg_split, - preg_replace, - preg_replace_callback, - exec, - system, - passthru, - - branch_regex_needs_compilation = 1, - - aggregate_regexp_functions = (_max_user_defined_aggregate + 1) << _shift_for_aggregate, -}; - -// cur_trace_level is level of tracing for current php script -// -1 - not inited, disabled (on php script finish, it's reset to -1) -// 0 - inited, but turned off -// (binlog is not written, on_rpc_query_send() and other "callbacks" are not called) -// 1 - turned on, this php script is traced in a regular mode -// (binlog is written, and when the script finishes, it's either flushed to a file or dismissed) -// 2 - turned on in advanced mode -// (same as 1, but more events with more details are written to binlog, significant overhead) -extern int cur_trace_level; - -[[gnu::always_inline]] inline bool is_turned_on() { - return cur_trace_level >= 1; -} - int generate_uniq_id(); using on_trace_finish_callback_t = std::function; @@ -129,7 +95,6 @@ double f$KphpDiv$$getEndTimestamp(const class_instance &v$this); std::tuple f$KphpDiv$$generateTraceCtxForChild(const class_instance &v$this, int64_t div_id, int64_t trace_flags); int64_t f$KphpDiv$$assignTraceCtx(class_instance v$this, int64_t int1, int64_t int2, const Optional &override_div_id); - // class KphpSpan struct C$KphpSpan : public refcountable_php_classes, private DummyVisitorMethods { @@ -156,63 +121,64 @@ struct C$KphpSpan : public refcountable_php_classes, private DummyVi }; inline void f$KphpSpan$$addAttributeString(const class_instance &v$this, const string &key, const string &value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeString(key, value); } } inline void f$KphpSpan$$addAttributeInt(const class_instance &v$this, const string &key, int64_t value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeInt(key, value); } } inline void f$KphpSpan$$addAttributeFloat(const class_instance &v$this, const string &key, double value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeFloat(key, value); } } inline void f$KphpSpan$$addAttributeBool(const class_instance &v$this, const string &key, bool value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeBool(key, value); } } inline void f$KphpSpan$$addAttributeEnum(const class_instance &v$this, const string &key, int64_t enum_id, int64_t value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeEnum(key, enum_id, value); } } -inline class_instance f$KphpSpan$$addEvent(const class_instance &v$this, const string &name, const Optional &manual_timestamp = {}) { - if (kphp_tracing::cur_trace_level >= 1) { +inline class_instance f$KphpSpan$$addEvent(const class_instance &v$this, const string &name, + const Optional &manual_timestamp = {}) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { return v$this->addEvent(name, manual_timestamp); } return {}; } inline void f$KphpSpan$$addLink(const class_instance &v$this, const class_instance &another) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addLink(another); } } inline void f$KphpSpan$$updateName(const class_instance &v$this, const string &title, const string &short_desc) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->updateName(title, short_desc); } } inline void f$KphpSpan$$finish(const class_instance &v$this, const Optional &manual_timestamp = {}) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->finish(manual_timestamp); } } -inline void f$KphpSpan$$finishWithError(const class_instance &v$this, int64_t error_code, const string &error_msg, const Optional &manual_timestamp = {}) { - if (kphp_tracing::cur_trace_level >= 1) { +inline void f$KphpSpan$$finishWithError(const class_instance &v$this, int64_t error_code, const string &error_msg, + const Optional &manual_timestamp = {}) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->finishWithError(error_code, error_msg, manual_timestamp); } } inline void f$KphpSpan$$exclude(const class_instance &v$this) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->exclude(); } } - // class KphpSpanEvent struct C$KphpSpanEvent : public refcountable_php_classes { @@ -228,27 +194,26 @@ struct C$KphpSpanEvent : public refcountable_php_classes { }; inline void f$KphpSpanEvent$$addAttributeString(const class_instance &v$this, const string &key, const string &value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeString(key, value); } } inline void f$KphpSpanEvent$$addAttributeInt(const class_instance &v$this, const string &key, int64_t value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeInt(key, value); } } inline void f$KphpSpanEvent$$addAttributeFloat(const class_instance &v$this, const string &key, double value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeFloat(key, value); } } inline void f$KphpSpanEvent$$addAttributeBool(const class_instance &v$this, const string &key, bool value) { - if (kphp_tracing::cur_trace_level >= 1) { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1) { v$this->addAttributeBool(key, value); } } - // global tracing functions class_instance f$kphp_tracing_init(const string &root_span_title); @@ -283,7 +248,6 @@ class_instance f$kphp_tracing_start_span(const string &title, const class_instance f$kphp_tracing_get_root_span(); class_instance f$kphp_tracing_get_current_active_span(); - // function calls: autogen from `@kphp-tracing` class KphpTracingFuncCallGuard { @@ -296,65 +260,16 @@ class KphpTracingFuncCallGuard { KphpTracingFuncCallGuard(const KphpTracingFuncCallGuard &) = delete; KphpTracingFuncCallGuard &operator=(const KphpTracingFuncCallGuard &) = delete; - KphpTracingFuncCallGuard() { - } + KphpTracingFuncCallGuard() {} inline void start(const char *f_name, int len, int trace_level) { - if (kphp_tracing::cur_trace_level >= trace_level) { // 1 or 2 + if (kphp_tracing::TracingContext::get().cur_trace_level >= trace_level) { // 1 or 2 on_started(f_name, len); } } - - ~KphpTracingFuncCallGuard() { - if (kphp_tracing::cur_trace_level >= 1 && span_id) { - on_finished(); - } - } -}; - -// function calls: autogen from `@kphp-tracing aggregate` - -class KphpTracingAggregateGuard { - int func_call_mask; - - void on_started(int func_call_mask); - void on_enter_branch(int branch_num) const; - void on_finished() const; - -public: - KphpTracingAggregateGuard(const KphpTracingAggregateGuard &) = delete; - KphpTracingAggregateGuard &operator=(const KphpTracingAggregateGuard &) = delete; - - KphpTracingAggregateGuard() { - // again, constructor is empty, does not even assign 0 to func_call_mask, - // because start() is codegenerated either immediately of in RESUMABLE_BEGIN - } - void start(int call_mask) { - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { - on_started(call_mask); - } - } - explicit KphpTracingAggregateGuard(kphp_tracing::BuiltinFuncID func_id, kphp_tracing::BuiltinFuncID aggregate_bits) { - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { - on_started(static_cast(func_id) | static_cast(aggregate_bits)); - } - } - - void enter_branch(int branch_num) { - // codegenerated instead of kphp_tracing_func_enter_branch(N) - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { - on_enter_branch(branch_num); - } - } - void enter_branch(kphp_tracing::BuiltinFuncID branch_num) { - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { - on_enter_branch(static_cast(branch_num)); - } - } - ~KphpTracingAggregateGuard() { - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { + ~KphpTracingFuncCallGuard() { + if (kphp_tracing::TracingContext::get().cur_trace_level >= 1 && span_id) { on_finished(); } } }; - diff --git a/runtime/php_assert.cpp b/runtime/php_assert.cpp index 03f6629707..76b476d52e 100644 --- a/runtime/php_assert.cpp +++ b/runtime/php_assert.cpp @@ -20,6 +20,7 @@ #include "common/fast-backtrace.h" #include "common/wrappers/pathname.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/critical_section.h" #include "runtime/exception.h" #include "runtime/kphp-backtrace.h" diff --git a/runtime/regexp.cpp b/runtime/regexp.cpp index 3fb1a67db7..98e1d90fd4 100644 --- a/runtime/regexp.cpp +++ b/runtime/regexp.cpp @@ -11,6 +11,7 @@ #endif #include "common/unicode/utf8-utils.h" +#include "runtime/allocator.h" #include "runtime/critical_section.h" #include "server/php-engine-vars.h" #include "server/php-runner.h" diff --git a/runtime/regexp.h b/runtime/regexp.h index 50b5aa9dc6..268d1f0424 100644 --- a/runtime/regexp.h +++ b/runtime/regexp.h @@ -9,9 +9,9 @@ #include "common/mixin/not_copyable.h" #include "runtime-common/core/runtime-core.h" -#include "runtime/context/runtime-context.h" -#include "runtime/kphp_tracing.h" #include "runtime-common/stdlib/string/mbstring-functions.h" +#include "runtime-common/stdlib/tracing/tracing.h" +#include "runtime/context/runtime-context.h" namespace re2 { class RE2; @@ -70,7 +70,7 @@ class regexp : vk::not_copyable { template inline string get_replacement(const T &replace_val, const string &subject, int64_t count) const; - void pattern_compilation_warning(const char *function, const char *file, char const *message, ...) noexcept __attribute__ ((format (printf, 4, 5))); + void pattern_compilation_warning(const char *function, const char *file, char const *message, ...) noexcept __attribute__((format(printf, 4, 5))); void check_pattern_compilation_warning() const noexcept; @@ -113,7 +113,6 @@ void global_init_regexp_lib(); inline void preg_add_match(array &v, const mixed &match, const string &name); inline void preg_add_match(array &v, const string &match, const string &name); - inline Optional f$preg_match(const regexp ®ex, const string &subject); inline Optional f$preg_match_all(const regexp ®ex, const string &subject); @@ -153,42 +152,56 @@ inline Optional f$preg_match_all(const mixed ®ex, const string &subj template> inline auto f$preg_replace(const T1 ®ex, const T2 &replace_val, const T3 &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); -inline Optional f$preg_replace(const regexp ®ex, const string &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline Optional f$preg_replace(const regexp ®ex, const string &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline Optional f$preg_replace(const regexp ®ex, const mixed &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline Optional f$preg_replace(const regexp ®ex, const mixed &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline mixed f$preg_replace(const regexp ®ex, const string &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline mixed f$preg_replace(const regexp ®ex, const string &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline mixed f$preg_replace(const regexp ®ex, const mixed &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline mixed f$preg_replace(const regexp ®ex, const mixed &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template -inline auto f$preg_replace(const string ®ex, const T1 &replace_val, const T2 &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline auto f$preg_replace(const string ®ex, const T1 &replace_val, const T2 &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline Optional f$preg_replace(const mixed ®ex, const string &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline Optional f$preg_replace(const mixed ®ex, const string &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline mixed f$preg_replace(const mixed ®ex, const string &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline mixed f$preg_replace(const mixed ®ex, const string &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline Optional f$preg_replace(const mixed ®ex, const mixed &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline Optional f$preg_replace(const mixed ®ex, const mixed &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); -inline mixed f$preg_replace(const mixed ®ex, const mixed &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +inline mixed f$preg_replace(const mixed ®ex, const mixed &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template> auto f$preg_replace_callback(const T1 ®ex, const T2 &replace_val, const T3 &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); template -Optional f$preg_replace_callback(const regexp ®ex, const T &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +Optional f$preg_replace_callback(const regexp ®ex, const T &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template -mixed f$preg_replace_callback(const regexp ®ex, const T &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +mixed f$preg_replace_callback(const regexp ®ex, const T &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template -auto f$preg_replace_callback(const string ®ex, const T &replace_val, const T2 &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +auto f$preg_replace_callback(const string ®ex, const T &replace_val, const T2 &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template -Optional f$preg_replace_callback(const mixed ®ex, const T &replace_val, const string &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +Optional f$preg_replace_callback(const mixed ®ex, const T &replace_val, const string &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); template -mixed f$preg_replace_callback(const mixed ®ex, const T &replace_val, const mixed &subject, int64_t limit = -1, int64_t &replace_count = preg_replace_count_dummy); +mixed f$preg_replace_callback(const mixed ®ex, const T &replace_val, const mixed &subject, int64_t limit = -1, + int64_t &replace_count = preg_replace_count_dummy); inline Optional> f$preg_split(const regexp ®ex, const string &subject, int64_t limit = -1, int64_t flags = 0); @@ -200,14 +213,12 @@ string f$preg_quote(const string &str, const string &delimiter = string()); inline int64_t f$preg_last_error(); - /* * * IMPLEMENTATION * */ - template<> inline string regexp::get_replacement(const string &replace_val, const string &subject, int64_t count) const { const string::size_type len = replace_val.size(); @@ -243,12 +254,11 @@ inline string regexp::get_replacement(const string &replace_val, const string &s } else { if (backref < count) { int64_t index = backref + backref; - kphp_runtime_context.static_SB.append(subject.c_str() + submatch[index], - static_cast(submatch[index + 1] - submatch[index])); + kphp_runtime_context.static_SB.append(subject.c_str() + submatch[index], static_cast(submatch[index + 1] - submatch[index])); } } } - return kphp_runtime_context.static_SB.str();//TODO optimize + return kphp_runtime_context.static_SB.str(); // TODO optimize } template @@ -270,11 +280,10 @@ string regexp::get_replacement(const T &replace_val, const string &subject, cons return f$strval(replace_val(result_set)); } - template Optional regexp::replace(const T &replace_val, const string &subject, int64_t limit, int64_t &replace_count) const { pcre_last_error = 0; - int64_t result_count = 0;//calls can be recursive, can't write to replace_count directly + int64_t result_count = 0; // calls can be recursive, can't write to replace_count directly check_pattern_compilation_warning(); if (pcre_regexp == nullptr && RE2_regexp == nullptr) { @@ -298,7 +307,7 @@ Optional regexp::replace(const T &replace_val, const string &subject, in while (offset <= int64_t{subject.size()} && limit > 0) { int64_t count = exec(subject, offset, second_try); -// fprintf (stderr, "Found %d submatches at %d:%d from pos %d, pcre_last_error = %d\n", count, submatch[0], submatch[1], offset, pcre_last_error); + // fprintf (stderr, "Found %d submatches at %d:%d from pos %d, pcre_last_error = %d\n", count, submatch[0], submatch[1], offset, pcre_last_error); if (count == 0) { if (second_try) { second_try = false; @@ -338,7 +347,6 @@ Optional regexp::replace(const T &replace_val, const string &subject, in return result; } - void preg_add_match(array &v, const mixed &match, const string &name) { if (name.size()) { v.set_value(name, match); @@ -451,7 +459,6 @@ Optional f$preg_match_all(const mixed ®ex, const string &subject, mi return f$preg_match_all(regexp(regex.to_string()), subject, matches, flags, offset); } - template inline auto f$preg_replace(const T1 ®ex, const T2 &replace_val, const T3 &subject, int64_t limit, int64_t &replace_count) { return f$preg_replace(regex, replace_val, subject.val(), limit, replace_count); @@ -675,5 +682,3 @@ Optional> f$preg_split(const mixed ®ex, const string &subject, i int64_t f$preg_last_error() { return regexp::last_error(); } - - diff --git a/runtime/resumable.cpp b/runtime/resumable.cpp index a113e12bdb..51d2c094b0 100644 --- a/runtime/resumable.cpp +++ b/runtime/resumable.cpp @@ -6,9 +6,11 @@ #include "common/kprintf.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" +#include "runtime/kphp_tracing.h" #include "runtime/net_events.h" #include "server/php-queries.h" -#include "runtime/kphp_tracing.h" DEFINE_VERBOSITY(resumable); @@ -510,7 +512,7 @@ int64_t fork_resumable(Resumable *resumable) noexcept { if (kphp_tracing::is_turned_on()) { kphp_tracing::on_fork_start(id); - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { kphp_tracing::on_fork_provide_name(id, string{typeid(*resumable).name()}); } } diff --git a/runtime/rpc.cpp b/runtime/rpc.cpp index d3f0ca9cd7..cc03932f43 100644 --- a/runtime/rpc.cpp +++ b/runtime/rpc.cpp @@ -12,6 +12,8 @@ #include "common/rpc-headers.h" #include "common/tl/constants/common.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/context/runtime-context.h" #include "runtime/critical_section.h" #include "runtime/exception.h" @@ -1192,7 +1194,7 @@ int64_t rpc_tl_query_impl(const class_instance &c, const mixed return 0; } - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { kphp_tracing::on_rpc_query_provide_details_after_send({}, tl_object); } diff --git a/runtime/typed_rpc.cpp b/runtime/typed_rpc.cpp index e327619f6a..3a7f8257d4 100644 --- a/runtime/typed_rpc.cpp +++ b/runtime/typed_rpc.cpp @@ -7,6 +7,7 @@ #include "common/containers/final_action.h" #include "common/rpc-error-codes.h" +#include "runtime-common/stdlib/tracing/tracing-context.h" #include "runtime/kphp_tracing.h" #include "runtime/resumable.h" #include "runtime/rpc.h" @@ -160,7 +161,7 @@ typed_rpc_tl_query_impl(const class_instance &connection, const if (query_id <= 0) { return 0; } - if (unlikely(kphp_tracing::cur_trace_level >= 2)) { + if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) { kphp_tracing::on_rpc_query_provide_details_after_send(req.get_tl_function(), {}); } if (flush) { diff --git a/server/php-runner.cpp b/server/php-runner.cpp index d02d04d3ef..c21c8c02b7 100644 --- a/server/php-runner.cpp +++ b/server/php-runner.cpp @@ -19,6 +19,7 @@ #include "common/precise-time.h" #include "common/wrappers/memory-utils.h" #include "common/wrappers/overloaded.h" +#include "runtime-common/stdlib/tracing/tracing-functions.h" #include "runtime/allocator.h" #include "runtime/critical_section.h" #include "runtime/curl.h" diff --git a/tests/phpt/tracing/change_level.php b/tests/phpt/tracing/change_level.php new file mode 100644 index 0000000000..3fe402138a --- /dev/null +++ b/tests/phpt/tracing/change_level.php @@ -0,0 +1,18 @@ +@ok k2_skip + +int(1) +int(2) +int(1) +