Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-per-core Architecture #382

Open
kosav7r opened this issue Feb 28, 2024 · 18 comments
Open

Thread-per-core Architecture #382

kosav7r opened this issue Feb 28, 2024 · 18 comments

Comments

@kosav7r
Copy link

kosav7r commented Feb 28, 2024

Hi folks,

I'm in the process of building a storage system and evaluating PhotonLib. Same architectural decisions on the system:

  1. Thread per core, no context switching is desired
  2. Share nothing; each thread will allocate resources including memory, network(sockets). The goal is to eliminate synchronization and improve cache efficiency, this is absolutely very important.
  3. Interrupt Affinity

Do you have any recommendations on thread-to-thread communication without sharing any memory?

Coroutine-based approach is new to me. Can I achieve my basic needs on PhotonLib? If so, are there any examples?

Thanks!

@kosav7r
Copy link
Author

kosav7r commented Mar 6, 2024

Kindly pinging :)

@beef9999
Copy link
Collaborator

beef9999 commented Mar 7, 2024

Yes, Photon coroutines can satisfy your requirements. Every resource is located in a single thread and shared among coroutines. You can read the documents for more details.

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 8, 2024

Linux thread is referred to as vCPU in Photon. Each of them has a dedicated scheduler for coroutines (Photon's threads), and a dedicated instance of event engine (e.g. epoll or io_uring). Their execution is basically independent of each other, unless you conduct inter-vCPU task coordination or migration.

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 8, 2024

You can realize interrupt affinity in the same way you do to other applications, e.g. pinning interrupt handler and corresponding photon vCPU (Linux thread) to the same physical CPU core.

@kosav7r
Copy link
Author

kosav7r commented Mar 8, 2024

What would you recommend to separate CPU and IO-bound jobs in the programming models? As far as I know, coroutines are for IO-bound jobs.

@beef9999
Copy link
Collaborator

beef9999 commented Mar 8, 2024

You can use the migrate API to move your CPU bound tasks to specific vCPU. It’s lightweight

@kosav7r
Copy link
Author

kosav7r commented Mar 10, 2024

What would you recommend for Interprocessor communication if shared memory is absolutely no?

@beef9999
Copy link
Collaborator

Is it a Photon related issue?

@kosav7r
Copy link
Author

kosav7r commented Mar 10, 2024

Already appreciate your answers so I apologize if it sounds unrelated. I am evaluating and comparing Photon with Seastar. Trying to map approaches in Seastar to Photon.

For example, In Seastar, it is mostly done by passing a lambda to a neighbor VCPU. I was wondering what do you think is a best approach to take as communication between VCPUs.

@beef9999
Copy link
Collaborator

beef9999 commented Mar 10, 2024

A Photon thread (coroutine) is essentially a function. Lambda is also the same.

The underlay implementation of thread migrate is eventfd notification and task queue.

Besides Photon also has a MPMC queue to transmit functions, encapsulated as the so-called WorkPool

@loongs-zhang
Copy link

loongs-zhang commented Mar 14, 2024

I've searched the code, how about use sched_setaffinity(linux)/thread_policy_set(macos) to bind vCPU to a single CPU core? @beef9999

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 15, 2024

@loongs-zhang Are you suggesting that we bind vCPU by default?

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 15, 2024

What would you recommend for Interprocessor communication if shared memory is absolutely no?

Multi-process without sharing memory? How about UNIX domain socket?

@loongs-zhang
Copy link

@loongs-zhang Are you suggesting that we bind vCPU by default?

yes

@loongs-zhang
Copy link

What would you recommend for Interprocessor communication if shared memory is absolutely no?
Multi-process without sharing memory? How about UNIX domain socket?

How about deep cloning and sharing?

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 15, 2024

@loongs-zhang Are you suggesting that we bind vCPU by default?

yes

As different apps require different binding configuration, it's difficult for us to do it by default.
For example, a typical scenario is file/storage server. We may need to consider IRQ handlers of the NICs and SSDs, and our service threads (vCPUs). The best binding configuration should minimize CPU switching along the execution.

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 15, 2024

How about deep cloning and sharing?

I not sure whether cloning is feasible, as it may imply sharing in the first place, and @kosav7r said it was "absolutely no".

@lihuiba
Copy link
Collaborator

lihuiba commented Mar 28, 2024

What would you recommend to separate CPU and IO-bound jobs in the programming models? As far as I know, coroutines are for IO-bound jobs.

Photon has a built-in WorkPool to deal with various kinds of background jobs. For IO-bound ones, you can initialize the worker vCPUs to enable coroutines and event engines. For CPU-bound ones, you can simple use kernel threads without initializing photon.

BTW, the jobs are efficiently passed to workers with lock-free shared memory ring queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants