Add stats serializer.

This commit is contained in:
Ian Gulliver
2016-02-17 08:30:32 +00:00
parent 0db4d15897
commit a0d94a4198
9 changed files with 73 additions and 8 deletions

View File

@@ -11,5 +11,5 @@ clean:
%.o: %.c *.h
$(CC) -c $(CFLAGS) $< -o $@
adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o common.o
$(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o common.o $(LIBS)
adsbus: adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o
$(CC) $(LDFLAGS) -o adsbus adsbus.o backend.o client.o airspy_adsb.o beast.o json.o stats.o common.o $(LIBS)

View File

@@ -11,6 +11,7 @@
#include "airspy_adsb.h"
#include "beast.h"
#include "json.h"
#include "stats.h"
static bool add_dump(char *format) {
@@ -108,6 +109,7 @@ int main(int argc, char *argv[]) {
airspy_adsb_init();
beast_init();
json_init();
stats_init();
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {

View File

@@ -7,6 +7,7 @@
#include <fcntl.h>
#include "json.h"
#include "stats.h"
#include "client.h"
struct client {
@@ -24,8 +25,11 @@ struct serializer {
{
.name = "json",
.serialize = json_serialize,
.client_head = NULL,
},
{
.name = "stats",
.serialize = stats_serialize,
}
};
#define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers))

View File

@@ -57,6 +57,12 @@ void buf_consume(struct buf *buf, size_t length) {
}
char *packet_type_names[] = {
"Mode-S short",
"Mode-S long",
};
uint64_t mlat_timestamp_scale_mhz_in(uint64_t timestamp, uint32_t mhz) {
return timestamp * (MLAT_MHZ / mhz);
}

View File

@@ -44,15 +44,16 @@ void buf_consume(struct buf *, size_t);
struct backend;
struct packet {
enum {
MODE_AC,
MODE_S_SHORT,
MODE_S_LONG,
NUM_TYPES,
} type;
uint8_t payload[DATA_LEN_MAX];
uint64_t mlat_timestamp;
uint32_t rssi;
struct backend *backend;
};
extern char *packet_type_names[];
//////// mlat

8
json.c
View File

@@ -14,7 +14,7 @@ void json_init() {
assert(JSON_INTEGER_IS_LONG_LONG);
}
static int json_buf_append_callback(const char *buffer, size_t size, void *data) {
int json_buf_append_callback(const char *buffer, size_t size, void *data) {
struct buf *buf = data;
if (size > BUF_LEN_MAX - buf->length - 1) {
return -1;
@@ -70,9 +70,6 @@ void json_serialize(struct packet *packet, struct buf *buf) {
}
switch (packet->type) {
case MODE_AC:
break;
case MODE_S_SHORT:
json_serialize_mode_s_short(packet, buf);
break;
@@ -80,5 +77,8 @@ void json_serialize(struct packet *packet, struct buf *buf) {
case MODE_S_LONG:
json_serialize_mode_s_long(packet, buf);
break;
case NUM_TYPES:
break;
}
}

2
json.h
View File

@@ -5,3 +5,5 @@
void json_init();
void json_serialize(struct packet *, struct buf *);
int json_buf_append_callback(const char *, size_t, void *);

43
stats.c Normal file
View File

@@ -0,0 +1,43 @@
#include <assert.h>
#include <time.h>
#include <jansson.h>
#include "common.h"
#include "backend.h"
#include "json.h"
#include "stats.h"
static struct stats_state {
uint64_t total_count;
uint64_t type_count[NUM_TYPES];
struct timespec start;
} stats_state = { 0 };
void stats_init() {
assert(clock_gettime(CLOCK_MONOTONIC, &stats_state.start) == 0);
}
void stats_serialize(struct packet *packet, struct buf *buf) {
if (packet) {
stats_state.total_count++;
stats_state.type_count[packet->type]++;
}
if (stats_state.total_count % 1000 != 0) {
return;
}
json_t *counts = json_object();
for (int i = 0; i < NUM_TYPES; i++) {
json_object_set_new(counts, packet_type_names[i], json_integer(stats_state.type_count[i]));
}
struct timespec now;
assert(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
json_t *out = json_pack("{sIso}",
"uptime_seconds", (json_int_t) (now.tv_sec - stats_state.start.tv_sec),
"packet_counts", counts);
assert(json_dump_callback(out, json_buf_append_callback, buf, 0) == 0);
json_decref(out);
buf_chr(buf, buf->length++) = '\n';
}

7
stats.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#include "common.h"
void stats_init();
void stats_serialize(struct packet *, struct buf *);