From patchwork Mon May 20 07:03:43 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 244859 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3EECC2C0079 for ; Mon, 20 May 2013 17:07:49 +1000 (EST) Received: from localhost ([::1]:45571 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UeKCB-0003H2-BD for incoming@patchwork.ozlabs.org; Mon, 20 May 2013 03:07:47 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59137) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UeK8s-0006vp-VZ for qemu-devel@nongnu.org; Mon, 20 May 2013 03:04:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UeK8q-0003mI-Hk for qemu-devel@nongnu.org; Mon, 20 May 2013 03:04:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36353) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UeK8q-0003m9-96 for qemu-devel@nongnu.org; Mon, 20 May 2013 03:04:20 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r4K74JVs023526 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 20 May 2013 03:04:19 -0400 Received: from fam-laptop.nay.redhat.com ([10.66.7.14]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r4K73nLm026470; Mon, 20 May 2013 03:04:17 -0400 From: Fam Zheng To: qemu-devel@nongnu.org Date: Mon, 20 May 2013 15:03:43 +0800 Message-Id: <1369033424-14594-10-git-send-email-famz@redhat.com> In-Reply-To: <1369033424-14594-1-git-send-email-famz@redhat.com> References: <1369033424-14594-1-git-send-email-famz@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, jcody@redhat.com, Fam Zheng , stefanha@redhat.com Subject: [Qemu-devel] [PATCH v3 09/10] curl: add cache quota. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- block/curl.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/block/curl.c b/block/curl.c index f7358de..f4e2571 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; @@ -518,6 +521,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: @@ -587,6 +592,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 (0 && 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, @@ -597,6 +623,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);