From patchwork Wed Aug 8 06:25:44 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 175877 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 C12FF2C0462 for ; Wed, 8 Aug 2012 17:20:36 +1000 (EST) Received: from localhost ([::1]:35239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SyzjJ-0001FV-Eo for incoming@patchwork.ozlabs.org; Wed, 08 Aug 2012 02:26:53 -0400 Received: from eggs.gnu.org ([208.118.235.92]:43746) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzio-000077-Ua for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Syzil-00020z-Lb for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:22 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:62109) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzil-0001sm-Gg for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:26:19 -0400 Received: by mail-ob0-f173.google.com with SMTP id ta14so635077obb.4 for ; Tue, 07 Aug 2012 23:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=l9AhTT/g3lA27XNSCtvSIVyy92CK6eZRe60vfbELmgM=; b=P2aTbVcCWOB/DAFniJG2aZYAMilc8RDTaag7RhQOhhQiML/vv41xCBy1HF8xkpvwwR 2NLcmg/rtbs30tlFo4WMqO4gtEoHSxYRuN2vkmU4yikGD4MfncyfOOqcXJCaKoTH8LdP eoAQ8dPoYDAAfS+hfeClUeOS8Dyo9pHETCDHEOnSHycBuzPyx7qV+SeS/gLsCpZbWH01 HqgSfxWmucqMhXL9fTNisbTGo6N+uzcXiJ8L0wdmaSCFGnvOAnJZfmudx1Gs3m1MIYOK V6P+NHdbc939cAtSyHnY+hAiBg2HUJVB3JzPctXsjskSrVIGpF5TApvcI/TRkARH53aG QzaQ== Received: by 10.60.1.165 with SMTP id 5mr28094343oen.70.1344407179224; Tue, 07 Aug 2012 23:26:19 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPS id kf5sm3749860obc.7.2012.08.07.23.26.17 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 07 Aug 2012 23:26:18 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 8 Aug 2012 14:25:44 +0800 Message-Id: <1344407156-25562-4-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.173 Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Marcelo Tosatti , qemulist@gmail.com, Blue Swirl , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [Qemu-devel] [PATCH 03/15] qom: introduce reclaimer to release obj 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 From: Liu Ping Fan Collect unused object and release them at caller demand. Signed-off-by: Liu Ping Fan --- include/qemu/reclaimer.h | 28 ++++++++++++++++++++++ main-loop.c | 5 ++++ qemu-tool.c | 5 ++++ qom/Makefile.objs | 2 +- qom/reclaimer.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 1 deletions(-) create mode 100644 include/qemu/reclaimer.h create mode 100644 qom/reclaimer.c diff --git a/include/qemu/reclaimer.h b/include/qemu/reclaimer.h new file mode 100644 index 0000000..9307e93 --- /dev/null +++ b/include/qemu/reclaimer.h @@ -0,0 +1,28 @@ +/* + * QEMU reclaimer + * + * Copyright IBM, Corp. 2012 + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#ifndef QEMU_RECLAIMER +#define QEMU_RECLAIMER + +typedef void ReleaseHandler(void *opaque); +typedef struct Chunk { + QLIST_ENTRY(Chunk) list; + void *opaque; + ReleaseHandler *release; +} Chunk; + +typedef struct ChunkHead { + struct Chunk *lh_first; +} ChunkHead; + +void reclaimer_enqueue(ChunkHead *head, void *opaque, ReleaseHandler *release); +void reclaimer_worker(ChunkHead *head); +void qemu_reclaimer_enqueue(void *opaque, ReleaseHandler *release); +void qemu_reclaimer(void); +#endif diff --git a/main-loop.c b/main-loop.c index eb3b6e6..be9d095 100644 --- a/main-loop.c +++ b/main-loop.c @@ -26,6 +26,7 @@ #include "qemu-timer.h" #include "slirp/slirp.h" #include "main-loop.h" +#include "qemu/reclaimer.h" #ifndef _WIN32 @@ -505,5 +506,9 @@ int main_loop_wait(int nonblocking) them. */ qemu_bh_poll(); + /* ref to device from iohandler/bh/timer do not obey the rules, so delay + * reclaiming until now. + */ + qemu_reclaimer(); return ret; } diff --git a/qemu-tool.c b/qemu-tool.c index 318c5fc..f5fe319 100644 --- a/qemu-tool.c +++ b/qemu-tool.c @@ -21,6 +21,7 @@ #include "main-loop.h" #include "qemu_socket.h" #include "slirp/libslirp.h" +#include "qemu/reclaimer.h" #include @@ -75,6 +76,10 @@ void qemu_mutex_unlock_iothread(void) { } +void qemu_reclaimer(void) +{ +} + int use_icount; void qemu_clock_warp(QEMUClock *clock) diff --git a/qom/Makefile.objs b/qom/Makefile.objs index 5ef060a..a579261 100644 --- a/qom/Makefile.objs +++ b/qom/Makefile.objs @@ -1,4 +1,4 @@ -qom-obj-y = object.o container.o qom-qobject.o +qom-obj-y = object.o container.o qom-qobject.o reclaimer.o qom-obj-twice-y = cpu.o common-obj-y = $(qom-obj-twice-y) user-obj-y = $(qom-obj-twice-y) diff --git a/qom/reclaimer.c b/qom/reclaimer.c new file mode 100644 index 0000000..6cb53e3 --- /dev/null +++ b/qom/reclaimer.c @@ -0,0 +1,58 @@ +/* + * QEMU reclaimer + * + * Copyright IBM, Corp. 2012 + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu-common.h" +#include "qemu-thread.h" +#include "main-loop.h" +#include "qemu-queue.h" +#include "qemu/reclaimer.h" + +static struct QemuMutex reclaimer_lock; +static QLIST_HEAD(rcl, Chunk) reclaimer_list; + +void reclaimer_enqueue(ChunkHead *head, void *opaque, ReleaseHandler *release) +{ + Chunk *r = g_malloc0(sizeof(Chunk)); + r->opaque = opaque; + r->release = release; + QLIST_INSERT_HEAD_RCU(head, r, list); +} + +void reclaimer_worker(ChunkHead *head) +{ + Chunk *cur, *next; + + QLIST_FOREACH_SAFE(cur, head, list, next) { + QLIST_REMOVE(cur, list); + cur->release(cur->opaque); + g_free(cur); + } +} + +void qemu_reclaimer_enqueue(void *opaque, ReleaseHandler *release) +{ + Chunk *r = g_malloc0(sizeof(Chunk)); + r->opaque = opaque; + r->release = release; + qemu_mutex_lock(&reclaimer_lock); + QLIST_INSERT_HEAD_RCU(&reclaimer_list, r, list); + qemu_mutex_unlock(&reclaimer_lock); +} + + +void qemu_reclaimer(void) +{ + Chunk *cur, *next; + + QLIST_FOREACH_SAFE(cur, &reclaimer_list, list, next) { + QLIST_REMOVE(cur, list); + cur->release(cur->opaque); + g_free(cur); + } +}