diff mbox

[RFC,rdma-core,1/3] util: Add common code for provider debug

Message ID 1484682413-56580-1-git-send-email-tatyana.e.nikolova@intel.com (mailing list archive)
State Changes Requested
Headers show

Commit Message

Nikolova, Tatyana E Jan. 17, 2017, 7:46 p.m. UTC
Add common utility functions to print error and
debug information, which can be used by all
providers.

The debug information can be classified with
VERBS_PROVIDER_DBG_MASK and can be enabled and
promoted with VERBS_PROVIDER_DBG_LEVEL,
both mask and level are environmental variables.

Signed-off-by: Mustafa Ismail <mustafa.ismail@intel.com>
Signed-off-by: Tatyana Nikolova <tatyana.e.nikolova@intel.com>
---
 buildlib/rdma_functions.cmake |  2 +-
 util/CMakeLists.txt           |  8 ++++
 util/dbg.c                    | 83 +++++++++++++++++++++++++++++++++++++
 util/dbg.h                    | 95 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 util/dbg.c
 create mode 100644 util/dbg.h

Comments

Jason Gunthorpe Jan. 17, 2017, 8:47 p.m. UTC | #1
> +++ b/buildlib/rdma_functions.cmake
> @@ -6,7 +6,7 @@
>  # Global list of pairs of (SHARED STATIC) libary target names
>  set(RDMA_STATIC_LIBS "" CACHE INTERNAL "Doc" FORCE)
>  
> -set(COMMON_LIBS_PIC ccan_pic)
> +set(COMMON_LIBS_PIC ccan_pic util_pic)
>  set(COMMON_LIBS ccan)

Also change COMMON_LIBS to have util

>  # Install a symlink during 'make install'
> diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
> index 1cda890..fb36103 100644
> +++ b/util/CMakeLists.txt
> @@ -1,3 +1,11 @@
>  publish_internal_headers(util
>    compiler.h
> +  dbg.h
>    )
> +
> +set(C_FILES
> +  dbg.c
> +  )
> +
> +add_library(util_pic ${C_FILES})

Use these two lines instead:

add_library(util STATIC ${C_FILES})
add_library(util_pic STATIC ${C_FILES})

> +#define VERBS_PROVIDER_DBG_MASK  "VERBS_PROVIDER_DBG_MASK"
> +#define VERBS_PROVIDER_DBG_LEVEL "VERBS_PROVIDER_DBG_LEVEL"
> +#define VERBS_PROVIDER_DBG_FILE  "VERBS_PROVIDER_DBG_FILE"

Probably don't need these as #defines

> +#define PRINT_ERR(fmt, ...)								\
> +do {											\
> +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> +} while (0)
> +
> +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)

This could just be LOG_DBG(stderr, ....)

> +
> +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)
> +
> +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> +do {											\
> +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> +		if (dbg_fp != stderr)							\
> +			fflush(dbg_fp);							\
> +} while (0)

Hum, wondering if it is worth retaining this FLUSH behavior or if we
should just have a way to open the log file with line buffering?

This patch should also include man page content please.

I think people will ask for the 'MLX5_DEBUG' style total compile out.
Should we have a --with-debug-log cmake option and disable it by
default?

'setup_debug' should probably happen someplace in verbs, not be in
every driver.

