Re: Help needed in using 'on_dsm_detach' callback

From: Gaddam Sai Ram <gaddamsairam(dot)n(at)zohocorp(dot)com>
To: "Thomas Munro" <thomas(dot)munro(at)enterprisedb(dot)com>
Cc: "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Help needed in using 'on_dsm_detach' callback
Date: 2018-01-31 10:51:01
Message-ID: 1614bd76150.11e66eb0f1552.1150622736561833399@zohocorp.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello Thomas,

Thank you for your suggestions, needless to say it helped a lot. As you suggested, I have created an initial dsm segment and used it create dsa area as well as to detach dsm. Thus, it helped me in using 'on_dsm_detach' callback. I have tested the code and it works well. I did post a code snippet, just verify if I have missed anything.

CREATE DSA:

old_context = MemoryContextSwitchTo(TopMemoryContext);

segment = dsm_create(DSA_INITIAL_SEGMENT_SIZE, 0); /* Initial segment size 1mb */

dsm_pin_segment(segment);

area = dsa_create_in_place(dsm_segment_address(segment), DSA_INITIAL_SEGMENT_SIZE, GraphIndex_tranche_id(), segment);

on_dsm_detach(segment, &amp;detach_func, PointerGetDatum(dsm_segment_address(segment)));

MemoryContextSwitchTo(old_context);

dsa_pin(area);

dsm_pin_mapping(segment);

dsa_pin_mapping(area);

/* Saving dsa_handle in shmem for serving other processes */

GraphIndexShmem-&gt;graphindex_dsa_handle = dsm_segment_handle(segment);

PROC_DSA_AREA = area;

ATTACH DSA:

old_context = MemoryContextSwitchTo(TopMemoryContext);

segment = dsm_attach(GraphIndexShmem-&gt;graphindex_dsa_handle);

area = dsa_attach_in_place(dsm_segment_address(segment), segment);

on_dsm_detach(segment, &amp;detach_func, PointerGetDatum(dsm_segment_address(segment)));

MemoryContextSwitchTo(old_context);

dsm_pin_mapping(segment);

dsa_pin_mapping(area);

PROC_DSA_AREA = area;

Thank you,
G. Sai Ram

---- On Wed, 24 Jan 2018 13:53:52 +0530 Thomas Munro &lt;thomas(dot)munro(at)enterprisedb(dot)com&gt; wrote ----

On Wed, Jan 24, 2018 at 8:37 PM, Gaddam Sai Ram

&lt;gaddamsairam(dot)n(at)zohocorp(dot)com&gt; wrote:

&gt; Found that there is a callback for dsa detach but that function requires

&gt; segment pointer as an argument, Should be as below:

&gt;

&gt; on_dsm_detach(PROC_DSA_AREA-&gt;segment_maps[0].segment, detach_func);

&gt; ...

&gt; But i couldn't access that segment, as DSA_AREA struct is defined in dsa.c,

&gt; so I am unable to include.

&gt;

&gt; Any other ways to get dsa detach event, or to access DSA Segment pointer?

There are two ways to create DSA areas: dsa_create() and

dsa_create_in_place(). In all existing in-core uses of DSA areas,

they are created "in place". That means that you have to supply the

initial DSM segment and then they live inside that, and they will

quietly create more DSM segments as needed if they run out of space.

If you do it that way you get to install detach hooks for that root

DSM segment because you have your hands on it. Maybe you should do

that too? Basically just replace your current DSA creation, handle

sharing and attaching code with the DSM equivalents (make it, say, 1MB

in size), and then use dsa_{create,attach}_in_place() in the space

inside it. The easiest way would be to give *all* the space in this

root DSM segment using dsm_segment_address() to get the "place" to put

the DSA area, or you could use a shm_toc to put that + other fixed

size stuff inside it (the way execParallel.c does).

It probably seems a bit weird and circular that DSA areas manage a set

of DSM segments, but can themselves live in a user-supplied DSM

segment. DSM segments have two roles: they are shared memory, and in

this capacity a DSA area owns and uses them as raw storage, but they

are also a kind of general shared resource ownership/management

mechanism that comes with some free shared space, and in this capacity

they can be used to own a DSA area (or shared files, or interprocess

queues, or ..).

Hope that helps.

--

Thomas Munro

http://www.enterprisedb.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2018-01-31 10:52:28 Re: WINDOW RANGE patch versus leakproofness
Previous Message Arthur Zakirov 2018-01-31 10:36:22 Re: Transform for pl/perl