gluon-radv-filterd: Use fscanf() instead of getline() & sscanf() for BATMAN data
This reduces the average CPU consumption (on a WDR4300) from 3% to 1%. Also, this commit adds error messages when the parsing fails and makes matching more flexible after all relevant fields have been found.
This commit is contained in:
parent
2f8eed6f9a
commit
bd85e99078
@ -352,12 +352,11 @@ static void update_tqs() {
|
|||||||
// translate all router's MAC addresses to originators simultaneously
|
// translate all router's MAC addresses to originators simultaneously
|
||||||
snprintf(path, PATH_MAX, TRANSTABLE_GLOBAL, G.mesh_iface);
|
snprintf(path, PATH_MAX, TRANSTABLE_GLOBAL, G.mesh_iface);
|
||||||
f = fopen(path, "r");
|
f = fopen(path, "r");
|
||||||
while (getline(&line, &len, f) != -1) {
|
// skip header
|
||||||
if (sscanf(line, " * " F_MAC " %*d (%*3u) via " F_MAC " (%*3u) (0x%*4x) [%*3c]",
|
while (fgetc(f) != '\n');
|
||||||
F_MAC_VAR(&mac_a), F_MAC_VAR(&mac_b)) != 12
|
while (fgetc(f) != '\n');
|
||||||
&& sscanf(line, " * " F_MAC " (%*3u) via " F_MAC " (%*3u) (0x%*4x) [%*3c]",
|
while (fscanf(f, " %*[*+] " F_MAC "%*[0-9 -] (%*3u) via " F_MAC " %*[^]]]\n",
|
||||||
F_MAC_VAR(&mac_a), F_MAC_VAR(&mac_b)) != 12)
|
F_MAC_VAR(&mac_a), F_MAC_VAR(&mac_b)) == 12) {
|
||||||
continue;
|
|
||||||
|
|
||||||
foreach(router, G.routers) {
|
foreach(router, G.routers) {
|
||||||
if (!memcmp(router->src, mac_a, sizeof(macaddr_t))) {
|
if (!memcmp(router->src, mac_a, sizeof(macaddr_t))) {
|
||||||
@ -367,6 +366,10 @@ static void update_tqs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!feof(f)) {
|
||||||
|
getline(&line, &len, f);
|
||||||
|
fprintf(stderr, "Parsing transtable_global aborted at this line: %s\n", line);
|
||||||
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,10 +377,11 @@ static void update_tqs() {
|
|||||||
G.max_tq = 0;
|
G.max_tq = 0;
|
||||||
snprintf(path, PATH_MAX, ORIGINATORS, G.mesh_iface);
|
snprintf(path, PATH_MAX, ORIGINATORS, G.mesh_iface);
|
||||||
f = fopen(path, "r");
|
f = fopen(path, "r");
|
||||||
while (getline(&line, &len, f) != -1) {
|
// skip header
|
||||||
if (sscanf(line, F_MAC " %*fs (%hhu) " F_MAC_IGN "[ %*s]: " F_MAC_IGN " (%*3u)",
|
while (fgetc(f) != '\n');
|
||||||
F_MAC_VAR(&mac_a), &tq) != 7)
|
while (fgetc(f) != '\n');
|
||||||
continue;
|
while (fscanf(f, F_MAC " %*fs (%hhu) " F_MAC_IGN " [ %*[^]]]: " F_MAC_IGN " (%*3u)\n",
|
||||||
|
F_MAC_VAR(&mac_a), &tq) == 7) {
|
||||||
|
|
||||||
foreach(router, G.routers) {
|
foreach(router, G.routers) {
|
||||||
if (!memcmp(router->originator, mac_a, sizeof(macaddr_t))) {
|
if (!memcmp(router->originator, mac_a, sizeof(macaddr_t))) {
|
||||||
@ -389,6 +393,10 @@ static void update_tqs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!feof(f)) {
|
||||||
|
getline(&line, &len, f);
|
||||||
|
fprintf(stderr, "Parsing originators aborted at this line: %s\n", line);
|
||||||
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
// if all routers have a TQ value, we don't need to check translocal
|
// if all routers have a TQ value, we don't need to check translocal
|
||||||
@ -400,10 +408,10 @@ static void update_tqs() {
|
|||||||
// rate local routers (if present) the highest
|
// rate local routers (if present) the highest
|
||||||
snprintf(path, PATH_MAX, TRANSTABLE_LOCAL, G.mesh_iface);
|
snprintf(path, PATH_MAX, TRANSTABLE_LOCAL, G.mesh_iface);
|
||||||
f = fopen(path, "r");
|
f = fopen(path, "r");
|
||||||
while (getline(&line, &len, f) != -1) {
|
// skip header
|
||||||
if (sscanf(line, " * " F_MAC "[%*5s] %*f", F_MAC_VAR(&mac_a)) != 6)
|
while (fgetc(f) != '\n');
|
||||||
continue;
|
while (fgetc(f) != '\n');
|
||||||
|
while (fscanf(f, " * " F_MAC " [%*5s] %*f", F_MAC_VAR(&mac_a)) == 6) {
|
||||||
foreach(router, G.routers) {
|
foreach(router, G.routers) {
|
||||||
if (!memcmp(router->src, mac_a, sizeof(macaddr_t))) {
|
if (!memcmp(router->src, mac_a, sizeof(macaddr_t))) {
|
||||||
DEBUG_MSG("Found router " F_MAC " in transtable_local, assigning TQ %d", F_MAC_VAR(router->src), LOCAL_TQ);
|
DEBUG_MSG("Found router " F_MAC " in transtable_local, assigning TQ %d", F_MAC_VAR(router->src), LOCAL_TQ);
|
||||||
@ -413,6 +421,10 @@ static void update_tqs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!feof(f)) {
|
||||||
|
getline(&line, &len, f);
|
||||||
|
fprintf(stderr, "Parsing transtable_local aborted at this line: %s\n", line);
|
||||||
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user