GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
gnunet-daemon-latency-logger.c File Reference

log latency values from neighbour connections into an SQLite database More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_ats_service.h"
#include <sqlite3.h>
Include dependency graph for gnunet-daemon-latency-logger.c:

Go to the source code of this file.

Data Structures

struct  Entry
 Entry type to be used in the map to store old latency values. More...
 

Macros

#define LOG(type, ...)   GNUNET_log (type, __VA_ARGS__)
 Logging shorthand. More...
 
#define DEBUG(...)   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 Debug logging shorthand. More...
 
#define LOG_SQLITE(db, msg, level, cmd)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno). More...
 

Functions

static int free_iterator (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
 Iterator over hash map entries. More...
 
static void do_shutdown (void *cls)
 Shutdown. More...
 
static void addr_info_cb (void *cls, const struct GNUNET_HELLO_Address *address, int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, const struct GNUNET_ATS_Properties *prop)
 Signature of a function that is called with QoS information about an address. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Main function that will be run. More...
 
int main (int argc, char *const *argv)
 Execution entry point. More...
 

Variables

static struct GNUNET_CONTAINER_MultiPeerMapmap
 Handle to the map used to store old latency values for peers. More...
 
static struct sqlite3 * db
 The SQLite database handle. More...
 
static struct GNUNET_ATS_PerformanceHandleats
 Handle to the ATS performance subsystem. More...
 
static struct sqlite3_stmt * stmt_insert
 Prepared statement for inserting values into the database table. More...
 

Detailed Description

log latency values from neighbour connections into an SQLite database

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in

Definition in file gnunet-daemon-latency-logger.c.

Macro Definition Documentation

◆ LOG

#define LOG (   type,
  ... 
)    GNUNET_log (type, __VA_ARGS__)

Logging shorthand.

Definition at line 36 of file gnunet-daemon-latency-logger.c.

Referenced by run().

◆ DEBUG

#define DEBUG (   ...)    LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)

Debug logging shorthand.

Definition at line 42 of file gnunet-daemon-latency-logger.c.

Referenced by run().

◆ LOG_SQLITE

#define LOG_SQLITE (   db,
  msg,
  level,
  cmd 
)
Value:
do { \
GNUNET_log_from (level, "sqlite", _ ( \
"`%s' failed at %s:%d with error: %s\n"), \
cmd, __FILE__, __LINE__, sqlite3_errmsg (db)); \
if (msg != NULL) \
GNUNET_asprintf (msg, _ ("`%s' failed at %s:%u with error: %s"), cmd, \
__FILE__, __LINE__, sqlite3_errmsg (db)); \
} while (0)
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static struct sqlite3 * db
The SQLite database handle.

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno).

Definition at line 50 of file gnunet-daemon-latency-logger.c.

Referenced by addr_info_cb(), and run().

Function Documentation

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Shutdown.

Parameters
clsNULL
Returns

Definition at line 132 of file gnunet-daemon-latency-logger.c.

References db, free_iterator(), GNUNET_assert, GNUNET_ATS_performance_done(), GNUNET_break, GNUNET_CONTAINER_multipeermap_destroy(), GNUNET_CONTAINER_multipeermap_iterate(), GNUNET_SYSERR, and stmt_insert.

Referenced by run().

