From bc2923d2eb13bb4f4603b403bb82bbf5d2182c84 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 25 Feb 2020 16:08:42 +0100 Subject: [PATCH] Try an optimization --- CMakeLists.txt | 2 ++ src/algo_lightning.c | 33 +++++++++++++++++------- src/evt_core.c | 60 -------------------------------------------- src/evt_core.h | 1 + src/faketor.c | 14 +++++------ 5 files changed, 34 insertions(+), 76 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b1777bc..bcffc17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,8 @@ list(APPEND CSOURCES src/cap_utils.c src/measure.h src/measure.c + src/stopwatch.h + src/stopwatch.c src/algo_lightning.c ) diff --git a/src/algo_lightning.c b/src/algo_lightning.c index 6ce3c06..bb5dfa1 100644 --- a/src/algo_lightning.c +++ b/src/algo_lightning.c @@ -94,6 +94,7 @@ struct light_ctx { int disable_scheduler; struct stat_entry stats[MAX_LINKS]; enum schedule_group_target sched_strat; + struct timing_fx tfx; }; void algo_lightning_free(void* v) { @@ -121,6 +122,7 @@ void algo_lightning_init(struct evt_core_ctx* ctx, struct algo_ctx* app_ctx, str lightc->disable_scheduler = 0; lightc->active = 0; lightc->sched_strat = SCHEDULE_BOTH; + timing_fx_init (&lightc->tfx, TIMING_ACTIVATED | TIMING_DISPLAY_END, "", "[udp-read] fn=%s"); uint64_t window = 2000; if (ap->algo_specific_params != NULL) { @@ -513,12 +515,17 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* set_now(&now); // Pad packet + timing_fx_start (&lightc->tfx); algo_lightning_pad (ctx, fdinfo, bp); + timing_fx_stop (&lightc->tfx, "Pad UDP packets to form a Donar packet"); // Prepare links + timing_fx_start (&lightc->tfx); algo_lightning_update_stats(lightc, ctx); + timing_fx_stop (&lightc->tfx, "Compute stats from history (monitoring)"); // Adapt tags quantity to active links + timing_fx_start (&lightc->tfx); struct evt_core_cat *cat = evt_core_get_from_cat (ctx, "tcp-write"); int target_to_use = lightc->fast_count*2 < cat->socklist->len ? lightc->fast_count*2 : cat->socklist->len; int diff = target_to_use - ((int) lightc->active); @@ -534,11 +541,18 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* } lightc->active = target_to_use; + timing_fx_stop (&lightc->tfx, "Adapt the number of 1st class / 2nd class links"); // Update link tags + timing_fx_start (&lightc->tfx); algo_lightning_update_used(lightc, &now); - algo_lightning_link_cat(lightc, target_to_use/2); + timing_fx_stop (&lightc->tfx, "Swap used and unused links"); + timing_fx_start (&lightc->tfx); + algo_lightning_link_cat(lightc, target_to_use/2); + timing_fx_stop (&lightc->tfx, "Recompute 1st class and 2nd class"); + + timing_fx_start (&lightc->tfx); if (ctx->verbose > 1) { printf("link ranking (%d fast links, %d total links)\nposition | port | score | class \n", target_to_use/2, target_to_use); for (int i = 0; i < lightc->total_links; i++) { @@ -554,10 +568,10 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* sel_link_last = now; lightc->selected_link = UINT8_MAX; for (int i = 0; i < lightc->total_links; i++) { - if (lightc->status[lightc->stats[i].link_id].used != LINK_FAST) continue; - if (timespec_lt (&lightc->status[lightc->stats[i].link_id].last, &sel_link_last)) { - lightc->selected_link = lightc->stats[i].link_id; - sel_link_last = lightc->status[lightc->stats[i].link_id].last; + if (lightc->status[i].used != LINK_FAST) continue; + if (timespec_lt (&lightc->status[i].last, &sel_link_last)) { + lightc->selected_link = i; + sel_link_last = lightc->status[i].last; } } if (lightc->is_measlat) tag_packet_measlat (ap, lightc->selected_link, 0); @@ -570,16 +584,17 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* sel_link_last = now; lightc->selected_link = UINT8_MAX; for (int i = 0; i < lightc->total_links; i++) { - if (lightc->status[lightc->stats[i].link_id].used != LINK_SLOW) continue; - if (timespec_lt (&lightc->status[lightc->stats[i].link_id].last, &sel_link_last)) { - lightc->selected_link = lightc->stats[i].link_id; - sel_link_last = lightc->status[lightc->stats[i].link_id].last; + if (lightc->status[i].used != LINK_SLOW) continue; + if (timespec_lt (&lightc->status[i].last, &sel_link_last)) { + lightc->selected_link = i; + sel_link_last = lightc->status[i].last; } } if (lightc->is_measlat) tag_packet_measlat (ap, lightc->selected_link, 1); send_message (ctx, bp); if (lightc->csv) printf("%ld,%d,slow\n", now_timestamp, lightc->selected_link); } + timing_fx_stop (&lightc->tfx, "Link selection"); // Update our algo context lightc->sched_strat = schedule_group_target_trans[lightc->sched_strat]; diff --git a/src/evt_core.c b/src/evt_core.c index ceb963b..44c3271 100644 --- a/src/evt_core.c +++ b/src/evt_core.c @@ -193,66 +193,6 @@ void evt_core_free(struct evt_core_ctx* ctx) { g_hash_table_destroy (ctx->urltofd); } -enum timing_config { - TIMING_ACTIVATED = 1 << 0, - TIMING_DISPLAY_START = 1 << 1, - TIMING_DISPLAY_END = 1 << 2, - TIMING_DISPLAY_BOTH = 1 << 3 -}; - -struct timing_fx { - struct timespec start; - enum timing_config config; - uint8_t activated_start; - char start_template[255], end_template[255]; -}; - -void timing_fx_init(struct timing_fx* tfx, enum timing_config conf, char* startt, char* endt) { - tfx->config = conf; - strncpy (tfx->start_template, startt, sizeof(tfx->start_template) - 1); - strncpy (tfx->end_template, endt, sizeof(tfx->end_template) - 1); - tfx->start_template[sizeof(tfx->start_template) - 1] = 0; // Enforce null terminated string - tfx->end_template[sizeof(tfx->end_template) - 1] = 0; -} - -void timing_fx_start(struct timing_fx* tfx, ...) { - va_list args; - if (!(tfx->config & TIMING_ACTIVATED)) return; - if (tfx->config & (TIMING_DISPLAY_START | TIMING_DISPLAY_BOTH)) { - va_start(args, tfx); - vfprintf(stderr, tfx->start_template, args); - va_end(args); - } - - if (clock_gettime(CLOCK_MONOTONIC, &tfx->start) == -1) { - perror("clock_gettime"); - exit(EXIT_FAILURE); - } -} - -double timing_fx_stop(struct timing_fx* tfx, ...) { - va_list args; - struct timespec stop; - double elapsed_in_cb; - - if (!(tfx->config & TIMING_ACTIVATED)) return 0.; - - if (clock_gettime(CLOCK_MONOTONIC, &stop) == -1) { - perror("clock_gettime"); - exit(EXIT_FAILURE); - } - - elapsed_in_cb = (double)elapsed_micros (&tfx->start, &stop) / 1000000.; - - if (tfx->config & (TIMING_DISPLAY_END | TIMING_DISPLAY_BOTH)) { - va_start(args, tfx); - vfprintf(stderr, tfx->end_template, args); - fprintf(stderr, ": done in %f sec\n", elapsed_in_cb); - va_end(args); - } - return elapsed_in_cb; -} - void evt_core_loop(struct evt_core_ctx* ctx) { struct epoll_event current_event, events[EVT_CORE_MAX_EVENTS]; struct evt_core_fdinfo* fdinfo; diff --git a/src/evt_core.h b/src/evt_core.h index af4a9bd..c6d2438 100644 --- a/src/evt_core.h +++ b/src/evt_core.h @@ -9,6 +9,7 @@ #include #include "net_tools.h" #include "utils.h" +#include "stopwatch.h" #define EVT_CORE_MAX_EVENTS 10 diff --git a/src/faketor.c b/src/faketor.c index 27c57af..707aa06 100644 --- a/src/faketor.c +++ b/src/faketor.c @@ -17,7 +17,7 @@ int faketor_socks5_listen(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdin if (conn_sock1 == -1) goto co_error; make_socket_non_blocking (conn_sock1); - printf("[%s][torfake] Accepted a new connection for socks5 \n", current_human_datetime ()); + printf("[%s][faketor] Accepted a new connection for socks5 \n", current_human_datetime ()); socks5_server_handle_req (ctx, conn_sock1); return EVT_CORE_FD_UNFINISHED; @@ -38,7 +38,7 @@ int faketor_control_listen(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdi if (conn_sock == -1) goto co_error; make_socket_non_blocking (conn_sock); - printf("[%s][torfake] Accepted a new connection for control port \n", current_human_datetime ()); + printf("[%s][faketor] Accepted a new connection for control port \n", current_human_datetime ()); tor_ctl_server_handle(ctx, conn_sock); return EVT_CORE_FD_UNFINISHED; @@ -56,7 +56,7 @@ int faketor_socks5_server_success(struct evt_core_ctx* ctx, struct evt_core_fdin struct evt_core_fdinfo *fdinfo2 = evt_core_dup(ctx, fdinfo, buffer); evt_core_mv_fd2(ctx, fdinfo, "ctos-read"); evt_core_mv_fd2(ctx, fdinfo2, "stoc-write"); - printf("success socks5:\n\t+read: %s\n\t+write: %s\n", fdinfo->url, fdinfo2->url); + printf("[%s][faketor] success socks5:\n\t+read: %s\n\t+write: %s\n", current_human_datetime (), fdinfo->url, fdinfo2->url); return EVT_CORE_FD_EXHAUSTED; } @@ -68,7 +68,7 @@ int faketor_torctl_server_success(struct evt_core_ctx* ctx, struct evt_core_fdin struct evt_core_fdinfo *fdinfo2 = evt_core_dup(ctx, fdinfo, buffer); evt_core_mv_fd2(ctx, fdinfo, "stoc-read"); evt_core_mv_fd2(ctx, fdinfo2, "ctos-write"); - printf("success torctl:\n\t+read: %s\n\t+write: %s\n", fdinfo->url, fdinfo2->url); + printf("[%s][faketor] success torctl:\n\t+read: %s\n\t+write: %s\n", current_human_datetime (), fdinfo->url, fdinfo2->url); return EVT_CORE_FD_EXHAUSTED; } @@ -102,9 +102,9 @@ void get_fdinfo_couple(struct evt_core_ctx* ctx, struct evt_core_fdinfo *fdinfo, strcpy(url_build + strlen(url_build), ":write"); *destination = evt_core_get_from_url (ctx, url_build); } - if (*source == NULL || *destination == NULL) { + /*if (*source == NULL || *destination == NULL) { fprintf(stderr, "[%s][faketor][WARN] Called with %s, computed %s but is not in fdpool.\n", current_human_datetime (), fdinfo->url, url_build); - } + }*/ } int faketor_bridge(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { @@ -116,7 +116,7 @@ int faketor_bridge(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { // 0. find source get_fdinfo_couple (ctx, fdinfo, &source, &target); if (source == NULL || target == NULL) return EVT_CORE_FD_EXHAUSTED; - printf("triggered event: %s, source: %s, target: %s\n", fdinfo->url, source->url, target->url); + //printf("triggered event: %s, source: %s, target: %s\n", fdinfo->url, source->url, target->url); // 1. read some data nread = recv(source->fd, buffer, sizeof(buffer), MSG_PEEK);