From patchwork Wed Jul 8 12:50:42 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Richard W.M. Jones" X-Patchwork-Id: 492892 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5EA0F1402AC for ; Wed, 8 Jul 2015 22:51:24 +1000 (AEST) Received: from localhost ([::1]:34931 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZCoor-0000eX-57 for incoming@patchwork.ozlabs.org; Wed, 08 Jul 2015 08:51:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57022) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZCooX-0000FZ-DA for qemu-devel@nongnu.org; Wed, 08 Jul 2015 08:51:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZCooW-0006sr-8M for qemu-devel@nongnu.org; Wed, 08 Jul 2015 08:51:01 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39223) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZCooS-0006rv-7M; Wed, 08 Jul 2015 08:50:56 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 7F6B4CA626; Wed, 8 Jul 2015 12:50:55 +0000 (UTC) Received: from choo.home.annexia.org (vpn1-7-115.ams2.redhat.com [10.36.7.115]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t68CooKD009588; Wed, 8 Jul 2015 08:50:54 -0400 From: "Richard W.M. Jones" To: armbru@redhat.com Date: Wed, 8 Jul 2015 13:50:42 +0100 Message-Id: <1436359843-15612-2-git-send-email-rjones@redhat.com> In-Reply-To: <1436359843-15612-1-git-send-email-rjones@redhat.com> References: <1436359843-15612-1-git-send-email-rjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 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, qemu-devel@nongnu.org, qemu-block@nongnu.org, agraf@suse.de Subject: [Qemu-devel] [PATCH v3 1/2] Add a simple mechanism to protect against error message floods. 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 You can now write code like this: FLOOD_COUNTER(errcount, 100); ... NO_FLOOD(errcount, error_report("oops, something bad happened"), error_report("further errors suppressed")); which would print the "oops, ..." error message up to 100 times, followed by the "further errors suppressed" message once, and then nothing else. Signed-off-by: Richard W.M. Jones --- include/qemu/no-flood.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 include/qemu/no-flood.h diff --git a/include/qemu/no-flood.h b/include/qemu/no-flood.h new file mode 100644 index 0000000..90a24da --- /dev/null +++ b/include/qemu/no-flood.h @@ -0,0 +1,34 @@ +/* + * Suppress error floods. + * + * Copyright (C) 2015 Red Hat, Inc. + * + * Author: Richard W.M. Jones + * + * 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_NO_FLOOD_H +#define __QEMU_NO_FLOOD_H 1 + +#include "qemu/atomic.h" + +/* Suggested initial value is 100 */ +#define FLOOD_COUNTER(counter_name, initial) \ + static int counter_name = (initial) + +#define NO_FLOOD(counter_name, code, suppress_message) \ + do { \ + int t_##counter_name = atomic_fetch_add(&counter_name, 0); \ + if (t_##counter_name > 0) { \ + code; \ + if (t_##counter_name == 1) { \ + suppress_message; \ + } \ + atomic_dec(&counter_name); \ + } \ +} while (0) + +#endif