Skip to content
Snippets Groups Projects
Commit 6737dc5d authored by clohr's avatar clohr
Browse files

Mesurer la bande passante consommée par xAAL...

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/C/trunk@1264 b32b6428-25c9-4566-ad07-03861ab6144f
parent 53874a25
No related branches found
No related tags found
No related merge requests found
netprobe.o
netprobe
PROG = netprobe
CFLAGS = -Wall -I. -g
LDFLAGS = -ljson-c -luuid -lm -L. -lxaal
SHELL = /bin/bash
all: $(PROG)
clean:
-rm -f *.o *~
proper: clean
-rm -f $(PROG)
test: $(PROG)
-LD_LIBRARY_PATH+=:. ./$(PROG) -a 224.0.29.200 -p 1234
Makefile.dep: $(PROG).c
$(CC) $(CFLAGS) -MM $^ > $@
include Makefile.dep
.svnignore:
echo $(PROG).o $(PROG) | tr ' ' '\012' > $@
svnignore: .svnignore
svn propset svn:ignore -F $< .
\ No newline at end of file
netprobe.o: netprobe.c xaal.h
# xAAL NetProbe
Measures bandwidth used by xAAL devices
Print a report every hours, and on SIGUSER1.
/* xAAL bus monitoring
* (c) 2015 Christophe Lohr <christophe.lohr@telecom-bretagne.eu>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <limits.h>
#include <float.h>
#include <sys/queue.h>
#include <json-c/json.h>
#include <xaal.h>
/*
* list of detected devices
*/
typedef LIST_HEAD(listhead, entry) seen_devs_t;
typedef struct entry {
char addr[37];
char *type;
unsigned long long bytes, msgs;
LIST_ENTRY(entry) entries;
} seen_dev_t;
seen_dev_t *append_dev(seen_devs_t *seen_devs, const char *addr, const char *devType) {
seen_dev_t *np = NULL;
LIST_FOREACH(np, seen_devs, entries)
if ( strncmp(np->addr, addr, 37) == 0 )
return np;
np = (seen_dev_t*) malloc( sizeof(seen_dev_t) );
strncpy(np->addr, addr, 36);
np->addr[36] = '\0';
np->type = strdup(devType);
np->bytes = 0;
np->msgs = 0;
LIST_INSERT_HEAD(seen_devs, np, entries);
return np;
}
/*
* global variables
* functions on global variables
*/
struct timeval start;
seen_devs_t *seen_devs = NULL;
unsigned long long bytes_count;
unsigned long long msgs_count;
void init_data() {
seen_dev_t *np;
bytes_count = 0;
msgs_count = 0;
if (seen_devs)
LIST_FOREACH(np, seen_devs, entries) {
LIST_REMOVE(np, entries);
free(np->type);
free(np);
}
else {
seen_devs = (seen_devs_t *) malloc( sizeof(seen_devs_t) );
LIST_INIT(seen_devs);
}
gettimeofday(&start, NULL);
printf("Initialization %s\n", ctime(&(start.tv_sec)));
}
void print_report(int s) {
seen_dev_t *np;
struct timeval now, elapsed;
unsigned long sec, min, hr, day, t;
double bw, bw_dev;
gettimeofday(&now, NULL);
printf("%s", ctime(&(now.tv_sec)));
timersub(&now, &start, &elapsed);
t = elapsed.tv_sec;
sec = t % 60;
t /= 60;
min = t % 60;
t /= 60;
hr = t % 24;
day = t / 24;
printf("uptime: %lu days %lu hours %lu minutes %lu seconds\n", day, hr, min, sec);
bw = (double)(bytes_count) / (double)(elapsed.tv_sec + elapsed.tv_usec*1E-6);
printf("bandwidth: %.3g kB/s (%llu messages)\n", bw/1024, msgs_count);
LIST_FOREACH(np, seen_devs, entries) {
bw_dev = (double)(np->bytes) / (double)(elapsed.tv_sec + elapsed.tv_usec*1E-6);
printf("%s %s bw:%.3g%% count:%.3g%%\n", np->addr, np->type, bw_dev/bw*100.0, (double)(np->msgs)/(double)(msgs_count)*100.0);
}
printf("\n");
switch (s) {
case SIGHUP:
case SIGINT:
exit(EXIT_SUCCESS);
case SIGALRM:
alarm(3600);
default:
signal(s, print_report);
}
}
int main(int argc, char **argv) {
xAAL_businfo_t bus;
int opt;
char *addr=NULL, *port=NULL;
int hops = -1;
bool arg_error = false;
struct json_object *jmsg, *jtargets;
const char *version, *source, *msgType, *devType, *action,
*cipher, *signature;
time_t timestamp;
seen_dev_t *seen_dev;
size_t sz;
/* Parse cmdline arguments */
while ((opt = getopt(argc, argv, "a:p:h:")) != -1) {
switch (opt) {
case 'a':
addr = optarg;
break;
case 'p':
port = optarg;
break;
case 'h':
hops = atoi(optarg);
break;
default: /* '?' */
arg_error = true;
}
}
if (optind < argc) {
fprintf(stderr, "Unknown argument %s\n", argv[optind]);
arg_error = true;
}
if (!addr || !port || arg_error) {
fprintf(stderr, "Usage: %s -a <addr> -p <port> [-h <hops>]\n",
argv[0]);
exit(EXIT_FAILURE);
}
/* Join the xAAL bus */
xAAL_error_log = stderr;
if ( !xAAL_join_bus(addr, port, hops, 1, &bus) )
exit(EXIT_FAILURE);
init_data();
signal(SIGHUP, print_report);
signal(SIGINT, print_report);
signal(SIGUSR1, print_report);
signal(SIGUSR2, print_report);
signal(SIGALRM, print_report);
alarm(3600);
for (;;) {
/* Recive a message */
if (!xAAL_read_bus(&bus, &jmsg, &version, &source, &jtargets, &msgType, &devType,
&action, &cipher, &signature, &timestamp))
continue;
sz = strlen(json_object_get_string(jmsg));
if (bytes_count > (ULLONG_MAX-sz)) {
init_data();
continue;
}
bytes_count += sz;
msgs_count++;
seen_dev = append_dev(seen_devs, source, devType);
seen_dev->bytes += sz;
seen_dev->msgs++;
print_report(SIGUSR1);
json_object_put(jmsg);
}
}
../libxaal/xaal.h
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment