[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v7 09/13] curl: add cache quota.
From: |
Fam Zheng |
Subject: |
[Qemu-devel] [PATCH v7 09/13] curl: add cache quota. |
Date: |
Thu, 6 Jun 2013 14:25:55 +0800 |
Introduce a cache quota: BDRVCURLState.cache_quota.
When adding new CURLDataCache to BDRVCURLState, if number of existing
CURLDataCache is larger than CURL_CACHE_QUOTA, try to release some first
to limit the in memory cache size.
A least used entry is selected for releasing.
Signed-off-by: Fam Zheng <address@hidden>
---
block/curl.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/block/curl.c b/block/curl.c
index 9b18238..6e893d0 100644
--- a/block/curl.c
+++ b/block/curl.c
@@ -40,6 +40,7 @@
#define SECTOR_SIZE 512
#define READ_AHEAD_SIZE (256 * 1024)
+#define CURL_CACHE_QUOTA 10
struct BDRVCURLState;
@@ -90,6 +91,8 @@ typedef struct BDRVCURLState {
QEMUTimer *timer;
/* List of data cache ordered by access, freed from tail */
QLIST_HEAD(, CURLDataCache) cache;
+ /* Threshold to release unused cache when cache list is longer than it */
+ int cache_quota;
/* Whether http server accept range in header */
bool accept_range;
} BDRVCURLState;
@@ -526,6 +529,8 @@ static int curl_open(BlockDriverState *bs, QDict *options,
int flags)
curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
qemu_opts_del(opts);
+ s->cache_quota = CURL_CACHE_QUOTA;
+
return 0;
out:
@@ -595,6 +600,27 @@ static void curl_readv_bh_cb(void *p)
cache->data_len = aio_bytes + s->readahead_size;
cache->write_pos = 0;
cache->data = g_malloc(cache->data_len);
+ /* Try to release some cache */
+ while (s->cache_quota <= 0) {
+ CURLDataCache *p;
+ CURLDataCache *q = NULL;
+ assert(!QLIST_EMPTY(&s->cache));
+ for (p = QLIST_FIRST(&s->cache);
+ p; p = QLIST_NEXT(p, next)) {
+ if (p->use_count == 0) {
+ q = p;
+ }
+ }
+ if (!q) {
+ break;
+ }
+ QLIST_REMOVE(q, next);
+ g_free(q->data);
+ q->data = NULL;
+ g_free(q);
+ q = NULL;
+ s->cache_quota++;
+ }
QLIST_INSERT_HEAD(&s->acbs, acb, next);
snprintf(state->range, sizeof(state->range) - 1, "%zd-%zd",
cache->base_pos,
@@ -605,6 +631,7 @@ static void curl_readv_bh_cb(void *p)
QLIST_INSERT_HEAD(&s->cache, cache, next);
state->cache = cache;
cache->use_count++;
+ s->cache_quota--;
curl_multi_add_handle(s->multi, state->curl);
/* kick off curl to start the action */
curl_multi_socket_action(s->multi, 0, CURL_SOCKET_TIMEOUT, &running);
--
1.8.3
- [Qemu-devel] [PATCH v7 00/13] curl: fix curl read, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 01/13] curl: introduce CURLSockInfo to BDRVCURLState., Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 02/13] curl: change magic number to sizeof, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 03/13] curl: change curl_multi_do to curl_fd_handler, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 04/13] curl: fix curl_open, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 05/13] curl: add timer to BDRVCURLState, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 06/13] curl: introduce CURLDataCache, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 07/13] curl: make use of CURLDataCache., Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 08/13] curl: use list to store CURLState, Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 09/13] curl: add cache quota.,
Fam Zheng <=
- [Qemu-devel] [PATCH v7 10/13] curl: introduce ssl_no_cert runtime option., Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 11/13] block/curl.c: Refuse to open the handle for writes., Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 12/13] curl: set s->url to NULL after free., Fam Zheng, 2013/06/06
- [Qemu-devel] [PATCH v7 13/13] curl: change timeout to 30 seconds, Fam Zheng, 2013/06/06
- Re: [Qemu-devel] [PATCH v7 00/13] curl: fix curl read, Richard W.M. Jones, 2013/06/06
- Re: [Qemu-devel] [PATCH v7 00/13] curl: fix curl read, Stefan Hajnoczi, 2013/06/07