libco
libco copied to clipboard
libco is a coroutine library which is widely used in wechat back-end service. It has been running on tens of thousands of machines since 2013.
现在libco 是一种类lua coroutine 的单线程模式 有些场景用多线程模型还是更方便,考虑支持类 Go/Erlang 的调度器执行模型: - 一个IO线程完成epoll_wait 事件轮训、定时器 - 一个线程池调度并行的执行 coroutine 这样引入一些带有block系统调用但耗时可控的第三方库也没啥问题
协程利用了cpu的的多个核心执行任务了吗?
leaq 8(%rsp), %rsp pushq 72(%rsi) movq 64(%rsi), %rsi ret 如果调入协程是新协程,上述代码会切换到新协程,并且使rsp指向ctx->ss_sp + ctx->ss_size,充分利用协程栈空间; 如果调入协程不是新协程,是个已经执行过的协程,本次只是重新调入而已,那么上述代码(leaq 8(%rsp), %rsp)是不是破坏了栈的完整性?是不是覆盖了rsp原有的数据?? 希望有空解释下
hi~ I found that in libgo, libcopp and other coroutine libraries, guard pages are used to detect stack overflow. I want to know whether libco's detection mechanism is the same...
archlinux x86_64 git latest commit dc6aafcc5e643d3b454a58acdc78e223634bbd1e ``` Scanning dependencies of target example_specific [ 23%] Building CXX object CMakeFiles/example_specific.dir/example_specific.cpp.o [ 26%] Linking CXX executable example_specific /usr/bin/ld: libcolib.a(co_hook_sys_call.cpp.o): in function `co_gethostbyname_r(char const*,...
这其实是很现实一点,举个最明显的例子,当使用多个共享栈的时候 ,co_routinue.cpp的save_stack_buffer中会多次对save_buffer进行拷贝,而这会调用大量的malloc/free。这里显然我们可以引入一个内存池(不是对象池),在我仿照libco实现的协程库中我引入了gperftools中的tcmalloc,在协程的创建和5000个以上的协程的频繁切换时均优于libco。当然直接使用tcmalloc会引入一个较重的框架,所以此时实现一个内存池就是一个较为正确的选择。
原实现: ``` if (update_occupy_co && update_pending_co && update_occupy_co != update_pending_co) { ... } ``` 当使用共享栈协程,且当有协程回收时,update_occupy_co有可能为NULL,此时进不了条件中执行协程栈恢复,建议改为: ``` if (update_pending_co && update_occupy_co != update_pending_co) { ... } ```
#64 这个 bug 2017 年就提了,困扰了好几天,建议确认修改下,谢谢!