Skip to content

Commit

Permalink
Compute average response times for used upstream destinations.
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Dec 25, 2019
1 parent 05eac8d commit 586fee1
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FTL_DEPS = *.h database/*.h api/*.h version.h
FTL_DB_OBJ = database/common.o database/query-table.o database/network-table.o database/gravity-db.o database/database-thread.o
FTL_API_OBJ = api/http-common.o api/routes.o api/ftl.o api/stats.o api/dns.o api/version.o api/auth.o api/settings.o api/stats_database.o
FTL_OBJ = $(FTL_DB_OBJ) $(FTL_API_OBJ) main.o memory.o log.o daemon.o datastructure.o signals.o files.o setupVars.o args.o gc.o config.o \
dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o overTime.o timers.o vector.o
dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o overTime.o timers.o vector.o math.o

DNSMASQ_DEPS = config.h dhcp-protocol.h dns-protocol.h radv-protocol.h dhcp6-protocol.h dnsmasq.h ip6addr.h metrics.h ../dnsmasq_interface.h
DNSMASQ_OBJ = arp.o dbus.o domain.o lease.o outpacket.o rrfilter.o auth.o dhcp6.o edns0.o log.o poll.o slaac.o blockdata.o dhcp.o forward.o \
Expand Down
19 changes: 19 additions & 0 deletions src/api/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "overTime.h"
// enum REGEX
#include "regex_r.h"
// my_sqrt()
#include "../math.h"

/* qsort comparision function (count field), sort ASC
static int __attribute__((pure)) cmpasc(const void *a, const void *b)
Expand Down Expand Up @@ -475,6 +477,7 @@ int api_stats_upstreams(struct mg_connection *conn)
{
int count = 0;
const char* ip, *name;
double responsetime = 0.0, uncertainty = 0.0;

if(i == -2)
{
Expand Down Expand Up @@ -507,6 +510,20 @@ int api_stats_upstreams(struct mg_connection *conn)

// Get percentage
count = forward->count;

// Compute average response time and uncertainty (unit: seconds)
if(forward->responses > 0)
{
// Wehave to multiply runcertainty by 1e-4 to get seconds
responsetime = 1e-4 * forward->rtime / forward->responses;
}
if(forward->responses > 1)
{
// The actual value will be somewhere in a neighborhood around the mean value.
// This neighborhood of values is the uncertainty in the mean.
// Wehave to multiply runcertainty by (1e-4)^2 to get seconds
uncertainty = my_sqrt(1e-8 * forward->rtuncertainty / forward->responses / (forward->responses-1));
}
}

// Send data:
Expand All @@ -518,6 +535,8 @@ int api_stats_upstreams(struct mg_connection *conn)
JSON_OBJ_REF_STR(upstream, "name", name);
JSON_OBJ_REF_STR(upstream, "ip", ip);
JSON_OBJ_ADD_NUMBER(upstream, "count", count);
JSON_OBJ_ADD_NUMBER(upstream, "responsetime", responsetime);
JSON_OBJ_ADD_NUMBER(upstream, "uncertainty", uncertainty);
JSON_ARRAY_ADD_ITEM(upstreams, upstream);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ int findForwardID(const char * forwardString, const bool count)
// to be done separately to be non-blocking
forward->new = true;
forward->namepos = 0; // 0 -> string with length zero
// Initialize response time values
forward->rtime = 0u;
forward->rtuncertainty = 0u;
forward->responses = 0u;
// Increase counter by one
counters->forwarded++;

Expand Down
3 changes: 3 additions & 0 deletions src/datastructure.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ typedef struct {
unsigned char magic;
size_t ippos;
size_t namepos;
unsigned long rtime;
unsigned long rtuncertainty;
unsigned int responses;
int count;
int failed;
bool new;
Expand Down
8 changes: 8 additions & 0 deletions src/dnsmasq_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,14 @@ void _FTL_reply(const unsigned short flags, const char *name, const struct all_a
}
else if((flags & F_FORWARD) && isExactMatch)
{
// Save query response time
forwardedData *forward = getForward(query->forwardID, true);
forward->responses++;
unsigned long rtime = converttimeval(response) - query->response;
forward->rtime += rtime;
unsigned long mean = forward->rtime / forward->responses;
forward->rtuncertainty += (mean - rtime)*(mean - rtime);

// Only proceed if query is not already known
// to have been blocked by Quad9
if(query->reply != QUERY_EXTERNAL_BLOCKED_IP &&
Expand Down
52 changes: 52 additions & 0 deletions src/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2019 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Math Implementation
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

#include "math.h"

// Recursive function that returns square root
// of a number with precision upto 5 decimal places
double Square(double n, double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;

if ((mul - n)*(mul - n) < (SQRT_PRECISION * SQRT_PRECISION))
{
return mid;
}
else if (mul < n)
{
return Square(n, mid, j);
}
else
{
return Square(n, i, mid);
}
}

// Function to find the square root of n
double my_sqrt(double n)
{
// While the square root is not found
for(double i = 1.0; true; i++)
{
if ((i - n)*(i - n) < (SQRT_PRECISION * SQRT_PRECISION))
{
// If n is a perfect square
return i;
}
else if (i * i > n)
{
// Square root will lie in the interval i-1 and i
return Square(n, i - 1, i);
}
i++;
}
}
25 changes: 25 additions & 0 deletions src/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2019 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* Math Prototypes
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

#ifndef MATH_H
#define MATH_H

#include <stdlib.h>
#include <stdbool.h>

#define SQRT_PRECISION 1e-5

// Recursive function that returns square root
double Square(double n, double i, double j);

// Function to find the square root of n
double my_sqrt(double n);

#endif //MATH_H
2 changes: 1 addition & 1 deletion src/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "api/ftl.h"

/// The version of shared memory used
#define SHARED_MEMORY_VERSION 8
#define SHARED_MEMORY_VERSION 9

/// The name of the shared memory. Use this when connecting to the shared memory.
#define SHARED_LOCK_NAME "/FTL-lock"
Expand Down

2 comments on commit 586fee1

@pralor-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/grafity-db-neu-erstellen/26258/12

@pralor-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Pi-hole Userspace. There might be relevant details there:

https://discourse.pi-hole.net/t/grafity-db-neu-erstellen/26258/12

Please sign in to comment.