All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Yang Jihong <yangjihong1@huawei.com>
Cc: peterz@infradead.org, mingo@redhat.com, namhyung@kernel.org,
	mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
	jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/5] perf sched: Move curr_pid and cpu_last_switched initialization to perf_sched__{lat|map|replay}()
Date: Mon, 5 Feb 2024 16:01:41 -0300	[thread overview]
Message-ID: <ZcEwlR9itdN6tIL1@x1> (raw)
In-Reply-To: <20240205104616.132417-5-yangjihong1@huawei.com>

On Mon, Feb 05, 2024 at 10:46:15AM +0000, Yang Jihong wrote:
> +static int setup_cpus_switch_event(struct perf_sched *sched)
> +{
> +	unsigned int i;
> +
> +	sched->cpu_last_switched = calloc(MAX_CPUS, sizeof(*(sched->cpu_last_switched)));
> +	if (!sched->cpu_last_switched)
> +		return -1;
> +
> +	sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
> +	if (!sched->curr_pid) {
> +		free(sched->cpu_last_switched);

		zfree(&sched->cpu_last_switched);

> +		return -1;
> +	}
> +
> +	for (i = 0; i < MAX_CPUS; i++)
> +		sched->curr_pid[i] = -1;
> +
> +	return 0;
> +}
> +
> +static void free_cpus_switch_event(struct perf_sched *sched)
> +{
> +	free(sched->curr_pid);
> +	free(sched->cpu_last_switched);

	zfree(&sched->curr_pid);
	zfree(&sched->cpu_last_switched);

> +}
> +
>  static int perf_sched__lat(struct perf_sched *sched)
>  {
> +	int rc = -1;
>  	struct rb_node *next;
>  
>  	setup_pager();
>  
> +	if (setup_cpus_switch_event(sched))
> +		return rc;
> +
>  	if (perf_sched__read_events(sched))
> -		return -1;
> +		goto out_free_cpus_switch_event;
>  
>  	perf_sched__merge_lat(sched);
>  	perf_sched__sort_lat(sched);
> @@ -3203,7 +3233,11 @@ static int perf_sched__lat(struct perf_sched *sched)
>  	print_bad_events(sched);
>  	printf("\n");
>  
> -	return 0;
> +	rc = 0;
> +
> +out_free_cpus_switch_event:
> +	free_cpus_switch_event(sched);
> +	return rc;
>  }
>  
>  static int setup_map_cpus(struct perf_sched *sched)
> @@ -3270,9 +3304,12 @@ static int perf_sched__map(struct perf_sched *sched)
>  	if (!sched->curr_thread)
>  		return rc;
>  
> -	if (setup_map_cpus(sched))
> +	if (setup_cpus_switch_event(sched))
>  		goto out_free_curr_thread;
>  
> +	if (setup_map_cpus(sched))
> +		goto out_free_cpus_switch_event;
> +
>  	if (setup_color_pids(sched))
>  		goto out_free_map_cpus;
>  
> @@ -3296,6 +3333,9 @@ static int perf_sched__map(struct perf_sched *sched)
>  	free(sched->map.comp_cpus);
>  	perf_cpu_map__put(sched->map.cpus);
>  
> +out_free_cpus_switch_event:
> +	free_cpus_switch_event(sched);
> +
>  out_free_curr_thread:
>  	free(sched->curr_thread);
>  	return rc;
> @@ -3309,6 +3349,10 @@ static int perf_sched__replay(struct perf_sched *sched)
>  	mutex_init(&sched->start_work_mutex);
>  	mutex_init(&sched->work_done_wait_mutex);
>  
> +	ret = setup_cpus_switch_event(sched);
> +	if (ret)
> +		goto out_mutex_destroy;
> +
>  	calibrate_run_measurement_overhead(sched);
>  	calibrate_sleep_measurement_overhead(sched);
>  
> @@ -3316,7 +3360,7 @@ static int perf_sched__replay(struct perf_sched *sched)
>  
>  	ret = perf_sched__read_events(sched);
>  	if (ret)
> -		goto out_mutex_destroy;
> +		goto out_free_cpus_switch_event;
>  
>  	printf("nr_run_events:        %ld\n", sched->nr_run_events);
>  	printf("nr_sleep_events:      %ld\n", sched->nr_sleep_events);
> @@ -3342,6 +3386,9 @@ static int perf_sched__replay(struct perf_sched *sched)
>  	sched->thread_funcs_exit = true;
>  	destroy_tasks(sched);
>  
> +out_free_cpus_switch_event:
> +	free_cpus_switch_event(sched);
> +
>  out_mutex_destroy:
>  	mutex_destroy(&sched->start_work_mutex);
>  	mutex_destroy(&sched->work_done_wait_mutex);
> @@ -3580,21 +3627,7 @@ int cmd_sched(int argc, const char **argv)
>  		.switch_event	    = replay_switch_event,
>  		.fork_event	    = replay_fork_event,
>  	};
> -	unsigned int i;
> -	int ret = 0;
> -
> -	sched.cpu_last_switched = calloc(MAX_CPUS, sizeof(*sched.cpu_last_switched));
> -	if (!sched.cpu_last_switched) {
> -		ret = -ENOMEM;
> -		goto out;
> -	}
> -	sched.curr_pid = malloc(MAX_CPUS * sizeof(*sched.curr_pid));
> -	if (!sched.curr_pid) {
> -		ret = -ENOMEM;
> -		goto out;
> -	}
> -	for (i = 0; i < MAX_CPUS; i++)
> -		sched.curr_pid[i] = -1;
> +	int ret;
>  
>  	argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
>  					sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
> @@ -3605,9 +3638,9 @@ int cmd_sched(int argc, const char **argv)
>  	 * Aliased to 'perf script' for now:
>  	 */
>  	if (!strcmp(argv[0], "script")) {
> -		ret = cmd_script(argc, argv);
> +		return cmd_script(argc, argv);
>  	} else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
> -		ret = __cmd_record(argc, argv);
> +		return __cmd_record(argc, argv);
>  	} else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) {
>  		sched.tp_handler = &lat_ops;
>  		if (argc > 1) {
> @@ -3616,7 +3649,7 @@ int cmd_sched(int argc, const char **argv)
>  				usage_with_options(latency_usage, latency_options);
>  		}
>  		setup_sorting(&sched, latency_options, latency_usage);
> -		ret = perf_sched__lat(&sched);
> +		return perf_sched__lat(&sched);
>  	} else if (!strcmp(argv[0], "map")) {
>  		if (argc) {
>  			argc = parse_options(argc, argv, map_options, map_usage, 0);
> @@ -3625,7 +3658,7 @@ int cmd_sched(int argc, const char **argv)
>  		}
>  		sched.tp_handler = &map_ops;
>  		setup_sorting(&sched, latency_options, latency_usage);
> -		ret = perf_sched__map(&sched);
> +		return perf_sched__map(&sched);
>  	} else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
>  		sched.tp_handler = &replay_ops;
>  		if (argc) {
> @@ -3633,7 +3666,7 @@ int cmd_sched(int argc, const char **argv)
>  			if (argc)
>  				usage_with_options(replay_usage, replay_options);
>  		}
> -		ret = perf_sched__replay(&sched);
> +		return perf_sched__replay(&sched);
>  	} else if (!strcmp(argv[0], "timehist")) {
>  		if (argc) {
>  			argc = parse_options(argc, argv, timehist_options,
> @@ -3649,21 +3682,16 @@ int cmd_sched(int argc, const char **argv)
>  				parse_options_usage(NULL, timehist_options, "w", true);
>  			if (sched.show_next)
>  				parse_options_usage(NULL, timehist_options, "n", true);
> -			ret = -EINVAL;
> -			goto out;
> +			return -EINVAL;
>  		}
>  		ret = symbol__validate_sym_arguments();
>  		if (ret)
> -			goto out;
> +			return ret;
>  
> -		ret = perf_sched__timehist(&sched);
> +		return perf_sched__timehist(&sched);
>  	} else {
>  		usage_with_options(sched_usage, sched_options);
>  	}
>  
> -out:
> -	free(sched.curr_pid);
> -	free(sched.cpu_last_switched);
> -
> -	return ret;
> +	return 0;
>  }
> -- 
> 2.34.1

  reply	other threads:[~2024-02-05 19:01 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-05 10:46 [PATCH 0/5] perf sched: Minor optimizations for resource initialization Yang Jihong
2024-02-05 10:46 ` [PATCH 1/5] perf sched: Move start_work_mutex and work_done_wait_mutex initialization to perf_sched__replay() Yang Jihong
2024-02-05 10:46 ` [PATCH 2/5] perf sched: Fix memory leak in perf_sched__map() Yang Jihong
2024-02-05 18:58   ` Arnaldo Carvalho de Melo
2024-02-06  7:08     ` Yang Jihong
2024-02-05 10:46 ` [PATCH 3/5] perf sched: Move curr_thread initialization to perf_sched__map() Yang Jihong
2024-02-05 18:59   ` Arnaldo Carvalho de Melo
2024-02-05 10:46 ` [PATCH 4/5] perf sched: Move curr_pid and cpu_last_switched initialization to perf_sched__{lat|map|replay}() Yang Jihong
2024-02-05 19:01   ` Arnaldo Carvalho de Melo [this message]
2024-02-05 10:46 ` [PATCH 5/5] perf thread_map: Free strlist on normal path in thread_map__new_by_tid_str() Yang Jihong
2024-02-05 19:09   ` Arnaldo Carvalho de Melo
2024-02-06  7:10     ` Yang Jihong

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=ZcEwlR9itdN6tIL1@x1 \
    --to=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=yangjihong1@huawei.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.