Skip to contentSkip to conversation

perf test: Fix cpu/thread map leaks

PATCH Applied Namhyung Kim 3 participants 16 messages Mar 1, 2021 – Mar 3, 2021 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf test: Fix cpu/thread map leaks

Hi,

This patchset fixes memory leaks in the perf test code. In my company setup, it runs daily with various sanitizers on, so I want to reduce the failures due to the leaks not the logic.

This time I've focused on the cpu and thread maps as they are obvious and easy to fix. I'll take a look at the rest failures.

I didn't add the Fixes: tags since most changes seem to predate the libperf change. I'm not sure if I could just add the original commit hash as this fix is meaningful only if Asan is enabled.. I'm afraid the stable tree maintainers will see patches not applied cleanly. But I can add them if you want, so please let me know.

It's also available at perf/asan-fix-v1 branch in

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung

Namhyung Kim (11):
perf test: Fix cpu and thread map leaks in basic mmap test perf test: Fix a memory leak in attr test
perf test: Fix cpu and thread map leaks in task_exit test perf test: Fix cpu and thread map leaks in sw_clock_freq test perf test: Fix cpu and thread map leaks in code_reading test perf test: Fix cpu and thread map leaks in keep_tracking test perf test: Fix cpu and thread map leaks in switch_tracking test perf test: Fix a thread map leak in thread_map_synthesize test perf test: Fix a memory leak in thread_map_remove test perf test: Fix cpu map leaks in cpu_map_print test perf test: Fix cpu and thread map leaks in perf_time_to_tsc test

 tools/perf/tests/attr.c             |  8 +++++++-
 tools/perf/tests/code-reading.c     | 10 +++-------
 tools/perf/tests/cpumap.c           |  2 ++
 tools/perf/tests/keep-tracking.c    |  5 ++---
 tools/perf/tests/mmap-basic.c       |  2 --
 tools/perf/tests/perf-time-to-tsc.c |  2 ++
 tools/perf/tests/sw-clock.c         | 12 ++++--------
 tools/perf/tests/switch-tracking.c  |  5 ++---
 tools/perf/tests/task-exit.c        | 10 +++-------
 tools/perf/tests/thread-map.c       |  8 +++-----
 10 files changed, 28 insertions(+), 36 deletions(-)
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#1 lore raw

PATCH 1/11 perf test: Fix cpu and thread map leaks in basic mmap test

The evlist has the maps with its own refcounts so we don't need to set the pointers to NULL. Otherwise following error was reported by Asan.

  # perf test -v 4
   4: Read samples using the mmap interface      :
  --- start ---
  test child forked, pid 139782
  mmap size 528384B

=================================================================
==139782==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:

    #0 0x7f1f76daee8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x564ba21a0fea in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x564ba21a1a0f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x564ba21a21cf in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x564ba21a21cf in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x564ba1e48298 in test__basic_mmap tests/mmap-basic.c:55
    #6 0x564ba1e278fb in run_test tests/builtin-test.c:428
    #7 0x564ba1e278fb in test_and_print tests/builtin-test.c:458
    #8 0x564ba1e29a53 in __cmd_test tests/builtin-test.c:679
    #9 0x564ba1e29a53 in cmd_test tests/builtin-test.c:825
    #10 0x564ba1e95cb4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x564ba1d1fa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x564ba1d1fa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x564ba1d1fa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7f1f768e4d09 in __libc_start_main ../csu/libc-start.c:308

    ...

test child finished with 1
---- end ----
Read samples using the mmap interface: FAILED!
failed to open shell test directory: /home/namhyung/libexec/perf-core/tests/shell

---
 tools/perf/tests/mmap-basic.c | 2 --
 1 file changed, 2 deletions(-)
tools/perf/tests/mmap-basic.c +0 -2
index 57093aeacc6f..73ae8f7aa066 100644
@@ -158,8 +158,6 @@ int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unuse
 
 out_delete_evlist:
 	evlist__delete(evlist);
