Skip to content

Commit

Permalink
server: simplify load calculation
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <hexian000@outlook.com>
  • Loading branch information
hexian000 committed Feb 29, 2024
1 parent 9b4b8c6 commit 1dd27d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ struct server *server_new(struct ev_loop *loop, struct config *restrict conf)
conf->keepalive * 3.0 + ping_timeout, 60.0, 1800.0),
.ping_timeout = ping_timeout,
.time_wait = conf->time_wait,
.clock = (clock_t)(-1),
.last_clock = (clock_t)(-1),
};

Expand Down Expand Up @@ -735,17 +734,16 @@ static bool update_load(
struct server *restrict s, char *buf, const size_t bufsize,
const double dt)
{
bool ok = false;
s->clock = clock();
if (s->clock != (clock_t)(-1) && s->last_clock != (clock_t)(-1) &&
s->clock > s->last_clock) {
const double load = (double)(s->clock - s->last_clock) /
(double)(CLOCKS_PER_SEC) / dt * 100.0;
(void)snprintf(buf, bufsize, "%.03f%%", load);
ok = true;
}
s->last_clock = s->clock;
return ok;
const clock_t last = s->last_clock;
const clock_t now = clock();
s->last_clock = now;
if (now == (clock_t)(-1) || last == (clock_t)(-1) || now < last) {
return false;
}
const double load =
(double)(now - last) / (double)(CLOCKS_PER_SEC) / dt * 100.0;
(void)snprintf(buf, bufsize, "%.03f%%", load);
return true;
}

struct vbuffer *
Expand Down Expand Up @@ -823,7 +821,6 @@ struct vbuffer *server_stats(
#undef FORMAT_BYTES

/* rotate stats */
s->last_clock = s->clock;
s->last_stats = s->stats;
s->last_stats_time = now;
return buf;
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct server {
ev_tstamp started;
ev_tstamp last_stats_time;
ev_tstamp last_resolve_time;
clock_t clock, last_clock;
clock_t last_clock;
};
};

Expand Down

0 comments on commit 1dd27d7

Please sign in to comment.