Re: [Mesa-dev] [PATCH 01/10] gallium/pb_cache: add a copy of cache bufmgr independent of pb_manager

2015-12-09 Thread Marek Olšák
On Dec 8, 2015 10:08 PM, "Nicolai Hähnle"  wrote:
>
> On 06.12.2015 19:00, Marek Olšák wrote:
>>
>> From: Marek Olšák 
>>
>> This simplified (basically duplicated) version of pb_cache_manager will
>> allow removing some ugly hacks from radeon and amdgpu winsyses and
>> flatten simplify their design.
>>
>> The difference is that winsyses must manually add buffers to the cache
>> in "destroy" functions and the cache doesn't know about the buffers
before
>> that. The integration is therefore trivial and the impact on the winsys
>> design is negligible.
>> ---
>>   src/gallium/auxiliary/Makefile.sources  |   1 +
>>   src/gallium/auxiliary/pipebuffer/pb_cache.c | 286

>>   src/gallium/auxiliary/pipebuffer/pb_cache.h |  74 +++
>>   3 files changed, 361 insertions(+)
>>   create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.c
>>   create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.h
>>
>> diff --git a/src/gallium/auxiliary/Makefile.sources
b/src/gallium/auxiliary/Makefile.sources
>> index 6160192..817308d 100644
>> --- a/src/gallium/auxiliary/Makefile.sources
>> +++ b/src/gallium/auxiliary/Makefile.sources
>> @@ -93,6 +93,7 @@ C_SOURCES := \
>> pipebuffer/pb_bufmgr_ondemand.c \
>> pipebuffer/pb_bufmgr_pool.c \
>> pipebuffer/pb_bufmgr_slab.c \
>> +   pipebuffer/pb_cache.c \
>
>
> I believe pb_cache.h needs to be added as well.
>
>
>> pipebuffer/pb_validate.c \
>> pipebuffer/pb_validate.h \
>> postprocess/filters.h \
>> diff --git a/src/gallium/auxiliary/pipebuffer/pb_cache.c
b/src/gallium/auxiliary/pipebuffer/pb_cache.c
>> new file mode 100644
>> index 000..45f600d
>> --- /dev/null
>> +++ b/src/gallium/auxiliary/pipebuffer/pb_cache.c
>
> ...
>
>> +/**
>> + * \return 1   if compatible and can be reclaimed
>> + * 0   if incompatible
>> + *-1   if compatible and can't be reclaimed
>> + */
>> +static int
>> +pb_cache_is_buffer_compat(struct pb_cache_entry *entry,
>> +  pb_size size, unsigned alignment, unsigned
usage)
>> +{
>> +   struct pb_buffer *buf = entry->buffer;
>> +
>> +   if (usage & entry->mgr->bypass_usage)
>> +  return 0;
>
>
> It should be possible to move this test to the top of
pb_cache_reclaim_buffer, right?

I don't know, maybe. I just copied the code as-is and I did notice the
bypass_usage documentation doesn't match the code very well. I think VMware
people added the flag, so I'll leave any possible cleanup to them to avoid
the risk of breaking their driver. The flag can also be moved to the caller
of pb_cache_reclaim_buffer.

Marek

>
>
>> +   if (buf->size < size)
>> +  return 0;
>> +
>> +   /* be lenient with size */
>> +   if (buf->size > (unsigned) (entry->mgr->size_factor * size))
>> +  return 0;
>> +
>> +   if (!pb_check_alignment(alignment, buf->alignment))
>> +  return 0;
>> +
>> +   if (!pb_check_usage(usage, buf->usage))
>> +  return 0;
>> +
>> +   return entry->mgr->can_reclaim(buf) ? 1 : -1;
>> +}
>> +
>> +/**
>> + * Find a compatible buffer in the cache, return it, and remove it
>> + * from the cache.
>> + */
>> +struct pb_buffer *
>> +pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
>> +unsigned alignment, unsigned usage)
>> +{
>> +   struct pb_cache_entry *entry;
>> +   struct pb_cache_entry *cur_entry;
>> +   struct list_head *cur, *next;
>> +   int64_t now;
>> +   int ret = 0;
>> +
>> +   pipe_mutex_lock(mgr->mutex);
>> +
>> +   entry = NULL;
>> +   cur = mgr->cache.next;
>> +   next = cur->next;
>> +
>> +   /* search in the expired buffers, freeing them in the process */
>> +   now = os_time_get();
>> +   while (cur != >cache) {
>> +  cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
>> +
>> +  if (!entry && (ret = pb_cache_is_buffer_compat(cur_entry, size,
>> + alignment, usage)
> 0))
>> + entry = cur_entry;
>> +  else if (os_time_timeout(cur_entry->start, cur_entry->end, now))
>> + destroy_buffer_locked(cur_entry);
>> +  else
>> + /* This buffer (and all hereafter) are still hot in cache */
>> + break;
>> +
>> +  /* the buffer is busy (and probably all remaining ones too) */
>> +  if (ret == -1)
>> + break;
>> +
>> +  cur = next;
>> +  next = cur->next;
>> +   }
>> +
>> +   /* keep searching in the hot buffers */
>> +   if (!entry && ret != -1) {
>> +  while (cur != >cache) {
>> + cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
>> + ret = pb_cache_is_buffer_compat(cur_entry, size, alignment,
usage);
>> +
>> + if (ret > 0) {
>> +entry = cur_entry;
>> +break;
>> + }
>> + if (ret == -1)
>> +break;
>> + /* no need to check the timeout here */
>> + cur = next;
>> + next = cur->next;
>> +  }
>> +  

Re: [Mesa-dev] [PATCH 01/10] gallium/pb_cache: add a copy of cache bufmgr independent of pb_manager

2015-12-08 Thread Nicolai Hähnle

On 06.12.2015 19:00, Marek Olšák wrote:

From: Marek Olšák 

This simplified (basically duplicated) version of pb_cache_manager will
allow removing some ugly hacks from radeon and amdgpu winsyses and
flatten simplify their design.

The difference is that winsyses must manually add buffers to the cache
in "destroy" functions and the cache doesn't know about the buffers before
that. The integration is therefore trivial and the impact on the winsys
design is negligible.
---
  src/gallium/auxiliary/Makefile.sources  |   1 +
  src/gallium/auxiliary/pipebuffer/pb_cache.c | 286 
  src/gallium/auxiliary/pipebuffer/pb_cache.h |  74 +++
  3 files changed, 361 insertions(+)
  create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.c
  create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.h

diff --git a/src/gallium/auxiliary/Makefile.sources 
b/src/gallium/auxiliary/Makefile.sources
index 6160192..817308d 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -93,6 +93,7 @@ C_SOURCES := \
pipebuffer/pb_bufmgr_ondemand.c \
pipebuffer/pb_bufmgr_pool.c \
pipebuffer/pb_bufmgr_slab.c \
+   pipebuffer/pb_cache.c \


I believe pb_cache.h needs to be added as well.


pipebuffer/pb_validate.c \
pipebuffer/pb_validate.h \
postprocess/filters.h \
diff --git a/src/gallium/auxiliary/pipebuffer/pb_cache.c 
b/src/gallium/auxiliary/pipebuffer/pb_cache.c
new file mode 100644
index 000..45f600d
--- /dev/null
+++ b/src/gallium/auxiliary/pipebuffer/pb_cache.c

...

+/**
+ * \return 1   if compatible and can be reclaimed
+ * 0   if incompatible
+ *-1   if compatible and can't be reclaimed
+ */
+static int
+pb_cache_is_buffer_compat(struct pb_cache_entry *entry,
+  pb_size size, unsigned alignment, unsigned usage)
+{
+   struct pb_buffer *buf = entry->buffer;
+
+   if (usage & entry->mgr->bypass_usage)
+  return 0;


It should be possible to move this test to the top of 
pb_cache_reclaim_buffer, right?



+   if (buf->size < size)
+  return 0;
+
+   /* be lenient with size */
+   if (buf->size > (unsigned) (entry->mgr->size_factor * size))
+  return 0;
+
+   if (!pb_check_alignment(alignment, buf->alignment))
+  return 0;
+
+   if (!pb_check_usage(usage, buf->usage))
+  return 0;
+
+   return entry->mgr->can_reclaim(buf) ? 1 : -1;
+}
+
+/**
+ * Find a compatible buffer in the cache, return it, and remove it
+ * from the cache.
+ */
+struct pb_buffer *
+pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
+unsigned alignment, unsigned usage)
+{
+   struct pb_cache_entry *entry;
+   struct pb_cache_entry *cur_entry;
+   struct list_head *cur, *next;
+   int64_t now;
+   int ret = 0;
+
+   pipe_mutex_lock(mgr->mutex);
+
+   entry = NULL;
+   cur = mgr->cache.next;
+   next = cur->next;
+
+   /* search in the expired buffers, freeing them in the process */
+   now = os_time_get();
+   while (cur != >cache) {
+  cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
+
+  if (!entry && (ret = pb_cache_is_buffer_compat(cur_entry, size,
+ alignment, usage) > 0))
+ entry = cur_entry;
+  else if (os_time_timeout(cur_entry->start, cur_entry->end, now))
+ destroy_buffer_locked(cur_entry);
+  else
+ /* This buffer (and all hereafter) are still hot in cache */
+ break;
+
+  /* the buffer is busy (and probably all remaining ones too) */
+  if (ret == -1)
+ break;
+
+  cur = next;
+  next = cur->next;
+   }
+
+   /* keep searching in the hot buffers */
+   if (!entry && ret != -1) {
+  while (cur != >cache) {
+ cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
+ ret = pb_cache_is_buffer_compat(cur_entry, size, alignment, usage);
+
+ if (ret > 0) {
+entry = cur_entry;
+break;
+ }
+ if (ret == -1)
+break;
+ /* no need to check the timeout here */
+ cur = next;
+ next = cur->next;
+  }
+   }
+
+   /* found a compatible buffer, return it */
+   if (entry) {
+  struct pb_buffer *buf = entry->buffer;
+
+  mgr->cache_size -= buf->size;
+  LIST_DEL(>head);
+  --mgr->num_buffers;
+  pipe_mutex_unlock(mgr->mutex);
+  /* Increase refcount */
+  pipe_reference_init(>reference, 1);
+  return buf;
+   }
+
+   pipe_mutex_unlock(mgr->mutex);
+   return NULL;
+}
+
+/**
+ * Empty the cache. Useful when there is not enough memory.
+ */
+void
+pb_cache_release_all_buffers(struct pb_cache *mgr)
+{
+   struct list_head *curr, *next;
+   struct pb_cache_entry *buf;
+
+   pipe_mutex_lock(mgr->mutex);
+   curr = mgr->cache.next;
+   next = curr->next;
+   while (curr != >cache) {
+  buf = LIST_ENTRY(struct pb_cache_entry, curr, head);
+  

[Mesa-dev] [PATCH 01/10] gallium/pb_cache: add a copy of cache bufmgr independent of pb_manager

2015-12-06 Thread Marek Olšák
From: Marek Olšák 

This simplified (basically duplicated) version of pb_cache_manager will
allow removing some ugly hacks from radeon and amdgpu winsyses and
flatten simplify their design.

The difference is that winsyses must manually add buffers to the cache
in "destroy" functions and the cache doesn't know about the buffers before
that. The integration is therefore trivial and the impact on the winsys
design is negligible.
---
 src/gallium/auxiliary/Makefile.sources  |   1 +
 src/gallium/auxiliary/pipebuffer/pb_cache.c | 286 
 src/gallium/auxiliary/pipebuffer/pb_cache.h |  74 +++
 3 files changed, 361 insertions(+)
 create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.c
 create mode 100644 src/gallium/auxiliary/pipebuffer/pb_cache.h

diff --git a/src/gallium/auxiliary/Makefile.sources 
b/src/gallium/auxiliary/Makefile.sources
index 6160192..817308d 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -93,6 +93,7 @@ C_SOURCES := \
pipebuffer/pb_bufmgr_ondemand.c \
pipebuffer/pb_bufmgr_pool.c \
pipebuffer/pb_bufmgr_slab.c \
+   pipebuffer/pb_cache.c \
pipebuffer/pb_validate.c \
pipebuffer/pb_validate.h \
postprocess/filters.h \
diff --git a/src/gallium/auxiliary/pipebuffer/pb_cache.c 
b/src/gallium/auxiliary/pipebuffer/pb_cache.c
new file mode 100644
index 000..45f600d
--- /dev/null
+++ b/src/gallium/auxiliary/pipebuffer/pb_cache.c
@@ -0,0 +1,286 @@
+/**
+ *
+ * Copyright 2007-2008 VMware, Inc.
+ * Copyright 2015 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **/
+
+#include "pb_cache.h"
+#include "util/u_memory.h"
+#include "util/u_time.h"
+
+
+/**
+ * Actually destroy the buffer.
+ */
+static void
+destroy_buffer_locked(struct pb_cache_entry *entry)
+{
+   struct pb_cache *mgr = entry->mgr;
+
+   assert(!pipe_is_referenced(>buffer->reference));
+   if (entry->head.next) {
+  LIST_DEL(>head);
+  assert(mgr->num_buffers);
+  --mgr->num_buffers;
+  mgr->cache_size -= entry->buffer->size;
+   }
+   entry->mgr->destroy_buffer(entry->buffer);
+}
+
+/**
+ * Free as many cache buffers from the list head as possible.
+ */
+static void
+release_expired_buffers_locked(struct pb_cache *mgr)
+{
+   struct list_head *curr, *next;
+   struct pb_cache_entry *entry;
+   int64_t now;
+
+   now = os_time_get();
+
+   curr = mgr->cache.next;
+   next = curr->next;
+   while (curr != >cache) {
+  entry = LIST_ENTRY(struct pb_cache_entry, curr, head);
+
+  if (!os_time_timeout(entry->start, entry->end, now))
+ break;
+
+  destroy_buffer_locked(entry);
+
+  curr = next;
+  next = curr->next;
+   }
+}
+
+/**
+ * Add a buffer to the cache. This is typically done when the buffer is
+ * being released.
+ */
+void
+pb_cache_add_buffer(struct pb_cache_entry *entry)
+{
+   struct pb_cache *mgr = entry->mgr;
+
+   pipe_mutex_lock(mgr->mutex);
+   assert(!pipe_is_referenced(>buffer->reference));
+
+   release_expired_buffers_locked(mgr);
+
+   /* Directly release any buffer that exceeds the limit. */
+   if (mgr->cache_size + entry->buffer->size > mgr->max_cache_size) {
+  entry->mgr->destroy_buffer(entry->buffer);
+  pipe_mutex_unlock(mgr->mutex);
+  return;
+   }
+
+   entry->start = os_time_get();
+   entry->end = entry->start + mgr->usecs;
+   LIST_ADDTAIL(>head, >cache);
+   ++mgr->num_buffers;
+   mgr->cache_size += entry->buffer->size;
+   pipe_mutex_unlock(mgr->mutex);
+}
+
+/**
+ * \return 1   if compatible and can be reclaimed
+ * 0   if incompatible
+ *-1   if compatible and can't be reclaimed
+ */
+static int