-	cpus	= NULL;
-	threads = NULL;
 out_free_cpus:
 	perf_cpu_map__put(cpus);
 out_free_threads:
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#2 lore raw

PATCH 2/11 perf test: Fix a memory leak in attr test

In reply to Namhyung Kim

The get_argv_exec_path() returns a dynamic memory so it should be freed after use.

  $ perf test -v 17
  ...
  ==141682==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 33 byte(s) in 1 object(s) allocated from:

    #0 0x7f09107d2e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7f091035f6a7 in __vasprintf_internal libio/vasprintf.c:71

SUMMARY: AddressSanitizer: 33 byte(s) leaked in 1 allocation(s).

---
 tools/perf/tests/attr.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/tests/attr.c +7 -1
index ec972e0892ab..dd39ce9b0277 100644
@@ -182,14 +182,20 @@ int test__attr(struct test *test __maybe_unused, int subtest __maybe_unused)
 	struct stat st;
 	char path_perf[PATH_MAX];
 	char path_dir[PATH_MAX];
+	char *exec_path;
 
 	/* First try development tree tests. */
 	if (!lstat("./tests", &st))
 		return run_dir("./tests", "./perf");
 
+	exec_path = get_argv_exec_path();
+	if (exec_path == NULL)
+		return -1;
+
 	/* Then installed path. */
-	snprintf(path_dir,  PATH_MAX, "%s/tests", get_argv_exec_path());
+	snprintf(path_dir,  PATH_MAX, "%s/tests", exec_path);
 	snprintf(path_perf, PATH_MAX, "%s/perf", BINDIR);
+	free(exec_path);
 
 	if (!lstat(path_dir, &st) &&
 	    !lstat(path_perf, &st))
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#3 lore raw

PATCH 3/11 perf test: Fix cpu and thread map leaks in task_exit test

In reply to Namhyung Kim

The evlist has the maps with its own refcounts so we don't need to set the pointers to NULL. Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 24
  24: Number of exit events of a simple workload :
  --- start ---
  test child forked, pid 145915
  mmap size 528384B

=================================================================
==145915==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fc44e50d1f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x561cf50f4d2e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x561cf4eeb949 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x561cf4db7fd2 in test__task_exit tests/task-exit.c:74
    #4 0x561cf4d798fb in run_test tests/builtin-test.c:428
    #5 0x561cf4d798fb in test_and_print tests/builtin-test.c:458
    #6 0x561cf4d7ba53 in __cmd_test tests/builtin-test.c:679
    #7 0x561cf4d7ba53 in cmd_test tests/builtin-test.c:825
    #8 0x561cf4de7d04 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x561cf4c71a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x561cf4c71a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x561cf4c71a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fc44e042d09 in __libc_start_main ../csu/libc-start.c:308

    ...

test child finished with 1
---- end ----
Number of exit events of a simple workload: FAILED!

---
 tools/perf/tests/task-exit.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/perf/tests/task-exit.c +3 -7
index bbf94e4aa145..4c2969db59b0 100644
@@ -75,14 +75,11 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 	if (!cpus || !threads) {
 		err = -ENOMEM;
 		pr_debug("Not enough memory to create thread/cpu maps\n");
-		goto out_free_maps;
+		goto out_delete_evlist;
 	}
 
 	perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-	cpus	= NULL;
