| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "test_netemul.h" |
| 37 |
#include <math.h> |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; |
| 57 |
static pthread_cond_t cnd = PTHREAD_COND_INITIALIZER; |
| 58 |
static pthread_t thr = (pthread_t)NULL; |
| 59 |
|
| 60 |
|
| 61 |
static struct fd_list input = FD_LIST_INITIALIZER(input); |
| 62 |
static struct fd_list forlat = FD_LIST_INITIALIZER(forlat); |
| 63 |
static struct fd_list waitlist = FD_LIST_INITIALIZER(waitlist); |
| 64 |
|
| 65 |
struct process_item { |
| 66 |
struct fd_list chain; |
| 67 |
struct timespec ts; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static int do_duplicates() |
| 75 |
{ |
| 76 |
TRACE_ENTRY(""); |
| 77 |
|
| 78 |
while (!FD_IS_LIST_EMPTY(&input)) { |
| 79 |
struct msg * m; |
| 80 |
struct process_item * pi = (struct process_item *)(input.next); |
| 81 |
|
| 82 |
|
| 83 |
fd_list_unlink(&pi->chain); |
| 84 |
|
| 85 |
|
| 86 |
fd_list_insert_before(&forlat, &pi->chain); |
| 87 |
|
| 88 |
|
| 89 |
if (tne_conf.dupl_proba != 0.0) { |
| 90 |
DiamId_t src; |
| 91 |
size_t srclen; |
| 92 |
|
| 93 |
double my_rand = drand48(); |
| 94 |
m = pi->chain.o; |
| 95 |
CHECK_FCT( fd_msg_source_get(m, &src, &srclen) ); |
| 96 |
|
| 97 |
while (my_rand < (double) tne_conf.dupl_proba) { |
| 98 |
|
| 99 |
struct process_item * npi; |
| 100 |
struct msg * nm; |
| 101 |
struct msg_hdr * nh; |
| 102 |
unsigned char * buf; |
| 103 |
size_t len; |
| 104 |
|
| 105 |
|
| 106 |
CHECK_FCT( fd_msg_bufferize(m, &buf, &len) ); |
| 107 |
CHECK_FCT( fd_msg_parse_buffer(&buf, len, &nm) ); |
| 108 |
CHECK_FCT( fd_msg_source_set(nm, src, srclen) ); |
| 109 |
CHECK_FCT( fd_msg_hdr(nm, &nh) ); |
| 110 |
nh->msg_flags |= CMD_FLAG_RETRANSMIT; |
| 111 |
TRACE_DEBUG(FULL, "[tne] Duplicated message %p as %p", m, nm); |
| 112 |
|
| 113 |
|
| 114 |
CHECK_MALLOC( npi = malloc(sizeof(struct process_item)) ); |
| 115 |
memset(npi, 0, sizeof(struct process_item)); |
| 116 |
fd_list_init(&npi->chain, nm); |
| 117 |
memcpy(&npi->ts, &pi->ts, sizeof(struct timespec)); |
| 118 |
|
| 119 |
|
| 120 |
fd_list_insert_before(&forlat, &npi->chain); |
| 121 |
|
| 122 |
|
| 123 |
if (!my_rand) |
| 124 |
break; |
| 125 |
my_rand *= 10.0; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static double get_rand_norm() |
| 137 |
{ |
| 138 |
double ru1, ru2; |
| 139 |
double rsq; |
| 140 |
|
| 141 |
|
| 142 |
do { |
| 143 |
ru1 = 2.0 * drand48() - 1.0; |
| 144 |
ru2 = 2.0 * drand48() - 1.0; |
| 145 |
rsq = ru1 * ru1 + ru2 * ru2; |
| 146 |
} while ((rsq >= 1.0) || (rsq == 0.0)); |
| 147 |
|
| 148 |
|
| 149 |
return ru1 * sqrt( -2.0 * log(rsq) / rsq ); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static __inline__ unsigned long get_latency() |
| 154 |
{ |
| 155 |
unsigned long lat = tne_conf.lat_avg; |
| 156 |
|
| 157 |
if (tne_conf.lat_dev) { |
| 158 |
|
| 159 |
double rn; |
| 160 |
|
| 161 |
rn = get_rand_norm(); |
| 162 |
rn = rn * ((double)tne_conf.lat_dev) / 100.0; |
| 163 |
rn = exp(rn); |
| 164 |
|
| 165 |
lat = (unsigned long)(rn * (double)lat); |
| 166 |
} |
| 167 |
|
| 168 |
return lat; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
static int do_latency() |
| 173 |
{ |
| 174 |
TRACE_ENTRY(""); |
| 175 |
|
| 176 |
while (!FD_IS_LIST_EMPTY(&forlat)) { |
| 177 |
struct process_item * pi = (struct process_item *)(forlat.next); |
| 178 |
struct fd_list * li; |
| 179 |
|
| 180 |
|
| 181 |
fd_list_unlink(&pi->chain); |
| 182 |
|
| 183 |
|
| 184 |
if (tne_conf.lat_avg) { |
| 185 |
unsigned long l = get_latency(); |
| 186 |
TRACE_DEBUG(FULL, "[tne] Set %lu ms latency for %p", l, pi->chain.o); |
| 187 |
pi->ts.tv_sec += l / 1000; |
| 188 |
l %= 1000; |
| 189 |
pi->ts.tv_nsec += l * 1000000; |
| 190 |
if (pi->ts.tv_nsec >= 1000000000) { |
| 191 |
pi->ts.tv_sec += 1; |
| 192 |
pi->ts.tv_nsec -= 1000000000; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
for (li = waitlist.prev; li != &waitlist; li=li->prev) { |
| 197 |
struct process_item * p = (struct process_item *)li; |
| 198 |
if (TS_IS_INFERIOR( &p->ts, &pi->ts )) |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
fd_list_insert_after(li, &pi->chain); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
static int send_all_ready() |
| 212 |
{ |
| 213 |
struct timespec now; |
| 214 |
|
| 215 |
TRACE_ENTRY(""); |
| 216 |
|
| 217 |
CHECK_SYS( clock_gettime(CLOCK_REALTIME, &now) ); |
| 218 |
|
| 219 |
while (!FD_IS_LIST_EMPTY(&waitlist)) { |
| 220 |
struct msg * m; |
| 221 |
struct process_item * pi = (struct process_item *)(waitlist.next); |
| 222 |
|
| 223 |
if (!TS_IS_INFERIOR( &pi->ts, &now)) |
| 224 |
break; |
| 225 |
|
| 226 |
|
| 227 |
fd_list_unlink(&pi->chain); |
| 228 |
m = pi->chain.o; |
| 229 |
free(pi); |
| 230 |
|
| 231 |
TRACE_DEBUG(FULL, "[tne] Sending now %p", m); |
| 232 |
CHECK_FCT( fd_msg_send(&m, NULL, NULL) ); |
| 233 |
} |
| 234 |
|
| 235 |
return 0; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
static void * tne_process_th(void * arg) |
| 243 |
{ |
| 244 |
TRACE_ENTRY("%p", arg); |
| 245 |
|
| 246 |
|
| 247 |
fd_log_threadname ( "test_netemul/process" ); |
| 248 |
|
| 249 |
CHECK_POSIX_DO( pthread_mutex_lock(&mtx), goto error ); |
| 250 |
pthread_cleanup_push( fd_cleanup_mutex, &mtx ); |
| 251 |
|
| 252 |
|
| 253 |
while (1) { |
| 254 |
|
| 255 |
pthread_testcancel(); |
| 256 |
|
| 257 |
|
| 258 |
CHECK_FCT_DO( send_all_ready(), break ); |
| 259 |
|
| 260 |
|
| 261 |
CHECK_FCT_DO( do_duplicates(), break ); |
| 262 |
|
| 263 |
|
| 264 |
CHECK_FCT_DO( do_latency(), break ); |
| 265 |
|
| 266 |
|
| 267 |
if (FD_IS_LIST_EMPTY(&waitlist)) { |
| 268 |
CHECK_POSIX_DO( pthread_cond_wait(&cnd, &mtx), break ); |
| 269 |
} else { |
| 270 |
CHECK_POSIX_DO2( pthread_cond_timedwait(&cnd, &mtx, &((struct process_item *)(waitlist.next))->ts), |
| 271 |
ETIMEDOUT, , |
| 272 |
break ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
pthread_cleanup_pop( 0 ); |
| 279 |
CHECK_POSIX_DO( pthread_mutex_unlock(&mtx), ); |
| 280 |
error: |
| 281 |
TRACE_DEBUG(INFO, "A fatal error occurred in test_netemul/process thread!"); |
| 282 |
ASSERT(0); |
| 283 |
CHECK_FCT_DO(fd_core_shutdown(), ); |
| 284 |
return NULL; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
int tne_process_init() |
| 290 |
{ |
| 291 |
CHECK_POSIX( pthread_create(&thr, NULL, tne_process_th, NULL) ); |
| 292 |
|
| 293 |
#if 0 |
| 294 |
int i; |
| 295 |
for (i=0; i< 20; i++) { |
| 296 |
printf("LAT: %lu\n", get_latency()); |
| 297 |
} |
| 298 |
#endif |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
int tne_process_fini() |
| 305 |
{ |
| 306 |
CHECK_FCT( fd_thr_term(&thr) ); |
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
int tne_process_message(struct msg * msg) |
| 312 |
{ |
| 313 |
struct process_item * pi; |
| 314 |
|
| 315 |
TRACE_ENTRY("%p", msg); |
| 316 |
|
| 317 |
|
| 318 |
CHECK_MALLOC( pi = malloc(sizeof(struct process_item)) ); |
| 319 |
memset(pi, 0, sizeof(struct process_item)); |
| 320 |
fd_list_init(&pi->chain, msg); |
| 321 |
CHECK_SYS(clock_gettime(CLOCK_REALTIME, &pi->ts)); |
| 322 |
|
| 323 |
|
| 324 |
CHECK_POSIX( pthread_mutex_lock(&mtx) ); |
| 325 |
fd_list_insert_before(&input, &pi->chain); |
| 326 |
CHECK_POSIX( pthread_mutex_unlock(&mtx) ); |
| 327 |
|
| 328 |
|
| 329 |
CHECK_POSIX( pthread_cond_signal(&cnd) ); |
| 330 |
|
| 331 |
|
| 332 |
return 0; |
| 333 |
} |