linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>,
	maddy@linux.vnet.ibm.com,
	Nageswara Sastry <rnsastry@linux.ibm.com>,
	kajoljain <kjain@linux.ibm.com>,
	linux-perf-users@vger.kernel.org, Jiri Olsa <jolsa@kernel.org>,
	disgoel@linux.vnet.ibm.com, linuxppc-dev@lists.ozlabs.org
Subject: Re: [PATCH V2 1/2] tools/perf: Add utility function to read /proc/cpuinfo for any field
Date: Tue, 10 May 2022 14:08:37 -0300	[thread overview]
Message-ID: <YnqcFZwtay4180Yj@kernel.org> (raw)
In-Reply-To: <BC9A38D1-E972-4F9B-8C76-8EBDC03FA6F1@linux.vnet.ibm.com>

Em Tue, May 10, 2022 at 07:08:47PM +0530, Athira Rajeev escreveu:
> 
> 
> > On 06-May-2022, at 3:03 PM, Athira Rajeev <atrajeev@linux.vnet.ibm.com> wrote:
> > 
> > 
> > 
> >> On 05-May-2022, at 10:54 PM, Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> >> 
> >> 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 <atrajeev@linux.vnet.ibm.com>
> >>> ---
> >>> 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?
> > 
> > 
> > Hi Arnaldo, Michael
> > 
> > Thanks for review comments. Based on suggestion from Michael, I am reworking on patch 2 to SKIP the test
> > if physical_id is set to -1 irrespective of value from cpuinfo.
> > 
> > In this patch, I had written "cpuinfo_field " function as generic function for retrieving any entry from /proc/cpuinfo.
> > But it won't be used in patch 2 now. Do you think this function is useful to keep ? Otherwise, I will drop patch 1

Lets add it when the need arises.

- Arnaldo
 
> Hi,
> 
> Requesting for suggestions on this change
> 
> Thanks
> Athira
> > 
> > Thanks
> > Athira Rajeev
> > 
> >> 
> >>> +			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

-- 

- Arnaldo

  reply	other threads:[~2022-05-10 17:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-05  9:39 [PATCH V2 0/2] Fix session topology test for powerpc and add utility function to get cpuinfo entries Athira Rajeev
2022-05-05  9:39 ` [PATCH V2 1/2] tools/perf: Add utility function to read /proc/cpuinfo for any field Athira Rajeev
2022-05-05 17:24   ` Arnaldo Carvalho de Melo
2022-05-06  9:33     ` Athira Rajeev
2022-05-10 13:38       ` Athira Rajeev
2022-05-10 17:08         ` Arnaldo Carvalho de Melo [this message]
2022-05-05  9:40 ` [PATCH V2 2/2] tools/perf/tests: Fix session topology test to skip the test in guest environment Athira Rajeev
2022-05-06  2:40   ` Michael Ellerman
2022-05-05 10:52 ` [PATCH V2 0/2] Fix session topology test for powerpc and add utility function to get cpuinfo entries kajoljain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YnqcFZwtay4180Yj@kernel.org \
    --to=acme@kernel.org \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=disgoel@linux.vnet.ibm.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kjain@linux.ibm.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.vnet.ibm.com \
    --cc=rnsastry@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).