-	threads = NULL;
-
 	err = evlist__prepare_workload(evlist, &target, argv, false, workload_exec_failed_signal);
 	if (err < 0) {
 		pr_debug("Couldn't run the workload!\n");
@@ -137,7 +134,7 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 		if (retry_count++ > 1000) {
 			pr_debug("Failed after retrying 1000 times\n");
 			err = -1;
-			goto out_free_maps;
+			goto out_delete_evlist;
 		}
 
 		goto retry;
@@ -148,10 +145,9 @@ int test__task_exit(struct test *test __maybe_unused, int subtest __maybe_unused
 		err = -1;
 	}
 
-out_free_maps:
+out_delete_evlist:
 	perf_cpu_map__put(cpus);
 	perf_thread_map__put(threads);
-out_delete_evlist:
 	evlist__delete(evlist);
 	return err;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#4 lore raw

PATCH 4/11 perf test: Fix cpu and thread map leaks in sw_clock_freq test

In reply to Namhyung Kim

The evlist has the maps with its own refcounts so we don't need to set the pointers to NULL. Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 25
  25: Software clock events period values        :
  --- start ---
  test child forked, pid 149154
  mmap size 528384B
  mmap size 528384B

=================================================================
==149154==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
    #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
    #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
    #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
    #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308

    ...

test child finished with 1
---- end ----
Software clock events period values : FAILED!

---
 tools/perf/tests/sw-clock.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
tools/perf/tests/sw-clock.c +4 -8
index a49c9e23053b..74988846be1d 100644
@@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		.disabled = 1,
 		.freq = 1,
 	};
-	struct perf_cpu_map *cpus;
-	struct perf_thread_map *threads;
+	struct perf_cpu_map *cpus = NULL;
+	struct perf_thread_map *threads = NULL;
 	struct mmap *md;
 
 	attr.sample_freq = 500;
@@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 	if (!cpus || !threads) {
 		err = -ENOMEM;
 		pr_debug("Not enough memory to create thread/cpu maps\n");
-		goto out_free_maps;
+		goto out_delete_evlist;
 	}
 
 	perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-	cpus	= NULL;
-	threads = NULL;
-
 	if (evlist__open(evlist)) {
 		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
 
@@ -129,10 +126,9 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		err = -1;
 	}
 
-out_free_maps:
+out_delete_evlist:
 	perf_cpu_map__put(cpus);
 	perf_thread_map__put(threads);
-out_delete_evlist:
 	evlist__delete(evlist);
 	return err;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
#5 lore raw

Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test

Quoted 57 lines · Namhyung Kim

The evlist has the maps with its own refcounts so we don't need to set the pointers to NULL. Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 25
  25: Software clock events period values        :
  --- start ---
  test child forked, pid 149154
  mmap size 528384B
  mmap size 528384B

=================================================================
==149154==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
    #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
    #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
    #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
    #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308

    ...

test child finished with 1
---- end ----
Software clock events period values : FAILED!

---
 tools/perf/tests/sw-clock.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
tools/perf/tests/sw-clock.c +2 -2
index a49c9e23053b..74988846be1d 100644
@@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		.disabled = 1,
 		.freq = 1,
 	};
-	struct perf_cpu_map *cpus;
-	struct perf_thread_map *threads;
+	struct perf_cpu_map *cpus = NULL;
+	struct perf_thread_map *threads = NULL;
 	struct mmap *md;
 
 	attr.sample_freq = 500;
@@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 	if (!cpus || !threads) {

tools/perf/tests/sw-clock.c static int __test__sw_clock_freq(enum perf_sw_ids clock_id) · line 75

Quoted 10 lines
tools/perf/tests/sw-clock.c +1 -3
 		err = -ENOMEM;
 		pr_debug("Not enough memory to create thread/cpu maps\n");
-		goto out_free_maps;
+		goto out_delete_evlist;
 	}
 
 	perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-	cpus	= NULL;
-	threads = NULL;

hum, so IIUC we added these and the other you remove in your patches long time ago, because there was no refcounting at that time, right?

jirka

Quoted 19 lines
tools/perf/tests/sw-clock.c +1 -3
-
 	if (evlist__open(evlist)) {
 		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
 
@@ -129,10 +126,9 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
 		err = -1;
 	}
 
-out_free_maps:
+out_delete_evlist:
 	perf_cpu_map__put(cpus);
 	perf_thread_map__put(threads);
-out_delete_evlist:
 	evlist__delete(evlist);
 	return err;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#6 lore raw

Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test

Hi Jiri,

Quoted 65 lines · Jiri Olsa
Quoted 63 lines · Namhyung Kim

The evlist has the maps with its own refcounts so we don't need to set the pointers to NULL. Otherwise following error was reported by Asan.

