dissecting memory sessions
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Nov 3 15:17:19 UTC 2022
On 03/11/2022 14:07, Radosław Smogura wrote:
>
> Hi Maurizo,
>
> Thank you for sharing this approach. I would have question related to
> closing arenas, and I was thinking about creating pooling arena which
> will return allocated segments back to the pool like in case of
> pooling allocator.
>
> Firstly, minor thing, Arena class is sealed so I can’t create wrapper
> around it?
>
I don't think Arena needs to be sealed, as there's nothing too magic
about it. Opening it up allows clients to decorate arenas easily. But
either way, it would be similarly easy for a client to define another
class which implements both SegmentAllocator and AutoCloseable, which
wraps an Arena.
>
> If memory session will not have close() method, I can close session
> only using Arena close() method.
>
Yes.
>
> However in case in pooling areana, I would have one “shared” arena –
> responsible for allocating native segments and it’s session will be
> long living session associated with pool.
>
> I would have to create number of child arenas. This would mean that
> PoolingArena has to track dependencies between arenas on their own,
> and preventing closing Pool when there’s at least single child arena
> alive.
>
I did some experiments with your pooled allocator - and there are two
possible approaches, both of which work:
* the pool can take an external session, and create new unsafe segments
that "alias" some memory in the pool, associated with the external
session. When creating the unsafe segments you would pass a cleanup
action to "return" the segments back to the pool.
* the pool can take no session and return a _new_ (confined) arena,
which is used by clients to allocate new segments using the pool - when
the arena is closed, all the memory is returned back to the pool.
In my experiments, the latter approach seems to give slightly better
performance - but that's mostly a choice for the pool API.
As in previous releases (e.g. Java 19), there is no explicit mechanism
to keep the pool alive when there's an allocator active - but this can
easily be worked around by having the pool memory be managed by an
implicit session.
When we'll get to nested session (either structured or unstructured)
there will be more ways for the pool to verify that the external
session/created arena feature the correct lifetime dependencies.
I hope this helps.
Maurizio
> Am I’m correct?
>
> Kind regards,
>
> Radoslaw Smogura
>
> *From: *Maurizio Cimadamore <mailto:maurizio.cimadamore at oracle.com>
> *Sent: *Wednesday, November 2, 2022 10:47 PM
> *To: *panama-dev at openjdk.java.net
> *Subject: *Re: dissecting memory sessions
>
> In case you are interested, below are some more pointers.
>
> First, the javadoc which contains the proposed FFM API changes:
>
> http://cr.openjdk.java.net/~mcimadamore/panama/session_handle_hidden_javadoc/javadoc/java.base/java/lang/foreign/package-summary.html
>
> For those of you who are more interested at the code, here is the branch
> that contains the code changes:
>
> https://github.com/mcimadamore/panama-foreign/tree/session_drop_min
> <https://urldefense.com/v3/__https://github.com/mcimadamore/panama-foreign/tree/session_drop_min__;!!ACWV5N9M2RV99hQ!IOtRU7vEmPqOt8IlQJvzJ7Boallwz_44pR4uQkJ-u5_wZG6TT999TwsvbndQKDkCTJvl7z2dScp3Y0u7FMxKIg$>
>
> And, finally, here is also a jextract branch that supports the proposed
> FFM API changes:
>
> https://github.com/mcimadamore/jextract/tree/panama_session_refresh
> <https://urldefense.com/v3/__https://github.com/mcimadamore/jextract/tree/panama_session_refresh__;!!ACWV5N9M2RV99hQ!IOtRU7vEmPqOt8IlQJvzJ7Boallwz_44pR4uQkJ-u5_wZG6TT999TwsvbndQKDkCTJvl7z2dScp3Y0uDZ9JQxw$>
>
> Cheers
> Maurizio
>
> On 02/11/2022 17:48, Maurizio Cimadamore wrote:
> > Hi,
> > After the first preview of the FFM API in Java 19, we have identified
> > a couple of areas where the API could use some improvements:
> >
> > * Clarify the relationship between MemorySegment and MemoryAddress
> > (this was addressed in [1]); and
> > * Polish the MemorySession API, and make segments easier to (safely)
> > share with external clients (what this email is about).
> >
> > While we have explored solutions to better encapsulate memory sessions
> > in the past (e.g. by dropping session accessors, as described in [2]),
> > nothing seemed to stick. So, for Java 19 we decided to leave session
> > accessors on memory segments in place, but give the option to
> > libraries to protect against "sneaky" close, by creating non-closeable
> > memory session views.
> >
> > After staring at this problem long enough, it became increasingly
> > clear that memory sessions, in their current shape and form, are
> > trying to do too much - from allocation, to lifecycle management and
> > more. The issue with "sneaky" close is mostly a manifestation of that
> > more fundamental problem. In that spirit, we have put together a
> > document which teases apart the various "traits" associated with
> > memory sessions, and repackages the same traits into an API that
> > provides better encapsulation and composition. The document can be
> > found here:
> >
> > http://cr.openjdk.java.net/~mcimadamore/panama/session_arenas.html
> >
> > The main move described in the document is to make MemorySession a
> > "pure" lifetime abstraction, thus dropping SegmentAllocator and
> > AutoCloseable capabilities. Instead, these capabilities are provided
> > by a *second* abstraction, called Arena. Crucially, an Arena _has_ a
> > memory session, which can e.g. be used to allocate segments that have
> > the same lifecycle as that of the arena. This subtle twist, gives us
> > an API that is easier to reason about (and to build upon), and one
> > where memory segments can be shared freely across clients - premature
> > calls to MemorySession::close are no longer possible. At the same
> > time, the API now makes a much clearer distinction between sessions
> > that are closeable (i.e. sessions created through an Arena) and those
> > that aren't (i.e. implicit and global sessions).
> >
> > Here's a list of the main API changes, and how they will impact
> > clients of the FFM API:
> >
> > * MemorySession no longer has a close() method; try-with-resources
> > against MemorySession will now need to use Arena instead;
> > * Support for non-closeable session views
> > (MemorySession::asNonCloseable), and related methods
> > (MemorySession::equals/hashCode) has been removed;
> > * MemorySession::addCloseAction has been removed; instead, clients can
> > specify a cleanup action when creating an unsafe segment (i.e. using
> > MemorySegment::ofAddress);
> > * Some of the predicates in MemorySession have been made more robust -
> > e.g. instead of MemorySession::ownerThread(), there is now a predicate
> > MemorySession::isOwnedBy(Thread).
> >
> > After careful consideration, we believe that the changes described in
> > this document are worth pursuing for the upcoming Java 20 integration
> > [3]: they make the API more principled (no more "sneaky" close), while
> > retaining a similar expressive power.
> >
> > Any feedback is greatly appreciated.
> >
> > Cheers
> > Maurizio
> >
> > [1] -
> > https://cr.openjdk.java.net/~mcimadamore/panama/segment_address.html
> > [2] -
> > https://mail.openjdk.org/pipermail/panama-dev/2022-February/016152.html
> > [3] -
> > https://mail.openjdk.org/pipermail/panama-dev/2022-February/016152.html
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20221103/e1edd9bd/attachment-0001.htm>
More information about the panama-dev
mailing list