1 /*
2  * Copyright (c) 2016  Machine Zone, Inc.
3  *
4  * Original author: Lev Walkin <lwalkin@machinezone.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14 
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #ifndef TCPKALI_RUN_H
28 #define TCPKALI_RUN_H
29 
30 #include "tcpkali_common.h"
31 #include "tcpkali_mavg.h"
32 #include "tcpkali_atomic.h"
33 #include "tcpkali_engine.h"
34 #include "tcpkali_statsd.h"
35 #include "tcpkali_signals.h"
36 
37 enum work_phase { PHASE_ESTABLISHING_CONNECTIONS, PHASE_STEADY_STATE };
38 
39 struct rate_modulator {
40     enum {
41         RM_UNMODULATED, /* Do not modulate request rate */
42         RM_MAX_RATE_AT_TARGET_LATENCY
43     } mode;
44     enum { RMS_STATE_INITIAL, RMS_RATE_RAMP_UP, RMS_RATE_BINARY_SEARCH } state;
45     double last_update_long;
46     double last_update_short;
47     double latency_target; /* In seconds. */
48     char *latency_target_s;
49     int binary_search_steps;
50     /*
51      * Runtime parameters.
52      */
53     /* Previous latency value */
54     double prev_latency;
55     int prev_max_latency_exceeded;
56     /*
57      * Rate bounds for binary search.
58      */
59     double rate_min_bound;
60     double rate_max_bound;
61     double suggested_rate_value;
62 };
63 
64 enum oc_return_value {
65     OC_CONNECTED,
66     OC_TIMEOUT,
67     OC_INTERRUPT,
68     OC_RATE_GOAL_MET,
69     OC_RATE_GOAL_FAILED
70 };
71 
72 struct oc_args {
73     struct engine *eng;
74     int max_connections;
75     double connect_rate;
76     double epoch_end;
77     double latency_window;
78     volatile sig_atomic_t term_flag;
79     struct stats_checkpoint {
80         double epoch_start; /* Start of current checkpoint epoch */
81         double last_update; /* Last we updated the checkpoint structure */
82         double last_latency_window_flush;   /* Last time we flushed statsd latencies */
83         non_atomic_traffic_stats initial_traffic_stats; /* Ramp-up phase traffic */
84         non_atomic_traffic_stats last_traffic_stats;
85     } checkpoint;
86     struct latency_snapshot *previous_window_latency;
87     mavg traffic_mavgs[2];
88     mavg count_mavgs[2];    /* --message-marker */
89     size_t connections_opened_tally;
90     Statsd *statsd;
91     struct rate_modulator *rate_modulator;
92     struct percentile_values *latency_percentiles;
93     int print_stats;
94 };
95 
96 enum oc_return_value open_connections_until_maxed_out(enum work_phase phase,
97                                                       struct oc_args *);
98 
99 #endif /* TCPKALI_RUN_H */
100