Also change the goto label since it doesn't need to have two.

  # perf test -v 25
  25: Software clock events period values        :
  --- start ---
  test child forked, pid 149154
  mmap size 528384B
  mmap size 528384B

=================================================================
==149154==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fef5cd071f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x56260d5e8b8e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x56260d3df7a9 in thread_map__new_by_tid util/thread_map.c:63
    #3 0x56260d2ac6b2 in __test__sw_clock_freq tests/sw-clock.c:65
    #4 0x56260d26d8fb in run_test tests/builtin-test.c:428
    #5 0x56260d26d8fb in test_and_print tests/builtin-test.c:458
    #6 0x56260d26fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x56260d26fa53 in cmd_test tests/builtin-test.c:825
    #8 0x56260d2dbb64 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x56260d165a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x56260d165a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x56260d165a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fef5c83cd09 in __libc_start_main ../csu/libc-start.c:308

    ...

test child finished with 1
---- end ----
Software clock events period values : FAILED!

---
 tools/perf/tests/sw-clock.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
tools/perf/tests/sw-clock.c +3 -3
index a49c9e23053b..74988846be1d 100644
@@ -42,8 +42,8 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
              .disabled = 1,
              .freq = 1,
      };
-     struct perf_cpu_map *cpus;
-     struct perf_thread_map *threads;
+     struct perf_cpu_map *cpus = NULL;
+     struct perf_thread_map *threads = NULL;
      struct mmap *md;
 
      attr.sample_freq = 500;
@@ -66,14 +66,11 @@ static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
      if (!cpus || !threads) {
              err = -ENOMEM;
              pr_debug("Not enough memory to create thread/cpu maps\n");
-             goto out_free_maps;
+             goto out_delete_evlist;
      }
 
Quoted 7 lines

tools/perf/tests/sw-clock.c static int __test__sw_clock_freq(enum perf_sw_ids clock_id) · line 75

Quoted 4 lines
tools/perf/tests/sw-clock.c +0 -2
      perf_evlist__set_maps(&evlist->core, cpus, threads);
 
-     cpus    = NULL;
-     threads = NULL;

hum, so IIUC we added these and the other you remove in your patches long time ago, because there was no refcounting at that time, right?

It seems my original patch just set the maps directly.

bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency

And after that Adrian changed it to use the set_maps() helper.

c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps

It seems we already had the refcounting at the moment. And then the libperf renaming happened later.

Thanks,
Namhyung

Acked-by
#7 lore raw

Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test

On Tue, Mar 02, 2021 at 10:50:15AM +0900, Namhyung Kim wrote:

SNIP

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
              err = -ENOMEM;
              pr_debug("Not enough memory to create thread/cpu maps\n");
tools/perf/tests/sw-clock.c +1 -1
-             goto out_free_maps;
+             goto out_delete_evlist;
      }
Quoted 7 lines

tools/perf/tests/sw-clock.c static int __test__sw_clock_freq(enum perf_sw_ids clock_id) · line 75

Quoted 4 lines
      perf_evlist__set_maps(&evlist->core, cpus, threads);
tools/perf/tests/sw-clock.c +0 -2
-     cpus    = NULL;
-     threads = NULL;

hum, so IIUC we added these and the other you remove in your patches long time ago, because there was no refcounting at that time, right?

Quoted 7 lines

It seems my original patch just set the maps directly.

bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency

And after that Adrian changed it to use the set_maps() helper.

c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps

ok, and after that there's this one:
a55e56637613 perf evlist: Reference count the cpu and thread maps at set_maps()

forcing the get calls when storing cpus and threads

for the patchset

thanks,
jirka

Quoted 7 lines

It seems we already had the refcounting at the moment. And then the libperf renaming happened later.

Thanks,
Namhyung

maintainer Applied
#8 lore raw

Re: [PATCH 04/11] perf test: Fix cpu and thread map leaks in sw_clock_freq test

Em Tue, Mar 02, 2021 at 02:03:05PM +0100, Jiri Olsa escreveu:

