Skip to content

Commit

Permalink
Move small part of KPHP tracing functionality into runtime-common/std…
Browse files Browse the repository at this point in the history
…lib (#1132)
  • Loading branch information
apolyakov authored Nov 3, 2024
1 parent f926025 commit a40bb1a
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 160 deletions.
27 changes: 27 additions & 0 deletions runtime-common/stdlib/tracing/tracing-context.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

#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
15 changes: 15 additions & 0 deletions runtime-common/stdlib/tracing/tracing-functions.h
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions runtime-common/stdlib/tracing/tracing.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

#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<int>(func_id) | static_cast<int>(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<int>(branch_num));
}
}

~KphpTracingAggregateGuard() {
if (unlikely(kphp_tracing::TracingContext::get().cur_trace_level >= 2)) {
on_finished();
}
}
};
1 change: 1 addition & 0 deletions runtime/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <curl/easy.h>
#include <curl/multi.h>

#include "runtime-common/stdlib/tracing/tracing-functions.h"
#include "runtime/context/runtime-context.h"
#include "runtime/critical_section.h"
#include "runtime/interface.h"
Expand Down
1 change: 1 addition & 0 deletions runtime/exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdlib>

#include "common/smart_ptrs/unique_ptr_with_delete_function.h"
#include "runtime-common/stdlib/tracing/tracing-functions.h"
#include "runtime/kphp_tracing.h"

namespace {
Expand Down
1 change: 1 addition & 0 deletions runtime/files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions runtime/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions runtime/job-workers/client-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <chrono>

#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"
Expand Down
5 changes: 3 additions & 2 deletions runtime/job-workers/processing-jobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 22 additions & 14 deletions runtime/kphp_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <chrono>

#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"
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -616,7 +623,7 @@ class_instance<C$KphpDiv> 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();
Expand All @@ -633,16 +640,17 @@ class_instance<C$KphpDiv> 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) {
Expand All @@ -661,7 +669,7 @@ void kphp_tracing_register_rpc_details_provider_impl(kphp_tracing::on_rpc_provid
class_instance<C$KphpSpan> f$kphp_tracing_start_span(const string &title, const string &short_desc, double start_timestamp) {
class_instance<C$KphpSpan> 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));
Expand Down Expand Up @@ -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<int>(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));
}
Loading

0 comments on commit a40bb1a

Please sign in to comment.