Lines Matching +full:min +full:- +full:sample +full:- +full:time
1 // SPDX-License-Identifier: GPL-2.0
3 * builtin-inject.c
20 #include "util/build-id.h"
25 #include "util/synthetic-events.h"
30 #include <subcmd/parse-options.h>
68 size = perf_data__write(&inject->output, buf, sz); in output_bytes()
70 return -errno; in output_bytes()
72 inject->bytes_written += size; in output_bytes()
82 return output_bytes(inject, event, event->header.size); in perf_event__repipe_synth()
104 return perf_event__repipe_synth(session->tool, event); in perf_event__repipe_op2_synth()
111 return perf_event__repipe_synth(session->tool, event); in perf_event__repipe_op4_synth()
126 if (!inject->output.is_pipe) in perf_event__repipe_attr()
148 ssz = read(fd, buf, min(size, (off_t)sizeof(buf))); in copy_bytes()
150 return -errno; in copy_bytes()
154 size -= ssz; in copy_bytes()
163 struct perf_tool *tool = session->tool; in perf_event__repipe_auxtrace()
168 inject->have_auxtrace = true; in perf_event__repipe_auxtrace()
170 if (!inject->output.is_pipe) { in perf_event__repipe_auxtrace()
173 offset = lseek(inject->output.file.fd, 0, SEEK_CUR); in perf_event__repipe_auxtrace()
174 if (offset == -1) in perf_event__repipe_auxtrace()
175 return -errno; in perf_event__repipe_auxtrace()
176 ret = auxtrace_index__auxtrace_event(&session->auxtrace_index, in perf_event__repipe_auxtrace()
182 if (perf_data__is_pipe(session->data) || !session->one_mmap) { in perf_event__repipe_auxtrace()
183 ret = output_bytes(inject, event, event->header.size); in perf_event__repipe_auxtrace()
186 ret = copy_bytes(inject, perf_data__fd(session->data), in perf_event__repipe_auxtrace()
187 event->auxtrace.size); in perf_event__repipe_auxtrace()
190 event->header.size + event->auxtrace.size); in perf_event__repipe_auxtrace()
195 return event->auxtrace.size; in perf_event__repipe_auxtrace()
205 return -EINVAL; in perf_event__repipe_auxtrace()
212 struct perf_sample *sample __maybe_unused, in perf_event__repipe()
220 struct perf_sample *sample __maybe_unused, in perf_event__drop()
228 struct perf_sample *sample, in perf_event__drop_aux() argument
233 if (!inject->aux_id) in perf_event__drop_aux()
234 inject->aux_id = sample->id; in perf_event__drop_aux()
242 struct perf_sample *sample) in perf_inject__cut_auxtrace_sample() argument
244 size_t sz1 = sample->aux_sample.data - (void *)event; in perf_inject__cut_auxtrace_sample()
245 size_t sz2 = event->header.size - sample->aux_sample.size - sz1; in perf_inject__cut_auxtrace_sample()
246 union perf_event *ev = (union perf_event *)inject->event_copy; in perf_inject__cut_auxtrace_sample()
248 if (sz1 > event->header.size || sz2 > event->header.size || in perf_inject__cut_auxtrace_sample()
249 sz1 + sz2 > event->header.size || in perf_inject__cut_auxtrace_sample()
254 memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2); in perf_inject__cut_auxtrace_sample()
255 ev->header.size = sz1 + sz2; in perf_inject__cut_auxtrace_sample()
256 ((u64 *)((void *)ev + sz1))[-1] = 0; in perf_inject__cut_auxtrace_sample()
263 struct perf_sample *sample,
269 struct perf_sample *sample, in perf_event__repipe_sample() argument
276 if (evsel && evsel->handler) { in perf_event__repipe_sample()
277 inject_handler f = evsel->handler; in perf_event__repipe_sample()
278 return f(tool, event, sample, evsel, machine); in perf_event__repipe_sample()
281 build_id__mark_dso_hit(tool, event, sample, evsel, machine); in perf_event__repipe_sample()
283 if (inject->itrace_synth_opts.set && sample->aux_sample.size) in perf_event__repipe_sample()
284 event = perf_inject__cut_auxtrace_sample(inject, event, sample); in perf_event__repipe_sample()
291 struct perf_sample *sample, in perf_event__repipe_mmap() argument
296 err = perf_event__process_mmap(tool, event, sample, machine); in perf_event__repipe_mmap()
297 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_mmap()
305 struct perf_sample *sample, in perf_event__jit_repipe_mmap() argument
315 ret = jit_process(inject->session, &inject->output, machine, in perf_event__jit_repipe_mmap()
316 event->mmap.filename, event->mmap.pid, &n); in perf_event__jit_repipe_mmap()
320 inject->bytes_written += n; in perf_event__jit_repipe_mmap()
323 return perf_event__repipe_mmap(tool, event, sample, machine); in perf_event__jit_repipe_mmap()
343 nsi = nsinfo__get(thread->nsinfo); in findnew_dso()
353 nnsi->need_setns = false; in findnew_dso()
362 dso->nsinfo = nsi; in findnew_dso()
372 struct perf_sample *sample, in perf_event__repipe_buildid_mmap() argument
377 dso = findnew_dso(event->mmap.pid, event->mmap.tid, in perf_event__repipe_buildid_mmap()
378 event->mmap.filename, NULL, machine); in perf_event__repipe_buildid_mmap()
380 if (dso && !dso->hit) { in perf_event__repipe_buildid_mmap()
381 dso->hit = 1; in perf_event__repipe_buildid_mmap()
382 dso__inject_build_id(dso, tool, machine, sample->cpumode, 0); in perf_event__repipe_buildid_mmap()
386 return perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_buildid_mmap()
391 struct perf_sample *sample, in perf_event__repipe_mmap2() argument
396 err = perf_event__process_mmap2(tool, event, sample, machine); in perf_event__repipe_mmap2()
397 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_mmap2()
405 struct perf_sample *sample, in perf_event__jit_repipe_mmap2() argument
415 ret = jit_process(inject->session, &inject->output, machine, in perf_event__jit_repipe_mmap2()
416 event->mmap2.filename, event->mmap2.pid, &n); in perf_event__jit_repipe_mmap2()
420 inject->bytes_written += n; in perf_event__jit_repipe_mmap2()
423 return perf_event__repipe_mmap2(tool, event, sample, machine); in perf_event__jit_repipe_mmap2()
429 struct perf_sample *sample, in perf_event__repipe_buildid_mmap2() argument
433 .maj = event->mmap2.maj, in perf_event__repipe_buildid_mmap2()
434 .min = event->mmap2.min, in perf_event__repipe_buildid_mmap2()
435 .ino = event->mmap2.ino, in perf_event__repipe_buildid_mmap2()
436 .ino_generation = event->mmap2.ino_generation, in perf_event__repipe_buildid_mmap2()
440 dso = findnew_dso(event->mmap2.pid, event->mmap2.tid, in perf_event__repipe_buildid_mmap2()
441 event->mmap2.filename, &dso_id, machine); in perf_event__repipe_buildid_mmap2()
443 if (dso && !dso->hit) { in perf_event__repipe_buildid_mmap2()
444 dso->hit = 1; in perf_event__repipe_buildid_mmap2()
445 dso__inject_build_id(dso, tool, machine, sample->cpumode, in perf_event__repipe_buildid_mmap2()
446 event->mmap2.flags); in perf_event__repipe_buildid_mmap2()
450 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_buildid_mmap2()
457 struct perf_sample *sample, in perf_event__repipe_fork() argument
462 err = perf_event__process_fork(tool, event, sample, machine); in perf_event__repipe_fork()
463 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_fork()
470 struct perf_sample *sample, in perf_event__repipe_comm() argument
475 err = perf_event__process_comm(tool, event, sample, machine); in perf_event__repipe_comm()
476 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_comm()
483 struct perf_sample *sample, in perf_event__repipe_namespaces() argument
486 int err = perf_event__process_namespaces(tool, event, sample, machine); in perf_event__repipe_namespaces()
488 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_namespaces()
495 struct perf_sample *sample, in perf_event__repipe_exit() argument
500 err = perf_event__process_exit(tool, event, sample, machine); in perf_event__repipe_exit()
501 perf_event__repipe(tool, event, sample, machine); in perf_event__repipe_exit()
511 perf_event__repipe_synth(session->tool, event); in perf_event__repipe_tracing_data()
521 if (dso->has_build_id) in dso__read_build_id()
524 nsinfo__mountns_enter(dso->nsinfo, &nsc); in dso__read_build_id()
525 if (filename__read_build_id(dso->long_name, &dso->bid) > 0) in dso__read_build_id()
526 dso->has_build_id = true; in dso__read_build_id()
529 return dso->has_build_id ? 0 : -1; in dso__read_build_id()
537 if (is_anon_memory(dso->long_name) || flags & MAP_HUGETLB) in dso__inject_build_id()
539 if (is_no_dso_memory(dso->long_name)) in dso__inject_build_id()
543 pr_debug("no build_id found for %s\n", dso->long_name); in dso__inject_build_id()
544 return -1; in dso__inject_build_id()
550 pr_err("Can't synthesize build_id event for %s\n", dso->long_name); in dso__inject_build_id()
551 return -1; in dso__inject_build_id()
558 struct perf_sample *sample, in perf_event__inject_buildid() argument
565 thread = machine__findnew_thread(machine, sample->pid, sample->tid); in perf_event__inject_buildid()
568 event->header.type); in perf_event__inject_buildid()
572 if (thread__find_map(thread, sample->cpumode, sample->ip, &al)) { in perf_event__inject_buildid()
573 if (!al.map->dso->hit) { in perf_event__inject_buildid()
574 al.map->dso->hit = 1; in perf_event__inject_buildid()
575 dso__inject_build_id(al.map->dso, tool, machine, in perf_event__inject_buildid()
576 sample->cpumode, al.map->flags); in perf_event__inject_buildid()
582 perf_event__repipe(tool, event, sample, machine); in perf_event__inject_buildid()
588 struct perf_sample *sample, in perf_inject__sched_process_exit() argument
595 list_for_each_entry(ent, &inject->samples, node) { in perf_inject__sched_process_exit()
596 if (sample->tid == ent->tid) { in perf_inject__sched_process_exit()
597 list_del_init(&ent->node); in perf_inject__sched_process_exit()
608 struct perf_sample *sample, in perf_inject__sched_switch() argument
615 perf_inject__sched_process_exit(tool, event, sample, evsel, machine); in perf_inject__sched_switch()
617 ent = malloc(event->header.size + sizeof(struct event_entry)); in perf_inject__sched_switch()
621 return -1; in perf_inject__sched_switch()
624 ent->tid = sample->tid; in perf_inject__sched_switch()
625 memcpy(&ent->event, event, event->header.size); in perf_inject__sched_switch()
626 list_add(&ent->node, &inject->samples); in perf_inject__sched_switch()
632 struct perf_sample *sample, in perf_inject__sched_stat() argument
640 u32 pid = evsel__intval(evsel, sample, "pid"); in perf_inject__sched_stat()
642 list_for_each_entry(ent, &inject->samples, node) { in perf_inject__sched_stat()
643 if (pid == ent->tid) in perf_inject__sched_stat()
649 event_sw = &ent->event[0]; in perf_inject__sched_stat()
652 sample_sw.period = sample->period; in perf_inject__sched_stat()
653 sample_sw.time = sample->time; in perf_inject__sched_stat()
654 perf_event__synthesize_sample(event_sw, evsel->core.attr.sample_type, in perf_inject__sched_stat()
655 evsel->core.attr.read_format, &sample_sw); in perf_inject__sched_stat()
667 struct perf_event_attr *attr = &evsel->core.attr; in evsel__check_stype()
670 if (!(attr->sample_type & sample_type)) { in evsel__check_stype()
673 return -EINVAL; in evsel__check_stype()
681 struct perf_sample *sample __maybe_unused, in drop_sample()
690 struct evlist *evlist = inject->session->evlist; in strip_init()
693 inject->tool.context_switch = perf_event__drop; in strip_init()
696 evsel->handler = drop_sample; in strip_init()
701 int ret = -EINVAL; in __cmd_inject()
702 struct perf_session *session = inject->session; in __cmd_inject()
703 struct perf_data *data_out = &inject->output; in __cmd_inject()
709 if (inject->build_ids || inject->sched_stat || in __cmd_inject()
710 inject->itrace_synth_opts.set || inject->build_id_all) { in __cmd_inject()
711 inject->tool.mmap = perf_event__repipe_mmap; in __cmd_inject()
712 inject->tool.mmap2 = perf_event__repipe_mmap2; in __cmd_inject()
713 inject->tool.fork = perf_event__repipe_fork; in __cmd_inject()
714 inject->tool.tracing_data = perf_event__repipe_tracing_data; in __cmd_inject()
717 output_data_offset = session->header.data_offset; in __cmd_inject()
719 if (inject->build_id_all) { in __cmd_inject()
720 inject->tool.mmap = perf_event__repipe_buildid_mmap; in __cmd_inject()
721 inject->tool.mmap2 = perf_event__repipe_buildid_mmap2; in __cmd_inject()
722 } else if (inject->build_ids) { in __cmd_inject()
723 inject->tool.sample = perf_event__inject_buildid; in __cmd_inject()
724 } else if (inject->sched_stat) { in __cmd_inject()
727 evlist__for_each_entry(session->evlist, evsel) { in __cmd_inject()
732 return -EINVAL; in __cmd_inject()
734 evsel->handler = perf_inject__sched_switch; in __cmd_inject()
736 evsel->handler = perf_inject__sched_process_exit; in __cmd_inject()
738 evsel->handler = perf_inject__sched_stat; in __cmd_inject()
740 } else if (inject->itrace_synth_opts.set) { in __cmd_inject()
741 session->itrace_synth_opts = &inject->itrace_synth_opts; in __cmd_inject()
742 inject->itrace_synth_opts.inject = true; in __cmd_inject()
743 inject->tool.comm = perf_event__repipe_comm; in __cmd_inject()
744 inject->tool.namespaces = perf_event__repipe_namespaces; in __cmd_inject()
745 inject->tool.exit = perf_event__repipe_exit; in __cmd_inject()
746 inject->tool.id_index = perf_event__process_id_index; in __cmd_inject()
747 inject->tool.auxtrace_info = perf_event__process_auxtrace_info; in __cmd_inject()
748 inject->tool.auxtrace = perf_event__process_auxtrace; in __cmd_inject()
749 inject->tool.aux = perf_event__drop_aux; in __cmd_inject()
750 inject->tool.itrace_start = perf_event__drop_aux, in __cmd_inject()
751 inject->tool.ordered_events = true; in __cmd_inject()
752 inject->tool.ordering_requires_timestamps = true; in __cmd_inject()
755 if (inject->strip) in __cmd_inject()
759 if (!inject->itrace_synth_opts.set) in __cmd_inject()
760 auxtrace_index__free(&session->auxtrace_index); in __cmd_inject()
762 if (!data_out->is_pipe) in __cmd_inject()
769 if (!data_out->is_pipe) { in __cmd_inject()
770 if (inject->build_ids) in __cmd_inject()
771 perf_header__set_feat(&session->header, in __cmd_inject()
777 if (perf_header__has_feat(&session->header, HEADER_BUILD_ID) && in __cmd_inject()
778 inject->have_auxtrace && !inject->itrace_synth_opts.set) in __cmd_inject()
784 if (inject->itrace_synth_opts.set) { in __cmd_inject()
785 perf_header__clear_feat(&session->header, in __cmd_inject()
787 if (inject->itrace_synth_opts.last_branch || in __cmd_inject()
788 inject->itrace_synth_opts.add_last_branch) in __cmd_inject()
789 perf_header__set_feat(&session->header, in __cmd_inject()
792 session->header.data_offset = output_data_offset; in __cmd_inject()
793 session->header.data_size = inject->bytes_written; in __cmd_inject()
794 perf_session__write_header(session, session->evlist, fd, true); in __cmd_inject()
804 .sample = perf_event__repipe_sample, in cmd_inject()
841 .input_name = "-", in cmd_inject()
844 .path = "-", in cmd_inject()
854 OPT_BOOLEAN('b', "build-ids", &inject.build_ids, in cmd_inject()
855 "Inject build-ids into the output stream"), in cmd_inject()
856 OPT_BOOLEAN(0, "buildid-all", &inject.build_id_all, in cmd_inject()
857 "Inject build-ids of all DSOs into the output stream"), in cmd_inject()
862 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat, in cmd_inject()
863 "Merge sched-stat and sched-switch for getting events " in cmd_inject()
878 "strip non-synthesized events (use with --itrace)"), in cmd_inject()
897 pr_err("--strip option requires --itrace option\n"); in cmd_inject()
898 return -1; in cmd_inject()
903 return -1; in cmd_inject()
911 if (zstd_init(&(inject.session->zstd_data), 0) < 0) in cmd_inject()
919 * inject the jit mmaps at the same time for now. in cmd_inject()
942 ret = symbol__init(&inject.session->header.env); in cmd_inject()
949 zstd_fini(&(inject.session->zstd_data)); in cmd_inject()