From mboxrd@z Thu Jan 1 00:00:00 1970 From: Liu Ping Fan Subject: [PATCH 03/15] qom: introduce reclaimer to release obj Date: Wed, 8 Aug 2012 14:25:44 +0800 Message-ID: <1344407156-25562-4-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Cc: kvm@vger.kernel.org, Anthony Liguori , Avi Kivity , Jan Kiszka , Marcelo Tosatti , Stefan Hajnoczi , Paolo Bonzini , Blue Swirl , =?UTF-8?q?Andreas=20F=C3=A4rber?= , qemulist@gmail.com To: qemu-devel@nongnu.org Return-path: Received: from mail-ob0-f174.google.com ([209.85.214.174]:51936 "EHLO mail-ob0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751931Ab2HHG0T (ORCPT ); Wed, 8 Aug 2012 02:26:19 -0400 Received: by mail-ob0-f174.google.com with SMTP id uo13so685038obb.19 for ; Tue, 07 Aug 2012 23:26:19 -0700 (PDT) In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: 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); + } +} -- 1.7.4.4 From mboxrd@z Thu Jan 1 00:00:00 1970 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) From: Liu Ping Fan Date: Wed, 8 Aug 2012 14:25:44 +0800 Message-Id: <1344407156-25562-4-git-send-email-qemulist@gmail.com> In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> Subject: [Qemu-devel] [PATCH 03/15] qom: introduce reclaimer to release obj List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org 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?= 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); + } +} -- 1.7.4.4