Quoted 26 lines · Namhyung Kim

SNIP

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
              err = -ENOMEM;
              pr_debug("Not enough memory to create thread/cpu maps\n");
tools/perf/tests/sw-clock.c +1 -1
-             goto out_free_maps;
+             goto out_delete_evlist;
      }
Quoted 7 lines

tools/perf/tests/sw-clock.c static int __test__sw_clock_freq(enum perf_sw_ids clock_id) · line 75

Quoted 4 lines
      perf_evlist__set_maps(&evlist->core, cpus, threads);
tools/perf/tests/sw-clock.c +0 -2
-     cpus    = NULL;
-     threads = NULL;

hum, so IIUC we added these and the other you remove in your patches long time ago, because there was no refcounting at that time, right?

Quoted 7 lines

It seems my original patch just set the maps directly.

bc96b361cbf9 perf tests: Add a test case for checking sw clock event frequency

And after that Adrian changed it to use the set_maps() helper.

c5e6bd2ed3e8 perf tests: Fix software clock events test setting maps

Quoted 8 lines

ok, and after that there's this one:
a55e56637613 perf evlist: Reference count the cpu and thread maps at set_maps()

forcing the get calls when storing cpus and threads

for the patchset

Thanks, applied.

- Arnaldo

Quoted 11 lines

thanks,
jirka

Quoted 7 lines

It seems we already had the refcounting at the moment. And then the libperf renaming happened later.

Thanks,
Namhyung

Signature · 1 line
- Arnaldo
author maintainer
#9 lore raw

PATCH 5/11 perf test: Fix cpu and thread map leaks in code_reading test

In reply to Namhyung Kim

The evlist and the cpu/thread maps should be released together. Otherwise following error was reported by Asan.

Note that this test still has memory leaks in DSOs so it still fails even after this change. I'll take a look at that too.

  # perf test -v 26
  26: Object code reading                        :
  --- start ---
  test child forked, pid 154184
  Looking at the vmlinux_path (8 entries long)
  symsrc__init: build id mismatch for vmlinux.
  symsrc__init: cannot get elf header.
  Using /proc/kcore for kernel data
  Using /proc/kallsyms for symbols
  Parsing event 'cycles'
  mmap size 528384B
  ...
  =================================================================
  ==154184==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 439 byte(s) in 1 object(s) allocated from:

    #0 0x7fcb66e77037 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x55ad9b7e821e in dso__new_id util/dso.c:1256
    #2 0x55ad9b8cfd4a in __machine__addnew_vdso util/vdso.c:132
    #3 0x55ad9b8cfd4a in machine__findnew_vdso util/vdso.c:347
    #4 0x55ad9b845b7e in map__new util/map.c:176
    #5 0x55ad9b8415a2 in machine__process_mmap2_event util/machine.c:1787
    #6 0x55ad9b8fab16 in perf_tool__process_synth_event util/synthetic-events.c:64
    #7 0x55ad9b8fab16 in perf_event__synthesize_mmap_events util/synthetic-events.c:499
    #8 0x55ad9b8fbfdf in __event__synthesize_thread util/synthetic-events.c:741
    #9 0x55ad9b8ff3e3 in perf_event__synthesize_thread_map util/synthetic-events.c:833
    #10 0x55ad9b738585 in do_test_code_reading tests/code-reading.c:608
    #11 0x55ad9b73b25d in test__code_reading tests/code-reading.c:722
    #12 0x55ad9b6f28fb in run_test tests/builtin-test.c:428
    #13 0x55ad9b6f28fb in test_and_print tests/builtin-test.c:458
    #14 0x55ad9b6f4a53 in __cmd_test tests/builtin-test.c:679
    #15 0x55ad9b6f4a53 in cmd_test tests/builtin-test.c:825
    #16 0x55ad9b760cc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #17 0x55ad9b5eaa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #18 0x55ad9b5eaa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #19 0x55ad9b5eaa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #20 0x7fcb669acd09 in __libc_start_main ../csu/libc-start.c:308

    ...

