Skip to contentSkip to conversation

tools/perf: Add loongarch cpuinfo and fix build warning

PATCH Superseded Yanteng Si 3 participants 7 messages Aug 25, 2023 – Aug 26, 2023

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yanteng Si

author
#0 lore raw

v2 cover tools/perf: Add loongarch cpuinfo and fix build warning

v2:
* Switch to strcasestr().
* Use "test -f" instead of "[-f FILE]".

v1:
* LoongArch and riscv don't have tools/arch/xxxxx/include/uapi/asm/mman.h, let's modify the detection rules.
* Allow to use cpuinfo on loongarch.

Yanteng Si (3):
perf/tools: Allow to use cpuinfo on LoongArch perf/trace: Fix mmap_flags for archs use generic mman.h perf/tools: Use "test -f" instead of "[-f FILE]"

 tools/perf/trace/beauty/mmap_flags.sh | 7 +++++--
 tools/perf/trace/beauty/mmap_prot.sh  | 5 +++--
 tools/perf/util/header.c              | 2 ++
 tools/perf/util/svghelper.c           | 5 ++++-
 4 files changed, 14 insertions(+), 5 deletions(-)
Signature · 1 line
2.31.4
author
#1 lore raw

PATCH v2 1/3 perf/tools: Allow to use cpuinfo on LoongArch

Define these macros so that the CPU name can be displayed when running perf report and perf timechart.

---
 tools/perf/util/header.c    | 2 ++
 tools/perf/util/svghelper.c | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/header.c +2 -0
