pb_bufmgr_cache.c revision bf21b7006c63c3dc47045c22d4f372dfe6c7ce67
1/**************************************************************************
2 *
3 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * \file
30 * Buffer cache.
31 *
32 * \author Jose Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
34 */
35
36
37#include "pipe/p_compiler.h"
38#include "util/u_debug.h"
39#include "os/os_thread.h"
40#include "util/u_memory.h"
41#include "util/u_double_list.h"
42#include "util/u_time.h"
43
44#include "pb_buffer.h"
45#include "pb_bufmgr.h"
46
47
48/**
49 * Convenience macro (type safe).
50 */
51#define SUPER(__derived) (&(__derived)->base)
52
53
54struct pb_cache_manager;
55
56
57/**
58 * Wrapper around a pipe buffer which adds delayed destruction.
59 */
60struct pb_cache_buffer
61{
62   struct pb_buffer base;
63
64   struct pb_buffer *buffer;
65   struct pb_cache_manager *mgr;
66
67   /** Caching time interval */
68   int64_t start, end;
69
70   struct list_head head;
71};
72
73
74struct pb_cache_manager
75{
76   struct pb_manager base;
77
78   struct pb_manager *provider;
79   unsigned usecs;
80
81   pipe_mutex mutex;
82
83   struct list_head delayed;
84   pb_size numDelayed;
85};
86
87
88static INLINE struct pb_cache_buffer *
89pb_cache_buffer(struct pb_buffer *buf)
90{
91   assert(buf);
92   return (struct pb_cache_buffer *)buf;
93}
94
95
96static INLINE struct pb_cache_manager *
97pb_cache_manager(struct pb_manager *mgr)
98{
99   assert(mgr);
100   return (struct pb_cache_manager *)mgr;
101}
102
103
104/**
105 * Actually destroy the buffer.
106 */
107static INLINE void
108_pb_cache_buffer_destroy(struct pb_cache_buffer *buf)
109{
110   struct pb_cache_manager *mgr = buf->mgr;
111
112   LIST_DEL(&buf->head);
113   assert(mgr->numDelayed);
114   --mgr->numDelayed;
115   assert(!pipe_is_referenced(&buf->base.base.reference));
116   pb_reference(&buf->buffer, NULL);
117   FREE(buf);
118}
119
120
121/**
122 * Free as many cache buffers from the list head as possible.
123 */
124static void
125_pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr)
126{
127   struct list_head *curr, *next;
128   struct pb_cache_buffer *buf;
129   int64_t now;
130
131   now = os_time_get();
132
133   curr = mgr->delayed.next;
134   next = curr->next;
135   while(curr != &mgr->delayed) {
136      buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
137
138      if(!os_time_timeout(buf->start, buf->end, now))
139	 break;
140
141      _pb_cache_buffer_destroy(buf);
142
143      curr = next;
144      next = curr->next;
145   }
146}
147
148
149static void
150pb_cache_buffer_destroy(struct pb_buffer *_buf)
151{
152   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
153   struct pb_cache_manager *mgr = buf->mgr;
154
155   pipe_mutex_lock(mgr->mutex);
156   assert(!pipe_is_referenced(&buf->base.base.reference));
157
158   _pb_cache_buffer_list_check_free(mgr);
159
160   buf->start = os_time_get();
161   buf->end = buf->start + mgr->usecs;
162   LIST_ADDTAIL(&buf->head, &mgr->delayed);
163   ++mgr->numDelayed;
164   pipe_mutex_unlock(mgr->mutex);
165}
166
167
168static void *
169pb_cache_buffer_map(struct pb_buffer *_buf,
170		    unsigned flags, void *flush_ctx)
171{
172   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
173   return pb_map(buf->buffer, flags, flush_ctx);
174}
175
176
177static void
178pb_cache_buffer_unmap(struct pb_buffer *_buf)
179{
180   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
181   pb_unmap(buf->buffer);
182}
183
184
185static enum pipe_error
186pb_cache_buffer_validate(struct pb_buffer *_buf,
187                         struct pb_validate *vl,
188                         unsigned flags)
189{
190   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
191   return pb_validate(buf->buffer, vl, flags);
192}
193
194
195static void
196pb_cache_buffer_fence(struct pb_buffer *_buf,
197                      struct pipe_fence_handle *fence)
198{
199   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
200   pb_fence(buf->buffer, fence);
201}
202
203
204static void
205pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
206                              struct pb_buffer **base_buf,
207                              pb_size *offset)
208{
209   struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
210   pb_get_base_buffer(buf->buffer, base_buf, offset);
211}
212
213
214const struct pb_vtbl
215pb_cache_buffer_vtbl = {
216      pb_cache_buffer_destroy,
217      pb_cache_buffer_map,
218      pb_cache_buffer_unmap,
219      pb_cache_buffer_validate,
220      pb_cache_buffer_fence,
221      pb_cache_buffer_get_base_buffer
222};
223
224
225static INLINE int
226pb_cache_is_buffer_compat(struct pb_cache_buffer *buf,
227                          pb_size size,
228                          const struct pb_desc *desc)
229{
230   void *map;
231
232   if(buf->base.base.size < size)
233      return 0;
234
235   /* be lenient with size */
236   if(buf->base.base.size >= 2*size)
237      return 0;
238
239   if(!pb_check_alignment(desc->alignment, buf->base.base.alignment))
240      return 0;
241
242   if(!pb_check_usage(desc->usage, buf->base.base.usage))
243      return 0;
244
245   map = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL);
246   if (!map) {
247      return -1;
248   }
249
250   pb_unmap(buf->buffer);
251
252   return 1;
253}
254
255
256static struct pb_buffer *
257pb_cache_manager_create_buffer(struct pb_manager *_mgr,
258                               pb_size size,
259                               const struct pb_desc *desc)
260{
261   struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
262   struct pb_cache_buffer *buf;
263   struct pb_cache_buffer *curr_buf;
264   struct list_head *curr, *next;
265   int64_t now;
266   int ret = 0;
267
268   pipe_mutex_lock(mgr->mutex);
269
270   buf = NULL;
271   curr = mgr->delayed.next;
272   next = curr->next;
273
274   /* search in the expired buffers, freeing them in the process */
275   now = os_time_get();
276   while(curr != &mgr->delayed) {
277      curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
278      if(!buf && (ret = pb_cache_is_buffer_compat(curr_buf, size, desc) > 0))
279         buf = curr_buf;
280      else if(os_time_timeout(curr_buf->start, curr_buf->end, now))
281         _pb_cache_buffer_destroy(curr_buf);
282      else
283         /* This buffer (and all hereafter) are still hot in cache */
284         break;
285      if (ret == -1)
286         break;
287      curr = next;
288      next = curr->next;
289   }
290
291   /* keep searching in the hot buffers */
292   if(!buf && ret != -1) {
293      while(curr != &mgr->delayed) {
294         curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
295         ret = pb_cache_is_buffer_compat(curr_buf, size, desc);
296         if (ret > 0) {
297            buf = curr_buf;
298            break;
299         }
300         if (ret == -1)
301            break;
302         /* no need to check the timeout here */
303         curr = next;
304         next = curr->next;
305      }
306   }
307
308   if(buf) {
309      LIST_DEL(&buf->head);
310      --mgr->numDelayed;
311      pipe_mutex_unlock(mgr->mutex);
312      /* Increase refcount */
313      pipe_reference_init(&buf->base.base.reference, 1);
314      return &buf->base;
315   }
316
317   pipe_mutex_unlock(mgr->mutex);
318
319   buf = CALLOC_STRUCT(pb_cache_buffer);
320   if(!buf)
321      return NULL;
322
323   buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
324   if(!buf->buffer) {
325      FREE(buf);
326      return NULL;
327   }
328
329   assert(pipe_is_referenced(&buf->buffer->base.reference));
330   assert(pb_check_alignment(desc->alignment, buf->buffer->base.alignment));
331   assert(pb_check_usage(desc->usage, buf->buffer->base.usage));
332   assert(buf->buffer->base.size >= size);
333
334   pipe_reference_init(&buf->base.base.reference, 1);
335   buf->base.base.alignment = buf->buffer->base.alignment;
336   buf->base.base.usage = buf->buffer->base.usage;
337   buf->base.base.size = buf->buffer->base.size;
338
339   buf->base.vtbl = &pb_cache_buffer_vtbl;
340   buf->mgr = mgr;
341
342   return &buf->base;
343}
344
345
346static void
347pb_cache_manager_flush(struct pb_manager *_mgr)
348{
349   struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
350   struct list_head *curr, *next;
351   struct pb_cache_buffer *buf;
352
353   pipe_mutex_lock(mgr->mutex);
354   curr = mgr->delayed.next;
355   next = curr->next;
356   while(curr != &mgr->delayed) {
357      buf = LIST_ENTRY(struct pb_cache_buffer, curr, head);
358      _pb_cache_buffer_destroy(buf);
359      curr = next;
360      next = curr->next;
361   }
362   pipe_mutex_unlock(mgr->mutex);
363
364   assert(mgr->provider->flush);
365   if(mgr->provider->flush)
366      mgr->provider->flush(mgr->provider);
367}
368
369
370static void
371pb_cache_manager_destroy(struct pb_manager *mgr)
372{
373   pb_cache_manager_flush(mgr);
374   FREE(mgr);
375}
376
377
378struct pb_manager *
379pb_cache_manager_create(struct pb_manager *provider,
380                     	unsigned usecs)
381{
382   struct pb_cache_manager *mgr;
383
384   if(!provider)
385      return NULL;
386
387   mgr = CALLOC_STRUCT(pb_cache_manager);
388   if (!mgr)
389      return NULL;
390
391   mgr->base.destroy = pb_cache_manager_destroy;
392   mgr->base.create_buffer = pb_cache_manager_create_buffer;
393   mgr->base.flush = pb_cache_manager_flush;
394   mgr->provider = provider;
395   mgr->usecs = usecs;
396   LIST_INITHEAD(&mgr->delayed);
397   mgr->numDelayed = 0;
398   pipe_mutex_init(mgr->mutex);
399
400   return &mgr->base;
401}
402