Re: Help needed in using 'on_dsm_detach' callback - Mailing list pgsql-hackers
From | Gaddam Sai Ram |
---|---|
Subject | Re: Help needed in using 'on_dsm_detach' callback |
Date | |
Msg-id | 1614bd76150.11e66eb0f1552.1150622736561833399@zohocorp.com Whole thread Raw |
In response to | Re: Help needed in using 'on_dsm_detach' callback (Thomas Munro <thomas.munro@enterprisedb.com>) |
List | 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:
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, &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->graphindex_dsa_handle = dsm_segment_handle(segment);
PROC_DSA_AREA = area;
ATTACH DSA:
old_context = MemoryContextSwitchTo(TopMemoryContext);
segment = dsm_attach(GraphIndexShmem->graphindex_dsa_handle);
area = dsa_attach_in_place(dsm_segment_address(segment), segment);
on_dsm_detach(segment, &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 <thomas.munro@enterprisedb.com> wrote ----
On Wed, Jan 24, 2018 at 8:37 PM, Gaddam Sai Ram<gaddamsairam.n@zohocorp.com> wrote:> Found that there is a callback for dsa detach but that function requires> segment pointer as an argument, Should be as below:>> on_dsm_detach(PROC_DSA_AREA->segment_maps[0].segment, detach_func);> ...> But i couldn't access that segment, as DSA_AREA struct is defined in dsa.c,> so I am unable to include.>> Any other ways to get dsa detach event, or to access DSA Segment pointer?There are two ways to create DSA areas: dsa_create() anddsa_create_in_place(). In all existing in-core uses of DSA areas,they are created "in place". That means that you have to supply theinitial DSM segment and then they live inside that, and they willquietly 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 rootDSM segment because you have your hands on it. Maybe you should dothat too? Basically just replace your current DSA creation, handlesharing and attaching code with the DSM equivalents (make it, say, 1MBin size), and then use dsa_{create,attach}_in_place() in the spaceinside it. The easiest way would be to give *all* the space in thisroot DSM segment using dsm_segment_address() to get the "place" to putthe DSA area, or you could use a shm_toc to put that + other fixedsize stuff inside it (the way execParallel.c does).It probably seems a bit weird and circular that DSA areas manage a setof DSM segments, but can themselves live in a user-supplied DSMsegment. DSM segments have two roles: they are shared memory, and inthis capacity a DSA area owns and uses them as raw storage, but theyare also a kind of general shared resource ownership/managementmechanism that comes with some free shared space, and in this capacitythey can be used to own a DSA area (or shared files, or interprocessqueues, or ..).Hope that helps.--Thomas Munro
pgsql-hackers by date: