Commit beaf6961 authored by Changpeng Liu's avatar Changpeng Liu
Browse files

blobfs: try to free a valid file cache buffer when allocating new caches



When allocating a new cache buffer, we will pick up a file and try to
free cache buffers with this file, but sometimes the file lock maybe
hold by other thread, so we use pthread_spin_trylock() here, when
pthread_spin_trylock() return a negative value, we will break the
loop, this is not efficient as the orginal logic, which may result
cache buffer allocation failure, so we will break the loop only when
there is a valid cache free.

Change-Id: I7be2fddb33c38dba466c2fcc2584ccf7ac16ea7a
Signed-off-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/950


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
parent b70c23fe
Loading
Loading
Loading
Loading
+36 −24
Original line number Diff line number Diff line
@@ -2019,7 +2019,7 @@ static void __file_flush(void *ctx);
/* Try to free some cache buffers of this file, this function must
 * be called while holding g_caches_lock.
 */
static void
static int
reclaim_cache_buffers(struct spdk_file *file)
{
	int rc;
@@ -2032,12 +2032,12 @@ reclaim_cache_buffers(struct spdk_file *file)
	 */
	rc = pthread_spin_trylock(&file->lock);
	if (rc != 0) {
		return;
		return -1;
	}

	if (file->tree->present_mask == 0) {
		pthread_spin_unlock(&file->lock);
		return;
		return -1;
	}
	spdk_tree_free_buffers(file->tree);

@@ -2049,6 +2049,8 @@ reclaim_cache_buffers(struct spdk_file *file)
		file->last = NULL;
	}
	pthread_spin_unlock(&file->lock);

	return 0;
}

static void *
@@ -2056,6 +2058,7 @@ alloc_cache_memory_buffer(struct spdk_file *context)
{
	struct spdk_file *file, *tmp;
	void *buf;
	int rc;

	buf = spdk_mempool_get(g_cache_pool);
	if (buf != NULL) {
@@ -2067,7 +2070,9 @@ alloc_cache_memory_buffer(struct spdk_file *context)
		if (!file->open_for_writing &&
		    file->priority == SPDK_FILE_PRIORITY_LOW &&
		    file != context) {
			reclaim_cache_buffers(file);
			rc = reclaim_cache_buffers(file);
			if (rc < 0) {
				continue;
			}
			buf = spdk_mempool_get(g_cache_pool);
			if (buf != NULL) {
@@ -2076,11 +2081,14 @@ alloc_cache_memory_buffer(struct spdk_file *context)
			}
			break;
		}
	}

	TAILQ_FOREACH_SAFE(file, &g_caches, cache_tailq, tmp) {
		if (!file->open_for_writing &&
		    file != context) {
			reclaim_cache_buffers(file);
			rc = reclaim_cache_buffers(file);
			if (rc < 0) {
				continue;
			}
			buf = spdk_mempool_get(g_cache_pool);
			if (buf != NULL) {
@@ -2089,10 +2097,13 @@ alloc_cache_memory_buffer(struct spdk_file *context)
			}
			break;
		}
	}

	TAILQ_FOREACH_SAFE(file, &g_caches, cache_tailq, tmp) {
		if (file != context) {
			reclaim_cache_buffers(file);
			rc = reclaim_cache_buffers(file);
			if (rc < 0) {
				continue;
			}
			buf = spdk_mempool_get(g_cache_pool);
			if (buf != NULL) {
@@ -2101,6 +2112,7 @@ alloc_cache_memory_buffer(struct spdk_file *context)
			}
			break;
		}
	}
	pthread_spin_unlock(&g_caches_lock);

	return NULL;