pb_bufmgr_cache.c revision 1672e8e05996d48e51a1998bd7e9b08b78e012f5
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 José 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 "pipe/p_debug.h" 39#include "pipe/p_winsys.h" 40#include "pipe/p_thread.h" 41#include "util/u_memory.h" 42#include "util/u_double_list.h" 43#include "util/u_time.h" 44 45#include "pb_buffer.h" 46#include "pb_bufmgr.h" 47 48 49/** 50 * Convenience macro (type safe). 51 */ 52#define SUPER(__derived) (&(__derived)->base) 53 54 55struct pb_cache_manager; 56 57 58/** 59 * Wrapper around a pipe buffer which adds delayed destruction. 60 */ 61struct pb_cache_buffer 62{ 63 struct pb_buffer base; 64 65 struct pb_buffer *buffer; 66 struct pb_cache_manager *mgr; 67 68 /** Caching time interval */ 69 struct util_time start, end; 70 71 struct list_head head; 72}; 73 74 75struct pb_cache_manager 76{ 77 struct pb_manager base; 78 79 struct pb_manager *provider; 80 unsigned usecs; 81 82 pipe_mutex mutex; 83 84 struct list_head delayed; 85 size_t numDelayed; 86}; 87 88 89static INLINE struct pb_cache_buffer * 90pb_cache_buffer(struct pb_buffer *buf) 91{ 92 assert(buf); 93 return (struct pb_cache_buffer *)buf; 94} 95 96 97static INLINE struct pb_cache_manager * 98pb_cache_manager(struct pb_manager *mgr) 99{ 100 assert(mgr); 101 return (struct pb_cache_manager *)mgr; 102} 103 104 105/** 106 * Actually destroy the buffer. 107 */ 108static INLINE void 109_pb_cache_buffer_destroy(struct pb_cache_buffer *buf) 110{ 111 struct pb_cache_manager *mgr = buf->mgr; 112 113 LIST_DEL(&buf->head); 114 assert(mgr->numDelayed); 115 --mgr->numDelayed; 116 assert(!buf->base.base.refcount); 117 pb_reference(&buf->buffer, NULL); 118 FREE(buf); 119} 120 121 122/** 123 * Free as many cache buffers from the list head as possible. 124 */ 125static void 126_pb_cache_buffer_list_check_free(struct pb_cache_manager *mgr) 127{ 128 struct list_head *curr, *next; 129 struct pb_cache_buffer *buf; 130 struct util_time now; 131 132 util_time_get(&now); 133 134 curr = mgr->delayed.next; 135 next = curr->next; 136 while(curr != &mgr->delayed) { 137 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head); 138 139 if(!util_time_timeout(&buf->start, &buf->end, &now)) 140 break; 141 142 _pb_cache_buffer_destroy(buf); 143 144 curr = next; 145 next = curr->next; 146 } 147} 148 149 150static void 151pb_cache_buffer_destroy(struct pb_buffer *_buf) 152{ 153 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 154 struct pb_cache_manager *mgr = buf->mgr; 155 156 pipe_mutex_lock(mgr->mutex); 157 assert(buf->base.base.refcount == 0); 158 159 _pb_cache_buffer_list_check_free(mgr); 160 161 util_time_get(&buf->start); 162 util_time_add(&buf->start, mgr->usecs, &buf->end); 163 LIST_ADDTAIL(&buf->head, &mgr->delayed); 164 ++mgr->numDelayed; 165 pipe_mutex_unlock(mgr->mutex); 166} 167 168 169static void * 170pb_cache_buffer_map(struct pb_buffer *_buf, 171 unsigned flags) 172{ 173 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 174 return pb_map(buf->buffer, flags); 175} 176 177 178static void 179pb_cache_buffer_unmap(struct pb_buffer *_buf) 180{ 181 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 182 pb_unmap(buf->buffer); 183} 184 185 186static void 187pb_cache_buffer_get_base_buffer(struct pb_buffer *_buf, 188 struct pb_buffer **base_buf, 189 unsigned *offset) 190{ 191 struct pb_cache_buffer *buf = pb_cache_buffer(_buf); 192 pb_get_base_buffer(buf->buffer, base_buf, offset); 193} 194 195 196const struct pb_vtbl 197pb_cache_buffer_vtbl = { 198 pb_cache_buffer_destroy, 199 pb_cache_buffer_map, 200 pb_cache_buffer_unmap, 201 pb_cache_buffer_get_base_buffer 202}; 203 204 205static INLINE boolean 206pb_cache_is_buffer_compat(struct pb_cache_buffer *buf, 207 size_t size, 208 const struct pb_desc *desc) 209{ 210 if(buf->base.base.size < size) 211 return FALSE; 212 213 /* be lenient with size */ 214 if(buf->base.base.size >= 2*size) 215 return FALSE; 216 217 if(!pb_check_alignment(desc->alignment, buf->base.base.alignment)) 218 return FALSE; 219 220 if(!pb_check_usage(desc->usage, buf->base.base.usage)) 221 return FALSE; 222 223 return TRUE; 224} 225 226 227static struct pb_buffer * 228pb_cache_manager_create_buffer(struct pb_manager *_mgr, 229 size_t size, 230 const struct pb_desc *desc) 231{ 232 struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 233 struct pb_cache_buffer *buf; 234 struct pb_cache_buffer *curr_buf; 235 struct list_head *curr, *next; 236 struct util_time now; 237 238 pipe_mutex_lock(mgr->mutex); 239 240 buf = NULL; 241 curr = mgr->delayed.next; 242 next = curr->next; 243 244 /* search in the expired buffers, freeing them in the process */ 245 util_time_get(&now); 246 while(curr != &mgr->delayed) { 247 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head); 248 if(!buf && pb_cache_is_buffer_compat(curr_buf, size, desc)) 249 buf = curr_buf; 250 else if(util_time_timeout(&curr_buf->start, &curr_buf->end, &now)) 251 _pb_cache_buffer_destroy(curr_buf); 252 else 253 /* This buffer (and all hereafter) are still hot in cache */ 254 break; 255 curr = next; 256 next = curr->next; 257 } 258 259 /* keep searching in the hot buffers */ 260 if(!buf) { 261 while(curr != &mgr->delayed) { 262 curr_buf = LIST_ENTRY(struct pb_cache_buffer, curr, head); 263 if(pb_cache_is_buffer_compat(curr_buf, size, desc)) { 264 buf = curr_buf; 265 break; 266 } 267 /* no need to check the timeout here */ 268 curr = next; 269 next = curr->next; 270 } 271 } 272 273 if(buf) { 274 LIST_DEL(&buf->head); 275 pipe_mutex_unlock(mgr->mutex); 276 ++buf->base.base.refcount; 277 return &buf->base; 278 } 279 280 pipe_mutex_unlock(mgr->mutex); 281 282 buf = CALLOC_STRUCT(pb_cache_buffer); 283 if(!buf) 284 return NULL; 285 286 buf->buffer = mgr->provider->create_buffer(mgr->provider, size, desc); 287 if(!buf->buffer) { 288 FREE(buf); 289 return NULL; 290 } 291 292 assert(buf->buffer->base.refcount >= 1); 293 assert(pb_check_alignment(desc->alignment, buf->buffer->base.alignment)); 294 assert(pb_check_usage(desc->usage, buf->buffer->base.usage)); 295 assert(buf->buffer->base.size >= size); 296 297 buf->base.base.refcount = 1; 298 buf->base.base.alignment = buf->buffer->base.alignment; 299 buf->base.base.usage = buf->buffer->base.usage; 300 buf->base.base.size = buf->buffer->base.size; 301 302 buf->base.vtbl = &pb_cache_buffer_vtbl; 303 buf->mgr = mgr; 304 305 return &buf->base; 306} 307 308 309static void 310pb_cache_manager_flush(struct pb_manager *_mgr) 311{ 312 struct pb_cache_manager *mgr = pb_cache_manager(_mgr); 313 struct list_head *curr, *next; 314 struct pb_cache_buffer *buf; 315 316 pipe_mutex_lock(mgr->mutex); 317 curr = mgr->delayed.next; 318 next = curr->next; 319 while(curr != &mgr->delayed) { 320 buf = LIST_ENTRY(struct pb_cache_buffer, curr, head); 321 _pb_cache_buffer_destroy(buf); 322 curr = next; 323 next = curr->next; 324 } 325 pipe_mutex_unlock(mgr->mutex); 326 327 assert(mgr->provider->flush); 328 if(mgr->provider->flush) 329 mgr->provider->flush(mgr->provider); 330} 331 332 333static void 334pb_cache_manager_destroy(struct pb_manager *mgr) 335{ 336 pb_cache_manager_flush(mgr); 337 FREE(mgr); 338} 339 340 341struct pb_manager * 342pb_cache_manager_create(struct pb_manager *provider, 343 unsigned usecs) 344{ 345 struct pb_cache_manager *mgr; 346 347 if(!provider) 348 return NULL; 349 350 mgr = CALLOC_STRUCT(pb_cache_manager); 351 if (!mgr) 352 return NULL; 353 354 mgr->base.destroy = pb_cache_manager_destroy; 355 mgr->base.create_buffer = pb_cache_manager_create_buffer; 356 mgr->base.flush = pb_cache_manager_flush; 357 mgr->provider = provider; 358 mgr->usecs = usecs; 359 LIST_INITHEAD(&mgr->delayed); 360 mgr->numDelayed = 0; 361 pipe_mutex_init(mgr->mutex); 362 363 return &mgr->base; 364} 365