SUMMARY: AddressSanitizer: 471 byte(s) leaked in 2 allocation(s). test child finished with 1
---- end ----
Object code reading: FAILED!

---
 tools/perf/tests/code-reading.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
tools/perf/tests/code-reading.c +3 -7
index 280f0348a09c..2fdc7b2f996e 100644
@@ -706,13 +706,9 @@ static int do_test_code_reading(bool try_kcore)
 out_put:
 	thread__put(thread);
 out_err:
-
-	if (evlist) {
-		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
-	}
+	evlist__delete(evlist);
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 	machine__delete_threads(machine);
 	machine__delete(machine);
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#10 lore raw

PATCH 6/11 perf test: Fix cpu and thread map leaks in keep_tracking test

In reply to Namhyung Kim

The evlist and the cpu/thread maps should be released together. Otherwise following error was reported by Asan.

  $ perf test -v 28
  28: Use a dummy software event to keep tracking:
  --- start ---
  test child forked, pid 156810
  mmap size 528384B

=================================================================
==156810==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:

    #0 0x7f637d2bce8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55cc6295cffa in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55cc6295da1f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x55cc6295e1df in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x55cc6295e1df in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x55cc626287cf in test__keep_tracking tests/keep-tracking.c:84
    #6 0x55cc625e38fb in run_test tests/builtin-test.c:428
    #7 0x55cc625e38fb in test_and_print tests/builtin-test.c:458
    #8 0x55cc625e5a53 in __cmd_test tests/builtin-test.c:679
    #9 0x55cc625e5a53 in cmd_test tests/builtin-test.c:825
    #10 0x55cc62651cc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x55cc624dba88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x55cc624dba88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x55cc624dba88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7f637cdf2d09 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s). test child finished with 1
---- end ----
Use a dummy software event to keep tracking: FAILED!

---
 tools/perf/tests/keep-tracking.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/perf/tests/keep-tracking.c +2 -3
