Subject: Re: [PATCH V2 1/2] tools/perf: Add utility function to read /proc/cpuinfo for any field

From: Arnaldo Carvalho de Melo <[email-masked]>
Date: Thu, 5 May 2022 14:24:49 -0300

Em Thu, May 05, 2022 at 03:09:59PM +0530, Athira Rajeev escreveu:
> /proc/cpuinfo provides information about type of processor, number
> of CPU's etc. Reading /proc/cpuinfo file outputs useful information
> by field name like cpu, platform, model (depending on architecture)
> and its value separated by colon.
> 
> Add new utility function "cpuinfo_field" in "util/header.c" which
> accepts field name as input string to search in /proc/cpuinfo content.
> This returns the first matching value as resulting string. Example,
> calling the function "cpuinfo_field(platform)" in powerpc returns
> the platform value. This can be used to fetch processor information
> from "cpuinfo" by other utilities/testcases.
> 
> Signed-off-by: Athira Rajeev <[email-masked]>
> ---
>  tools/perf/util/header.c | 53 ++++++++++++++++++++++++++++++++++++++++
>  tools/perf/util/header.h |  1 +
>  2 files changed, 54 insertions(+)
> 
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index a27132e5a5ef..f08857f96606 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -983,6 +983,59 @@ static int write_dir_format(struct feat_fd *ff,
>  	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
>  }
>  
> +/*
> + * Return entry from /proc/cpuinfo
> + * indicated by "search" parameter.
> + */
> +char *cpuinfo_field(const char *search)
> +{
> +	FILE *file;
> +	char *buf = NULL;
> +	char *copy_buf = NULL, *p;
> +	size_t len = 0;
> +
> +	if (!search)
> +		return NULL;
> +
> +	file = fopen("/proc/cpuinfo", "r");
> +	if (!file)
> +		return NULL;
> +
> +	while (getline(&buf, &len, file) > 0) {
> +		if (!strncmp(buf, search, strlen(search)))

Can you save the search string lenght in a variable and use it instead
of calling strlen() for the same buffer for each line in /proc/cpuinfo?

> +			break;
> +	}
> +
> +	if (feof(file))
> +		goto done;
> +
> +	/*
> +	 * Trim the new line and separate
> +	 * value for search field from ":"
> +	 * in cpuinfo line output.
> +	 * Example output line:
> +	 * platform : <value>
> +	 */
> +	copy_buf = buf;
> +	p = strchr(copy_buf, ':');

So you assume that this will always be there, right? Shouldn't we not
assume that and check if p is NULL and bail out instead?

> +
> +	/* Go to string after ":" */
> +	copy_buf = p + 1;
> +	p = strchr(copy_buf, '\n');

Ditto.

> +	if (p)
> +		*p = '\0';
> +
> +	/* Copy the filtered string after removing space to buf */
> +	strcpy(buf, strim(copy_buf));
> +
> +	fclose(file);
> +	return buf;
> +
> +done:

Please rename this goto label to "not_found", "done" isn't intention
revealing.

> +	free(buf);
> +	fclose(file);
> +	return NULL;
> +}
>  /*
>   * Check whether a CPU is online
>   *
> diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
> index 0eb4bc29a5a4..b0f754364bd4 100644
> --- a/tools/perf/util/header.h
> +++ b/tools/perf/util/header.h
> @@ -166,4 +166,5 @@ int get_cpuid(char *buffer, size_t sz);
>  
>  char *get_cpuid_str(struct perf_pmu *pmu __maybe_unused);
>  int strcmp_cpuid_str(const char *s1, const char *s2);
> +char *cpuinfo_field(const char *search);
>  #endif /* __PERF_HEADER_H */
> -- 
> 2.35.1

-- 

- Arnaldo