2019-07-28 10:31:56 +08:00
|
|
|
#include "lib_acl.h"
|
2018-11-30 14:38:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include "fiber/libfiber.h"
|
|
|
|
#include "stamp.h"
|
|
|
|
|
|
|
|
static int __max_loop = 1000;
|
|
|
|
static int __max_fiber = 1000;
|
|
|
|
static int __display = 0;
|
|
|
|
|
|
|
|
static __thread struct timeval __begin;
|
2022-03-30 19:11:25 +08:00
|
|
|
static __thread long long __count = 0;
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-04-01 17:37:58 +08:00
|
|
|
#define DUMMY_SIZE 512000
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
static void stack_dummy(ACL_FIBER *fiber acl_unused)
|
|
|
|
{
|
2022-04-01 17:37:58 +08:00
|
|
|
char buf[DUMMY_SIZE];
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2022-04-10 19:35:16 +08:00
|
|
|
printf("%s: called OK, dummy_size=%d\r\n", __FUNCTION__, DUMMY_SIZE);
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fiber_main(ACL_FIBER *fiber, void *ctx acl_unused)
|
|
|
|
{
|
|
|
|
int i;
|
2022-04-01 17:37:58 +08:00
|
|
|
size_t shared_stack_size = acl_fiber_get_shared_stack_size();
|
|
|
|
|
|
|
|
printf("\r\nshared_stack_size=%zd\r\n\r\n", shared_stack_size);
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-04-07 23:48:05 +08:00
|
|
|
if (acl_fiber_use_share_stack(fiber) && shared_stack_size > DUMMY_SIZE) {
|
2022-04-03 21:52:23 +08:00
|
|
|
stack_dummy(fiber);
|
2022-04-01 17:37:58 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
errno = acl_fiber_errno(fiber);
|
2022-03-30 19:11:25 +08:00
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
for (i = 0; i < __max_loop; i++) {
|
2022-03-30 19:11:25 +08:00
|
|
|
if (__count < 10) {
|
2022-04-01 17:37:58 +08:00
|
|
|
printf("fiber-%d, run, begin to yield\r\n",
|
|
|
|
acl_fiber_id(fiber));
|
2022-03-30 19:11:25 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
acl_fiber_yield();
|
|
|
|
|
2022-03-30 19:11:25 +08:00
|
|
|
if (__count++ < 10) {
|
2022-04-01 17:37:58 +08:00
|
|
|
printf("fiber-%d, wakeup errno: %d\r\n",
|
|
|
|
acl_fiber_id(fiber), errno);
|
|
|
|
printf("---------------------------------------\r\n");
|
2022-03-30 19:11:25 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:37:58 +08:00
|
|
|
printf("%s: fiber-%d exiting, count=%lld ...\r\n",
|
|
|
|
__FUNCTION__, acl_fiber_id(fiber), __count);
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
2022-04-07 23:48:05 +08:00
|
|
|
static void *thread_main(void *ctx)
|
2018-11-30 14:38:22 +08:00
|
|
|
{
|
2022-04-07 23:48:05 +08:00
|
|
|
ACL_FIBER_ATTR *attr = (ACL_FIBER_ATTR *) ctx;
|
2018-11-30 14:38:22 +08:00
|
|
|
int i;
|
|
|
|
|
2022-03-30 19:11:25 +08:00
|
|
|
for (i = 0; i < __max_fiber; i++) {
|
2022-04-07 23:48:05 +08:00
|
|
|
acl_fiber_create2(attr, fiber_main, NULL);
|
2022-03-30 19:11:25 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
acl_fiber_schedule();
|
|
|
|
|
|
|
|
printf("thread: %lu\r\n", (unsigned long) acl_pthread_self());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-03 21:52:23 +08:00
|
|
|
static void calc_speed(void)
|
|
|
|
{
|
|
|
|
long long count = __max_fiber * __max_loop;
|
|
|
|
struct timeval end;
|
|
|
|
double spent, speed;
|
|
|
|
|
|
|
|
gettimeofday(&end, NULL);
|
|
|
|
spent = stamp_sub(&end, &__begin);
|
|
|
|
speed = (count * 1000) / (spent > 0 ? spent : 1);
|
|
|
|
printf("\r\nfibers: %d, count: %lld, spent: %.2f, speed: %.2f\r\n",
|
|
|
|
__max_fiber, count, spent, speed);
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
static void usage(const char *procname)
|
|
|
|
{
|
|
|
|
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"
|
2022-04-07 23:48:05 +08:00
|
|
|
" -e [if display]\r\n"
|
|
|
|
" -S [if use shared stack]\r\n"
|
|
|
|
, procname);
|
2018-11-30 14:38:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ch, i, nthreads = 1;
|
|
|
|
acl_pthread_attr_t attr;
|
|
|
|
acl_pthread_t *tids;
|
2022-04-07 23:48:05 +08:00
|
|
|
ACL_FIBER_ATTR fiber_attr;
|
|
|
|
size_t stack_size = 64000;
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-04-07 23:48:05 +08:00
|
|
|
acl_fiber_attr_init(&fiber_attr);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hn:c:t:ed:S")) > 0) {
|
2018-11-30 14:38:22 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 'n':
|
|
|
|
__max_loop = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
__max_fiber = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
nthreads = atoi(optarg);
|
|
|
|
if (nthreads <= 0)
|
|
|
|
nthreads = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2022-04-07 23:48:05 +08:00
|
|
|
stack_size = (size_t) atoi(optarg);
|
|
|
|
acl_fiber_attr_setstacksize(&fiber_attr, stack_size);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
acl_fiber_attr_setsharestack(&fiber_attr, 1);
|
2018-11-30 14:38:22 +08:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
__display = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:06:54 +08:00
|
|
|
acl_fiber_set_shared_stack_size(8000000);
|
|
|
|
|
2018-11-30 14:38:22 +08:00
|
|
|
acl_pthread_attr_init(&attr);
|
|
|
|
tids = (acl_pthread_t *) acl_mycalloc(nthreads, sizeof(acl_pthread_t));
|
|
|
|
|
2022-04-03 21:52:23 +08:00
|
|
|
gettimeofday(&__begin, NULL);
|
|
|
|
|
2022-03-30 19:11:25 +08:00
|
|
|
for (i = 0; i < nthreads; i++) {
|
2022-04-07 23:48:05 +08:00
|
|
|
acl_pthread_create(&tids[i], &attr, thread_main, &fiber_attr);
|
2022-03-30 19:11:25 +08:00
|
|
|
}
|
2018-11-30 14:38:22 +08:00
|
|
|
|
2022-03-30 19:11:25 +08:00
|
|
|
for (i = 0; i < nthreads; i++) {
|
2018-11-30 14:38:22 +08:00
|
|
|
acl_pthread_join(tids[i], NULL);
|
2022-03-30 19:11:25 +08:00
|
|
|
}
|
|
|
|
|
2022-04-03 21:52:23 +08:00
|
|
|
calc_speed();
|
2022-03-30 19:11:25 +08:00
|
|
|
printf("All fiber threads exited now ...\r\n");
|
2018-11-30 14:38:22 +08:00
|
|
|
|
|
|
|
acl_myfree(tids);
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
printf("enter any key to exit ..."); fflush(stdout);
|
|
|
|
getchar();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|