00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00036 #ifndef _core_bsched_h_
00037 #define _core_bsched_h_
00038
00039 #include "sockets.h"
00040
00041 #include "lib/inputevt.h"
00042 #include "lib/tm.h"
00043 #include "if/core/hosts.h"
00044 #include "if/core/nodes.h"
00045 #include "if/core/bsched.h"
00046
00075 struct bsched {
00076 tm_t last_period;
00077 GList *sources;
00078 GSList *stealers;
00079 gchar *name;
00080 gint count;
00081 gint type;
00082 gint flags;
00083 gint period;
00084 gint min_period;
00085 gint max_period;
00086 gint period_ema;
00087 gint bw_per_second;
00088 gint bw_max;
00089 gint bw_actual;
00090 gint bw_last_period;
00091 gint bw_last_capped;
00092 gint bw_slot;
00093 gint bw_ema;
00094 gint bw_stolen;
00095 gint bw_stolen_ema;
00096 gint bw_delta;
00097 gint bw_unwritten;
00098 gint bw_capped;
00099 gint last_used;
00100 gint current_used;
00101 gboolean looped;
00102 };
00103
00104
00105
00106
00107
00108 #define BS_T_STREAM 1
00109 #define BS_T_RANDOM 2
00111
00112
00113
00114
00115 #define BS_F_ENABLED 0x00000001
00116 #define BS_F_READ 0x00000002
00117 #define BS_F_WRITE 0x00000004
00118 #define BS_F_NOBW 0x00000008
00119 #define BS_F_FROZEN_SLOT 0x00000010
00120 #define BS_F_CHANGED_BW 0x00000020
00121 #define BS_F_CLEARED 0x00000040
00122 #define BS_F_DATA_READ 0x00000080
00124 #define BS_F_RW (BS_F_READ|BS_F_WRITE)
00125
00126 #define bsched_bps(b) ((b)->bw_last_period * 1000 / (b)->period)
00127 #define bsched_pct(b) (bsched_bps(b) * 100 / (1+(b)->bw_per_second))
00128 #define bsched_avg_bps(b) ((b)->bw_ema * 1000 / (b)->period)
00129 #define bsched_avg_pct(b) (bsched_avg_bps(b) * 100 / (1+(b)->bw_per_second))
00130
00131 #define bsched_bwps(b) ((b)->bw_per_second)
00132 #define bsched_saturated(b) ((b)->bw_actual > (b)->bw_max)
00133
00134 #define bsched_enabled(b) ((b)->flags & BS_F_ENABLED)
00135
00140 struct bws_set {
00141 bsched_t *out;
00142 bsched_t *in;
00143 bsched_t *gout;
00144 bsched_t *gin;
00145 bsched_t *gout_udp;
00146 bsched_t *gin_udp;
00147 bsched_t *glout;
00148 bsched_t *glin;
00149 };
00150
00151 typedef struct sendfile_ctx {
00152 void *map;
00153 off_t map_start, map_end;
00154 } sendfile_ctx_t;
00155
00156
00157 struct iovec;
00158 extern struct bws_set bws;
00159
00160
00161
00162
00163
00164 bsched_t *bsched_make(gchar *name,
00165 gint type, guint32 mode, gint bandwidth, gint period);
00166 void bsched_init(void);
00167 void bsched_shutdown(void);
00168 void bsched_close(void);
00169 void bsched_set_peermode(node_peer_t mode);
00170 void bsched_enable(bsched_t *bs);
00171 void bsched_disable(bsched_t *bs);
00172 void bsched_enable_all(void);
00173 bio_source_t *bsched_source_add(bsched_t *bs, wrap_io_t *wio, guint32 flags,
00174 inputevt_handler_t callback, gpointer arg);
00175 void bsched_source_remove(bio_source_t *bio);
00176 void bsched_set_bandwidth(bsched_t *bs, gint bandwidth);
00177 bio_source_t *bsched_source_add(bsched_t *bs, wrap_io_t *wio, guint32 flags,
00178 inputevt_handler_t callback, gpointer arg);
00179 void bio_add_callback(bio_source_t *bio,
00180 inputevt_handler_t callback, gpointer arg);
00181 void bio_remove_callback(bio_source_t *bio);
00182 ssize_t bio_write(bio_source_t *bio, gconstpointer data, size_t len);
00183 ssize_t bio_writev(bio_source_t *bio, struct iovec *iov, gint iovcnt);
00184 ssize_t bio_sendto(bio_source_t *bio, gnet_host_t *to,
00185 gconstpointer data, size_t len);
00186 ssize_t bio_sendfile(sendfile_ctx_t *ctx, bio_source_t *bio, gint in_fd,
00187 off_t *offset, size_t len);
00188 ssize_t bio_read(bio_source_t *bio, gpointer data, size_t len);
00189 ssize_t bio_readv(bio_source_t *bio, struct iovec *iov, gint iovcnt);
00190 ssize_t bws_write(bsched_t *bs, wrap_io_t *wio, gconstpointer data, size_t len);
00191 ssize_t bws_read(bsched_t *bs, wrap_io_t *wio, gpointer data, size_t len);
00192 void bsched_timer(void);
00193
00194 void bws_sock_connect(enum socket_type type);
00195 void bws_sock_connected(enum socket_type type);
00196 void bws_sock_accepted(enum socket_type type);
00197 void bws_sock_connect_timeout(enum socket_type type);
00198 void bws_sock_connect_failed(enum socket_type type);
00199 void bws_sock_closed(enum socket_type type, gboolean remote);
00200 gboolean bws_can_connect(enum socket_type type);
00201
00202 void bws_udp_count_written(gint len);
00203 void bws_udp_count_read(gint len);
00204
00205 gboolean bsched_enough_up_bandwidth(void);
00206
00207 void bsched_config_steal_http_gnet(void);
00208 void bsched_config_steal_gnet(void);
00209
00210 #endif
00211
00212