diff options
Diffstat (limited to 'tools/perf/builtin-inject.c')
| -rw-r--r-- | tools/perf/builtin-inject.c | 116 | 
1 files changed, 56 insertions, 60 deletions
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 423875c999b..16c7c11ad06 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -15,20 +15,20 @@  #include "util/tool.h"  #include "util/debug.h"  #include "util/build-id.h" +#include "util/data.h"  #include "util/parse-options.h"  #include <linux/list.h>  struct perf_inject { -	struct perf_tool tool; -	bool		 build_ids; -	bool		 sched_stat; -	const char	 *input_name; -	int		 pipe_output, -			 output; -	u64		 bytes_written; -	struct list_head samples; +	struct perf_tool	tool; +	bool			build_ids; +	bool			sched_stat; +	const char		*input_name; +	struct perf_data_file	output; +	u64			bytes_written; +	struct list_head	samples;  };  struct event_entry { @@ -41,21 +41,14 @@ static int perf_event__repipe_synth(struct perf_tool *tool,  				    union perf_event *event)  {  	struct perf_inject *inject = container_of(tool, struct perf_inject, tool); -	uint32_t size; -	void *buf = event; +	ssize_t size; -	size = event->header.size; - -	while (size) { -		int ret = write(inject->output, buf, size); -		if (ret < 0) -			return -errno; - -		size -= ret; -		buf += ret; -		inject->bytes_written += ret; -	} +	size = perf_data_file__write(&inject->output, event, +				     event->header.size); +	if (size < 0) +		return -errno; +	inject->bytes_written += size;  	return 0;  } @@ -71,12 +64,17 @@ static int perf_event__repipe_attr(struct perf_tool *tool,  				   union perf_event *event,  				   struct perf_evlist **pevlist)  { +	struct perf_inject *inject = container_of(tool, struct perf_inject, +						  tool);  	int ret;  	ret = perf_event__process_attr(tool, event, pevlist);  	if (ret)  		return ret; +	if (!inject->output.is_pipe) +		return 0; +  	return perf_event__repipe_synth(tool, event);  } @@ -100,8 +98,8 @@ static int perf_event__repipe_sample(struct perf_tool *tool,  				     struct perf_evsel *evsel,  				     struct machine *machine)  { -	if (evsel->handler.func) { -		inject_handler f = evsel->handler.func; +	if (evsel->handler) { +		inject_handler f = evsel->handler;  		return f(tool, event, sample, evsel, machine);  	} @@ -161,38 +159,38 @@ static int perf_event__repipe_tracing_data(struct perf_tool *tool,  	return err;  } -static int dso__read_build_id(struct dso *self) +static int dso__read_build_id(struct dso *dso)  { -	if (self->has_build_id) +	if (dso->has_build_id)  		return 0; -	if (filename__read_build_id(self->long_name, self->build_id, -				    sizeof(self->build_id)) > 0) { -		self->has_build_id = true; +	if (filename__read_build_id(dso->long_name, dso->build_id, +				    sizeof(dso->build_id)) > 0) { +		dso->has_build_id = true;  		return 0;  	}  	return -1;  } -static int dso__inject_build_id(struct dso *self, struct perf_tool *tool, +static int dso__inject_build_id(struct dso *dso, struct perf_tool *tool,  				struct machine *machine)  {  	u16 misc = PERF_RECORD_MISC_USER;  	int err; -	if (dso__read_build_id(self) < 0) { -		pr_debug("no build_id found for %s\n", self->long_name); +	if (dso__read_build_id(dso) < 0) { +		pr_debug("no build_id found for %s\n", dso->long_name);  		return -1;  	} -	if (self->kernel) +	if (dso->kernel)  		misc = PERF_RECORD_MISC_KERNEL; -	err = perf_event__synthesize_build_id(tool, self, misc, perf_event__repipe, +	err = perf_event__synthesize_build_id(tool, dso, misc, perf_event__repipe,  					      machine);  	if (err) { -		pr_err("Can't synthesize build_id event for %s\n", self->long_name); +		pr_err("Can't synthesize build_id event for %s\n", dso->long_name);  		return -1;  	} @@ -211,7 +209,7 @@ static int perf_event__inject_buildid(struct perf_tool *tool,  	cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; -	thread = machine__findnew_thread(machine, sample->pid, sample->pid); +	thread = machine__findnew_thread(machine, sample->pid, sample->tid);  	if (thread == NULL) {  		pr_err("problem processing %d event, skipping it.\n",  		       event->header.type); @@ -231,7 +229,7 @@ static int perf_event__inject_buildid(struct perf_tool *tool,  				 * account this as unresolved.  				 */  			} else { -#ifdef LIBELF_SUPPORT +#ifdef HAVE_LIBELF_SUPPORT  				pr_warning("no symbols found in %s, maybe "  					   "install a debug package?\n",  					   al.map->dso->long_name); @@ -314,15 +312,12 @@ found:  	sample_sw.period = sample->period;  	sample_sw.time	 = sample->time;  	perf_event__synthesize_sample(event_sw, evsel->attr.sample_type, -				      evsel->attr.sample_regs_user,  				      evsel->attr.read_format, &sample_sw,  				      false);  	build_id__mark_dso_hit(tool, event_sw, &sample_sw, evsel, machine);  	return perf_event__repipe(tool, event_sw, &sample_sw, machine);  } -extern volatile int session_done; -  static void sig_handler(int sig __maybe_unused)  {  	session_done = 1; @@ -347,6 +342,11 @@ static int __cmd_inject(struct perf_inject *inject)  {  	struct perf_session *session;  	int ret = -EINVAL; +	struct perf_data_file file = { +		.path = inject->input_name, +		.mode = PERF_DATA_MODE_READ, +	}; +	struct perf_data_file *file_out = &inject->output;  	signal(SIGINT, sig_handler); @@ -357,7 +357,7 @@ static int __cmd_inject(struct perf_inject *inject)  		inject->tool.tracing_data = perf_event__repipe_tracing_data;  	} -	session = perf_session__new(inject->input_name, O_RDONLY, false, true, &inject->tool); +	session = perf_session__new(&file, true, &inject->tool);  	if (session == NULL)  		return -ENOMEM; @@ -368,29 +368,29 @@ static int __cmd_inject(struct perf_inject *inject)  		inject->tool.ordered_samples = true; -		list_for_each_entry(evsel, &session->evlist->entries, node) { +		evlist__for_each(session->evlist, evsel) {  			const char *name = perf_evsel__name(evsel);  			if (!strcmp(name, "sched:sched_switch")) {  				if (perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID"))  					return -EINVAL; -				evsel->handler.func = perf_inject__sched_switch; +				evsel->handler = perf_inject__sched_switch;  			} else if (!strcmp(name, "sched:sched_process_exit")) -				evsel->handler.func = perf_inject__sched_process_exit; +				evsel->handler = perf_inject__sched_process_exit;  			else if (!strncmp(name, "sched:sched_stat_", 17)) -				evsel->handler.func = perf_inject__sched_stat; +				evsel->handler = perf_inject__sched_stat;  		}  	} -	if (!inject->pipe_output) -		lseek(inject->output, session->header.data_offset, SEEK_SET); +	if (!file_out->is_pipe) +		lseek(file_out->fd, session->header.data_offset, SEEK_SET);  	ret = perf_session__process_events(session, &inject->tool); -	if (!inject->pipe_output) { +	if (!file_out->is_pipe) {  		session->header.data_size = inject->bytes_written; -		perf_session__write_header(session, session->evlist, inject->output, true); +		perf_session__write_header(session, session->evlist, file_out->fd, true);  	}  	perf_session__delete(session); @@ -419,14 +419,17 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)  		},  		.input_name  = "-",  		.samples = LIST_HEAD_INIT(inject.samples), +		.output = { +			.path = "-", +			.mode = PERF_DATA_MODE_WRITE, +		},  	}; -	const char *output_name = "-";  	const struct option options[] = {  		OPT_BOOLEAN('b', "build-ids", &inject.build_ids,  			    "Inject build-ids into the output stream"),  		OPT_STRING('i', "input", &inject.input_name, "file",  			   "input file name"), -		OPT_STRING('o', "output", &output_name, "file", +		OPT_STRING('o', "output", &inject.output.path, "file",  			   "output file name"),  		OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,  			    "Merge sched-stat and sched-switch for getting events " @@ -448,16 +451,9 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)  	if (argc)  		usage_with_options(inject_usage, options); -	if (!strcmp(output_name, "-")) { -		inject.pipe_output = 1; -		inject.output = STDOUT_FILENO; -	} else { -		inject.output = open(output_name, O_CREAT | O_WRONLY | O_TRUNC, -						  S_IRUSR | S_IWUSR); -		if (inject.output < 0) { -			perror("failed to create output file"); -			return -1; -		} +	if (perf_data_file__open(&inject.output)) { +		perror("failed to create output file"); +		return -1;  	}  	if (symbol__init() < 0)  | 
