Skip to content

Commit

Permalink
add script_time_stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Sadokhov committed Sep 6, 2023
1 parent 67e85d1 commit 86ac071
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion server/php-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,7 @@ int run_main(int argc, char **argv, php_mode mode) {
max_special_connections = 1;
set_on_active_special_connections_update_callback([] (bool on_accept) {
if (on_accept) {
PhpScript::last_conn_start_processing_time = get_utime_monotonic();
PhpScript::script_time_stats.conn_accept_time = get_utime_monotonic();
}
vk::singleton<ServerStats>::get().update_active_connections(active_special_connections, max_special_connections);
});
Expand Down
26 changes: 13 additions & 13 deletions server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ void PhpScript::init(script_t *script, php_query_data *data_to_set) noexcept {

error_message = "??? error";

script_time = 0;
net_time = 0;
cur_timestamp = dl_time();
script_time_stats.script_time = 0;
script_time_stats.net_time = 0;
queries_cnt = 0;
long_queries_cnt = 0;
cur_timestamp = dl_time();

query_stats_id++;
memset(&query_stats, 0, sizeof(query_stats));
Expand Down Expand Up @@ -267,15 +267,15 @@ void PhpScript::update_net_time() noexcept {
kprintf("Awakening net event: %s\n", event->get_description());
}
}
net_time += net_add;
script_time_stats.net_time += net_add;
last_net_time_delta = net_add;

cur_timestamp = new_cur_timestamp;
}

void PhpScript::update_script_time() noexcept {
double new_cur_timestamp = dl_time();
script_time += new_cur_timestamp - cur_timestamp;
script_time_stats.script_time += new_cur_timestamp - cur_timestamp;
cur_timestamp = new_cur_timestamp;
}

Expand Down Expand Up @@ -307,7 +307,9 @@ void PhpScript::finish() noexcept {
const auto &script_mem_stats = dl::get_script_memory_stats();
state = run_state_t::uncleared;
update_net_time();
vk::singleton<ServerStats>::get().add_request_stats(script_time, net_time, last_script_start_time - last_worker_init_time, last_script_start_time - last_conn_start_processing_time,
double script_init_time_sec = script_time_stats.script_start_time - script_time_stats.worker_init_time;
double connection_process_time_sec = script_time_stats.script_start_time - script_time_stats.conn_accept_time;
vk::singleton<ServerStats>::get().add_request_stats(script_time_stats.script_time, script_time_stats.net_time, script_init_time_sec, connection_process_time_sec,
queries_cnt, long_queries_cnt, script_mem_stats.max_memory_used,
script_mem_stats.max_real_memory_used, vk::singleton<CurlMemoryUsage>::get().total_allocated, error_type);
if (save_state == run_state_t::error) {
Expand Down Expand Up @@ -339,7 +341,7 @@ void PhpScript::finish() noexcept {
}
}
kprintf("[worked = %.3lf, net = %.3lf, script = %.3lf, queries_cnt = %5d, long_queries_cnt = %5d, heap_memory_used = %9d, peak_script_memory = %9d, total_script_memory = %9d] %s\n",
script_time + net_time, net_time, script_time, queries_cnt, long_queries_cnt,
script_time_stats.script_time + script_time_stats.net_time, script_time_stats.net_time, script_time_stats.script_time, queries_cnt, long_queries_cnt,
(int)dl::get_heap_memory_used(),
(int)script_mem_stats.max_real_memory_used,
(int)script_mem_stats.real_memory_used, buf);
Expand Down Expand Up @@ -428,7 +430,7 @@ void PhpScript::run() noexcept {
check_net_context_errors();

CurException = Optional<bool>{};
PhpScript::last_script_start_time = get_utime_monotonic();
PhpScript::script_time_stats.script_start_time = get_utime_monotonic();
run_main->run();
if (CurException.is_null()) {
set_script_result(nullptr);
Expand Down Expand Up @@ -465,7 +467,7 @@ void PhpScript::reset_script_timeout() noexcept {
}

double PhpScript::get_net_time() const noexcept {
return net_time;
return script_time_stats.net_time;
}

long long PhpScript::memory_get_total_usage() const noexcept {
Expand All @@ -475,7 +477,7 @@ long long PhpScript::memory_get_total_usage() const noexcept {
double PhpScript::get_script_time() noexcept {
assert_state(run_state_t::running);
update_script_time();
return script_time;
return script_time_stats.script_time;
}

int PhpScript::get_net_queries_count() const noexcept {
Expand All @@ -487,9 +489,7 @@ ucontext_t_portable PhpScript::exit_context;
volatile bool PhpScript::in_script_context = false;
volatile bool PhpScript::time_limit_exceeded = false;
volatile bool PhpScript::memory_limit_exceeded = false;
double PhpScript::last_conn_start_processing_time = 0;
double PhpScript::last_worker_init_time = 0;
double PhpScript::last_script_start_time = 0;
PhpScript::script_time_stats_t PhpScript::script_time_stats;

static __inline__ void *get_sp() {
return __builtin_frame_address(0);
Expand Down
15 changes: 10 additions & 5 deletions server/php-runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ class PhpScriptStack : vk::not_copyable {
* It stores state of the script: current execution point, pointers to allocated script memory, stack for script context, etc.
*/
class PhpScript {
double cur_timestamp{0}, net_time{0}, script_time{0};
double cur_timestamp{0};
double last_net_time_delta{0};
int queries_cnt{0};
int long_queries_cnt{0};

struct script_time_stats_t {
double net_time{0};
double script_time{0};
double conn_accept_time{0};
double worker_init_time{0};
double script_start_time{0};
};

private:
int swapcontext_helper(ucontext_t_portable *oucp, const ucontext_t_portable *ucp);

Expand All @@ -98,10 +106,7 @@ class PhpScript {
volatile static bool in_script_context;
volatile static bool time_limit_exceeded;
volatile static bool memory_limit_exceeded;

static double last_conn_start_processing_time;
static double last_worker_init_time;
static double last_script_start_time;
static script_time_stats_t script_time_stats;

run_state_t state{run_state_t::empty};
const char *error_message{nullptr};
Expand Down
2 changes: 1 addition & 1 deletion server/php-worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ PhpWorker::PhpWorker(php_worker_mode_t mode_, connection *c, http_query_data *ht
, mode(mode_)
, req_id(req_id_)
{
PhpScript::last_worker_init_time = init_time;
PhpScript::script_time_stats.worker_init_time = init_time;
assert(c != nullptr);
if (conn->target) {
target_fd = static_cast<int>(conn->target - Targets);
Expand Down

0 comments on commit 86ac071

Please sign in to comment.