From 7b7a5a079bc6dd618f02fba261135076413eae59 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 25 Feb 2020 17:00:12 +0100 Subject: [PATCH] Try to remove some loops --- src/algo_lightning.c | 16 +++------------- src/packet.c | 13 +++++++------ src/proxy.c | 12 +++++------- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/algo_lightning.c b/src/algo_lightning.c index bb5dfa1..8f4f0be 100644 --- a/src/algo_lightning.c +++ b/src/algo_lightning.c @@ -394,6 +394,7 @@ int send_message(struct evt_core_ctx* ctx, struct buffer_packet* bp) { struct algo_ctx* app_ctx = cat->app_ctx; struct light_ctx* lightc = app_ctx->misc; + if (lightc->selected_link >= lightc->total_links) { fprintf(stderr, "[algo_lightning] PACKET DROPPED! Selected link id %d is greater than the total number of links %d\n", lightc->selected_link, lightc->total_links); return 0; @@ -439,7 +440,9 @@ int send_message(struct evt_core_ctx* ctx, struct buffer_packet* bp) { dump_buffer_packet(bp_dup); fprintf(stderr, " [algo_lightning] Will send this info\n"); } + timing_fx_start (&lightc->tfx); main_on_tcp_write(ctx, to_fdinfo); + timing_fx_stop (&lightc->tfx, "write packet to tcp"); return 1; } @@ -515,17 +518,12 @@ 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); @@ -541,18 +539,11 @@ 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); - 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++) { @@ -594,7 +585,6 @@ int algo_lightning_on_datagram(struct evt_core_ctx* ctx, struct evt_core_fdinfo* 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/packet.c b/src/packet.c index 90f156a..68e9ee3 100644 --- a/src/packet.c +++ b/src/packet.c @@ -107,15 +107,16 @@ enum FD_STATE read_packet_from_tcp(struct evt_core_fdinfo* fdinfo, struct buffer enum FD_STATE write_packet_to_tcp(struct evt_core_fdinfo* fdinfo, struct buffer_packet* bp) { ssize_t nwrite; union abstract_packet* ap = (union abstract_packet*) &bp->ip; + size_t buffs = buffer_full_size(bp); //dump_buffer_packet (bp); if (bp->mode != BP_WRITING) return FDS_ERR; - while (bp->awrite < buffer_full_size(bp)) { - nwrite = send(fdinfo->fd, &(ap->raw) + bp->awrite, buffer_full_size(bp) - bp->awrite, 0); - if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN; - if (nwrite == -1) return FDS_ERR; - bp->awrite += nwrite; - } + nwrite = send(fdinfo->fd, &(ap->raw) + bp->awrite, buffs - bp->awrite, 0); + if (nwrite == -1 && errno == EAGAIN) return FDS_AGAIN; + if (nwrite == -1) return FDS_ERR; + bp->awrite += nwrite; + if (bp->awrite < buffs) return FDS_AGAIN; + bp->mode = BP_READING; bp->aread = 0; bp->ap_count = 0; diff --git a/src/proxy.c b/src/proxy.c index c569047..77ba61c 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -112,11 +112,9 @@ int main_on_tcp_write(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) if (app_ctx->ap.is_waiting_bootstrap && !app_ctx->is_rdy) goto free_buffer; // 2. Write data from the buffer to the socket - while (bp->mode == BP_WRITING) { - write_res = write_packet_to_tcp(fdinfo, bp); - if (write_res == FDS_ERR) goto co_error; - if (write_res == FDS_AGAIN) return 1; - } + write_res = write_packet_to_tcp(fdinfo, bp); + if (write_res == FDS_ERR) goto co_error; + if (write_res == FDS_AGAIN) return EVT_CORE_FD_EXHAUSTED; app_ctx->cell_sent++; @@ -126,12 +124,12 @@ free_buffer: mv_buffer_wtof(&app_ctx->br, fdinfo); notify_read(ctx, &app_ctx->br); - return 0; + return EVT_CORE_FD_UNFINISHED; co_error: perror("Failed to TCP write"); mv_buffer_wtof (&app_ctx->br, fdinfo); evt_core_rm_fd (ctx, fdinfo->fd); - return 1; + return EVT_CORE_FD_EXHAUSTED; } int main_on_udp_write (struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {