From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f43.google.com (mail-wm0-f43.google.com [74.125.82.43]) by dpdk.org (Postfix) with ESMTP id 7BDED532D for ; Mon, 9 Nov 2015 14:50:51 +0100 (CET) Received: by wmww144 with SMTP id w144so76963016wmw.1 for ; Mon, 09 Nov 2015 05:50:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind_com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mail-followup-to:references :mime-version:content-type:content-disposition:in-reply-to; bh=9Ax8JZQMCV/ylK0lV5XpCBGZIFiT9T7L96LKcRiPF0Q=; b=uoAHgCLR4g7Nvb+1+Miv/OqS0V91A/MasGfa16uuwB0+/LGpjyXMCygyw8NBWXkrn9 63dU6P9w8d9g0FPIExsailQXSDt9tfbtyvqu6qpeH6Y5sfl5fq7H6Rjs4Ffy2wWRWgpr f7gcyUoURfus44i99vuu5oCdWjqlPJ1rUMUljwWIFEndkecey3gg3IYoCwbFFOXm/0Th iv24gfH/PmuG1jmd9/9AiQJeln26dsop3JPEk4ead8BUWabEZ/ChgsJTBPYwlbvkMPy6 Gs726m+o7F98QY3dDcs61UdNMAoAMXqe3Rezue49tCIGn++DCi++8BMsi3GgAZfRYoL5 XkHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-type:content-disposition :in-reply-to; bh=9Ax8JZQMCV/ylK0lV5XpCBGZIFiT9T7L96LKcRiPF0Q=; b=lmlKfHyqOZPv8wsr4aqLqGecB578gpKNAbEXa/mzrKzqTaOEV5bePvox4tJ9xLE9ei vZAmA4yVUBbIGQNODr8sYqXPRZfqu7oE8OR9KtBbg8MUpDKS1h1g84FBZjLq8crmKyxg 7q8W1PnDYdv7OUI8q4uwbotCIoITy4jRPBItA+30jq7m9gedBxCCkXFobMaPHw9KL6yy t+Rje/km8emcLCH0lkXsFm0fBgCne4wWuyEp3580S4VMUyyERqQmhdopRoWxGIFps1v1 w0c0iea5F2B1i/vm6B4+mdstfReAeZ4I3VoPwGSax0Yzazcr0If0dL9rTnNsbcvE74p5 pP9Q== X-Gm-Message-State: ALoCoQlq02aDhA8jr4SzfGykwYT2LSc73UH4cG7XUYTa4AHIR8iS1PjRpJ13Wni8VR4ESRneIGhE X-Received: by 10.194.60.179 with SMTP id i19mr29918792wjr.135.1447077051312; Mon, 09 Nov 2015 05:50:51 -0800 (PST) Received: from 6wind.com (guy78-3-82-239-227-177.fbx.proxad.net. [82.239.227.177]) by smtp.gmail.com with ESMTPSA id m7sm14399123wma.16.2015.11.09.05.50.50 (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 09 Nov 2015 05:50:50 -0800 (PST) Date: Mon, 9 Nov 2015 14:50:31 +0100 From: Adrien Mazarguil To: Bruce Richardson , Stephen Hemminger , Thomas Monjalon , dev@dpdk.org Message-ID: <20151109135031.GM4013@6wind.com> Mail-Followup-To: Bruce Richardson , Stephen Hemminger , Thomas Monjalon , dev@dpdk.org References: <1441811374-28984-1-git-send-email-bruce.richardson@intel.com> <1446552059-5446-1-git-send-email-bruce.richardson@intel.com> <1446552059-5446-3-git-send-email-bruce.richardson@intel.com> <4698587.GS9blBozDC@xps13> <20151104102418.GN3518@6wind.com> <20151104103957.4cabd090@xeon-e3> <20151105150918.GV3518@6wind.com> <20151106171007.GB19512@bricha3-MOBL3> <20151106172227.GC19512@bricha3-MOBL3> <20151109133905.GL4013@6wind.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20151109133905.GL4013@6wind.com> Subject: Re: [dpdk-dev] [PATCH v3 2/4] ethdev: move error checking macros to header X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 Nov 2015 13:50:51 -0000 On Mon, Nov 09, 2015 at 02:39:05PM +0100, Adrien Mazarguil wrote: > On Fri, Nov 06, 2015 at 05:22:27PM +0000, Bruce Richardson wrote: > > On Fri, Nov 06, 2015 at 05:10:07PM +0000, Bruce Richardson wrote: > > > On Thu, Nov 05, 2015 at 04:09:18PM +0100, Adrien Mazarguil wrote: > > > > > > > > I won't argue against this as it's obviously more complex than the original > > > > method, however note that users of the RTE_PMD_DEBUG_TRACE() macro do not > > > > have to modify their code. They shouldn't care about the implementation. > > > > > > > > Also note that we can do much cleaner code if we drop the all macros > > > > implementation using a (much easier to debug) static inline function, > > > > only perhaps with a wrapper macro that provides __LINE__, __func__ and > > > > __FILE__ as arguments. Nontrival code shouldn't be done in macros anyway. > > > > > > > Getting something working with __FILE__ and probably __LINE__ would be easy enough > > > with a helper macro, but __func__ is not so easy as it's not a preprocessor symbol > > > [since the pre-processor has no idea what function you are in]. > > > > > > However, using func, here is the best I've come up with so far. It's not that > > > pretty, but it's probably easier to work with than the macro version. > > > > > > #ifdef RTE_LIBRTE_ETHDEV_DEBUG > > > -#define RTE_PMD_DEBUG_TRACE(fmt, args...) \ > > > - RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args) > > > +#define RTE_PMD_DEBUG_TRACE(...) \ > > > + rte_pmd_debug_trace(__func__, __VA_ARGS__) > > > + > > > +static inline void > > > +rte_pmd_debug_trace(const char *func_name, const char *fmt, ...) > > > +{ > > > + static __thread char buffer[128]; > > > + char *out_buf = buffer; > > > + unsigned count; > > > + va_list ap; > > > + > > > + count = snprintf(buffer, sizeof(buffer), "%s: %s", func_name, fmt); > > > + if (count >= sizeof(buffer)) { // truncated output > > > + char *new_buf = malloc(count + 1); > > > + if (new_buf == NULL) // no memory, just print 128 chars > > > + goto print_buffer; > > > + snprintf(new_buf, count + 1, "%s: %s", func_name, fmt); > > > + va_start(ap, fmt); > > > + rte_vlog(RTE_LOG_ERR, RTE_LOGTYPE_PMD, buffer, ap); > > > + va_end(ap); > > > + free(new_buf); > > > + return; > > > + } > > > + > > > +print_buffer: > > > + va_start(ap, fmt); > > > + rte_vlog(RTE_LOG_ERR, RTE_LOGTYPE_PMD, out_buf, ap); > > > + va_end(ap); > > > +} > > > #else > > > #define RTE_PMD_DEBUG_TRACE(fmt, args...) > > > #endif > > > > > > Comments or improvements? > > Such a function shouldn't malloc() anything. The entire line should fit on > the stack (crashing is fine if it does not, then it's probably a bug). We > did something in two passes along these lines in mlx5_defs.h (not pretty but > quite useful): > > /* Allocate a buffer on the stack and fill it with a printf format string. */ > #define MKSTR(name, ...) \ > char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \ > \ > snprintf(name, sizeof(name), __VA_ARGS__) > > Untested but I guess modifying that function accordingly would look like: > > static inline void > rte_pmd_debug_trace(const char *func_name, const char *fmt, ...) > { > va_list ap; > va_start(ap, fmt); > > static __thread char buffer[vsnprintf(NULL, 0, fmt, ap)]; Of course this line should read: static __thread char buffer[vsnprintf(NULL, 0, fmt, ap) + 1]; > va_end(ap); > va_start(ap, fmt); > vsnprintf(buffer, sizeof(buffer), fmt, ap); > va_end(ap); > rte_log(RTE_LOG_ERR, RTE_LOGTYPE_PMD, "%s: %s", func_name, buffer); > } > > > And here's the version if we are happy to have file and line number instead of > > function name. I think this might be the best option. > > > > /Bruce > > > > #ifdef RTE_LIBRTE_ETHDEV_DEBUG > > -#define RTE_PMD_DEBUG_TRACE(fmt, args...) \ > > - RTE_LOG(ERR, PMD, "%s: " fmt, __func__, ## args) > > +#define RTE_PMD_DEBUG_TRACE(...) \ > > + RTE_LOG(ERR, PMD, __FILE__", " RTE_STR(__LINE__) ": " __VA_ARGS__) > > #else > > -#define RTE_PMD_DEBUG_TRACE(fmt, args...) > > +#define RTE_PMD_DEBUG_TRACE(...) > > #endif > > Much cleaner indeed, however __func__ might be useful when comparing log > outputs from different source code versions. I think we should keep it. -- Adrien Mazarguil 6WIND