* [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
@ 2025-01-15 22:52 stanhu (Stan Hu) via ruby-core
2025-03-13 9:49 ` [ruby-core:121324] " ko1 (Koichi Sasada) via ruby-core
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: stanhu (Stan Hu) via ruby-core @ 2025-01-15 22:52 UTC (permalink / raw)
To: ruby-core; +Cc: stanhu (Stan Hu)
Issue #21042 has been reported by stanhu (Stan Hu).
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042
* Author: stanhu (Stan Hu)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:121324] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
@ 2025-03-13 9:49 ` ko1 (Koichi Sasada) via ruby-core
2025-03-13 11:45 ` [ruby-core:121332] " matz (Yukihiro Matsumoto) via ruby-core
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2025-03-13 9:49 UTC (permalink / raw)
To: ruby-core; +Cc: ko1 (Koichi Sasada)
Issue #21042 has been updated by ko1 (Koichi Sasada).
Matz said he is positive on this proposal.
I have some considerations.
* Now current proposal doesn't increase much overhead. However I'm afraid that people want to introduce many measurements per threads, or per fibers, and it can be overhead (speed and memory).
* API
* It should be similar to `GC.stat`.
* Maybe it should be accessible from other threads?
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042#change-112290
* Author: stanhu (Stan Hu)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:121332] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
2025-03-13 9:49 ` [ruby-core:121324] " ko1 (Koichi Sasada) via ruby-core
@ 2025-03-13 11:45 ` matz (Yukihiro Matsumoto) via ruby-core
2025-03-13 14:40 ` [ruby-core:121349] " Eregon (Benoit Daloze) via ruby-core
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-03-13 11:45 UTC (permalink / raw)
To: ruby-core; +Cc: matz (Yukihiro Matsumoto)
Issue #21042 has been updated by matz (Yukihiro Matsumoto).
I am positive, but I also feel this API should be implementation dependent. Not all implementations could easily provide this information.
Matz.
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042#change-112302
* Author: stanhu (Stan Hu)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:121349] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
2025-03-13 9:49 ` [ruby-core:121324] " ko1 (Koichi Sasada) via ruby-core
2025-03-13 11:45 ` [ruby-core:121332] " matz (Yukihiro Matsumoto) via ruby-core
@ 2025-03-13 14:40 ` Eregon (Benoit Daloze) via ruby-core
2025-03-13 17:18 ` [ruby-core:121353] " byroot (Jean Boussier) via ruby-core
2025-05-08 9:07 ` [ruby-core:121903] [Ruby " ko1 (Koichi Sasada) via ruby-core
4 siblings, 0 replies; 6+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-03-13 14:40 UTC (permalink / raw)
To: ruby-core; +Cc: Eregon (Benoit Daloze)
Issue #21042 has been updated by Eregon (Benoit Daloze).
On JVM there is [`ThreadMXBean#getThreadAllocatedBytes`](https://docs.oracle.com/en/java/javase/11/docs/api/jdk.management/com/sun/management/ThreadMXBean.html#getThreadAllocatedBytes(long)).
There is no way on JVM to know the number of objects allocated, only bytes.
So this means TruffleRuby and JRuby could implement `Thread.current.total_allocated_bytes`, but not the other ones proposed in the description.
Maybe `total_malloc_bytes` should be renamed to `total_allocated_bytes` then, if that also count the bytes for Ruby object allocations.
If that's not the case, maybe CRuby should consider exposing `total_allocated_bytes` (counting native/malloc + Ruby object allocations in bytes).
BTW https://github.com/ruby/ruby/pull/3978#issuecomment-764731240 mentions
> This needs to be enabled with `Thread.trace_memory_allocations=true`
I think this is good to have. The JVM has the same concept with `setThreadAllocatedMemoryEnabled`.
Enabling automatically on the first call doesn't seem so nice, because it means no allocations tracked before that.
Also @ioquatix mentioned in https://github.com/ruby/ruby/pull/3978#issuecomment-2705075374 it would be nice to be per Fiber instead of per Thread.
On JRuby and TruffleRuby it would already be per Fiber, since each Fiber uses a java.lang.Thread (or VirtualThread < java.lang.Thread).
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042#change-112321
* Author: stanhu (Stan Hu)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:121353] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
` (2 preceding siblings ...)
2025-03-13 14:40 ` [ruby-core:121349] " Eregon (Benoit Daloze) via ruby-core
@ 2025-03-13 17:18 ` byroot (Jean Boussier) via ruby-core
2025-05-08 9:07 ` [ruby-core:121903] [Ruby " ko1 (Koichi Sasada) via ruby-core
4 siblings, 0 replies; 6+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2025-03-13 17:18 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #21042 has been updated by byroot (Jean Boussier).
> It should be similar to GC.stat.
> Maybe it should be accessible from other threads?
What about something like `GC.thread_stats(thread) # => {}`. This way it's understood that like `GC.stat`, the actual statistic are implementation dependent and can be there or not.
Now of course if it is decided that it isn't per thread but per fiber, it would need another name.
> However I'm afraid that people want to introduce many measurements per threads, or per fibers, and it can be overhead (speed and memory).
Indeed, if you wanted it on all 3 levels (Ractor/Thread/Fiber) this means 6 extra additions on every object allocation, might actually be significant in such an hot path.
Yet another option could be to have a way to turn this off? Supposedly with branch prediction, checking a boolean first should be very little overhead.
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042#change-112325
* Author: stanhu (Stan Hu)
* Status: Open
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
* [ruby-core:121903] [Ruby Feature#21042] Add and expose Thread#memory_allocations memory allocation counters
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
` (3 preceding siblings ...)
2025-03-13 17:18 ` [ruby-core:121353] " byroot (Jean Boussier) via ruby-core
@ 2025-05-08 9:07 ` ko1 (Koichi Sasada) via ruby-core
4 siblings, 0 replies; 6+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2025-05-08 9:07 UTC (permalink / raw)
To: ruby-core; +Cc: ko1 (Koichi Sasada)
Issue #21042 has been updated by ko1 (Koichi Sasada).
only 3 statistics are acceptable.
I don't want to increase more and more measurements from performance perspective.
----------------------------------------
Feature #21042: Add and expose Thread#memory_allocations memory allocation counters
https://bugs.ruby-lang.org/issues/21042#change-112972
* Author: stanhu (Stan Hu)
* Status: Assigned
* Assignee: ioquatix (Samuel Williams)
----------------------------------------
For the last 5 years, we've been patching our Ruby interpreter with https://github.com/ruby/ruby/pull/3978 in order to track memory allocations over time. This has been running in production at GitLab for a long time.
I'd like to request approval for this patch to land upstream since we're getting tired of maintaining this patch, and this data seems like it would be generally useful. If this can be done via a C extension, let me know, and I can look at that.
Copying from that pull request:
### Design
This is designed to measure a memory allocations in a multi-threaded environments (concurrent requests processing) with an accurate information about allocated memory within a given execution context.
The idea here is to provide as cheap as possible counter without an overhead of calling callbacks, and provide this information on a per-thread basis.
### Implementation
This adds `Thread.current.memory_allocations`, which provides information about:
* total_allocated_objects
* total_malloc_bytes
* total_mallocs
This is based on a expectation, that allocation for a given thread always happens
with a `rb_current_thread()` properly indicating a thread performing allocation.
This measures total number of allocations as counters.
Now, since the removal of objects is async and happening at random moment,
the same cannot be said for deallocations, thus only allocations are tracked.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-05-08 9:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-15 22:52 [ruby-core:120710] [Ruby master Feature#21042] Add and expose Thread#memory_allocations memory allocation counters stanhu (Stan Hu) via ruby-core
2025-03-13 9:49 ` [ruby-core:121324] " ko1 (Koichi Sasada) via ruby-core
2025-03-13 11:45 ` [ruby-core:121332] " matz (Yukihiro Matsumoto) via ruby-core
2025-03-13 14:40 ` [ruby-core:121349] " Eregon (Benoit Daloze) via ruby-core
2025-03-13 17:18 ` [ruby-core:121353] " byroot (Jean Boussier) via ruby-core
2025-05-08 9:07 ` [ruby-core:121903] [Ruby " ko1 (Koichi Sasada) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).