Lines Matching full:inject

3  * builtin-inject.c
5 * Builtin inject command: Examine the live mode (stdin) event stream
162 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
166 size = perf_data__write(&inject->output, buf, sz);
170 inject->bytes_written += size;
178 struct perf_inject *inject = container_of(tool, struct perf_inject,
181 return output_bytes(inject, event, event->header.size);
218 struct perf_inject *inject = container_of(tool, struct perf_inject,
227 if (!inject->output.is_pipe)
242 static int copy_bytes(struct perf_inject *inject, struct perf_data *data, off_t size)
252 ret = output_bytes(inject, buf, ssz);
265 struct perf_inject *inject = container_of(tool, struct perf_inject,
269 inject->have_auxtrace = true;
271 if (!inject->output.is_pipe) {
274 offset = lseek(inject->output.file.fd, 0, SEEK_CUR);
284 ret = output_bytes(inject, event, event->header.size);
287 ret = copy_bytes(inject, session->data,
290 ret = output_bytes(inject, event,
332 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
334 if (!inject->aux_id)
335 inject->aux_id = sample->id;
341 perf_inject__cut_auxtrace_sample(struct perf_inject *inject,
349 if (inject->event_copy == NULL) {
350 inject->event_copy = malloc(PERF_SAMPLE_MAX_SIZE);
351 if (!inject->event_copy)
354 ev = (union perf_event *)inject->event_copy;
380 struct perf_inject *inject = container_of(tool, struct perf_inject,
390 if (inject->itrace_synth_opts.set && sample->aux_sample.size) {
391 event = perf_inject__cut_auxtrace_sample(inject, event, sample);
449 static const struct evsel *inject__mmap_evsel(struct perf_inject *inject)
453 if (inject->mmap_evsel)
454 return inject->mmap_evsel;
456 evlist__for_each_entry(inject->session->evlist, pos) {
458 inject->mmap_evsel = pos;
480 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
485 if (inject->jit_mode) {
489 /* If jit marker, then inject jit mmaps and generate ELF images. */
490 ret = jit_process(inject->session, &inject->output, machine,
495 inject->bytes_written += n;
504 /* mark it not to inject build-id */
508 if (inject->build_id_style == BID_RWS__INJECT_HEADER_ALL) {
515 struct evsel *evsel = evlist__event2evsel(inject->session->evlist, event);
531 if ((inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
532 inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) &&
533 !inject->mmap_evsel)
534 inject->mmap_evsel = evlist__event2evsel(inject->session->evlist, event);
536 /* Create the thread, map, etc. Not done for the unordered inject all case. */
544 if ((inject->build_id_style == BID_RWS__MMAP2_BUILDID_ALL) &&
546 struct evsel *evsel = evlist__event2evsel(inject->session->evlist, event);
565 if (inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY)
731 static bool perf_inject__lookup_known_build_id(struct perf_inject *inject,
737 strlist__for_each_entry(pos, inject->known_build_ids) {
765 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
773 if (inject->known_build_ids != NULL &&
774 perf_inject__lookup_known_build_id(inject, dso))
833 static int mark_dso_hit(const struct perf_inject *inject,
863 if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY) {
870 } else if (inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
895 const struct perf_inject *inject;
907 return mark_dso_hit(args->inject, args->tool, args->sample, args->machine,
918 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
920 .inject = inject,
928 .mmap_evsel = inject__mmap_evsel(inject),
940 mark_dso_hit(inject, tool, sample, machine, args.mmap_evsel, al.map,
960 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
963 list_for_each_entry(ent, &inject->samples, node) {
980 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
994 list_add(&ent->node, &inject->samples);
1008 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1011 list_for_each_entry(ent, &inject->samples, node) {
1214 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1216 return perf_event__process_attr(tool, event, &inject->session->evlist);
1221 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
1253 ret = perf_event__synthesize_attr(&inject->tool, &attr, evsel->core.ids,
1262 sid = evlist__id2sid(inject->session->evlist, id_array[i]);
1290 static int synthesize_id_index(struct perf_inject *inject, size_t new_cnt)
1292 struct perf_session *session = inject->session;
1297 return __perf_event__synthesize_id_index(&inject->tool, perf_event__repipe,
1343 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1345 return perf_event__process_build_id(inject->session, event);
1348 static int synthesize_build_id(struct perf_inject *inject, struct dso *dso, pid_t machine_pid)
1350 struct machine *machine = perf_session__findnew_machine(inject->session, machine_pid);
1368 return perf_event__synthesize_build_id(&inject->tool, &synth_sample, machine,
1369 process_build_id, inject__mmap_evsel(inject),
1377 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
1382 return synthesize_build_id(inject, dso, gs->machine_pid);
1388 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
1391 perf_header__set_feat(&inject->session->header, HEADER_BUILD_ID);
1414 char tmp_file_name[] = "/tmp/perf-inject-guest_session-XXXXXX";
1531 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
1533 get_tsc_conv(&gs->host_tc, &inject->session->time_conv);
1651 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
1740 ret = evlist__append_id_sample(inject->session->evlist, ev, sample);
1751 ret = output_bytes(inject, ev, ev->header.size);
1767 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1770 ret = guest_session__inject_events(&inject->guest_session, sample->time);
1779 struct perf_inject *inject = container_of(session->tool, struct perf_inject, tool);
1780 struct guest_session *gs = &inject->guest_session;
1808 ret = synthesize_id_index(inject, gs->session->evlist->core.nr_entries);
1831 * which flushes host events to file up until the last flush time. Then inject
1839 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1846 ret = guest_session__inject_events(&inject->guest_session, timestamp);
1858 struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
1860 struct guest_session *gs = &inject->guest_session;
1916 static void strip_init(struct perf_inject *inject)
1918 struct evlist *evlist = inject->session->evlist;
1921 inject->tool.context_switch = perf_event__drop;
1929 struct perf_inject *inject = opt->value;
1936 inject->itrace_synth_opts.set = true;
1937 inject->itrace_synth_opts.vm_time_correlation = true;
1938 inject->in_place_update = true;
1945 inject->itrace_synth_opts.vm_tm_corr_dry_run = true;
1946 inject->in_place_update_dry_run = true;
1952 inject->itrace_synth_opts.vm_tm_corr_args = strdup(args);
1954 return inject->itrace_synth_opts.vm_tm_corr_args ? 0 : -ENOMEM;
1959 struct perf_inject *inject = opt->value;
1960 struct guest_session *gs = &inject->guest_session;
1980 inject->output.is_dir = true;
1986 if (!inject->guest_session.machine_pid)
2016 struct perf_inject *inject = data;
2018 inject->secs[feat] = *section;
2022 static int save_section_info(struct perf_inject *inject)
2024 struct perf_header *header = &inject->session->header;
2025 int fd = perf_data__fd(inject->session->data);
2027 return perf_header__process_sections(header, fd, inject, save_section_info_cb);
2083 static int feat_copy(struct perf_inject *inject, int feat, struct feat_writer *fw)
2085 int fd = perf_data__fd(inject->session->data);
2086 u64 offs = inject->secs[feat].offset;
2087 size_t sz = inject->secs[feat].size;
2106 struct perf_inject *inject;
2112 struct perf_inject *inject = inj_fc->inject;
2115 if (!inject->secs[feat].offset ||
2119 ret = feat_copy(inject, feat, fw);
2126 static int copy_kcore_dir(struct perf_inject *inject)
2132 inject->input_name, inject->output.path);
2143 struct perf_inject *inject = container_of(gs, struct perf_inject, guest_session);
2148 gs->perf_data_file, inject->output.path, gs->machine_pid);
2157 static int output_fd(struct perf_inject *inject)
2159 return inject->in_place_update ? -1 : perf_data__fd(&inject->output);
2162 static int __cmd_inject(struct perf_inject *inject)
2165 struct guest_session *gs = &inject->guest_session;
2166 struct perf_session *session = inject->session;
2167 int fd = output_fd(inject);
2174 bool write_attrs_after_data = !inject->output.is_pipe && inject->session->data->is_pipe;
2178 if (inject->build_id_style != BID_RWS__NONE || inject->sched_stat ||
2179 inject->itrace_synth_opts.set) {
2180 inject->tool.mmap = perf_event__repipe_mmap;
2181 inject->tool.mmap2 = perf_event__repipe_mmap2;
2182 inject->tool.fork = perf_event__repipe_fork;
2184 inject->tool.tracing_data = perf_event__repipe_tracing_data;
2188 if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
2189 inject->build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
2190 inject->tool.sample = perf_event__inject_buildid;
2191 } else if (inject->sched_stat) {
2209 } else if (inject->itrace_synth_opts.vm_time_correlation) {
2210 session->itrace_synth_opts = &inject->itrace_synth_opts;
2211 memset(&inject->tool, 0, sizeof(inject->tool));
2212 inject->tool.id_index = perf_event__process_id_index;
2213 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
2214 inject->tool.auxtrace = perf_event__process_auxtrace;
2215 inject->tool.auxtrace_error = perf_event__process_auxtrace_error;
2216 inject->tool.ordered_events = true;
2217 inject->tool.ordering_requires_timestamps = true;
2218 } else if (inject->itrace_synth_opts.set) {
2219 session->itrace_synth_opts = &inject->itrace_synth_opts;
2220 inject->itrace_synth_opts.inject = true;
2221 inject->tool.comm = perf_event__repipe_comm;
2222 inject->tool.namespaces = perf_event__repipe_namespaces;
2223 inject->tool.exit = perf_event__repipe_exit;
2224 inject->tool.id_index = perf_event__process_id_index;
2225 inject->tool.auxtrace_info = perf_event__process_auxtrace_info;
2226 inject->tool.auxtrace = perf_event__process_auxtrace;
2227 inject->tool.aux = perf_event__drop_aux;
2228 inject->tool.itrace_start = perf_event__drop_aux;
2229 inject->tool.aux_output_hw_id = perf_event__drop_aux;
2230 inject->tool.ordered_events = true;
2231 inject->tool.ordering_requires_timestamps = true;
2234 if (inject->strip)
2235 strip_init(inject);
2243 inject->tool.mmap = host__repipe;
2244 inject->tool.mmap2 = host__repipe;
2245 inject->tool.comm = host__repipe;
2246 inject->tool.fork = host__repipe;
2247 inject->tool.exit = host__repipe;
2248 inject->tool.lost = host__repipe;
2249 inject->tool.context_switch = host__repipe;
2250 inject->tool.ksymbol = host__repipe;
2251 inject->tool.text_poke = host__repipe;
2257 inject->tool.finished_init = host__finished_init;
2259 inject->tool.finished_round = host__finished_round;
2261 inject->tool.context_switch = host__context_switch;
2266 inject->tool.ordered_events = true;
2267 inject->tool.ordering_requires_timestamps = true;
2279 if (!inject->itrace_synth_opts.set)
2282 if (!inject->output.is_pipe && !inject->in_place_update)
2301 if (!inject->output.is_pipe && !inject->in_place_update) {
2304 .inject = inject,
2307 if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
2308 inject->build_id_style == BID_RWS__INJECT_HEADER_ALL)
2315 inject->have_auxtrace && !inject->itrace_synth_opts.set)
2321 if (inject->itrace_synth_opts.set) {
2324 if (inject->itrace_synth_opts.last_branch ||
2325 inject->itrace_synth_opts.add_last_branch)
2330 session->header.data_size = inject->bytes_written;
2334 if (inject->copy_kcore_dir) {
2335 ret = copy_kcore_dir(inject);
2355 struct perf_inject inject = {
2357 .samples = LIST_HEAD_INIT(inject.samples),
2377 "Inject build-ids into the output stream"),
2379 "Inject build-ids of all DSOs into the output stream"),
2387 OPT_STRING('i', "input", &inject.input_name, "file",
2389 OPT_STRING('o', "output", &inject.output.path, "file",
2391 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat,
2395 OPT_BOOLEAN('j', "jit", &inject.jit_mode, "merge jitdump files into perf.data file"),
2406 OPT_CALLBACK_OPTARG(0, "itrace", &inject.itrace_synth_opts,
2410 OPT_BOOLEAN(0, "strip", &inject.strip,
2412 OPT_CALLBACK_OPTARG(0, "vm-time-correlation", &inject, NULL, "opts",
2415 OPT_CALLBACK_OPTARG(0, "guest-data", &inject, NULL, "opts",
2416 "inject events from a guest perf.data file",
2424 "perf inject [<options>]",
2429 if (!inject.itrace_synth_opts.set) {
2430 /* Disable eager loading of kernel symbols that adds overhead to perf inject. */
2445 if (inject.strip && !inject.itrace_synth_opts.set) {
2453 if (inject.in_place_update) {
2454 if (!strcmp(inject.input_name, "-")) {
2458 if (strcmp(inject.output.path, "-")) {
2462 if (!data.force && !inject.in_place_update_dry_run) {
2467 if (!inject.in_place_update_dry_run)
2470 if (strcmp(inject.output.path, "-") && !inject.strip &&
2471 has_kcore_dir(inject.input_name)) {
2472 inject.output.is_dir = true;
2473 inject.copy_kcore_dir = true;
2475 if (perf_data__open(&inject.output)) {
2481 inject.build_id_style = BID_RWS__MMAP2_BUILDID_LAZY;
2483 inject.build_id_style = BID_RWS__MMAP2_BUILDID_ALL;
2485 inject.build_id_style = BID_RWS__INJECT_HEADER_LAZY;
2487 inject.build_id_style = BID_RWS__INJECT_HEADER_ALL;
2489 data.path = inject.input_name;
2491 ordered_events = inject.jit_mode || inject.sched_stat ||
2492 inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
2493 inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY;
2494 perf_tool__init(&inject.tool, ordered_events);
2495 inject.tool.sample = perf_event__repipe_sample;
2496 inject.tool.read = perf_event__repipe_sample;
2497 inject.tool.mmap = perf_event__repipe;
2498 inject.tool.mmap2 = perf_event__repipe;
2499 inject.tool.comm = perf_event__repipe;
2500 inject.tool.namespaces = perf_event__repipe;
2501 inject.tool.cgroup = perf_event__repipe;
2502 inject.tool.fork = perf_event__repipe;
2503 inject.tool.exit = perf_event__repipe;
2504 inject.tool.lost = perf_event__repipe;
2505 inject.tool.lost_samples = perf_event__repipe;
2506 inject.tool.aux = perf_event__repipe;
2507 inject.tool.itrace_start = perf_event__repipe;
2508 inject.tool.aux_output_hw_id = perf_event__repipe;
2509 inject.tool.context_switch = perf_event__repipe;
2510 inject.tool.throttle = perf_event__repipe;
2511 inject.tool.unthrottle = perf_event__repipe;
2512 inject.tool.ksymbol = perf_event__repipe;
2513 inject.tool.bpf = perf_event__repipe;
2514 inject.tool.text_poke = perf_event__repipe;
2515 inject.tool.attr = perf_event__repipe_attr;
2516 inject.tool.event_update = perf_event__repipe_event_update;
2517 inject.tool.tracing_data = perf_event__repipe_op2_synth;
2518 inject.tool.finished_round = perf_event__repipe_oe_synth;
2519 inject.tool.build_id = perf_event__repipe_op2_synth;
2520 inject.tool.id_index = perf_event__repipe_op2_synth;
2521 inject.tool.auxtrace_info = perf_event__repipe_op2_synth;
2522 inject.tool.auxtrace_error = perf_event__repipe_op2_synth;
2523 inject.tool.time_conv = perf_event__repipe_op2_synth;
2524 inject.tool.thread_map = perf_event__repipe_op2_synth;
2525 inject.tool.cpu_map = perf_event__repipe_op2_synth;
2526 inject.tool.stat_config = perf_event__repipe_op2_synth;
2527 inject.tool.stat = perf_event__repipe_op2_synth;
2528 inject.tool.stat_round = perf_event__repipe_op2_synth;
2529 inject.tool.feature = perf_event__repipe_op2_synth;
2530 inject.tool.finished_init = perf_event__repipe_op2_synth;
2531 inject.tool.compressed = perf_event__repipe_op4_synth;
2532 inject.tool.auxtrace = perf_event__repipe_auxtrace;
2533 inject.tool.dont_split_sample_group = true;
2534 inject.session = __perf_session__new(&data, &inject.tool,
2535 /*trace_event_repipe=*/inject.output.is_pipe);
2537 if (IS_ERR(inject.session)) {
2538 ret = PTR_ERR(inject.session);
2542 if (zstd_init(&(inject.session->zstd_data), 0) < 0)
2546 ret = save_section_info(&inject);
2550 if (inject.output.is_pipe) {
2551 ret = perf_header__write_pipe(perf_data__fd(&inject.output));
2563 ret = perf_event__synthesize_for_pipe(&inject.tool,
2564 inject.session,
2565 &inject.output,
2572 if (inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
2573 inject.build_id_style == BID_RWS__MMAP2_BUILDID_LAZY) {
2578 * inject the jit mmaps at the same time for now.
2580 inject.tool.ordering_requires_timestamps = true;
2582 if (inject.build_id_style != BID_RWS__NONE && known_build_ids != NULL) {
2583 inject.known_build_ids =
2586 if (inject.known_build_ids == NULL) {
2593 if (inject.jit_mode) {
2594 inject.tool.mmap2 = perf_event__repipe_mmap2;
2595 inject.tool.mmap = perf_event__repipe_mmap;
2596 inject.tool.ordering_requires_timestamps = true;
2601 inject.tool.finished_round = perf_event__drop_oe;
2604 ret = symbol__init(&inject.session->header.env);
2608 ret = __cmd_inject(&inject);
2610 guest_session__exit(&inject.guest_session);
2613 strlist__delete(inject.known_build_ids);
2614 zstd_fini(&(inject.session->zstd_data));
2615 perf_session__delete(inject.session);
2617 if (!inject.in_place_update)
2618 perf_data__close(&inject.output);
2619 free(inject.itrace_synth_opts.vm_tm_corr_args);
2620 free(inject.event_copy);
2621 free(inject.guest_session.ev.event_buf);