1 /**************************************************************************
2  *
3  * Copyright 2007-2008 VMware, Inc.
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 VMWARE 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 <jfonseca-at-vmware-dot-com>
33  * \author Thomas Hellström <thellstrom-at-vmware-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/list.h"
42 
43 #include "pb_buffer.h"
44 #include "pb_bufmgr.h"
45 #include "pb_cache.h"
46 
47 
48 /**
49  * Wrapper around a pipe buffer which adds delayed destruction.
50  */
51 struct pb_cache_buffer
52 {
53    struct pb_buffer base;
54    struct pb_buffer *buffer;
55    struct pb_cache_manager *mgr;
56    struct pb_cache_entry cache_entry;
57 };
58 
59 
60 struct pb_cache_manager
61 {
62    struct pb_manager base;
63    struct pb_manager *provider;
64    struct pb_cache cache;
65 };
66 
67 
68 static inline struct pb_cache_buffer *
pb_cache_buffer(struct pb_buffer * buf)69 pb_cache_buffer(struct pb_buffer *buf)
70 {
71    assert(buf);
72    return (struct pb_cache_buffer *)buf;
73 }
74 
75 
76 static inline struct pb_cache_manager *
pb_cache_manager(struct pb_manager * mgr)77 pb_cache_manager(struct pb_manager *mgr)
78 {
79    assert(mgr);
80    return (struct pb_cache_manager *)mgr;
81 }
82 
83 
84 void
pb_cache_manager_remove_buffer(struct pb_buffer * pb_buf)85 pb_cache_manager_remove_buffer(struct pb_buffer *pb_buf)
86 {
87    struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf);
88 
89    /* the buffer won't be added if mgr is NULL */
90    buf->mgr = NULL;
91 }
92 
93 /**
94  * Actually destroy the buffer.
95  */
96 static void
_pb_cache_buffer_destroy(struct pb_buffer * pb_buf)97 _pb_cache_buffer_destroy(struct pb_buffer *pb_buf)
98 {
99    struct pb_cache_buffer *buf = pb_cache_buffer(pb_buf);
100 
101    assert(!pipe_is_referenced(&buf->base.reference));
102    pb_reference(&buf->buffer, NULL);
103    FREE(buf);
104 }
105 
106 
107 static void
pb_cache_buffer_destroy(struct pb_buffer * _buf)108 pb_cache_buffer_destroy(struct pb_buffer *_buf)
109 {
110    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
111    struct pb_cache_manager *mgr = buf->mgr;
112 
113    if (!mgr) {
114       pb_reference(&buf->buffer, NULL);
115       FREE(buf);
116       return;
117    }
118 
119    pb_cache_add_buffer(&buf->cache_entry);
120 }
121 
122 
123 static void *
pb_cache_buffer_map(struct pb_buffer * _buf,enum pb_usage_flags flags,void * flush_ctx)124 pb_cache_buffer_map(struct pb_buffer *_buf,
125 		    enum pb_usage_flags flags, void *flush_ctx)
126 {
127    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
128    return pb_map(buf->buffer, flags, flush_ctx);
129 }
130 
131 
132 static void
pb_cache_buffer_unmap(struct pb_buffer * _buf)133 pb_cache_buffer_unmap(struct pb_buffer *_buf)
134 {
135    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
136    pb_unmap(buf->buffer);
137 }
138 
139 
140 static enum pipe_error
pb_cache_buffer_validate(struct pb_buffer * _buf,struct pb_validate * vl,enum pb_usage_flags flags)141 pb_cache_buffer_validate(struct pb_buffer *_buf,
142                          struct pb_validate *vl,
143                          enum pb_usage_flags flags)
144 {
145    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
146    return pb_validate(buf->buffer, vl, flags);
147 }
148 
149 
150 static void
pb_cache_buffer_fence(struct pb_buffer * _buf,struct pipe_fence_handle * fence)151 pb_cache_buffer_fence(struct pb_buffer *_buf,
152                       struct pipe_fence_handle *fence)
153 {
154    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
155    pb_fence(buf->buffer, fence);
156 }
157 
158 
159 static void
pb_cache_buffer_get_base_buffer(struct pb_buffer * _buf,struct pb_buffer ** base_buf,pb_size * offset)160 pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf,
161                               struct pb_buffer **base_buf,
162                               pb_size *offset)
163 {
164    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
165    pb_get_base_buffer(buf->buffer, base_buf, offset);
166 }
167 
168 
169 const struct pb_vtbl
170 pb_cache_buffer_vtbl = {
171       pb_cache_buffer_destroy,
172       pb_cache_buffer_map,
173       pb_cache_buffer_unmap,
174       pb_cache_buffer_validate,
175       pb_cache_buffer_fence,
176       pb_cache_buffer_get_base_buffer
177 };
178 
179 
180 static bool
pb_cache_can_reclaim_buffer(struct pb_buffer * _buf)181 pb_cache_can_reclaim_buffer(struct pb_buffer *_buf)
182 {
183    struct pb_cache_buffer *buf = pb_cache_buffer(_buf);
184 
185    if (buf->mgr->provider->is_buffer_busy) {
186       if (buf->mgr->provider->is_buffer_busy(buf->mgr->provider, buf->buffer))
187          return false;
188    } else {
189       void *ptr = pb_map(buf->buffer, PB_USAGE_DONTBLOCK, NULL);
190 
191       if (!ptr)
192          return false;
193 
194       pb_unmap(buf->buffer);
195    }
196 
197    return true;
198 }
199 
200 
201 static struct pb_buffer *
pb_cache_manager_create_buffer(struct pb_manager * _mgr,pb_size size,const struct pb_desc * desc)202 pb_cache_manager_create_buffer(struct pb_manager *_mgr,
203                                pb_size size,
204                                const struct pb_desc *desc)
205 {
206    struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
207    struct pb_cache_buffer *buf;
208 
209    /* get a buffer from the cache */
210    buf = (struct pb_cache_buffer *)
211          pb_cache_reclaim_buffer(&mgr->cache, size, desc->alignment,
212                                  desc->usage, 0);
213    if (buf)
214       return &buf->base;
215 
216    /* create a new one */
217    buf = CALLOC_STRUCT(pb_cache_buffer);
218    if (!buf)
219       return NULL;
220 
221    buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
222 
223    /* Empty the cache and try again. */
224    if (!buf->buffer) {
225       pb_cache_release_all_buffers(&mgr->cache);
226       buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc);
227    }
228 
229    if(!buf->buffer) {
230       FREE(buf);
231       return NULL;
232    }
233 
234    assert(pipe_is_referenced(&buf->buffer->reference));
235    assert(pb_check_alignment(desc->alignment, buf->buffer->alignment));
236    assert(buf->buffer->size >= size);
237 
238    pipe_reference_init(&buf->base.reference, 1);
239    buf->base.alignment = buf->buffer->alignment;
240    buf->base.usage = buf->buffer->usage;
241    buf->base.size = buf->buffer->size;
242 
243    buf->base.vtbl = &pb_cache_buffer_vtbl;
244    buf->mgr = mgr;
245    pb_cache_init_entry(&mgr->cache, &buf->cache_entry, &buf->base, 0);
246 
247    return &buf->base;
248 }
249 
250 
251 static void
pb_cache_manager_flush(struct pb_manager * _mgr)252 pb_cache_manager_flush(struct pb_manager *_mgr)
253 {
254    struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
255 
256    pb_cache_release_all_buffers(&mgr->cache);
257 
258    assert(mgr->provider->flush);
259    if(mgr->provider->flush)
260       mgr->provider->flush(mgr->provider);
261 }
262 
263 
264 static void
pb_cache_manager_destroy(struct pb_manager * _mgr)265 pb_cache_manager_destroy(struct pb_manager *_mgr)
266 {
267    struct pb_cache_manager *mgr = pb_cache_manager(_mgr);
268 
269    pb_cache_deinit(&mgr->cache);
270    FREE(mgr);
271 }
272 
273 /**
274  * Create a caching buffer manager
275  *
276  * @param provider The buffer manager to which cache miss buffer requests
277  * should be redirected.
278  * @param usecs Unused buffers may be released from the cache after this
279  * time
280  * @param size_factor Declare buffers that are size_factor times bigger than
281  * the requested size as cache hits.
282  * @param bypass_usage Bitmask. If (requested usage & bypass_usage) != 0,
283  * buffer allocation requests are redirected to the provider.
284  * @param maximum_cache_size  Maximum size of all unused buffers the cache can
285  * hold.
286  */
287 struct pb_manager *
pb_cache_manager_create(struct pb_manager * provider,unsigned usecs,float size_factor,unsigned bypass_usage,uint64_t maximum_cache_size)288 pb_cache_manager_create(struct pb_manager *provider,
289                         unsigned usecs,
290                         float size_factor,
291                         unsigned bypass_usage,
292                         uint64_t maximum_cache_size)
293 {
294    struct pb_cache_manager *mgr;
295 
296    if (!provider)
297       return NULL;
298 
299    mgr = CALLOC_STRUCT(pb_cache_manager);
300    if (!mgr)
301       return NULL;
302 
303    mgr->base.destroy = pb_cache_manager_destroy;
304    mgr->base.create_buffer = pb_cache_manager_create_buffer;
305    mgr->base.flush = pb_cache_manager_flush;
306    mgr->provider = provider;
307    pb_cache_init(&mgr->cache, 1, usecs, size_factor, bypass_usage,
308                  maximum_cache_size,
309                  _pb_cache_buffer_destroy,
310                  pb_cache_can_reclaim_buffer);
311    return &mgr->base;
312 }
313