index 52fbf526fe74..c6e19192bd28 100644
@@ -456,6 +456,8 @@ static int write_cpudesc(struct feat_fd *ff,
 #define CPUINFO_PROC	{ "Processor", }
 #elif defined(__xtensa__)
 #define CPUINFO_PROC	{ "core ID", }
+#elif defined(__loongarch__)
+#define CPUINFO_PROC	{ "Model Name", }
 #else
 #define CPUINFO_PROC	{ "model name", }
 #endif
tools/perf/util/svghelper.c +4 -1
index 5c62d3118c41..5744a3036b01 100644
@@ -331,7 +331,10 @@ static char *cpu_model(void)
 	file = fopen("/proc/cpuinfo", "r");
 	if (file) {
 		while (fgets(buf, 255, file)) {
-			if (strstr(buf, "model name")) {
+			if (strcasestr(buf, "Model Name")) {
+				strlcpy(cpu_m, &buf[13], 255);
+				break;
+			} else if (strcasestr(buf, "model name")) {
 				strlcpy(cpu_m, &buf[13], 255);
 				break;
 			}
Signature · 1 line
2.31.4
#2 lore raw

Re: [PATCH v2 1/3] perf/tools: Allow to use cpuinfo on LoongArch

Em Fri, Aug 25, 2023 at 07:28:02PM +0800, Yanteng Si escreveu:

Quoted 27 lines

Define these macros so that the CPU name can be displayed when running perf report and perf timechart.

---
 tools/perf/util/header.c    | 2 ++
 tools/perf/util/svghelper.c | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/header.c +2 -0
index 52fbf526fe74..c6e19192bd28 100644
@@ -456,6 +456,8 @@ static int write_cpudesc(struct feat_fd *ff,
 #define CPUINFO_PROC	{ "Processor", }
 #elif defined(__xtensa__)
 #define CPUINFO_PROC	{ "core ID", }
+#elif defined(__loongarch__)
+#define CPUINFO_PROC	{ "Model Name", }
 #else
 #define CPUINFO_PROC	{ "model name", }
 #endif
tools/perf/util/svghelper.c +0 -0
index 5c62d3118c41..5744a3036b01 100644

tools/perf/util/svghelper.c static char *cpu_model(void) · line 338

Quoted 9 lines
tools/perf/util/svghelper.c +4 -1
@@ -331,7 +331,10 @@ static char *cpu_model(void)
 	file = fopen("/proc/cpuinfo", "r");
 	if (file) {
 		while (fgets(buf, 255, file)) {
-			if (strstr(buf, "model name")) {
+			if (strcasestr(buf, "Model Name")) {
+				strlcpy(cpu_m, &buf[13], 255);
+				break;
+			} else if (strcasestr(buf, "model name")) {
 				strlcpy(cpu_m, &buf[13], 255);

Hey, the point of strcasestr() is to do a case _insensitive_ substring search, so all we need is:

tools/perf/util/svghelper.c static char *cpu_model(void) · line 336

Quoted 10 lines
tools/perf/util/svghelper.c +1 -1
@@ -331,7 +331,10 @@ static char *cpu_model(void)
 	file = fopen("/proc/cpuinfo", "r");
 	if (file) {
 		while (fgets(buf, 255, file)) {
-			if (strstr(buf, "model name")) {
+			if (strcasestr(buf, "model Name")) {
 				strlcpy(cpu_m, &buf[13], 255);
 				break;

Right? I'm doing this change while applying your patches.

See:

[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "Model NAME" needle=Model NAME
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "model name" needle=model name
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "Model Name" needle=Model Name
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
acme@five c]$ echo "Model Name : AMD Ryzen 9 5950X 16-Core Processor" | ./strcasestr "model name" needle=model name
haystack=Model Name : AMD Ryzen 9 5950X 16-Core Processor

found: Model Name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ echo "MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor" | ./strcasestr "model name" needle=model name
haystack=MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor

found: MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor [acme@five c]$
[acme@five c]$ cat strcasestr.c
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])

{
	char *needle = argv[1], haystack[128], *found;
	printf("needle=%s\n", needle);
	fgets(haystack, sizeof(haystack), stdin);
	printf("haystack=%s\n", haystack);
	found = strcasestr(haystack, needle);
	printf("found: %s", found);
	printf("found[13..]: %s", found + 13);
	return 0;
}

[acme@five c]$

author
#3 lore raw

Re: [PATCH v2 1/3] perf/tools: Allow to use cpuinfo on LoongArch

Quoted 40 lines · Arnaldo Carvalho de Melo

Em Fri, Aug 25, 2023 at 07:28:02PM +0800, Yanteng Si escreveu:

Quoted 27 lines

Define these macros so that the CPU name can be displayed when running perf report and perf timechart.

---
  tools/perf/util/header.c    | 2 ++
  tools/perf/util/svghelper.c | 5 ++++-
  2 files changed, 6 insertions(+), 1 deletion(-)
tools/perf/util/header.c +2 -0
index 52fbf526fe74..c6e19192bd28 100644
@@ -456,6 +456,8 @@ static int write_cpudesc(struct feat_fd *ff,
  #define CPUINFO_PROC	{ "Processor", }
  #elif defined(__xtensa__)
  #define CPUINFO_PROC	{ "core ID", }
+#elif defined(__loongarch__)
+#define CPUINFO_PROC	{ "Model Name", }
  #else
  #define CPUINFO_PROC	{ "model name", }
  #endif
tools/perf/util/svghelper.c +0 -0
index 5c62d3118c41..5744a3036b01 100644

tools/perf/util/svghelper.c static char *cpu_model(void) · line 338

Quoted 9 lines
tools/perf/util/svghelper.c +4 -1
@@ -331,7 +331,10 @@ static char *cpu_model(void)
  	file = fopen("/proc/cpuinfo", "r");
  	if (file) {
  		while (fgets(buf, 255, file)) {
-			if (strstr(buf, "model name")) {
+			if (strcasestr(buf, "Model Name")) {
+				strlcpy(cpu_m, &buf[13], 255);
+				break;
+			} else if (strcasestr(buf, "model name")) {
  				strlcpy(cpu_m, &buf[13], 255);

Hey, the point of strcasestr() is to do a case _insensitive_ substring search, so all we need is:

Quoted 10 lines

tools/perf/util/svghelper.c static char *cpu_model(void) · line 336

Quoted 10 lines
tools/perf/util/svghelper.c +1 -1
@@ -331,7 +331,10 @@ static char *cpu_model(void)
  	file = fopen("/proc/cpuinfo", "r");
  	if (file) {
  		while (fgets(buf, 255, file)) {
-			if (strstr(buf, "model name")) {
+			if (strcasestr(buf, "model Name")) {
  				strlcpy(cpu_m, &buf[13], 255);
  				break;

Thanks a million!

Quoted 51 lines

Right? I'm doing this change while applying your patches.

See:

[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "Model NAME" needle=Model NAME
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "model name" needle=model name
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ grep -m1 "model name" /proc/cpuinfo | ./strcasestr "Model Name" needle=Model Name
haystack=model name : AMD Ryzen 9 5950X 16-Core Processor

found: model name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
acme@five c]$ echo "Model Name : AMD Ryzen 9 5950X 16-Core Processor" | ./strcasestr "model name" needle=model name
haystack=Model Name : AMD Ryzen 9 5950X 16-Core Processor

found: Model Name : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor
[acme@five c]$ echo "MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor" | ./strcasestr "model name" needle=model name
haystack=MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor

found: MODEL NAME : AMD Ryzen 9 5950X 16-Core Processor
found[13..]: AMD Ryzen 9 5950X 16-Core Processor [acme@five c]$
[acme@five c]$ cat strcasestr.c
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])I will send v3

{
	char *needle = argv[1], haystack[128], *found;
	printf("needle=%s\n", needle);
	fgets(haystack, sizeof(haystack), stdin);
	printf("haystack=%s\n", haystack);
	found = strcasestr(haystack, needle);
	printf("found: %s", found);
	printf("found[13..]: %s", found + 13);
	return 0;
}
Quoted 1 line

[acme@five c]$

I see. I will send v3.

Thanks,

Yanteng

author
#4 lore raw

PATCH v2 2/3 perf/trace: Fix mmap_flags for archs use generic mman.h

In reply to Yanteng Si

Silence this warning:

grep: /root/linux-next/tools/arch/xxxxx/include/uapi/asm//mman.h:
No such file or directory

---
 tools/perf/trace/beauty/mmap_flags.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/trace/beauty/mmap_flags.sh +1 -1
index 3022597c8c17..d035b29fc5ec 100755
@@ -19,7 +19,7 @@ arch_mman=${arch_header_dir}/mman.h
 
 printf "static const char *mmap_flags[] = {\n"
 regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+MAP_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
-grep -E -q $regex ${arch_mman} && \
+([ -f ${arch_mman} ] && grep -E -q $regex ${arch_mman}) && \
 (grep -E $regex ${arch_mman} | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
Signature · 1 line
2.31.4
author
#5 lore raw

PATCH v2 3/3 perf/tools: Use "test -f" instead of "[-f FILE]"

In reply to Yanteng Si

It will be simpler without that extra subshell, so let's use "test -f".

---
 tools/perf/trace/beauty/mmap_flags.sh | 9 ++++++---
 tools/perf/trace/beauty/mmap_prot.sh  | 5 +++--
 2 files changed, 9 insertions(+), 5 deletions(-)
tools/perf/trace/beauty/mmap_flags.sh +6 -3
index d035b29fc5ec..6ecdb3c5a99e 100755
@@ -19,7 +19,8 @@ arch_mman=${arch_header_dir}/mman.h
 
 printf "static const char *mmap_flags[] = {\n"
 regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+MAP_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'
-([ -f ${arch_mman} ] && grep -E -q $regex ${arch_mman}) && \
+test -f ${arch_mman} && \
+grep -E -q $regex ${arch_mman} && \
 (grep -E $regex ${arch_mman} | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
@@ -28,12 +29,14 @@ grep -E -q $regex ${linux_mman} && \
 	grep -E -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g" | \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
+( ! test -f ${arch_mman} || \
+grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
 (grep -E $regex ${header_dir}/mman-common.h | \
 	grep -E -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.h>.*' ${arch_mman}) &&
+( ! test -f ${arch_mman} || \
+grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.h>.*' ${arch_mman}) &&
 (grep -E $regex ${header_dir}/mman.h | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
tools/perf/trace/beauty/mmap_prot.sh +3 -2
index 49e8c865214b..4436fcd6e861 100755
@@ -17,12 +17,13 @@ prefix="PROT"
 
 printf "static const char *mmap_prot[] = {\n"
 regex=`printf '^[[:space:]]*#[[:space:]]*define[[:space:]]+%s_([[:alnum:]_]+)[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*' ${prefix}`
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
+( ! test -f ${arch_mman} \
+|| grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
 (grep -E $regex ${common_mman} | \
 	grep -E -vw PROT_NONE | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
 	xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef ${prefix}_%s\n#define ${prefix}_%s %s\n#endif\n")
-[ -f ${arch_mman} ] && grep -E -q $regex ${arch_mman} &&
+test -f ${arch_mman} && grep -E -q $regex ${arch_mman} &&
 (grep -E $regex ${arch_mman} | \
 	grep -E -vw PROT_NONE | \
 	sed -r "s/$regex/\2 \1 \1 \1 \2/g"	| \
Signature · 1 line
2.31.4
#6 lore raw

Re: [PATCH v2 3/3] perf/tools: Use "test -f" instead of "[-f FILE]"

Quoted 2 lines · Yanteng Si

It will be simpler without that extra subshell, so let's use "test -f".

I don't understand this sentence. I'm pretty sure [ ... ] does not start any subshell.

Quoted 76 lines

---
 tools/perf/trace/beauty/mmap_flags.sh | 9 ++++++--- tools/perf/trace/beauty/mmap_prot.sh  | 5 +++--

 2 files changed, 9 insertions(+), 5 deletions(-)
tools/perf/trace/beauty/mmap_flags.sh +0 -0
index d035b29fc5ec..6ecdb3c5a99e 100755
@@ -19,7 +19,8 @@ arch_mman=${arch_header_dir}/mman.h


 printf "static const char *mmap_flags[] = {\n" regex='^[[:space:]]*#[[:space:]]*define[[:space:]]+MAP_([[:alnum:]_]+ )[[:space:]]+(0x[[:xdigit:]]+)[[:space:]]*.*'

tools/perf/trace/beauty/mmap_flags.sh +2 -1
-([ -f ${arch_mman} ] && grep -E -q $regex ${arch_mman}) && \
+test -f ${arch_mman} && \
+grep -E -q $regex ${arch_mman} && \

 (grep -E $regex ${arch_mman} | \
        sed -r "s/$regex/\2 \1 \1 \1 \2/g"      | \
        xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")

tools/perf/trace/beauty/mmap_flags.sh +0 -0
@@ -28,12 +29,14 @@ grep -E -q $regex ${linux_mman} && \
         grep -E -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
         sed -r "s/$regex/\2 \1 \1 \1 \2/g" | \

        xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&

tools/perf/trace/beauty/mmap_flags.sh +2 -0
+( ! test -f ${arch_mman} || \
+grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-

generic/mman.*' ${arch_mman}) &&
 (grep -E $regex ${header_dir}/mman-common.h | \
        grep -E -vw 'MAP_(UNINITIALIZED|TYPE|SHARED_VALIDATE)' | \
        sed -r "s/$regex/\2 \1 \1 \1 \2/g"      | \
        xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.h>.*' ${arch_mman}) &&

tools/perf/trace/beauty/mmap_flags.sh +2 -0
+( ! test -f ${arch_mman} || \
+grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-

generic/mman.h>.*' ${arch_mman}) &&
 (grep -E $regex ${header_dir}/mman.h | \
        sed -r "s/$regex/\2 \1 \1 \1 \2/g"      | \
        xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef MAP_%s\n#define MAP_%s %s\n#endif\n")

tools/perf/trace/beauty/mmap_prot.sh +0 -0
index 49e8c865214b..4436fcd6e861 100755
@@ -17,12 +17,13 @@ prefix="PROT"


 printf "static const char *mmap_prot[] = {\n" regex=printf '^[[:space:]]*#[[:space:]]*define[[:space:]]+%s_([[:alnum:]_]+)[[:spac e:]]+(0x[[:xdigit:]]+)[[:space:]]*.*' ${prefix}
-([ ! -f ${arch_mman} ] || grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm-generic/mman.*' ${arch_mman}) &&
+( ! test -f ${arch_mman} \
+|| grep -E -q '#[[:space:]]*include[[:space:]]+.*uapi/asm- generic/mman.*' ${arch_mman}) &&
 (grep -E $regex ${common_mman} | \
        grep -E -vw PROT_NONE | \
        sed -r "s/$regex/\2 \1 \1 \1 \2/g"      | \
        xargs printf "\t[ilog2(%s) + 1] = \"%s\",\n#ifndef ${prefix}_%s\n#define ${prefix}_%s %s\n#endif\n")
-[ -f ${arch_mman} ] && grep -E -q $regex ${arch_mman} && +test -f ${arch_mman} && grep -E -q $regex ${arch_mman} && (grep -E $regex ${arch_mman} | \
        grep -E -vw PROT_NONE | \
        sed -r "s/$regex/\2 \1 \1 \1 \2/g"      | \

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University