133 {
135  ats = NULL;
136  if (NULL != stmt_insert)
137  {
138  sqlite3_finalize (stmt_insert);
139  stmt_insert = NULL;
140  }
141  GNUNET_break (SQLITE_OK == sqlite3_close (db));
142  db = NULL;
143  if (NULL != map)
144  {
147  NULL));
149  map = NULL;
150  }
151 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_ATS_performance_done(struct GNUNET_ATS_PerformanceHandle *ph)
Client is done using the ATS performance subsystem, release resources.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
static struct sqlite3 * db
The SQLite database handle.
static struct sqlite3_stmt * stmt_insert
Prepared statement for inserting values into the database table.
static struct GNUNET_ATS_PerformanceHandle * ats
Handle to the ATS performance subsystem.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
static int free_iterator(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Iterator over hash map entries.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ addr_info_cb()

static void addr_info_cb ( void *  cls,
const struct GNUNET_HELLO_Address address,
int  address_active,
struct GNUNET_BANDWIDTH_Value32NBO  bandwidth_out,
struct GNUNET_BANDWIDTH_Value32NBO  bandwidth_in,
const struct GNUNET_ATS_Properties prop 
)
static

Signature of a function that is called with QoS information about an address.

Parameters
clsclosure
addressthe address
address_activeGNUNET_YES if this address is actively used to maintain a connection to a peer; GNUNET_NO if the address is not actively used; GNUNET_SYSERR if this address is no longer available for ATS
bandwidth_outassigned outbound bandwidth for the connection
bandwidth_inassigned inbound bandwidth for the connection
propperformance data for the address (as far as known)

Definition at line 168 of file gnunet-daemon-latency-logger.c.

References db, GNUNET_ATS_Properties::delay, GNUNET_assert, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_CONTAINER_multipeermap_contains(), GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_put(), GNUNET_ERROR_TYPE_ERROR, GNUNET_i2s(), GNUNET_new, GNUNET_SCHEDULER_shutdown(), GNUNET_YES, Entry::id, Entry::latency, LOG_SQLITE, GNUNET_HELLO_Address::peer, GNUNET_TIME_Relative::rel_value_us, and stmt_insert.

Referenced by GNUNET_ATS_performance_init(), and run().

174 {
175  static const char *query_insert =
176  "INSERT INTO ats_info("
177  " id,"
178  " val,"
179  " timestamp"
180  ") VALUES ("
181  " ?1,"
182  " ?2,"
183  " datetime('now')"
184  ");";
185  struct Entry *entry;
186  int latency; /* FIXME: type!? */
187 
188  if (NULL == address)
189  {
190  /* ATS service temporarily disconnected */
191  return;
192  }
193 
194  GNUNET_assert (NULL != db);
195  if (GNUNET_YES != address_active)
196  return;
197  latency = (int) prop->delay.rel_value_us;
198  entry = NULL;
200  &address->peer))
201  {
202  entry = GNUNET_CONTAINER_multipeermap_get (map, &address->peer);
203  GNUNET_assert (NULL != entry);
204  if (latency == entry->latency)
205  return;
206  }
207  if (NULL == stmt_insert)
208  {
209  if (SQLITE_OK != sqlite3_prepare_v2 (db, query_insert, -1, &stmt_insert,
210  NULL))
211  {
212  LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
213  goto err_shutdown;
214  }
215  }
216  if ((SQLITE_OK != sqlite3_bind_text (stmt_insert, 1,
217  GNUNET_i2s (&address->peer), -1,
218  SQLITE_STATIC)) ||
219  (SQLITE_OK != sqlite3_bind_int (stmt_insert, 2, latency)))
220  {
221  LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_text");
222  goto err_shutdown;
223  }
224  if (SQLITE_DONE != sqlite3_step (stmt_insert))
225  {
226  LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_step");
227  goto err_shutdown;
228  }
229  if (SQLITE_OK != sqlite3_reset (stmt_insert))
230  {
231  LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_insert");
232  goto err_shutdown;
233  }
234  if (NULL == entry)
235  {
236  entry = GNUNET_new (struct Entry);
237  entry->id = address->peer;
239  &entry->id, entry,
241  }
242  entry->latency = latency;
243  return;
244 
245 err_shutdown:
247 }
uint64_t rel_value_us
The actual value.
unsigned int latency
The last known value for latency.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define LOG_SQLITE(db, msg, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
struct GNUNET_PeerIdentity id
The peer&#39;s identity.
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
static struct sqlite3 * db
The SQLite database handle.
static struct sqlite3_stmt * stmt_insert
Prepared statement for inserting values into the database table.
struct GNUNET_TIME_Relative delay
Delay.
struct GNUNET_PeerIdentity peer
For which peer is this an address?
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL)...
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
Entry type to be used in the map to store old latency values.
Definition: gnunet-bcd.c:84
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Main function that will be run.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

