Blame view

trunk/research/st/srs.c 12.1 KB
winlin authored
1
#include <unistd.h>
2
#include <stdlib.h>
winlin authored
3 4
#include <stdio.h>
winlin authored
5 6 7 8 9 10 11 12
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
winlin authored
13 14
#include "public.h"
winlin authored
15 16
#define srs_trace(msg, ...)   printf(msg, ##__VA_ARGS__);printf("\n")
winlin authored
17
int io_port = 1990;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
int sleep_ms = 100;

void stack_print(long int previous_sp, int level)
{
    if (level <= 0) {
        return;
    }
    
    register long int rsp asm("sp");
    char buf[level * 1024];
    
    stack_print(rsp, level - 1);
    
    srs_trace("%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)", 
        level, previous_sp, rsp, (int)(previous_sp - rsp), 
        (int)(previous_sp - rsp - sizeof(buf)), (int)(sizeof(buf) / 1024));
}

int huge_stack_test()
{
    srs_trace("===================================================");
    srs_trace("huge_stack test: start");
    
    register long int rsp asm("sp");
    stack_print(rsp, 10);
    
    srs_trace("huge_stack test: end");
    
    return 0;
}
49 50 51 52 53 54
int sleep_test()
{
    srs_trace("===================================================");
    srs_trace("sleep test: start");
    
    srs_trace("1. sleep...");
55
    st_utime_t start = st_utime();
56
    st_usleep(sleep_ms * 1000);
57
    st_utime_t end = st_utime();
58
    
59 60 61
    srs_trace("2. sleep ok, sleep=%dus, deviation=%dus", 
        (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000));
62 63 64 65 66
    srs_trace("sleep test: end");
    
    return 0;
}
winlin authored
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
void* sleep2_func0(void* arg)
{
    int sleep_ms = 100;
    st_utime_t start = st_utime();
    st_usleep(sleep_ms * 1000);
    st_utime_t end = st_utime();
    
    srs_trace("sleep ok, sleep=%dus, deviation=%dus", 
        (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000));
        
    return NULL;
}

void* sleep2_func1(void* arg)
{
    int sleep_ms = 250;
    st_utime_t start = st_utime();
    st_usleep(sleep_ms * 1000);
    st_utime_t end = st_utime();
    
    srs_trace("sleep ok, sleep=%dus, deviation=%dus", 
        (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000));
        
    return NULL;
}

int sleep2_test()
{
    srs_trace("===================================================");
    srs_trace("sleep2 test: start");
    
    st_thread_t trd0 = st_thread_create(sleep2_func0, NULL, 1, 0);
    st_thread_t trd1 = st_thread_create(sleep2_func1, NULL, 1, 0);
    st_thread_join(trd0, NULL);
    st_thread_join(trd1, NULL);

    srs_trace("sleep test: end");
104
    
winlin authored
105 106 107
    return 0;
}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
st_mutex_t sleep_work_cond = NULL;
void* sleep_deviation_func(void* arg)
{
    st_mutex_lock(sleep_work_cond);
    srs_trace("2. work thread start.");

    int64_t i;
    for (i = 0; i < 3000000000ULL; i++) {
    }
    
    st_mutex_unlock(sleep_work_cond);
    srs_trace("3. work thread end.");
    
    return NULL;
}

int sleep_deviation_test()
{
    srs_trace("===================================================");
    srs_trace("sleep deviation test: start");
    
    sleep_work_cond = st_mutex_new();
    
    st_thread_create(sleep_deviation_func, NULL, 0, 0);
    st_mutex_lock(sleep_work_cond);
    
    srs_trace("1. sleep...");
    st_utime_t start = st_utime();
    
    // other thread to do some complex work.
    st_mutex_unlock(sleep_work_cond);
    st_usleep(1000 * 1000);
    
    st_utime_t end = st_utime();
    
    srs_trace("4. sleep ok, sleep=%dus, deviation=%dus", 
        (int)(sleep_ms * 1000), (int)(end - start - sleep_ms * 1000));

    st_mutex_lock(sleep_work_cond);
    srs_trace("sleep deviation test: end");
    
    st_mutex_destroy(sleep_work_cond);
    
    return 0;
}
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
void* thread_func(void* arg)
{
    srs_trace("1. thread run");
    st_usleep(sleep_ms * 1000);
    srs_trace("2. thread completed");
    return NULL;
}

