After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 458796 - MAP_SHARED for GMappedFile
MAP_SHARED for GMappedFile
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: Allison Karlitskaya (desrt)
gtkdev
: 631061 720184 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2007-07-20 21:46 UTC by Allison Karlitskaya (desrt)
Modified: 2018-05-24 11:04 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
MAP_SHARED, ref, unref (9.95 KB, patch)
2007-07-20 21:51 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
[PATCH] Bug 458796 – refcounting for GMappedFile (5.29 KB, patch)
2009-06-15 19:59 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
[PATCH] GMappedFile: add refcounting, switch to GSlice (5.07 KB, patch)
2009-06-15 23:56 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Allison Karlitskaya (desrt) 2007-07-20 21:46:26 UTC
.
Comment 1 Allison Karlitskaya (desrt) 2007-07-20 21:51:53 UTC
Created attachment 92068 [details] [review]
MAP_SHARED, ref, unref

also ref/unref and move to g_slice
Comment 2 Matthias Clasen 2007-07-22 00:09:12 UTC
Looks reasonable to me. Needs to wait for the new devel cycle to open though.
Also needs proper review.
Comment 3 Matthias Clasen 2007-11-08 14:47:58 UTC
Ryan said he doesn't need this anymore. 
So lets hold off on this until someone else needs shared mappings
Comment 4 Matthias Clasen 2007-11-08 14:48:57 UTC
Actually, lets leave it open as a dupe-trap
Comment 5 Behdad Esfahbod 2007-11-08 17:09:23 UTC
The ref/unref/gslice part is still useful.
Comment 6 Allison Karlitskaya (desrt) 2009-06-15 19:59:20 UTC
Created attachment 136659 [details] [review]
[PATCH] Bug 458796 – refcounting for GMappedFile

(In reply to comment #5)
> The ref/unref/gslice part is still useful.
> 

agreed.  i want to commit this part of it.
Comment 7 Matthias Clasen 2009-06-15 20:18:54 UTC
+ * Return value: a newly allocated #GMappedFile which must be unref'd
+ *    with g_mapped_file_unref(), or %NULL if the mapping failed. 

If you go so far to replace _free in doc references, should it be deprecated ?


 * Atomically decrements the reference count of @file by one.
+ * If the reference count drops to 0, unmaps the buffer of @file and
+ * frees it. This function is MT-safe and may be called from any thread.


I kinda dislike this style of documentation (even if it is just copied from ghash.c). "MT-safe" is not really a good term to use in the docs, I think.
And it always makes me quiver a little bit if ref/unref docs are written in terms of incrementing/decrementing a count, and the fact that that happens atomically is really just an implementation detail.


Do we have any users for this ?
Comment 8 Allison Karlitskaya (desrt) 2009-06-15 20:22:53 UTC
i want to use this.  it's why i was inspired :)

my argument against deprecation is that we didn't do it in the case of g_hash_table_destroy().  not a strong argument.  i'm happy either way.

i'll change the docs.
Comment 9 Matthias Clasen 2009-06-15 20:40:04 UTC
g_hash_table_destroy doesn't do the same thing as g_hash_table_unref...
Comment 10 Allison Karlitskaya (desrt) 2009-06-15 20:46:07 UTC
sometimes it does the same thing -- ie: for the last reference.

for the other cases you could use g_hash_table_remove_all() -- _destroy() could easily be deprecated.

in any case, i am sensing that you prefer deprecation here -- will post a new patch.  :)
Comment 11 Allison Karlitskaya (desrt) 2009-06-15 23:56:00 UTC
Created attachment 136676 [details] [review]
[PATCH] GMappedFile: add refcounting, switch to GSlice

sorry for the delay :)
Comment 12 Matthias Clasen 2009-06-18 03:01:53 UTC
Looks ok, but you took my complaint about the docs too literally; it is still worth pointing out that ref/unref are safe to be called from any thread without locking, unlike other mapped file functions.

If you add some blurb about thread-safety (without technobabble like "MT-safe"), its fine to commit. But I'd ask you to also clear GTK+ of the newly deprecated api after you did so, and bump the GLib dep in GTK+ configure.in.
Comment 13 Allison Karlitskaya (desrt) 2009-06-18 19:02:28 UTC
Committed with changes.  Leaving the bug report open for the same reason it was open before.
Comment 14 Allison Karlitskaya (desrt) 2010-10-01 14:13:01 UTC
*** Bug 631061 has been marked as a duplicate of this bug. ***
Comment 15 Anatol Pomozov 2010-10-01 21:34:15 UTC
The patch allows to use MAP_SHARED. It means that dirty pages will be eventually flushed back to file.

But the moment of flushing is not defined. As far as I understand it might happen right after change or at munmap. 

But sometimes you need to force flushing. What do you think about adding something like g_mapped_file_sync(GmappedFile *) that invokes msync or WinNT equivalent?

Or there is a better way to manage cross-platform memory mapped files?
Comment 16 Allison Karlitskaya (desrt) 2010-10-02 02:17:02 UTC
Files must be mapped MAP_SHARED or MAP_PRIVATE.

Your statements lead me to believe that you'd rather not use MAP_SHARED, which means you must want to use MAP_PRIVATE.

Calling msync() on MAP_PRIVATE does absolutely nothing, for the reason I mentioned in bug 631061.  See http://www.opengroup.org/onlinepubs/009695399/functions/msync.html:

"""
When the msync() function is called on MAP_PRIVATE mappings, any modified data shall not be written to the underlying object and shall not cause such data to be made visible to other processes.
"""

One thing that can be known, though, is that with any decent OS, using MAP_SHARED results in you sharing the copy of the page with that is in the page cache.  This means that all processes mapping the file are using the same physical memory and any changes made will be seen right away and (assuming you make appropriate arrangements) the writes will even be visible in exactly the order they occur.
Comment 17 Behdad Esfahbod 2010-10-03 01:21:17 UTC
(In reply to comment #16)

> One thing that can be known, though, is that with any decent OS, using
> MAP_SHARED results in you sharing the copy of the page with that is in the page
> cache.  This means that all processes mapping the file are using the same
> physical memory and any changes made will be seen right away and (assuming you
> make appropriate arrangements) the writes will even be visible in exactly the
> order they occur.

How about multi-core?  I think that's a huge lot of assumptions.

One difference between MAP_SHARED and MAP_PRIVATE, at least for writable mappings, is that a writable PRIVATE mapping allocates swap space to ensure that modifying the area never segfaults.  Whereas MAP_SHARED or MAP_PRIVATE|MAP_NORESERVE doesn't reserve swap.  So, for things that are not expected to be modified (icon cache, fonts, etc), writable MAP_PRIVATE is suboptimal.  Normally the answer is: don't make it writable then, however, since g_mapped_file doesn't have API to change from readonly to writable after creation, there are legitimate usecases where modification is the exception, but one has to create a writable mapping just in case.
Comment 18 Allison Karlitskaya (desrt) 2010-10-03 01:36:15 UTC
My comment about 'make appropriate arragements' was meant to address the issue of cross-processor cache consistency (use of barriers or sync primatives or what-have-you).

Also note that MAP_PRIVATE, in practice, tends -not- to allocate sufficient swap space due to the practice of memory over-commit by the kernel.
Comment 19 Philip Withnall 2018-02-16 13:23:29 UTC
*** Bug 720184 has been marked as a duplicate of this bug. ***
Comment 20 Philip Withnall 2018-02-16 13:24:06 UTC
Bug #720184 had some patches for this too; they have the advantage over these patches that they support creating a shared mapping from an existing open FD.
Comment 21 GNOME Infrastructure Team 2018-05-24 11:04:16 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/99.