2016-06-11 23:18:06 +08:00
|
|
|
#include "lib_acl.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "fiber/lib_fiber.h"
|
2016-06-30 22:20:30 +08:00
|
|
|
#include "stamp.h"
|
2016-06-11 23:18:06 +08:00
|
|
|
|
|
|
|
static int __max_loop = 1000;
|
|
|
|
static int __max_fiber = 1000;
|
2016-08-11 13:36:20 +08:00
|
|
|
static int __display = 0;
|
2016-08-17 15:17:07 +08:00
|
|
|
static int __stack_size = 64000;
|
2016-06-11 23:18:06 +08:00
|
|
|
|
|
|
|
static __thread struct timeval __begin;
|
|
|
|
static __thread int __left_fiber = 1000;
|
|
|
|
|
2016-08-11 13:36:20 +08:00
|
|
|
static void stack_dummy(ACL_FIBER *fiber acl_unused)
|
|
|
|
{
|
|
|
|
char buf[81920];
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2016-07-05 15:19:05 +08:00
|
|
|
static void fiber_main(ACL_FIBER *fiber, void *ctx acl_unused)
|
2016-06-11 23:18:06 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2016-08-11 13:36:20 +08:00
|
|
|
if (0)
|
|
|
|
stack_dummy(fiber);
|
|
|
|
|
2016-07-05 15:19:05 +08:00
|
|
|
errno = acl_fiber_errno(fiber);
|
2016-06-23 13:34:54 +08:00
|
|
|
for (i = 0; i < __max_loop; i++) {
|
2016-07-05 15:19:05 +08:00
|
|
|
acl_fiber_yield();
|
2016-08-11 13:36:20 +08:00
|
|
|
if (!__display)
|
|
|
|
continue;
|
|
|
|
|
2016-06-23 13:34:54 +08:00
|
|
|
if (i <= 2)
|
2016-07-05 15:19:05 +08:00
|
|
|
printf("fiber-%d, errno: %d\r\n",
|
|
|
|
acl_fiber_id(fiber), errno);
|
2016-06-23 13:34:54 +08:00
|
|
|
}
|
2016-06-11 23:18:06 +08:00
|
|
|
|
|
|
|
if (--__left_fiber == 0) {
|
|
|
|
long long count = __max_fiber * __max_loop;
|
|
|
|
struct timeval end;
|
|
|
|
double spent;
|
|
|
|
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
spent = stamp_sub(&end, &__begin);
|
|
|
|
printf("fibers: %d, count: %lld, spent: %.2f, speed: %.2f\r\n",
|
|
|
|
__max_fiber, count, spent,
|
|
|
|
(count * 1000) / (spent > 0 ? spent : 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 16:05:55 +08:00
|
|
|
__thread int __nloop = 0;
|
|
|
|
static void loop_callback(void *ctx acl_unused)
|
|
|
|
{
|
|
|
|
if (++__nloop <= 10)
|
|
|
|
printf("loop_callback called\r\n");
|
|
|
|
}
|
|
|
|
|
2016-06-11 23:18:06 +08:00
|
|
|
static void *thread_main(void *ctx acl_unused)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
gettimeofday(&__begin, NULL);
|
|
|
|
__left_fiber = __max_fiber;
|
|
|
|
|
|
|
|
for (i = 0; i < __max_fiber; i++)
|
2016-08-17 15:17:07 +08:00
|
|
|
acl_fiber_create(fiber_main, NULL, __stack_size);
|
2016-06-11 23:18:06 +08:00
|
|
|
|
2016-09-28 16:05:55 +08:00
|
|
|
acl_fiber_post_event(loop_callback, NULL);
|
2016-07-05 15:19:05 +08:00
|
|
|
acl_fiber_schedule();
|
2016-06-11 23:18:06 +08:00
|
|
|
|
2016-09-28 16:05:55 +08:00
|
|
|
printf("thread: %lu, loop: %d\r\n",
|
|
|
|
(unsigned long) acl_pthread_self(), __nloop);
|
|
|
|
|
2016-06-11 23:18:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *procname)
|
|
|
|
{
|
2016-08-17 15:17:07 +08:00
|
|
|
printf("usage: %s -h [help]\r\n"
|
|
|
|
" -n max_loop\r\n"
|
|
|
|
" -c max_fiber\r\n"
|
|
|
|
" -t max_threads\r\n"
|
|
|
|
" -d stack_size\r\n"
|
|
|
|
" -e [if display]\r\n", procname);
|
2016-06-11 23:18:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch, i, nthreads = 1;
|
|
|
|
acl_pthread_attr_t attr;
|
|
|
|
acl_pthread_t *tids;
|
|
|
|
|
2016-08-17 15:17:07 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hn:c:t:d:e")) > 0) {
|
2016-06-11 23:18:06 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'n':
|
|
|
|
__max_loop = atoi(optarg);
|
|
|
|
break;
|
2016-07-18 22:07:31 +08:00
|
|
|
case 'c':
|
2016-06-11 23:18:06 +08:00
|
|
|
__max_fiber = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
nthreads = atoi(optarg);
|
|
|
|
if (nthreads <= 0)
|
|
|
|
nthreads = 1;
|
|
|
|
break;
|
2016-08-17 15:17:07 +08:00
|
|
|
case 'd':
|
|
|
|
__stack_size = atoi(optarg);
|
|
|
|
break;
|
2016-08-11 13:36:20 +08:00
|
|
|
case 'e':
|
|
|
|
__display = 1;
|
|
|
|
break;
|
2016-06-11 23:18:06 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl_pthread_attr_init(&attr);
|
|
|
|
tids = (acl_pthread_t *) acl_mycalloc(nthreads, sizeof(acl_pthread_t));
|
|
|
|
|
|
|
|
for (i = 0; i < nthreads; i++)
|
|
|
|
acl_pthread_create(&tids[i], &attr, thread_main, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < nthreads; i++)
|
|
|
|
acl_pthread_join(tids[i], NULL);
|
|
|
|
|
|
|
|
acl_myfree(tids);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|