Skip to content

Commit

Permalink
Outsource counter resetting into a dedicated function periodically ru…
Browse files Browse the repository at this point in the history
…n by the GC thread to make the average computation independet from external trigger sources. This solves the problem of artificially high QPS values when no queries are processed by FTL.

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Oct 2, 2024
1 parent 6c0c875 commit ea778e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ void *GC_thread(void *val)
reread_config();
}

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

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

thread_sleepms(GC, 1000);
}

Expand Down
30 changes: 12 additions & 18 deletions src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,31 +1210,25 @@ 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;

// 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;

// Update the last slot index
shmSettings->qps.last = slot;
}
const unsigned int slot = timestamp % QPS_AVGLEN;

// Add the query
shmSettings->qps.buf[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;

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

// Compute queries per second (qps) value
double __attribute__((pure)) get_qps(void)
{
Expand Down
3 changes: 2 additions & 1 deletion src/shmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,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

0 comments on commit ea778e1

Please sign in to comment.