Issue #18339 has been updated by byroot (Jean Boussier).


> to increase the number of threads when the elapsed time is short, and to decrease the number when the time is long. Is my understanding right?

That is correct.

Another potential use case is to simply instrument the number of waiting thread, less precise, but faster.

> I have no idea how to use RUBY_INTERNAL_EVENT_GVL_RELEASE.

I mostly suggest it for completness. I don't have an use case for it, but maybe you might want to instrument how long threads hold the GVL etc.

> I think the hooks are called without GVL

Yes, that is indeed a concern. I looked at the GC hooks and I was under the impression that it was already the case there. I might be wrong.

> Another idea is to provide a method Thread.gvl_waiting_thread_count. 

Yes, it would be less flexible and less precise but would help. I'd still prefer the C hooks though.

How I see it `gvl_waiting_thread_count` is useful for low overhead monitoring, it will tell you you might have a problem. Once you see there's one, you might want to temporarily enable actual time measurement to finely tune your service.

> require to saturate one process with fake workload / I couldn't understand this. Could you explain this?

Yes, to measure the effects of your number of threads, you must saturate your server. Meaning having all threads always working, otherwise your setting might look fine one day, but once you have a spike of traffic the performance tanks.

So usually you do this with some synthetic (fake) traffic.

The problem is that not all work loads are equal. Some endpoints will be CPU intensive, some other IO intensive, it's very hard to create synthetic traffic that perfectly reflect production. And even if you can, in a few weeks or months the pattern may have changed.

That is why I think constant monitoring of the situation in production is preferable to a "test bench" method using fake load. Does it make more sense?



----------------------------------------
Feature #18339: GVL instrumentation API
https://bugs.ruby-lang.org/issues/18339#change-94681

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
# GVL instrumentation API

### Context

One of the most common if not the most common way to deploy Ruby application these days is through threaded runners (typically `puma`, `sidekiq`, etc).

While threaded runners can offer more throughput per RAM than forking runners, they do require to carefully set the concurrency level (number of threads).
If you increase concurrency too much, you'll experience GVL contention and the latency will suffer.

The common way today is to start with a relatively low number of threads, and then increase it until CPU usage reach an acceptably high level (generally `~75%`).
But this method really isn't precise, require to saturate one process with fake workload, and doesn't tell how much threads are waiting on the GVLs, just how much the CPU is used.

Because of this, lots of threaded applications are not that well tuned, even more so because the ideal configuration is very dependant on the workload and can vary over time. So a decent setting might not be so good six months later.

Ideally, application owners should be able to continuously see the impact of the GVL contention on their latency metric, so they can more accurately decide what throughput vs latency tradeoff is best for them and regularly adjust it.

### Existing instrumentation methods

Currently, if you want to measure how much GVL contention is happening, you have to use some lower level tracing tools
such as `bpftrace`, or `dtrace`. These are quite advanced tools and require either `root` access or to compile Ruby with different configuration flags etc.

They're also external, so common Application Performance Monitoring (APM) tools can't really report it.

### Proposal

I'd like to have a C-level hook API around the GVL, with 3 events:

  - `RUBY_INTERNAL_EVENT_GVL_ACQUIRE_ENTER`
  - `RUBY_INTERNAL_EVENT_GVL_ACQUIRE_EXIT`
  - `RUBY_INTERNAL_EVENT_GVL_RELEASE`

Such API would allow to implement C extensions that collect various metrics about the GVL impact, such as median / p90 / p99 wait time, or even per thread total wait time.

Additionaly it would be very useful if the hook would pass some metadata, most importantly, the number of threads currently waiting.
People interested in a lower overhead monitoring method not calling `clock_gettime` could instrument that number of waiting thread instead. It would be less accurate, but enough to tell wether there might be problem.

With such metrics, application owners would be able to much more precisely tune their concurrency setting, and delibarately chose their own tradeoff between throughput and latency.




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>