int thread_test()
{
    srs_trace("===================================================");
    srs_trace("thread test: start");
    
    st_thread_t trd = st_thread_create(thread_func, NULL, 1, 0);
    if (trd == NULL) {
        srs_trace("st_thread_create failed");
        return -1;
    }
    
    st_thread_join(trd, NULL);
    srs_trace("3. thread joined");
    
    srs_trace("thread test: end");
    
    return 0;
}
winlin authored
180
181 182 183
st_mutex_t sync_start = NULL;
st_cond_t sync_cond = NULL;
st_mutex_t sync_mutex = NULL;
184
st_cond_t sync_end = NULL;
185
186
void* sync_master(void* arg)
winlin authored
187
{
188 189 190
    // wait for main to sync_start this thread.
    st_mutex_lock(sync_start);
    st_mutex_unlock(sync_start);
191
    
192
    st_usleep(sleep_ms * 1000);
193
    st_cond_signal(sync_cond);
194
    
195
    st_mutex_lock(sync_mutex);
196
    srs_trace("2. st mutex is ok");
197
    st_mutex_unlock(sync_mutex);
198
    
199
    st_usleep(sleep_ms * 1000);
200
    srs_trace("3. st thread is ok");
201
    st_cond_signal(sync_cond);
202
    
winlin authored
203 204 205
    return NULL;
}
206
void* sync_slave(void* arg)
207 208
{
    // lock mutex to control thread.
209
    st_mutex_lock(sync_mutex);
210
    
211 212 213
    // wait for main to sync_start this thread.
    st_mutex_lock(sync_start);
    st_mutex_unlock(sync_start);
214 215
    
    // wait thread to ready.
216
    st_cond_wait(sync_cond);
217 218 219
    srs_trace("1. st cond is ok");
    
    // release mutex to control thread
220
    st_usleep(sleep_ms * 1000);
221
    st_mutex_unlock(sync_mutex);
222 223
    
    // wait thread to exit.
224
    st_cond_wait(sync_cond);
225 226
    srs_trace("4. st is ok");
    
227 228
    st_cond_signal(sync_end);
    
229 230 231
    return NULL;
}
232
int sync_test()
winlin authored
233
{
winlin authored
234
    srs_trace("===================================================");
235 236
    srs_trace("sync test: start");
    
237 238
    if ((sync_start = st_mutex_new()) == NULL) {
        srs_trace("st_mutex_new sync_start failed");
winlin authored
239 240
        return -1;
    }
241
    st_mutex_lock(sync_start);
242
243
    if ((sync_cond = st_cond_new()) == NULL) {
244
        srs_trace("st_cond_new cond failed");
245 246
        return -1;
    }
247 248 249 250 251

    if ((sync_end = st_cond_new()) == NULL) {
        srs_trace("st_cond_new end failed");
        return -1;
    }
252
    
253
    if ((sync_mutex = st_mutex_new()) == NULL) {
254
        srs_trace("st_mutex_new mutex failed");
255 256
        return -1;
    }
winlin authored
257
    
258
    if (!st_thread_create(sync_master, NULL, 0, 0)) {
winlin authored
259 260 261 262
        srs_trace("st_thread_create failed");
        return -1;
    }
    
263
    if (!st_thread_create(sync_slave, NULL, 0, 0)) {
264 265 266
        srs_trace("st_thread_create failed");
        return -1;
    }
267
    
268
    // run all threads.
269 270
    st_mutex_unlock(sync_start);
    
271 272 273
    st_cond_wait(sync_end);
    srs_trace("sync test: end");
    
274 275 276
    return 0;
}
winlin authored
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
void* io_client(void* arg)
{
    
    int fd;
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        srs_trace("create linux socket error.");
        return NULL;
    }
    srs_trace("6. client create linux socket success. fd=%d", fd);
    
    st_netfd_t stfd;
    if ((stfd = st_netfd_open_socket(fd)) == NULL){
        srs_trace("st_netfd_open_socket open socket failed.");
        return NULL;
    }
    srs_trace("7. client st open socket success. fd=%d", fd);
    
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(io_port);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in), ST_UTIME_NO_TIMEOUT) == -1) {
        srs_trace("bind socket error.");
        return NULL;
    }
    
    char buf[1024];
    if (st_read_fully(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
        srs_trace("st_read_fully failed");
        return NULL;
    }
    if (st_write(stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
        srs_trace("st_write failed");
        return NULL;
    }
    
313 314
    st_netfd_close(stfd);
    
winlin authored
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    return NULL;
}

int io_test()
{
    srs_trace("===================================================");
    srs_trace("io test: start, port=%d", io_port);
    
    int fd;
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        srs_trace("create linux socket error.");
        return -1;
    }
    srs_trace("1. server create linux socket success. fd=%d", fd);
    
    int reuse_socket = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_socket, sizeof(int)) == -1) {
        srs_trace("setsockopt reuse-addr error.");
        return -1;
    }
    srs_trace("2. server setsockopt reuse-addr success. fd=%d", fd);
    
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(io_port);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1) {
        srs_trace("bind socket error.");
        return -1;
    }
    srs_trace("3. server bind socket success. fd=%d", fd);
    
    if (listen(fd, 10) == -1) {
        srs_trace("listen socket error.");
        return -1;
    }
    srs_trace("4. server listen socket success. fd=%d", fd);
    
    st_netfd_t stfd;
    if ((stfd = st_netfd_open_socket(fd)) == NULL){
        srs_trace("st_netfd_open_socket open socket failed.");
        return -1;
    }
    srs_trace("5. server st open socket success. fd=%d", fd);
    
    if (!st_thread_create(io_client, NULL, 0, 0)) {
        srs_trace("st_thread_create failed");
        return -1;
    }
    
    st_netfd_t client_stfd = st_accept(stfd, NULL, NULL, ST_UTIME_NO_TIMEOUT);
    srs_trace("8. server get a client. fd=%d", st_netfd_fileno(client_stfd));
    
    char buf[1024];
    if (st_write(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
        srs_trace("st_write failed");
        return -1;
    }
    if (st_read_fully(client_stfd, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) != sizeof(buf)) {
        srs_trace("st_read_fully failed");
        return -1;
    }
    srs_trace("9. server io completed.");
    
379 380 381
    st_netfd_close(stfd);
    st_netfd_close(client_stfd);
    
winlin authored
382 383 384 385
    srs_trace("io test: end");
    return 0;
}
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
int pipe_test()
{
    srs_trace("===================================================");
    srs_trace("pipe test: start");
    
    int fds[2];
    if (pipe(fds) < 0) {
        srs_trace("pipe failed");
        return -1;
    }
    srs_trace("1. pipe ok, %d=>%d", fds[1], fds[0]);
    
    st_netfd_t fdw;
    if ((fdw = st_netfd_open_socket(fds[1])) == NULL) {
        srs_trace("st_netfd_open_socket open socket failed.");
        return -1;
    }
    srs_trace("2. open write fd ok");
    
    st_netfd_t fdr;
    if ((fdr = st_netfd_open_socket(fds[0])) == NULL) {
        srs_trace("st_netfd_open_socket open socket failed.");
        return -1;
    }
    srs_trace("3. open read fd ok");
    
    char buf[1024];
    if (st_write(fdw, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) < 0) {
        srs_trace("st_write socket failed.");
        return -1;
    }
    srs_trace("4. write to pipe ok");
    
    if (st_read(fdr, buf, sizeof(buf), ST_UTIME_NO_TIMEOUT) < 0) {
        srs_trace("st_read socket failed.");
        return -1;
    }
    srs_trace("5. read from pipe ok");
    
    st_netfd_close(fdw);
    st_netfd_close(fdr);
    
    srs_trace("pipe test: end");
    return 0;
}
432 433
int main(int argc, char** argv)
{
434 435
    srs_trace("ETIME=%d", ETIME);
    
436 437 438 439 440 441 442 443 444 445
    if (st_set_eventsys(ST_EVENTSYS_ALT) < 0) {
        srs_trace("st_set_eventsys failed");
        return -1;
    }
    
    if (st_init() < 0) {
        srs_trace("st_init failed");
        return -1;
    }
    
winlin authored
446 447
    if (sleep2_test() < 0) {
        srs_trace("sleep2_test failed");
448 449 450
        return -1;
    }
    
451 452 453 454 455
    if (sleep_test() < 0) {
        srs_trace("sleep_test failed");
        return -1;
    }
    
456 457 458 459 460
    if (sleep_deviation_test() < 0) {
        srs_trace("sleep_deviation_test failed");
        return -1;
    }
    
winlin authored
461 462 463 464 465
    if (huge_stack_test() < 0) {
        srs_trace("huge_stack_test failed");
        return -1;
    }
    
466 467 468 469 470
    if (thread_test() < 0) {
        srs_trace("thread_test failed");
        return -1;
    }
    
471 472 473 474
    if (sync_test() < 0) {
        srs_trace("sync_test failed");
        return -1;
    }
winlin authored
475
    
winlin authored
476 477 478 479 480
    if (io_test() < 0) {
        srs_trace("io_test failed");
        return -1;
    }
    
481 482 483 484 485
    if (pipe_test() < 0) {
        srs_trace("pipe_test failed");
        return -1;
    }
    
486
    // cleanup.
487
    srs_trace("wait for all thread completed");
488
    st_thread_exit(NULL);
489 490 491 492 493
    // the following never enter, 
    // the above code will exit when all thread exit,
    // current is a primordial st-thread, when all thread exit,
    // the st idle thread will exit(0), see _st_idle_thread_start()
    srs_trace("all thread completed");
494
    
winlin authored
495 496 497
    return 0;
}