* Free as many cache buffers from the list head as possible.
*/
static void
-release_expired_buffers_locked(struct pb_cache *mgr)
+release_expired_buffers_locked(struct list_head *cache)
{
struct list_head *curr, *next;
struct pb_cache_entry *entry;
now = os_time_get();
- curr = mgr->cache.next;
+ curr = cache->next;
next = curr->next;
- while (curr != &mgr->cache) {
+ while (curr != cache) {
entry = LIST_ENTRY(struct pb_cache_entry, curr, head);
if (!os_time_timeout(entry->start, entry->end, now))
pb_cache_add_buffer(struct pb_cache_entry *entry)
{
struct pb_cache *mgr = entry->mgr;
+ struct list_head *cache = &mgr->buckets[entry->bucket_index];
+ unsigned i;
pipe_mutex_lock(mgr->mutex);
assert(!pipe_is_referenced(&entry->buffer->reference));
- release_expired_buffers_locked(mgr);
+ for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++)
+ release_expired_buffers_locked(&mgr->buckets[i]);
/* Directly release any buffer that exceeds the limit. */
if (mgr->cache_size + entry->buffer->size > mgr->max_cache_size) {
entry->start = os_time_get();
entry->end = entry->start + mgr->usecs;
- LIST_ADDTAIL(&entry->head, &mgr->cache);
+ LIST_ADDTAIL(&entry->head, cache);
++mgr->num_buffers;
mgr->cache_size += entry->buffer->size;
pipe_mutex_unlock(mgr->mutex);
*/
struct pb_buffer *
pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
- unsigned alignment, unsigned usage)
+ unsigned alignment, unsigned usage,
+ unsigned bucket_index)
{
struct pb_cache_entry *entry;
struct pb_cache_entry *cur_entry;
struct list_head *cur, *next;
int64_t now;
int ret = 0;
+ struct list_head *cache = &mgr->buckets[bucket_index];
pipe_mutex_lock(mgr->mutex);
entry = NULL;
- cur = mgr->cache.next;
+ cur = cache->next;
next = cur->next;
/* search in the expired buffers, freeing them in the process */
now = os_time_get();
- while (cur != &mgr->cache) {
+ while (cur != cache) {
cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
if (!entry && (ret = pb_cache_is_buffer_compat(cur_entry, size,
/* keep searching in the hot buffers */
if (!entry && ret != -1) {
- while (cur != &mgr->cache) {
+ while (cur != cache) {
cur_entry = LIST_ENTRY(struct pb_cache_entry, cur, head);
ret = pb_cache_is_buffer_compat(cur_entry, size, alignment, usage);
{
struct list_head *curr, *next;
struct pb_cache_entry *buf;
+ unsigned i;
pipe_mutex_lock(mgr->mutex);
- curr = mgr->cache.next;
- next = curr->next;
- while (curr != &mgr->cache) {
- buf = LIST_ENTRY(struct pb_cache_entry, curr, head);
- destroy_buffer_locked(buf);
- curr = next;
+ for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++) {
+ struct list_head *cache = &mgr->buckets[i];
+
+ curr = cache->next;
next = curr->next;
+ while (curr != cache) {
+ buf = LIST_ENTRY(struct pb_cache_entry, curr, head);
+ destroy_buffer_locked(buf);
+ curr = next;
+ next = curr->next;
+ }
}
pipe_mutex_unlock(mgr->mutex);
}
void
pb_cache_init_entry(struct pb_cache *mgr, struct pb_cache_entry *entry,
- struct pb_buffer *buf)
+ struct pb_buffer *buf, unsigned bucket_index)
{
memset(entry, 0, sizeof(*entry));
entry->buffer = buf;
entry->mgr = mgr;
+ entry->bucket_index = bucket_index;
}
/**
void (*destroy_buffer)(struct pb_buffer *buf),
bool (*can_reclaim)(struct pb_buffer *buf))
{
- LIST_INITHEAD(&mgr->cache);
+ unsigned i;
+
+ for (i = 0; i < ARRAY_SIZE(mgr->buckets); i++)
+ LIST_INITHEAD(&mgr->buckets[i]);
+
pipe_mutex_init(mgr->mutex);
mgr->cache_size = 0;
mgr->max_cache_size = maximum_cache_size;
struct pb_buffer *buffer; /**< Pointer to the structure this is part of. */
struct pb_cache *mgr;
int64_t start, end; /**< Caching time interval */
+ unsigned bucket_index;
};
struct pb_cache
{
- struct list_head cache;
+ /* The cache is divided into buckets for minimizing cache misses.
+ * The driver controls which buffer goes into which bucket.
+ */
+ struct list_head buckets[8];
+
pipe_mutex mutex;
uint64_t cache_size;
uint64_t max_cache_size;
void pb_cache_add_buffer(struct pb_cache_entry *entry);
struct pb_buffer *pb_cache_reclaim_buffer(struct pb_cache *mgr, pb_size size,
- unsigned alignment, unsigned usage);
+ unsigned alignment, unsigned usage,
+ unsigned bucket_index);
void pb_cache_release_all_buffers(struct pb_cache *mgr);
void pb_cache_init_entry(struct pb_cache *mgr, struct pb_cache_entry *entry,
- struct pb_buffer *buf);
+ struct pb_buffer *buf, unsigned bucket_index);
void pb_cache_init(struct pb_cache *mgr, uint usecs, float size_factor,
unsigned bypass_usage, uint64_t maximum_cache_size,
void (*destroy_buffer)(struct pb_buffer *buf),