Skip to content

Commit 5a15635

Browse files
captain5050namhyung
authored andcommitted
perf test: Avoid use perf_env
The perf_env global variable holds the host perf_env data but its use is hit and miss. Switch to using local perf_env variables and ensure scoped perf_env__init and perf_env__exit. This loses command line setting of the perf_env, but this doesn't matter for tests. So the perf_env is fully initialized, clear it with memset in perf_env__init. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent b743a13 commit 5a15635

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

tools/perf/tests/code-reading.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,13 @@ static int do_test_code_reading(bool try_kcore)
651651
struct dso *dso;
652652
const char *events[] = { "cycles", "cycles:u", "cpu-clock", "cpu-clock:u", NULL };
653653
int evidx = 0;
654+
struct perf_env host_env;
654655

655656
pid = getpid();
656657

657658
machine = machine__new_host();
658-
machine->env = &perf_env;
659+
perf_env__init(&host_env);
660+
machine->env = &host_env;
659661

660662
ret = machine__create_kernel_maps(machine);
661663
if (ret < 0) {
@@ -791,6 +793,7 @@ static int do_test_code_reading(bool try_kcore)
791793
perf_cpu_map__put(cpus);
792794
perf_thread_map__put(threads);
793795
machine__delete(machine);
796+
perf_env__exit(&host_env);
794797

795798
return err;
796799
}

tools/perf/tests/dlfilter-test.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,12 @@ static int run_perf_script(struct test_data *td)
319319

320320
static int test__dlfilter_test(struct test_data *td)
321321
{
322+
struct perf_env host_env;
322323
u64 sample_type = TEST_SAMPLE_TYPE;
323324
pid_t pid = 12345;
324325
pid_t tid = 12346;
325326
u64 id = 99;
326-
int err;
327+
int err = TEST_OK;
327328

328329
if (get_dlfilters_path(td->name, td->dlfilters, PATH_MAX))
329330
return test_result("dlfilters not found", TEST_SKIP);
@@ -353,37 +354,42 @@ static int test__dlfilter_test(struct test_data *td)
353354

354355
pr_debug("Creating new host machine structure\n");
355356
td->machine = machine__new_host();
356-
td->machine->env = &perf_env;
357+
perf_env__init(&host_env);
358+
td->machine->env = &host_env;
357359

358360
td->fd = creat(td->perf_data_file_name, 0644);
359361
if (td->fd < 0)
360362
return test_result("Failed to create test perf.data file", TEST_FAIL);
361363

362364
err = perf_header__write_pipe(td->fd);
363-
if (err < 0)
364-
return test_result("perf_header__write_pipe() failed", TEST_FAIL);
365-
365+
if (err < 0) {
366+
err = test_result("perf_header__write_pipe() failed", TEST_FAIL);
367+
goto out;
368+
}
366369
err = write_attr(td, sample_type, &id);
367-
if (err)
368-
return test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
369-
370-
if (write_comm(td->fd, pid, tid, "test-prog"))
371-
return TEST_FAIL;
372-
373-
if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name))
374-
return TEST_FAIL;
375-
376-
if (write_sample(td, sample_type, id, pid, tid) != TEST_OK)
377-
return TEST_FAIL;
378-
370+
if (err) {
371+
err = test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
372+
goto out;
373+
}
374+
if (write_comm(td->fd, pid, tid, "test-prog")) {
375+
err = TEST_FAIL;
376+
goto out;
377+
}
378+
if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name)) {
379+
err = TEST_FAIL;
380+
goto out;
381+
}
382+
if (write_sample(td, sample_type, id, pid, tid) != TEST_OK) {
383+
err = TEST_FAIL;
384+
goto out;
385+
}
379386
if (verbose > 1)
380387
system_cmd("%s script -i %s -D", td->perf, td->perf_data_file_name);
381388

382-
err = run_perf_script(td);
383-
if (err)
384-
return TEST_FAIL;
385-
386-
return TEST_OK;
389+
err = run_perf_script(td) ? TEST_FAIL : TEST_OK;
390+
out:
391+
perf_env__exit(&host_env);
392+
return err;
387393
}
388394

389395
static void unlink_path(const char *path)

tools/perf/util/env.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ void perf_env__exit(struct perf_env *env)
271271

272272
void perf_env__init(struct perf_env *env)
273273
{
274+
memset(env, 0, sizeof(*env));
274275
#ifdef HAVE_LIBBPF_SUPPORT
275276
env->bpf_progs.infos = RB_ROOT;
276277
env->bpf_progs.btfs = RB_ROOT;

0 commit comments

Comments
 (0)