Definition at line 259 of file gnunet-daemon-latency-logger.c.

References addr_info_cb(), db, DEBUG, do_shutdown(), GNUNET_ATS_performance_init(), GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONTAINER_multipeermap_create(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_OK, GNUNET_SCHEDULER_add_shutdown(), GNUNET_YES, LOG, and LOG_SQLITE.

Referenced by main().

261 {
262  const char *query_create =
263  "CREATE TABLE ats_info ("
264  "id TEXT,"
265  "val INTEGER,"
266  "timestamp NUMERIC"
267  ");";
268  char *dbfile;
269 
270  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "LATENCY-LOGGER",
271  "DBFILE",
272  &dbfile))
273  {
274  GNUNET_break (0);
275  return;
276  }
277  if (SQLITE_OK != sqlite3_open (dbfile, &db))
278  {
279  if (NULL != db)
280  {
281  LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
282  GNUNET_break (SQLITE_OK == sqlite3_close (db));
283  }
284  else
285  LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
286  GNUNET_free (dbfile);
287  return;
288  }
289  if (0 != sqlite3_exec (db, query_create, NULL, NULL, NULL))
290  DEBUG ("SQLite Error: %d. Perhaps the database `%s' already exits.\n",
291  sqlite3_errcode (db), dbfile);
292  DEBUG ("Opened database %s\n", dbfile);
293  GNUNET_free (dbfile);
294  dbfile = NULL;
298 }
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
#define LOG_SQLITE(db, msg, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
static void addr_info_cb(void *cls, const struct GNUNET_HELLO_Address *address, int address_active, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in, const struct GNUNET_ATS_Properties *prop)
Signature of a function that is called with QoS information about an address.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
static struct sqlite3 * db
The SQLite database handle.
static struct GNUNET_ATS_PerformanceHandle * ats
Handle to the ATS performance subsystem.
struct GNUNET_ATS_PerformanceHandle * GNUNET_ATS_performance_init(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_ATS_AddressInformationCallback addr_info_cb, void *addr_info_cb_cls)
Get handle to access performance API of the ATS subsystem.
static void do_shutdown(void *cls)
Shutdown.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
#define GNUNET_YES
Definition: gnunet_common.h:77
#define DEBUG(...)
Debug logging shorthand.
#define GNUNET_free(ptr)
Wrapper around free.
#define LOG(type,...)
Logging shorthand.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

Execution entry point.

Definition at line 305 of file gnunet-daemon-latency-logger.c.

References _, GNUNET_free, GNUNET_GETOPT_OPTION_END, GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), ret, and run().

306 {
307  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
309  };
310  int ret;
311 
312  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
313  return 2;
314  ret =
315  (GNUNET_OK ==
316  GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-latency-logger",
317  _ (
318  "Daemon to log latency values of connections to neighbours"),
319  options, &run, NULL)) ? 0 : 1;
320  GNUNET_free ((void*) argv);
321  return ret;
322 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1438
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:

Variable Documentation

◆ map

struct GNUNET_CONTAINER_MultiPeerMap* map
static

◆ db

struct sqlite3* db
static

The SQLite database handle.

Definition at line 87 of file gnunet-daemon-latency-logger.c.

Referenced by addr_info_cb(), do_shutdown(), and run().

◆ ats

struct GNUNET_ATS_PerformanceHandle* ats
static

Handle to the ATS performance subsystem.

Definition at line 92 of file gnunet-daemon-latency-logger.c.

◆ stmt_insert

struct sqlite3_stmt* stmt_insert
static

Prepared statement for inserting values into the database table.

Definition at line 97 of file gnunet-daemon-latency-logger.c.

Referenced by addr_info_cb(), and do_shutdown().