Otherwise looks essentially reasonable to me.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Leon Romanovsky Jan. 18, 2017, 5:31 a.m. UTC | #2
On Tue, Jan 17, 2017 at 01:46:53PM -0600, Tatyana Nikolova wrote:
> Add common utility functions to print error and
> debug information, which can be used by all
> providers.
>
> The debug information can be classified with
> VERBS_PROVIDER_DBG_MASK and can be enabled and
> promoted with VERBS_PROVIDER_DBG_LEVEL,
> both mask and level are environmental variables.
>
> Signed-off-by: Mustafa Ismail <mustafa.ismail@intel.com>
> Signed-off-by: Tatyana Nikolova <tatyana.e.nikolova@intel.com>
> ---
>  buildlib/rdma_functions.cmake |  2 +-
>  util/CMakeLists.txt           |  8 ++++
>  util/dbg.c                    | 83 +++++++++++++++++++++++++++++++++++++
>  util/dbg.h                    | 95 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 187 insertions(+), 1 deletion(-)
>  create mode 100644 util/dbg.c
>  create mode 100644 util/dbg.h
>
> diff --git a/buildlib/rdma_functions.cmake b/buildlib/rdma_functions.cmake
> index fcd47a3..84e2841 100644
> --- a/buildlib/rdma_functions.cmake
> +++ b/buildlib/rdma_functions.cmake
> @@ -6,7 +6,7 @@
>  # Global list of pairs of (SHARED STATIC) libary target names
>  set(RDMA_STATIC_LIBS "" CACHE INTERNAL "Doc" FORCE)
>
> -set(COMMON_LIBS_PIC ccan_pic)
> +set(COMMON_LIBS_PIC ccan_pic util_pic)
>  set(COMMON_LIBS ccan)
>
>  # Install a symlink during 'make install'
> diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
> index 1cda890..fb36103 100644
> --- a/util/CMakeLists.txt
> +++ b/util/CMakeLists.txt
> @@ -1,3 +1,11 @@
>  publish_internal_headers(util
>    compiler.h
> +  dbg.h
>    )
> +
> +set(C_FILES
> +  dbg.c
> +  )
> +
> +add_library(util_pic ${C_FILES})
> +set_property(TARGET util_pic PROPERTY POSITION_INDEPENDENT_CODE TRUE)
> diff --git a/util/dbg.c b/util/dbg.c
> new file mode 100644
> index 0000000..8e97b35
> --- /dev/null
> +++ b/util/dbg.c
> @@ -0,0 +1,83 @@
> +/*******************************************************************************
> +*
> +* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
> +* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
> +* Copyright (c) 2017 Intel Corporation.  All rights reserved.
> +*
> +* This software is available to you under a choice of one of two
> +* licenses.  You may choose to be licensed under the terms of the GNU
> +* General Public License (GPL) Version 2, available from the file
> +* COPYING in the main directory of this source tree, or the
> +* OpenFabrics.org BSD license below:
> +*
> +*   Redistribution and use in source and binary forms, with or
> +*   without modification, are permitted provided that the following
> +*   conditions are met:
> +*
> +*    - Redistributions of source code must retain the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer.
> +*
> +*    - Redistributions in binary form must reproduce the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer in the documentation and/or other materials
> +*	provided with the distribution.
> +*
> +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
> +* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
> +* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
> +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +* SOFTWARE.
> +*
> +*******************************************************************************/
> +
> +#include "dbg.h"
> +
> +uint64_t rdma_dbg_mask;
> +uint32_t rdma_dbg_level;
> +
> +void setup_debug(void)
> +{
> +	char *env;
> +
> +	env = getenv(VERBS_PROVIDER_DBG_MASK);
> +	if (env)
> +		rdma_dbg_mask = strtoull(env, NULL, 0);
> +
> +	env = getenv(VERBS_PROVIDER_DBG_LEVEL);
> +	if (env)
> +		rdma_dbg_level = strtoul(env, NULL, 0);
> +
> +	if (!rdma_dbg_level && rdma_dbg_mask)
> +		rdma_dbg_level = VERBS_DBG_LEVEL_VERBOSE;
> +
> +	if (rdma_dbg_level && !rdma_dbg_mask)
> +		rdma_dbg_mask = VERBS_DBG_MASK_ALL;
> +}
> +
> +FILE *open_debug_file(void)
> +{
> +	FILE *dbg_fp;
> +	char *env;
> +
> +	env = getenv(VERBS_PROVIDER_DBG_FILE);
> +	if (!env)
> +		return NULL;
> +
> +	dbg_fp = fopen(env, "aw+");
> +	if (!dbg_fp) {
> +		PRINT_ERR("Unable to open debug file %s, using stderr\n", env);
> +		return NULL;
> +	}
> +
> +	return dbg_fp;
> +}
> +
> +void close_debug_file(FILE *dbg_fp)
> +{
> +	if (dbg_fp != stderr)
> +		fclose(dbg_fp);
> +}
> diff --git a/util/dbg.h b/util/dbg.h
> new file mode 100644
> index 0000000..e9e66bf
> --- /dev/null
> +++ b/util/dbg.h
> @@ -0,0 +1,95 @@
> +/*******************************************************************************
> +*
> +* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
> +* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
> +* Copyright (c) 2017 Intel Corporation.  All rights reserved.
> +*
> +* This software is available to you under a choice of one of two
> +* licenses.  You may choose to be licensed under the terms of the GNU
> +* General Public License (GPL) Version 2, available from the file
> +* COPYING in the main directory of this source tree, or the
> +* OpenFabrics.org BSD license below:
> +*
> +*   Redistribution and use in source and binary forms, with or
> +*   without modification, are permitted provided that the following
> +*   conditions are met:
> +*
> +*    - Redistributions of source code must retain the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer.
> +*
> +*    - Redistributions in binary form must reproduce the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer in the documentation and/or other materials
> +*	provided with the distribution.
> +*
> +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
> +* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
> +* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
> +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +* SOFTWARE.
> +*
> +*******************************************************************************/
> +
> +#ifndef UTIL_DBG_H
> +#define UTIL_DBG_H
> +
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <stddef.h>
> +
> +#define VERBS_PROVIDER_DBG_MASK  "VERBS_PROVIDER_DBG_MASK"
> +#define VERBS_PROVIDER_DBG_LEVEL "VERBS_PROVIDER_DBG_LEVEL"
> +#define VERBS_PROVIDER_DBG_FILE  "VERBS_PROVIDER_DBG_FILE"
> +
> +#define VERBS_DBG_MASK_QP      0x1
> +#define VERBS_DBG_MASK_CQ      0x2
> +#define VERBS_DBG_MASK_QP_SEND 0x4
> +#define VERBS_DBG_MASK_QP_RECV 0x8
> +#define VERBS_DBG_MASK_MR      0x10
> +#define VERBS_DBG_MASK_PD      0x20
> +#define VERBS_DBG_MASK_CONTIG  0x40
> +#define VERBS_DBG_MASK_ALL     0xFFFFFFFF
> +
> +/* a debug level enables all lower levels */
> +#define VERBS_DBG_LEVEL_ERR     0x1
> +#define VERBS_DBG_LEVEL_WARN    0x2
> +#define VERBS_DBG_LEVEL_INFO    0x3
> +#define VERBS_DBG_LEVEL_VERBOSE 0xF
> +
> +extern uint64_t rdma_dbg_mask;
> +extern uint32_t rdma_dbg_level;
> +
> +#define PRINT_ERR(fmt, ...)								\
> +do {											\
> +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> +} while (0)
> +
> +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)
> +
> +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)
> +
> +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> +do {											\
> +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> +		if (dbg_fp != stderr)							\
> +			fflush(dbg_fp);							\
> +} while (0)

IMHO, these macros should be inline functions, and better to avoid global variables.

> +
> +void setup_debug(void);
> +FILE *open_debug_file(void);
> +void close_debug_file(FILE *);
> +
> +#endif
> --
> 1.8.5.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Yishai Hadas Jan. 18, 2017, 3:41 p.m. UTC | #3
On 1/17/2017 9:46 PM, Tatyana Nikolova wrote:
> Add common utility functions to print error and
> debug information, which can be used by all
> providers.
>
> The debug information can be classified with
> VERBS_PROVIDER_DBG_MASK and can be enabled and
> promoted with VERBS_PROVIDER_DBG_LEVEL,
> both mask and level are environmental variables.
>
> Signed-off-by: Mustafa Ismail <mustafa.ismail@intel.com>
> Signed-off-by: Tatyana Nikolova <tatyana.e.nikolova@intel.com>
> ---
>  buildlib/rdma_functions.cmake |  2 +-
>  util/CMakeLists.txt           |  8 ++++
>  util/dbg.c                    | 83 +++++++++++++++++++++++++++++++++++++
>  util/dbg.h                    | 95 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 187 insertions(+), 1 deletion(-)
>  create mode 100644 util/dbg.c
>  create mode 100644 util/dbg.h
>
> diff --git a/buildlib/rdma_functions.cmake b/buildlib/rdma_functions.cmake
> index fcd47a3..84e2841 100644
> --- a/buildlib/rdma_functions.cmake
> +++ b/buildlib/rdma_functions.cmake
> @@ -6,7 +6,7 @@
>  # Global list of pairs of (SHARED STATIC) libary target names
>  set(RDMA_STATIC_LIBS "" CACHE INTERNAL "Doc" FORCE)
>
> -set(COMMON_LIBS_PIC ccan_pic)
> +set(COMMON_LIBS_PIC ccan_pic util_pic)
>  set(COMMON_LIBS ccan)
>
>  # Install a symlink during 'make install'
> diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
> index 1cda890..fb36103 100644
> --- a/util/CMakeLists.txt
> +++ b/util/CMakeLists.txt
> @@ -1,3 +1,11 @@
>  publish_internal_headers(util
>    compiler.h
> +  dbg.h
>    )
> +
> +set(C_FILES
> +  dbg.c
> +  )
> +
> +add_library(util_pic ${C_FILES})
> +set_property(TARGET util_pic PROPERTY POSITION_INDEPENDENT_CODE TRUE)
> diff --git a/util/dbg.c b/util/dbg.c
> new file mode 100644
> index 0000000..8e97b35
> --- /dev/null
> +++ b/util/dbg.c
> @@ -0,0 +1,83 @@
> +/*******************************************************************************
> +*
> +* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
> +* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
> +* Copyright (c) 2017 Intel Corporation.  All rights reserved.
> +*
> +* This software is available to you under a choice of one of two
> +* licenses.  You may choose to be licensed under the terms of the GNU
> +* General Public License (GPL) Version 2, available from the file
> +* COPYING in the main directory of this source tree, or the
> +* OpenFabrics.org BSD license below:
> +*
> +*   Redistribution and use in source and binary forms, with or
> +*   without modification, are permitted provided that the following
> +*   conditions are met:
> +*
> +*    - Redistributions of source code must retain the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer.
> +*
> +*    - Redistributions in binary form must reproduce the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer in the documentation and/or other materials
> +*	provided with the distribution.
> +*
> +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
> +* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
> +* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
> +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +* SOFTWARE.
> +*
> +*******************************************************************************/
> +
> +#include "dbg.h"
> +
> +uint64_t rdma_dbg_mask;
> +uint32_t rdma_dbg_level;
> +
> +void setup_debug(void)
> +{
> +	char *env;
> +
> +	env = getenv(VERBS_PROVIDER_DBG_MASK);
> +	if (env)
> +		rdma_dbg_mask = strtoull(env, NULL, 0);
> +
> +	env = getenv(VERBS_PROVIDER_DBG_LEVEL);
> +	if (env)
> +		rdma_dbg_level = strtoul(env, NULL, 0);
> +
> +	if (!rdma_dbg_level && rdma_dbg_mask)
> +		rdma_dbg_level = VERBS_DBG_LEVEL_VERBOSE;
> +
> +	if (rdma_dbg_level && !rdma_dbg_mask)
> +		rdma_dbg_mask = VERBS_DBG_MASK_ALL;
> +}
> +
> +FILE *open_debug_file(void)
> +{
> +	FILE *dbg_fp;
> +	char *env;
> +
> +	env = getenv(VERBS_PROVIDER_DBG_FILE);
> +	if (!env)
> +		return NULL;
> +
> +	dbg_fp = fopen(env, "aw+");
> +	if (!dbg_fp) {
> +		PRINT_ERR("Unable to open debug file %s, using stderr\n", env);
> +		return NULL;
> +	}

Assuming that sharing the debug layer comes also to enable one output 
file with all the trace in one place, correct ?

If so, in case an application uses 2 vendors the second call to that 
function will fail as file was already opened for writing, as a result 
its output will go to stderr instead of to that file.

Note: looking at current code it seems that this issue already exists 
today if 2 contexts are opened from same vendor, however if moving to 
one shared layer better to address that note from day one.

> +	return dbg_fp;
> +}
> +
> +void close_debug_file(FILE *dbg_fp)
> +{
> +	if (dbg_fp != stderr)
> +		fclose(dbg_fp);
> +}
> diff --git a/util/dbg.h b/util/dbg.h
> new file mode 100644
> index 0000000..e9e66bf
> --- /dev/null
> +++ b/util/dbg.h
> @@ -0,0 +1,95 @@
> +/*******************************************************************************
> +*
> +* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
> +* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
> +* Copyright (c) 2017 Intel Corporation.  All rights reserved.
> +*
> +* This software is available to you under a choice of one of two
> +* licenses.  You may choose to be licensed under the terms of the GNU
> +* General Public License (GPL) Version 2, available from the file
> +* COPYING in the main directory of this source tree, or the
> +* OpenFabrics.org BSD license below:
> +*
> +*   Redistribution and use in source and binary forms, with or
> +*   without modification, are permitted provided that the following
> +*   conditions are met:
> +*
> +*    - Redistributions of source code must retain the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer.
> +*
> +*    - Redistributions in binary form must reproduce the above
> +*	copyright notice, this list of conditions and the following
> +*	disclaimer in the documentation and/or other materials
> +*	provided with the distribution.
> +*
> +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
> +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
> +* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
> +* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
> +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> +* SOFTWARE.
> +*
> +*******************************************************************************/
> +
> +#ifndef UTIL_DBG_H
> +#define UTIL_DBG_H
> +
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <stddef.h>
> +
> +#define VERBS_PROVIDER_DBG_MASK  "VERBS_PROVIDER_DBG_MASK"
> +#define VERBS_PROVIDER_DBG_LEVEL "VERBS_PROVIDER_DBG_LEVEL"
> +#define VERBS_PROVIDER_DBG_FILE  "VERBS_PROVIDER_DBG_FILE"
> +
> +#define VERBS_DBG_MASK_QP      0x1
> +#define VERBS_DBG_MASK_CQ      0x2
> +#define VERBS_DBG_MASK_QP_SEND 0x4
> +#define VERBS_DBG_MASK_QP_RECV 0x8
> +#define VERBS_DBG_MASK_MR      0x10
> +#define VERBS_DBG_MASK_PD      0x20
> +#define VERBS_DBG_MASK_CONTIG  0x40
> +#define VERBS_DBG_MASK_ALL     0xFFFFFFFF
> +
> +/* a debug level enables all lower levels */

You may consider adding VERBS_DBG_LEVEL_NONE 0x0

> +#define VERBS_DBG_LEVEL_ERR     0x1

> +#define VERBS_DBG_LEVEL_WARN    0x2
> +#define VERBS_DBG_LEVEL_INFO    0x3
> +#define VERBS_DBG_LEVEL_VERBOSE 0xF
> +
> +extern uint64_t rdma_dbg_mask;
> +extern uint32_t rdma_dbg_level;
> +
> +#define PRINT_ERR(fmt, ...)								\
> +do {											\
> +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> +} while (0)
> +
> +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)
> +
> +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> +do {											\
> +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> +} while (0)
> +
> +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> +do {											\
> +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> +		if (dbg_fp != stderr)							\
> +			fflush(dbg_fp);							\
> +} while (0)
> +
> +void setup_debug(void);
> +FILE *open_debug_file(void);
> +void close_debug_file(FILE *);
> +
> +#endif
>

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jason Gunthorpe Jan. 18, 2017, 4:44 p.m. UTC | #4
On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > +#define PRINT_ERR(fmt, ...)								\
> > +do {											\
> > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > +} while (0)
> > +
> > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > +do {											\
> > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > +} while (0)
> > +
> > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > +do {											\
> > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > +} while (0)
> > +
> > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > +do {											\
> > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > +		if (dbg_fp != stderr)							\
> > +			fflush(dbg_fp);							\
> > +} while (0)
> 
> IMHO, these macros should be inline functions, and better to avoid global variables.

It is more expensive at the call site to inline something that calls
via va_args than via ..., so these should remain as macros.

When doing this people generally like the 'if' test to be outside, for
performance.

I'm still not sure about these.. Why do we need print & log? I think
there should only be one. Providers should not print to stderr unless
the user asks for debugging.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Leon Romanovsky Jan. 18, 2017, 6:31 p.m. UTC | #5
On Wed, Jan 18, 2017 at 09:44:44AM -0700, Jason Gunthorpe wrote:
> On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > > +#define PRINT_ERR(fmt, ...)								\
> > > +do {											\
> > > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > > +} while (0)
> > > +
> > > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > > +do {											\
> > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > +} while (0)
> > > +
> > > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > > +do {											\
> > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > +} while (0)
> > > +
> > > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > > +do {											\
> > > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > > +		if (dbg_fp != stderr)							\
> > > +			fflush(dbg_fp);							\
> > > +} while (0)
> >
> > IMHO, these macros should be inline functions, and better to avoid global variables.
>
> It is more expensive at the call site to inline something that calls
> via va_args than via ..., so these should remain as macros.

These macros/functions need to be compiled in for DEBUG mode only, in
such case you can pay extra CPU cycles.

>
> When doing this people generally like the 'if' test to be outside, for
> performance.
>
> I'm still not sure about these.. Why do we need print & log? I think
> there should only be one. Providers should not print to stderr unless
> the user asks for debugging.
>
> Jason
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jason Gunthorpe Jan. 18, 2017, 6:44 p.m. UTC | #6
On Wed, Jan 18, 2017 at 08:31:04PM +0200, Leon Romanovsky wrote:
> On Wed, Jan 18, 2017 at 09:44:44AM -0700, Jason Gunthorpe wrote:
> > On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > > > +#define PRINT_ERR(fmt, ...)								\
> > > > +do {											\
> > > > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > > > +} while (0)
> > > > +
> > > > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > > > +do {											\
> > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > +} while (0)
> > > > +
> > > > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > > > +do {											\
> > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > +} while (0)
> > > > +
> > > > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > > > +do {											\
> > > > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > > > +		if (dbg_fp != stderr)							\
> > > > +			fflush(dbg_fp);							\
> > > > +} while (0)
> > >
> > > IMHO, these macros should be inline functions, and better to avoid global variables.
> >
> > It is more expensive at the call site to inline something that calls
> > via va_args than via ..., so these should remain as macros.
> 
> These macros/functions need to be compiled in for DEBUG mode only, in
> such case you can pay extra CPU cycles.

One of the reasonable ways to use this is to always compile in *error
case* debug prints, as these are intrinsically on the slow error path
already - however for that use we certainly want to minimize icache
consumption at the call site.

If we don't want to micro-optimize the DEBUG enabled case then I
recommend this approach for the macro:

#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)
    ({
      static const struct verbs_log_loc loc = {
       .dbg_mask = dbg_mask,
       .dbg_level = dbg_level,
       .fmt = fmt,
       .line = __LINE__,
       .func = __func__ };
    __check_fmt_args(fmt, ## __VA_ARGS__);
    verbs_log_dbg(dbg_fp, &loc, ## __VA_ARGS__);
   })

verbs_log_dbg would be implemented out of line.

That has the lowest call-site icache overhead as most of the required
data is pushed into the .rodata pointer (instead of in .text) and we
can continue to use the register calling convention for the va_args.

We can probably also eliminate dbg_fp as an argument, there is no
reason to have the providers store that, IHMO.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Leon Romanovsky Jan. 19, 2017, 8:09 a.m. UTC | #7
On Wed, Jan 18, 2017 at 11:44:11AM -0700, Jason Gunthorpe wrote:
> On Wed, Jan 18, 2017 at 08:31:04PM +0200, Leon Romanovsky wrote:
> > On Wed, Jan 18, 2017 at 09:44:44AM -0700, Jason Gunthorpe wrote:
> > > On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > > > > +#define PRINT_ERR(fmt, ...)								\
> > > > > +do {											\
> > > > > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > > > > +} while (0)
> > > > > +
> > > > > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > > > > +do {											\
> > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > +} while (0)
> > > > > +
> > > > > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > > > > +do {											\
> > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > +} while (0)
> > > > > +
> > > > > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > > > > +do {											\
> > > > > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > > > > +		if (dbg_fp != stderr)							\
> > > > > +			fflush(dbg_fp);							\
> > > > > +} while (0)
> > > >
> > > > IMHO, these macros should be inline functions, and better to avoid global variables.
> > >
> > > It is more expensive at the call site to inline something that calls
> > > via va_args than via ..., so these should remain as macros.
> >
> > These macros/functions need to be compiled in for DEBUG mode only, in
> > such case you can pay extra CPU cycles.
>
> One of the reasonable ways to use this is to always compile in *error
> case* debug prints, as these are intrinsically on the slow error path
> already - however for that use we certainly want to minimize icache
> consumption at the call site.

There are debug prints in data path too.
https://patchwork.kernel.org/patch/9521817/
See mlx5_get_next_cqe and mlx5_cq_read_wc_opcode functions.

>
> If we don't want to micro-optimize the DEBUG enabled case then I
> recommend this approach for the macro:
>
> #define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)
>     ({
>       static const struct verbs_log_loc loc = {
>        .dbg_mask = dbg_mask,
>        .dbg_level = dbg_level,
>        .fmt = fmt,
>        .line = __LINE__,
>        .func = __func__ };
>     __check_fmt_args(fmt, ## __VA_ARGS__);
>     verbs_log_dbg(dbg_fp, &loc, ## __VA_ARGS__);
>    })
>
> verbs_log_dbg would be implemented out of line.
>
> That has the lowest call-site icache overhead as most of the required
> data is pushed into the .rodata pointer (instead of in .text) and we
> can continue to use the register calling convention for the va_args.
>
> We can probably also eliminate dbg_fp as an argument, there is no
> reason to have the providers store that, IHMO.
>
> Jason
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jason Gunthorpe Jan. 19, 2017, 5:58 p.m. UTC | #8
On Thu, Jan 19, 2017 at 10:09:24AM +0200, Leon Romanovsky wrote:
> On Wed, Jan 18, 2017 at 11:44:11AM -0700, Jason Gunthorpe wrote:
> > On Wed, Jan 18, 2017 at 08:31:04PM +0200, Leon Romanovsky wrote:
> > > On Wed, Jan 18, 2017 at 09:44:44AM -0700, Jason Gunthorpe wrote:
> > > > On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > > > > > +#define PRINT_ERR(fmt, ...)								\
> > > > > > +do {											\
> > > > > > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > > > > > +} while (0)
> > > > > > +
> > > > > > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > > > > > +do {											\
> > > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > > +} while (0)
> > > > > > +
> > > > > > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > > > > > +do {											\
> > > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > > +} while (0)
> > > > > > +
> > > > > > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > > > > > +do {											\
> > > > > > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > > > > > +		if (dbg_fp != stderr)							\
> > > > > > +			fflush(dbg_fp);							\
> > > > > > +} while (0)
> > > > >
> > > > > IMHO, these macros should be inline functions, and better to avoid global variables.
> > > >
> > > > It is more expensive at the call site to inline something that calls
> > > > via va_args than via ..., so these should remain as macros.
> > >
> > > These macros/functions need to be compiled in for DEBUG mode only, in
> > > such case you can pay extra CPU cycles.
> >
> > One of the reasonable ways to use this is to always compile in *error
> > case* debug prints, as these are intrinsically on the slow error path
> > already - however for that use we certainly want to minimize icache
> > consumption at the call site.
> 
> There are debug prints in data path too.
> https://patchwork.kernel.org/patch/9521817/
> See mlx5_get_next_cqe and mlx5_cq_read_wc_opcode functions.

I was suggesting two debug entry points, one that is compiled out and the
other that is not.

Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Leon Romanovsky Jan. 22, 2017, 8:11 a.m. UTC | #9
On Thu, Jan 19, 2017 at 10:58:38AM -0700, Jason Gunthorpe wrote:
> On Thu, Jan 19, 2017 at 10:09:24AM +0200, Leon Romanovsky wrote:
> > On Wed, Jan 18, 2017 at 11:44:11AM -0700, Jason Gunthorpe wrote:
> > > On Wed, Jan 18, 2017 at 08:31:04PM +0200, Leon Romanovsky wrote:
> > > > On Wed, Jan 18, 2017 at 09:44:44AM -0700, Jason Gunthorpe wrote:
> > > > > On Wed, Jan 18, 2017 at 07:31:30AM +0200, Leon Romanovsky wrote:
> > > > > > > +#define PRINT_ERR(fmt, ...)								\
> > > > > > > +do {											\
> > > > > > > +	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
> > > > > > > +} while (0)
> > > > > > > +
> > > > > > > +#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
> > > > > > > +do {											\
> > > > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > > > +		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > > > +} while (0)
> > > > > > > +
> > > > > > > +#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
> > > > > > > +do {											\
> > > > > > > +	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
> > > > > > > +		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
> > > > > > > +} while (0)
> > > > > > > +
> > > > > > > +#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
> > > > > > > +do {											\
> > > > > > > +		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
> > > > > > > +		if (dbg_fp != stderr)							\
> > > > > > > +			fflush(dbg_fp);							\
> > > > > > > +} while (0)
> > > > > >
> > > > > > IMHO, these macros should be inline functions, and better to avoid global variables.
> > > > >
> > > > > It is more expensive at the call site to inline something that calls
> > > > > via va_args than via ..., so these should remain as macros.
> > > >
> > > > These macros/functions need to be compiled in for DEBUG mode only, in
> > > > such case you can pay extra CPU cycles.
> > >
> > > One of the reasonable ways to use this is to always compile in *error
> > > case* debug prints, as these are intrinsically on the slow error path
> > > already - however for that use we certainly want to minimize icache
> > > consumption at the call site.
> >
> > There are debug prints in data path too.
> > https://patchwork.kernel.org/patch/9521817/
> > See mlx5_get_next_cqe and mlx5_cq_read_wc_opcode functions.
>
> I was suggesting two debug entry points, one that is compiled out and the
> other that is not.

Exactly, I just didn't understand it from your previous responses.

Thanks

>
> Jason
> --
> To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/buildlib/rdma_functions.cmake b/buildlib/rdma_functions.cmake
index fcd47a3..84e2841 100644
--- a/buildlib/rdma_functions.cmake
+++ b/buildlib/rdma_functions.cmake
@@ -6,7 +6,7 @@ 
 # Global list of pairs of (SHARED STATIC) libary target names
 set(RDMA_STATIC_LIBS "" CACHE INTERNAL "Doc" FORCE)
 
-set(COMMON_LIBS_PIC ccan_pic)
+set(COMMON_LIBS_PIC ccan_pic util_pic)
 set(COMMON_LIBS ccan)
 
 # Install a symlink during 'make install'
diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt
index 1cda890..fb36103 100644
--- a/util/CMakeLists.txt
+++ b/util/CMakeLists.txt
@@ -1,3 +1,11 @@ 
 publish_internal_headers(util
   compiler.h
+  dbg.h
   )
+
+set(C_FILES
+  dbg.c
+  )
+
+add_library(util_pic ${C_FILES})
+set_property(TARGET util_pic PROPERTY POSITION_INDEPENDENT_CODE TRUE)
diff --git a/util/dbg.c b/util/dbg.c
new file mode 100644
index 0000000..8e97b35
--- /dev/null
+++ b/util/dbg.c
@@ -0,0 +1,83 @@ 
+/*******************************************************************************
+*
+* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
+* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
+* Copyright (c) 2017 Intel Corporation.  All rights reserved.
+*
+* This software is available to you under a choice of one of two
+* licenses.  You may choose to be licensed under the terms of the GNU
+* General Public License (GPL) Version 2, available from the file
+* COPYING in the main directory of this source tree, or the
+* OpenFabrics.org BSD license below:
+*
+*   Redistribution and use in source and binary forms, with or
+*   without modification, are permitted provided that the following
+*   conditions are met:
+*
+*    - Redistributions of source code must retain the above
+*	copyright notice, this list of conditions and the following
+*	disclaimer.
+*
+*    - Redistributions in binary form must reproduce the above
+*	copyright notice, this list of conditions and the following
+*	disclaimer in the documentation and/or other materials
+*	provided with the distribution.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+* SOFTWARE.
+*
+*******************************************************************************/
+
+#include "dbg.h"
+
+uint64_t rdma_dbg_mask;
+uint32_t rdma_dbg_level;
+
+void setup_debug(void)
+{
+	char *env;
+
+	env = getenv(VERBS_PROVIDER_DBG_MASK);
+	if (env)
+		rdma_dbg_mask = strtoull(env, NULL, 0);
+
+	env = getenv(VERBS_PROVIDER_DBG_LEVEL);
+	if (env)
+		rdma_dbg_level = strtoul(env, NULL, 0);
+
+	if (!rdma_dbg_level && rdma_dbg_mask)
+		rdma_dbg_level = VERBS_DBG_LEVEL_VERBOSE;
+
+	if (rdma_dbg_level && !rdma_dbg_mask)
+		rdma_dbg_mask = VERBS_DBG_MASK_ALL;
+}
+
+FILE *open_debug_file(void)
+{
+	FILE *dbg_fp;
+	char *env;
+
+	env = getenv(VERBS_PROVIDER_DBG_FILE);
+	if (!env)
+		return NULL;
+
+	dbg_fp = fopen(env, "aw+");
+	if (!dbg_fp) {
+		PRINT_ERR("Unable to open debug file %s, using stderr\n", env);
+		return NULL;
+	}
+
+	return dbg_fp;
+}
+
+void close_debug_file(FILE *dbg_fp)
+{
+	if (dbg_fp != stderr)
+		fclose(dbg_fp);
+}
diff --git a/util/dbg.h b/util/dbg.h
new file mode 100644
index 0000000..e9e66bf
--- /dev/null
+++ b/util/dbg.h
@@ -0,0 +1,95 @@ 
+/*******************************************************************************
+*
+* Copyright (c) 2012 Mellanox Technologies, Inc.  All rights reserved.
+* Copyright (c) 2015-2016 QLogic Corporation.  All rights reserved.
+* Copyright (c) 2017 Intel Corporation.  All rights reserved.
+*
+* This software is available to you under a choice of one of two
+* licenses.  You may choose to be licensed under the terms of the GNU
+* General Public License (GPL) Version 2, available from the file
+* COPYING in the main directory of this source tree, or the
+* OpenFabrics.org BSD license below:
+*
+*   Redistribution and use in source and binary forms, with or
+*   without modification, are permitted provided that the following
+*   conditions are met:
+*
+*    - Redistributions of source code must retain the above
+*	copyright notice, this list of conditions and the following
+*	disclaimer.
+*
+*    - Redistributions in binary form must reproduce the above
+*	copyright notice, this list of conditions and the following
+*	disclaimer in the documentation and/or other materials
+*	provided with the distribution.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+* SOFTWARE.
+*
+*******************************************************************************/
+
+#ifndef UTIL_DBG_H
+#define UTIL_DBG_H
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+
+#define VERBS_PROVIDER_DBG_MASK  "VERBS_PROVIDER_DBG_MASK"
+#define VERBS_PROVIDER_DBG_LEVEL "VERBS_PROVIDER_DBG_LEVEL"
+#define VERBS_PROVIDER_DBG_FILE  "VERBS_PROVIDER_DBG_FILE"
+
+#define VERBS_DBG_MASK_QP      0x1
+#define VERBS_DBG_MASK_CQ      0x2
+#define VERBS_DBG_MASK_QP_SEND 0x4
+#define VERBS_DBG_MASK_QP_RECV 0x8
+#define VERBS_DBG_MASK_MR      0x10
+#define VERBS_DBG_MASK_PD      0x20
+#define VERBS_DBG_MASK_CONTIG  0x40
+#define VERBS_DBG_MASK_ALL     0xFFFFFFFF
+
+/* a debug level enables all lower levels */
+#define VERBS_DBG_LEVEL_ERR     0x1
+#define VERBS_DBG_LEVEL_WARN    0x2
+#define VERBS_DBG_LEVEL_INFO    0x3
+#define VERBS_DBG_LEVEL_VERBOSE 0xF
+
+extern uint64_t rdma_dbg_mask;
+extern uint32_t rdma_dbg_level;
+
+#define PRINT_ERR(fmt, ...)								\
+do {											\
+	fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);		\
+} while (0)
+
+#define PRINT_DBG(dbg_mask, dbg_level, fmt, ...)					\
+do {											\
+	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
+		fprintf(stderr, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
+} while (0)
+
+#define LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ...)					\
+do {											\
+	if ((rdma_dbg_mask & dbg_mask) && (rdma_dbg_level >= dbg_level))		\
+		fprintf(dbg_fp, "[%s:%d]" fmt, __func__, __LINE__, ##__VA_ARGS__);	\
+} while (0)
+
+#define LOG_DBG_FLUSH(dbg_fp, dbg_mask, dbg_level, fmt, ...)				\
+do {											\
+		LOG_DBG(dbg_fp, dbg_mask, dbg_level, fmt, ##__VA_ARGS__);		\
+		if (dbg_fp != stderr)							\
+			fflush(dbg_fp);							\
+} while (0)
+
+void setup_debug(void);
+FILE *open_debug_file(void);
+void close_debug_file(FILE *);
+
+#endif