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

Improve QPS calculation #2076

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ void *GC_thread(void *val)
while(!killed)
{
const time_t now = time(NULL);
const double time_start = double_time();
if(config.dns.rateLimit.interval.v.ui > 0 &&
(unsigned int)(now - lastRateLimitCleaner) >= config.dns.rateLimit.interval.v.ui)
{
Expand Down Expand Up @@ -560,7 +561,26 @@ void *GC_thread(void *val)
reread_config();
}

thread_sleepms(GC, 1000);
// Intermediate cancellation-point
if(killed)
break;

// Reset the queries-per-second counter
lock_shm();
reset_qps(now);
unlock_shm();

// Intermediate cancellation-point
if(killed)
break;

// Sleep for the remaining time of the interval (if any)
const double time_end = double_time();
const double time_diff = time_end - time_start;
const double sleep_time = 1.0 - time_diff; // 1.0 second interval

if(sleep_time > 0)
thread_sleepms(GC, (unsigned int)(sleep_time*1000));
}

// Close inotify watcher
Expand Down
32 changes: 13 additions & 19 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,29 +1210,23 @@ int __attribute__((pure)) is_shm_fd(const int fd)
// Update queries per second (qps) value
// This is done in shared memory to allow for both UDP and TCP workers to
// contribute.
void update_qps(const double timestamp)
void update_qps(const time_t timestamp)
{
// Get the timeslot for the current timestamp
const unsigned int slot = (unsigned int)timestamp % QPS_AVGLEN;
const unsigned int slot = timestamp % QPS_AVGLEN;

// Check if the timestamp is in the same slot as the last one
if(shmSettings->qps.last != slot)
{
// Reset all the slots in between
// This is relevant if less than one query per second is
// received and the intermediate slots are not updated
for(unsigned int i = (shmSettings->qps.last + 1) % QPS_AVGLEN; i != slot; i = (i + 1) % QPS_AVGLEN)
shmSettings->qps.buf[i] = 0;

// Reset the current slot
shmSettings->qps.buf[slot] = 0;
// Add the query
shmSettings->qps[slot]++;
}

// Update the last slot index
shmSettings->qps.last = slot;
}
// Reset queries per second (qps) value for a given timeslot
void reset_qps(const time_t timestamp)
{
// Get the timeslot for the current timestamp
const unsigned int slot = timestamp % QPS_AVGLEN;

// Add the query
shmSettings->qps.buf[slot]++;
// Reset the query count
shmSettings->qps[slot] = 0;
}

// Compute queries per second (qps) value
Expand All @@ -1245,7 +1239,7 @@ double __attribute__((pure)) get_qps(void)
//
double qps = 0.0;
for(unsigned int i = 0; i < QPS_AVGLEN; i++)
qps += shmSettings->qps.buf[i];
qps += shmSettings->qps[i];

return qps / QPS_AVGLEN;
}
8 changes: 3 additions & 5 deletions src/shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ typedef struct {
pid_t pid;
unsigned int global_shm_counter;
unsigned int next_str_pos;
struct {
unsigned int last;
unsigned int buf[QPS_AVGLEN];
} qps;
unsigned int qps[QPS_AVGLEN];
} ShmSettings;

typedef struct {
Expand Down Expand Up @@ -155,7 +152,8 @@ void set_per_client_regex(const int clientID, const int regexID, const bool valu
// Used in dnsmasq/utils.c
int is_shm_fd(const int fd);

void update_qps(const double timestamp);
void update_qps(const time_t timestamp);
void reset_qps(const time_t timestamp);
double get_qps(void) __attribute__((pure));

#endif //SHARED_MEMORY_SERVER_H
Loading