Search code examples
c++performancec++11smp

How seastar invoke codes on different cpus?


I am currently working with seastart framework, and find it claims that seastart can submit task to different cpus.

Seastar claims that it shares nothing between different coers.

enter image description here

So I think seastar binds differnet threads to different cpus, and make different tasks submited to different background threads(called engine、container or something).

How seastar achieve this, use pthread_setaffinity_np ?

But before task submitted, codes still work on random thread? Just key resources like network socket or storages are distributed to different cpus by coding using smp::submit_to explicitly?


Solution

  • I've never heard of Seastar until now and the picture looks horribly inefficient. As is confirmed by benchmarks. It is way more efficient to have a single stack per thread, since the top of the stack is always "hot" (cached in L1).

    Anyway, yes, to pin a thread to a core you can use platform-specific API. In case of POSIX that would be pthread_setaffinity_np:

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(core, &cpuset); // core number starts from 0
    int rc = pthread_setaffinity_np(myThread.native_handle(), (cpu_set_t), &cpuset);
    

    Or SetThreadAffinityMask on Windows:

    DWORD_PTR mask = (DWORD_PTR) (1 << core); // core number starts from 0
    auto rc = SetThreadAffinityMask(GetCurrentThread(), mask);