index e6f1b2a38e03..a0438b0f0805 100644
@@ -154,10 +154,9 @@ int test__keep_tracking(struct test *test __maybe_unused, int subtest __maybe_un
 	if (evlist) {
 		evlist__disable(evlist);
 		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
 	}
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 
 	return err;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#11 lore raw

PATCH 7/11 perf test: Fix cpu and thread map leaks in switch_tracking test

In reply to Namhyung Kim

The evlist and cpu/thread maps should be released together. Otherwise the following error was reported by Asan.

  $ perf test -v 35
  35: Track with sched_switch                    :
  --- start ---
  test child forked, pid 159287
  Using CPUID GenuineIntel-6-8E-C
  mmap size 528384B
  1295 events recorded

=================================================================
==159287==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:

    #0 0x7fa28d9a2e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x5652f5a5affa in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x5652f5a5ba1f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x5652f5a5c1df in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x5652f5a5c1df in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x5652f5723bbf in test__switch_tracking tests/switch-tracking.c:350
    #6 0x5652f56e18fb in run_test tests/builtin-test.c:428
    #7 0x5652f56e18fb in test_and_print tests/builtin-test.c:458
    #8 0x5652f56e3a53 in __cmd_test tests/builtin-test.c:679
    #9 0x5652f56e3a53 in cmd_test tests/builtin-test.c:825
    #10 0x5652f574fcc4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x5652f55d9a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x5652f55d9a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x5652f55d9a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7fa28d4d8d09 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s). test child finished with 1
---- end ----
Track with sched_switch: FAILED!

---
 tools/perf/tests/switch-tracking.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/perf/tests/switch-tracking.c +2 -3
index 15a2ab765d89..3ebaa758df77 100644
@@ -574,10 +574,9 @@ int test__switch_tracking(struct test *test __maybe_unused, int subtest __maybe_
 	if (evlist) {
 		evlist__disable(evlist);
 		evlist__delete(evlist);
-	} else {
-		perf_cpu_map__put(cpus);
-		perf_thread_map__put(threads);
 	}
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 
 	return err;
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#12 lore raw

PATCH 8/11 perf test: Fix a thread map leak in thread_map_synthesize test

In reply to Namhyung Kim

It missed to call perf_thread_map__put() after using the map.

  $ perf test -v 43
  43: Synthesize thread map                      :
  --- start ---
  test child forked, pid 162640

=================================================================
==162640==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 32 byte(s) in 1 object(s) allocated from:

    #0 0x7fd48cdaa1f8 in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:164
    #1 0x563e6d5f8d0e in perf_thread_map__realloc /home/namhyung/project/linux/tools/lib/perf/threadmap.c:23
    #2 0x563e6d3ef69a in thread_map__new_by_pid util/thread_map.c:46
    #3 0x563e6d2cec90 in test__thread_map_synthesize tests/thread-map.c:97
    #4 0x563e6d27d8fb in run_test tests/builtin-test.c:428
    #5 0x563e6d27d8fb in test_and_print tests/builtin-test.c:458
    #6 0x563e6d27fa53 in __cmd_test tests/builtin-test.c:679
    #7 0x563e6d27fa53 in cmd_test tests/builtin-test.c:825
    #8 0x563e6d2ebce4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #9 0x563e6d175a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #10 0x563e6d175a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #11 0x563e6d175a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #12 0x7fd48c8dfd09 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 8224 byte(s) leaked in 2 allocation(s). test child finished with 1
---- end ----
Synthesize thread map: FAILED!

---
 tools/perf/tests/thread-map.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/tests/thread-map.c +1 -0
index 28f51c4bd373..9e1cf11149ef 100644
@@ -102,6 +102,7 @@ int test__thread_map_synthesize(struct test *test __maybe_unused, int subtest __
 	TEST_ASSERT_VAL("failed to synthesize map",
 		!perf_event__synthesize_thread_map2(NULL, threads, process_event, NULL));
 
+	perf_thread_map__put(threads);
 	return 0;
 }
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#13 lore raw

PATCH 9/11 perf test: Fix a memory leak in thread_map_remove test

In reply to Namhyung Kim

The str should be freed after creating a thread map. Also change the open-coded thread map deletion to a call to perf_thread_map__put().

  $ perf test -v 44
  44: Remove thread map                          :
  --- start ---
  test child forked, pid 165536
  2 threads: 165535, 165536
  1 thread: 165536
  0 thread:

=================================================================
==165536==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 14 byte(s) in 1 object(s) allocated from:

    #0 0x7f54453ffe8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x7f5444f8c6a7 in __vasprintf_internal libio/vasprintf.c:71

SUMMARY: AddressSanitizer: 14 byte(s) leaked in 1 allocation(s). test child finished with 1
---- end ----
Remove thread map: FAILED!

---
 tools/perf/tests/thread-map.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
tools/perf/tests/thread-map.c +2 -5
index 9e1cf11149ef..d1e208b4a571 100644
@@ -110,12 +110,12 @@ int test__thread_map_remove(struct test *test __maybe_unused, int subtest __mayb
 {
 	struct perf_thread_map *threads;
 	char *str;
-	int i;
 
 	TEST_ASSERT_VAL("failed to allocate map string",
 			asprintf(&str, "%d,%d", getpid(), getppid()) >= 0);
 
 	threads = thread_map__new_str(str, NULL, 0, false);
+	free(str);
 
 	TEST_ASSERT_VAL("failed to allocate thread_map",
 			threads);
@@ -142,9 +142,6 @@ int test__thread_map_remove(struct test *test __maybe_unused, int subtest __mayb
 	TEST_ASSERT_VAL("failed to not remove thread",
 			thread_map__remove(threads, 0));
 
-	for (i = 0; i < threads->nr; i++)
-		zfree(&threads->map[i].comm);
-
-	free(threads);
+	perf_thread_map__put(threads);
 	return 0;
 }
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#14 lore raw

PATCH 10/11 perf test: Fix cpu map leaks in cpu_map_print test

In reply to Namhyung Kim

It should be released after printing the map.

  $ perf test -v 52
  52: Print cpu map                              :
  --- start ---
  test child forked, pid 172233

=================================================================
==172233==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 156 byte(s) in 1 object(s) allocated from:

    #0 0x7fc472518e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55e63b378f7a in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55e63b37a05c in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:237
    #3 0x55e63b056d16 in cpu_map_print tests/cpumap.c:102
    #4 0x55e63b056d16 in test__cpu_map_print tests/cpumap.c:120
    #5 0x55e63afff8fb in run_test tests/builtin-test.c:428
    #6 0x55e63afff8fb in test_and_print tests/builtin-test.c:458
    #7 0x55e63b001a53 in __cmd_test tests/builtin-test.c:679
    #8 0x55e63b001a53 in cmd_test tests/builtin-test.c:825
    #9 0x55e63b06dc44 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #10 0x55e63aef7a88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #11 0x55e63aef7a88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #12 0x55e63aef7a88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #13 0x7fc47204ed09 in __libc_start_main ../csu/libc-start.c:308

...

SUMMARY: AddressSanitizer: 448 byte(s) leaked in 7 allocation(s). test child finished with 1
---- end ----
Print cpu map: FAILED!

---
 tools/perf/tests/cpumap.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/tests/cpumap.c +2 -0
index 29c793ac7d10..0472b110fe65 100644
@@ -106,6 +106,8 @@ static int cpu_map_print(const char *str)
 		return -1;
 
 	cpu_map__snprint(map, buf, sizeof(buf));
+	perf_cpu_map__put(map);
+
 	return !strcmp(buf, str);
 }
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
author maintainer
#15 lore raw

PATCH 11/11 perf test: Fix cpu and thread map leaks in perf_time_to_tsc test

In reply to Namhyung Kim

It should release the maps at the end.

  $ perf test -v 71
  71: Convert perf time to TSC                   :
  --- start ---
  test child forked, pid 178744
  mmap size 528384B
  1st event perf time 59207256505278 tsc 13187166645142
  rdtsc          time 59207256542151 tsc 13187166723020
  2nd event perf time 59207256543749 tsc 13187166726393

=================================================================
==178744==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:

    #0 0x7faf601f9e8f in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145
    #1 0x55b620cfc00a in cpu_map__trim_new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:79
    #2 0x55b620cfca2f in perf_cpu_map__read /home/namhyung/project/linux/tools/lib/perf/cpumap.c:149
    #3 0x55b620cfd1ef in cpu_map__read_all_cpu_map /home/namhyung/project/linux/tools/lib/perf/cpumap.c:166
    #4 0x55b620cfd1ef in perf_cpu_map__new /home/namhyung/project/linux/tools/lib/perf/cpumap.c:181
    #5 0x55b6209ef1b2 in test__perf_time_to_tsc tests/perf-time-to-tsc.c:73
    #6 0x55b6209828fb in run_test tests/builtin-test.c:428
    #7 0x55b6209828fb in test_and_print tests/builtin-test.c:458
    #8 0x55b620984a53 in __cmd_test tests/builtin-test.c:679
    #9 0x55b620984a53 in cmd_test tests/builtin-test.c:825
    #10 0x55b6209f0cd4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313
    #11 0x55b62087aa88 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365
    #12 0x55b62087aa88 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409
    #13 0x55b62087aa88 in main /home/namhyung/project/linux/tools/perf/perf.c:539
    #14 0x7faf5fd2fd09 in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 72 byte(s) leaked in 2 allocation(s). test child finished with 1
---- end ----
Convert perf time to TSC: FAILED!

---
 tools/perf/tests/perf-time-to-tsc.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/tests/perf-time-to-tsc.c +2 -0
index 7cff02664d0e..680c3cffb128 100644
@@ -167,6 +167,8 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybe
 
 out_err:
 	evlist__delete(evlist);
+	perf_cpu_map__put(cpus);
+	perf_thread_map__put(threads);
 	return err;
 }
 
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog