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 739440 - GstGLMemory lazy malloc system memory
GstGLMemory lazy malloc system memory
Status: RESOLVED FIXED
Product: GStreamer
Classification: Platform
Component: gst-plugins-bad
git master
Other Linux
: Normal enhancement
: 1.7.1
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on:
Blocks:
 
 
Reported: 2014-10-31 08:47 UTC by comicfans44
Modified: 2016-09-22 08:08 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
misc patch to lazy malloc glmemory's system memory (2.83 KB, patch)
2014-10-31 08:47 UTC, comicfans44
needs-work Details | Review
misc patch to lazy malloc glmemory's system memory (4.73 KB, patch)
2014-11-07 03:17 UTC, comicfans44
none Details | Review
misc patch to lazy malloc glmemory's system memory (4.91 KB, patch)
2014-11-27 08:34 UTC, comicfans44
none Details | Review

Description comicfans44 2014-10-31 08:47:56 UTC
Created attachment 289714 [details] [review]
misc patch to lazy malloc glmemory's system memory

gst_gl_memory_alloc always allocate system memory by g_malloc,
but this memory is only used when map buffer for system memory read/write,
should this memory be lazy allocated only if in this case ?  attachment is a 
misc patch to do this, but out of memory can only be detected when map 
buffer for system read/write, not the allocate time.
Comment 1 Matthew Waters (ystreet00) 2014-11-06 05:13:59 UTC
Review of attachment 289714 [details] [review]:

Just on thread safety comment :)

Looks good otheriwise.

::: gst-libs/gst/gl/gstglmemory.c
@@ +661,3 @@
+
+    if (!gl_mem->data) {
+      gl_mem->data = g_malloc (gl_mem->mem.maxsize);

Nothing stops the map function being called concurrently so this needs to be protected by either a GOnce or a mutex to avoid accidentally double allocating.
Comment 2 comicfans44 2014-11-07 03:17:02 UTC
Created attachment 290131 [details] [review]
misc patch to lazy malloc glmemory's system memory

(In reply to comment #1)
> Review of attachment 289714 [details] [review]:
> 
> Just on thread safety comment :)
> 
> Nothing stops the map function being called concurrently so this needs to be
> protected by either a GOnce or a mutex to avoid accidentally double allocating.


use g_once to avoid double allocating, also lazy allocate memory 
in copy codepath (NEEDS_DOWNLOAD=true but not mapped for system R/W yet).
not familiar with g_once, please point out my problem if any :)
Comment 3 comicfans44 2014-11-27 08:34:26 UTC
Created attachment 291618 [details] [review]
misc patch to lazy malloc glmemory's system memory

I misunderstood g_once totally so previous patch is broken (map for system memory didn't work at all).
 this one use mutex instead and worked as I test. sorry for previous incorrect one.
Comment 4 Ilya Konstantinov 2015-04-27 23:46:18 UTC
The patch no longer applies due to changes from f2dc9b2b8f9b8b180f41e0ae88d52229653c21b4, b8f168cd65fbdc85199bfb4eca2afd1445215880 and others.

However, _gl_mem_alloc_data is still being performed in:
* _gl_mem_copy
* gst_gl_memory_wrapped_texture
* gst_gl_memory_alloc
and not lazily postponed to mem_map, so refreshing that patch still sounds like a good idea.
Comment 5 Nicolas Dufresne (ndufresne) 2015-04-28 11:22:45 UTC
One note, this is virtual memory, so the kernel already is doing lazy allocation, and we can't know if we are out of memory. I don't think this change will make any difference.
Comment 6 Ilya Konstantinov 2015-04-28 12:52:28 UTC
That's a good point. I suppose it won't make difference in physical memory usage.

If anything, we should care about the malloc time. In some pipelines, upload and download never happens so the malloc serves no purpose. Can't say I've done any profiling to prove the need for this optimization, though :)
Comment 7 Nicolas Dufresne (ndufresne) 2015-04-28 13:09:32 UTC
My comment is mostly for "but out of memory can only be detected when map" since we can't be sure if the memory is available or not. It can't hurt not doing a malloc needlessly. Though as said, I doubt the benchmarks will show anything significant.
Comment 8 comicfans44 2015-05-07 04:22:19 UTC
this patch is based on googleperftool malloc profile result ( not cpu performance benchmark), it can only reduce the malloc/free times and gives less total malloc size.  since I haven't read g_malloc/malloc/kernel detail ,I'm not sure the underlying difference (maybe it can reduce some book-keeping in multi-level allocator? ).
Comment 9 Matthew Waters (ystreet00) 2016-09-22 08:08:19 UTC
This seems to have been solved in the transition to glbasememory.

gst_gl_base_memory_alloc_data() (which controls the sysmem allocation) is only called when necessary from the memcpy implementation or from the cpu accesses of the dependant classes.

commit 27e724df8f0b7bb0d98a6c1589777633b3dadcba
Author: Matthew Waters <matthew@centricular.com>
Date:   Mon Dec 14 12:59:02 2015 +1100

    gl: add a base memory object
    
    It handles the following
    - GstAllocationParams -> gst_memory_init transformation
    - Makes sure that map/unmap/create/destroy happen on the GL thread with
      a GL context current.
    - Holds a possible sysmem accessible data pointer with alignment.
    - Holds the need upload/download transfer state