[PATCH i-g-t v4 1/2] lib/igt_sysfs: Implement dynamic adjustment of debug log level

Modem, Bhanuprakash bhanuprakash.modem at intel.com
Thu Jul 11 11:27:12 UTC 2024


Hi Pranay,

On 11-07-2024 03:48 pm, Pranay Samala wrote:
> Adjust debug log levels dynamically to prevent machine
> disk overflow during excessive test debug logs.
> 
> Introduce function to modify log levels as needed,
> with an exit handler restoring default settings post-test.
> 
> v2:
> - Using proper function name to represent its
>    functionality (Bhanu)
> - Added Documentation for each functions (Bhanu)
> - Removed the magic number and instead reading the
>    default value first (Bhanu)
> - Using recommended apis for better handling (Bhanu)
> 
> v3:
> - Adding module drm_open_param function to open
>    the sysfs directory (Bhanu)
> - Reducing the current drm loglevel by step value
>    to make it more robust (Bhanu)
> 
> v4:
> - Remove all the extra new lines (Bhanu)
> - Remove the unused declaration (Bhanu)
> - Edit API to only update the requested drm debug
>    log value from the caller (Bhanu)
> 
> Signed-off-by: Pranay Samala <pranay.samala at intel.com>
> ---
>   lib/igt_sysfs.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++
>   lib/igt_sysfs.h |   5 +++
>   2 files changed, 108 insertions(+)
> 
> diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c
> index ffeec1ca2..84d6d8f9a 100644
> --- a/lib/igt_sysfs.c
> +++ b/lib/igt_sysfs.c
> @@ -412,6 +412,109 @@ int igt_sysfs_get_num_gt(int device)
>   	return num_gts;
>   }
>   
> +/**
> + * igt_sysfs_drm_module_params_open:
> + *
> + * This opens the sysfs directory corresponding to drm module
> + * parameters.
> + *
> + * Returns:
> + * The directory fd, or -1 on failure.
> + */
> +int igt_sysfs_drm_module_params_open(void)
> +{
> +	char path[] = "/sys/module/drm/parameters";
> +
> +	if (access(path, F_OK))
> +		return -1;
> +
> +	return open(path, O_RDONLY);
> +}
> +
> +static int log_level = -1;
> +
> +/**
> + * igt_drm_debug_level_get:
> + *
> + * This reads the current debug log level of the machine on
> + * which the test is currently executing.
> + *
> + * Returns:
> + * The current log level, or -1 on error.
> + */
> +int igt_drm_debug_level_get(int dir)
> +{
> +	char buf[20];
> +
> +	if (log_level >= 0)
> +		return log_level;
> +
> +	if (igt_sysfs_read(dir, "debug", buf, sizeof(buf) - 1) < 0)
> +		return -1;
> +
> +	return atoi(buf);
> +}
> +
> +/**
> + * igt_drm_debug_level_reset:
> + *
> + * This modifies the current debug log level of the machine
> + * to the default value post-test.
> + *
> + */
> +void igt_drm_debug_level_reset(void)
> +{
> +	char buf[20];
> +	int dir;
> +
> +	if (log_level < 0)
> +		return;
> +
> +	dir = igt_sysfs_drm_module_params_open();
> +	if (dir < 0)
> +		return;
> +
> +	igt_debug("Resetting DRM debug level to %d\n", log_level);
> +	snprintf(buf, sizeof(buf), "%d", log_level);
> +	igt_assert(igt_sysfs_set(dir, "debug", buf));
> +
> +	close(dir);
> +}
> +
> +static void igt_drm_debug_level_reset_exit_handler(int sig)
> +{
> +	igt_drm_debug_level_reset();
> +}
> +
> +/**
> + * igt_drm_debug_level_update:
> + * @debug_level: new debug level to set
> + *
> + * This modifies the current drm debug log level to the new value.
> + */
> +void igt_drm_debug_level_update(unsigned int new_log_level)
> +{
> +	char buf[20];
> +	int dir;
> +
> +	dir = igt_sysfs_drm_module_params_open();
> +	if (dir < 0)
> +		return;
> +
> +	log_level = igt_drm_debug_level_get(dir);
> +	if (log_level < 0) {
> +		close(dir);
> +		return;
> +	}
> +
> +	igt_debug("Setting DRM debug level to %d\n", new_log_level);
> +	snprintf(buf, sizeof(buf), "%d", new_log_level);
> +	igt_assert(igt_sysfs_set(dir, "debug", buf));
> +
> +	close(dir);
> +	igt_install_exit_handler(igt_drm_debug_level_reset_exit_handler);

Multiple exit handlers will install if this API gets called multiple 
times. As of now this change is fair enough to merge, as we are using 
this API as single time.

But let's explore and fix this later, please add a TODO/FIXME for 
tracking purpose.

With this change, this patch is
Reviewed-by: Bhanuprakash Modem <bhanuprakash.modem at intel.com>

- Bhanu

> +}
> +
>   /**
>    * igt_sysfs_write:
>    * @dir: sysfs directory
> diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h
> index 6c604d939..5d050c786 100644
> --- a/lib/igt_sysfs.h
> +++ b/lib/igt_sysfs.h
> @@ -138,6 +138,11 @@ void igt_sysfs_set_boolean(int dir, const char *attr, bool value);
>   void bind_fbcon(bool enable);
>   void fbcon_blink_enable(bool enable);
>   
> +void igt_drm_debug_level_update(unsigned int new_log_level);
> +void igt_drm_debug_level_reset(void);
> +int igt_drm_debug_level_get(int dir);
> +int igt_sysfs_drm_module_params_open(void);
> +
>   /**
>    * igt_sysfs_rw_attr:
>    * @dir: file descriptor for parent directory


More information about the igt-dev mailing list