Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 2 | #include <dirent.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 3 | #include <errno.h> |
Arnaldo Carvalho de Melo | c23c2a0 | 2017-09-11 10:50:26 -0300 | [diff] [blame] | 4 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 5 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 7 | #include <linux/types.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <unistd.h> |
David Carrillo-Cisneros | 16eb813 | 2017-04-11 23:49:19 -0700 | [diff] [blame] | 11 | #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 12 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 5ab8c68 | 2017-04-25 15:30:47 -0300 | [diff] [blame] | 13 | #include <linux/perf_event.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 14 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 15 | #include "event.h" |
| 16 | #include "debug.h" |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 17 | #include "hist.h" |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 18 | #include "machine.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 19 | #include "sort.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 20 | #include "string2.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 21 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 22 | #include "thread.h" |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 23 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | f371102 | 2019-08-29 15:16:27 -0300 | [diff] [blame^] | 24 | #include "time-utils.h" |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 25 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | 1101f69 | 2019-01-27 13:42:37 +0100 | [diff] [blame] | 26 | #include "map.h" |
Arnaldo Carvalho de Melo | daecf9e | 2019-01-28 00:03:34 +0100 | [diff] [blame] | 27 | #include "symbol.h" |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 28 | #include "symbol/kallsyms.h" |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 29 | #include "asm/bug.h" |
| 30 | #include "stat.h" |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 31 | #include "session.h" |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 32 | #include "bpf-event.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 33 | |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 34 | #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500 |
| 35 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 36 | static const char *perf_event__names[] = { |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 37 | [0] = "TOTAL", |
| 38 | [PERF_RECORD_MMAP] = "MMAP", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 39 | [PERF_RECORD_MMAP2] = "MMAP2", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 40 | [PERF_RECORD_LOST] = "LOST", |
| 41 | [PERF_RECORD_COMM] = "COMM", |
| 42 | [PERF_RECORD_EXIT] = "EXIT", |
| 43 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 44 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 45 | [PERF_RECORD_FORK] = "FORK", |
| 46 | [PERF_RECORD_READ] = "READ", |
| 47 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 48 | [PERF_RECORD_AUX] = "AUX", |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 49 | [PERF_RECORD_ITRACE_START] = "ITRACE_START", |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 50 | [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES", |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 51 | [PERF_RECORD_SWITCH] = "SWITCH", |
| 52 | [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE", |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 53 | [PERF_RECORD_NAMESPACES] = "NAMESPACES", |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 54 | [PERF_RECORD_KSYMBOL] = "KSYMBOL", |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 55 | [PERF_RECORD_BPF_EVENT] = "BPF_EVENT", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 56 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
| 57 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
| 58 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
| 59 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
| 60 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 61 | [PERF_RECORD_ID_INDEX] = "ID_INDEX", |
Adrian Hunter | a16ac02 | 2015-04-09 18:53:43 +0300 | [diff] [blame] | 62 | [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO", |
| 63 | [PERF_RECORD_AUXTRACE] = "AUXTRACE", |
Adrian Hunter | e9bf54d | 2015-04-09 18:53:47 +0300 | [diff] [blame] | 64 | [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR", |
Jiri Olsa | 5f3339d | 2015-10-25 15:51:19 +0100 | [diff] [blame] | 65 | [PERF_RECORD_THREAD_MAP] = "THREAD_MAP", |
Jiri Olsa | 6640b6c | 2015-10-25 15:51:23 +0100 | [diff] [blame] | 66 | [PERF_RECORD_CPU_MAP] = "CPU_MAP", |
Jiri Olsa | 374fb9e | 2015-10-25 15:51:27 +0100 | [diff] [blame] | 67 | [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG", |
Jiri Olsa | d80518c | 2015-10-25 15:51:30 +0100 | [diff] [blame] | 68 | [PERF_RECORD_STAT] = "STAT", |
Jiri Olsa | 2d8f0f1 | 2015-10-25 15:51:33 +0100 | [diff] [blame] | 69 | [PERF_RECORD_STAT_ROUND] = "STAT_ROUND", |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 70 | [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE", |
Adrian Hunter | 46bc29b | 2016-03-08 10:38:44 +0200 | [diff] [blame] | 71 | [PERF_RECORD_TIME_CONV] = "TIME_CONV", |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 72 | [PERF_RECORD_HEADER_FEATURE] = "FEATURE", |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 73 | [PERF_RECORD_COMPRESSED] = "COMPRESSED", |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 74 | }; |
| 75 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 76 | static const char *perf_ns__names[] = { |
| 77 | [NET_NS_INDEX] = "net", |
| 78 | [UTS_NS_INDEX] = "uts", |
| 79 | [IPC_NS_INDEX] = "ipc", |
| 80 | [PID_NS_INDEX] = "pid", |
| 81 | [USER_NS_INDEX] = "user", |
| 82 | [MNT_NS_INDEX] = "mnt", |
| 83 | [CGROUP_NS_INDEX] = "cgroup", |
| 84 | }; |
| 85 | |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 86 | unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT; |
| 87 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 88 | const char *perf_event__name(unsigned int id) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 89 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 90 | if (id >= ARRAY_SIZE(perf_event__names)) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 91 | return "INVALID"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 92 | if (!perf_event__names[id]) |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 93 | return "UNKNOWN"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 94 | return perf_event__names[id]; |
Thomas Gleixner | 3835bc00 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 97 | static const char *perf_ns__name(unsigned int id) |
| 98 | { |
| 99 | if (id >= ARRAY_SIZE(perf_ns__names)) |
| 100 | return "UNKNOWN"; |
| 101 | return perf_ns__names[id]; |
| 102 | } |
| 103 | |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 104 | int perf_tool__process_synth_event(struct perf_tool *tool, |
| 105 | union perf_event *event, |
| 106 | struct machine *machine, |
| 107 | perf_event__handler_t process) |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 108 | { |
| 109 | struct perf_sample synth_sample = { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 110 | .pid = -1, |
| 111 | .tid = -1, |
| 112 | .time = -1, |
| 113 | .stream_id = -1, |
| 114 | .cpu = -1, |
| 115 | .period = 1, |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 116 | .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK, |
| 117 | }; |
| 118 | |
| 119 | return process(tool, event, &synth_sample, machine); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 120 | }; |
| 121 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 122 | /* |
| 123 | * Assumes that the first 4095 bytes of /proc/pid/stat contains |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 124 | * the comm, tgid and ppid. |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 125 | */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 126 | static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, |
| 127 | pid_t *tgid, pid_t *ppid) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 128 | { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 129 | char filename[PATH_MAX]; |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 130 | char bf[4096]; |
| 131 | int fd; |
Andrzej Hajda | 3834966 | 2015-10-06 11:00:17 +0200 | [diff] [blame] | 132 | size_t size = 0; |
| 133 | ssize_t n; |
Taeung Song | bdd97ca | 2017-04-07 23:24:21 +0900 | [diff] [blame] | 134 | char *name, *tgids, *ppids; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 135 | |
| 136 | *tgid = -1; |
| 137 | *ppid = -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 138 | |
| 139 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 140 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 141 | fd = open(filename, O_RDONLY); |
| 142 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 143 | pr_debug("couldn't open %s\n", filename); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 144 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 145 | } |
| 146 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 147 | n = read(fd, bf, sizeof(bf) - 1); |
| 148 | close(fd); |
| 149 | if (n <= 0) { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 150 | pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n", |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 151 | pid); |
| 152 | return -1; |
| 153 | } |
| 154 | bf[n] = '\0'; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 155 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 156 | name = strstr(bf, "Name:"); |
| 157 | tgids = strstr(bf, "Tgid:"); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 158 | ppids = strstr(bf, "PPid:"); |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 159 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 160 | if (name) { |
Jiri Olsa | 9d43f5e | 2017-04-20 11:24:30 +0200 | [diff] [blame] | 161 | char *nl; |
| 162 | |
Arnaldo Carvalho de Melo | 3285848 | 2019-06-26 11:42:03 -0300 | [diff] [blame] | 163 | name = skip_spaces(name + 5); /* strlen("Name:") */ |
Jiri Olsa | 9d43f5e | 2017-04-20 11:24:30 +0200 | [diff] [blame] | 164 | nl = strchr(name, '\n'); |
| 165 | if (nl) |
| 166 | *nl = '\0'; |
| 167 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 168 | size = strlen(name); |
| 169 | if (size >= len) |
| 170 | size = len - 1; |
| 171 | memcpy(comm, name, size); |
| 172 | comm[size] = '\0'; |
| 173 | } else { |
| 174 | pr_debug("Name: string not found for pid %d\n", pid); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 175 | } |
| 176 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 177 | if (tgids) { |
| 178 | tgids += 5; /* strlen("Tgid:") */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 179 | *tgid = atoi(tgids); |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 180 | } else { |
| 181 | pr_debug("Tgid: string not found for pid %d\n", pid); |
| 182 | } |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 183 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 184 | if (ppids) { |
| 185 | ppids += 5; /* strlen("PPid:") */ |
| 186 | *ppid = atoi(ppids); |
| 187 | } else { |
| 188 | pr_debug("PPid: string not found for pid %d\n", pid); |
| 189 | } |
| 190 | |
| 191 | return 0; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 192 | } |
| 193 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 194 | static int perf_event__prepare_comm(union perf_event *event, pid_t pid, |
| 195 | struct machine *machine, |
| 196 | pid_t *tgid, pid_t *ppid) |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 197 | { |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 198 | size_t size; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 199 | |
| 200 | *ppid = -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 201 | |
| 202 | memset(&event->comm, 0, sizeof(event->comm)); |
| 203 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 204 | if (machine__is_host(machine)) { |
| 205 | if (perf_event__get_comm_ids(pid, event->comm.comm, |
| 206 | sizeof(event->comm.comm), |
| 207 | tgid, ppid) != 0) { |
| 208 | return -1; |
| 209 | } |
| 210 | } else { |
| 211 | *tgid = machine->pid; |
| 212 | } |
Dongsheng Yang | f5db57c4 | 2013-12-20 15:52:59 -0500 | [diff] [blame] | 213 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 214 | if (*tgid < 0) |
| 215 | return -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 216 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 217 | event->comm.pid = *tgid; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 218 | event->comm.header.type = PERF_RECORD_COMM; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 219 | |
| 220 | size = strlen(event->comm.comm) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 221 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 222 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 223 | event->comm.header.size = (sizeof(event->comm) - |
| 224 | (sizeof(event->comm.comm) - size) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 225 | machine->id_hdr_size); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 226 | event->comm.tid = pid; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 227 | |
| 228 | return 0; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 229 | } |
| 230 | |
Namhyung Kim | e803cf9 | 2015-09-22 09:24:55 +0900 | [diff] [blame] | 231 | pid_t perf_event__synthesize_comm(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 232 | union perf_event *event, pid_t pid, |
| 233 | perf_event__handler_t process, |
| 234 | struct machine *machine) |
| 235 | { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 236 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 237 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 238 | if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0) |
| 239 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 240 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 241 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 242 | return -1; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 243 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 244 | return tgid; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 245 | } |
| 246 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 247 | static void perf_event__get_ns_link_info(pid_t pid, const char *ns, |
| 248 | struct perf_ns_link_info *ns_link_info) |
| 249 | { |
| 250 | struct stat64 st; |
| 251 | char proc_ns[128]; |
| 252 | |
| 253 | sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns); |
| 254 | if (stat64(proc_ns, &st) == 0) { |
| 255 | ns_link_info->dev = st.st_dev; |
| 256 | ns_link_info->ino = st.st_ino; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | int perf_event__synthesize_namespaces(struct perf_tool *tool, |
| 261 | union perf_event *event, |
| 262 | pid_t pid, pid_t tgid, |
| 263 | perf_event__handler_t process, |
| 264 | struct machine *machine) |
| 265 | { |
| 266 | u32 idx; |
| 267 | struct perf_ns_link_info *ns_link_info; |
| 268 | |
| 269 | if (!tool || !tool->namespace_events) |
| 270 | return 0; |
| 271 | |
| 272 | memset(&event->namespaces, 0, (sizeof(event->namespaces) + |
| 273 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 274 | machine->id_hdr_size)); |
| 275 | |
| 276 | event->namespaces.pid = tgid; |
| 277 | event->namespaces.tid = pid; |
| 278 | |
| 279 | event->namespaces.nr_namespaces = NR_NAMESPACES; |
| 280 | |
| 281 | ns_link_info = event->namespaces.link_info; |
| 282 | |
| 283 | for (idx = 0; idx < event->namespaces.nr_namespaces; idx++) |
| 284 | perf_event__get_ns_link_info(pid, perf_ns__name(idx), |
| 285 | &ns_link_info[idx]); |
| 286 | |
| 287 | event->namespaces.header.type = PERF_RECORD_NAMESPACES; |
| 288 | |
| 289 | event->namespaces.header.size = (sizeof(event->namespaces) + |
| 290 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 291 | machine->id_hdr_size); |
| 292 | |
| 293 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
| 294 | return -1; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 299 | static int perf_event__synthesize_fork(struct perf_tool *tool, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 300 | union perf_event *event, |
| 301 | pid_t pid, pid_t tgid, pid_t ppid, |
| 302 | perf_event__handler_t process, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 303 | struct machine *machine) |
| 304 | { |
| 305 | memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); |
| 306 | |
David Ahern | 7764a38 | 2015-04-09 12:48:27 -0400 | [diff] [blame] | 307 | /* |
| 308 | * for main thread set parent to ppid from status file. For other |
| 309 | * threads set parent pid to main thread. ie., assume main thread |
| 310 | * spawns all threads in a process |
| 311 | */ |
| 312 | if (tgid == pid) { |
| 313 | event->fork.ppid = ppid; |
| 314 | event->fork.ptid = ppid; |
| 315 | } else { |
| 316 | event->fork.ppid = tgid; |
| 317 | event->fork.ptid = tgid; |
| 318 | } |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 319 | event->fork.pid = tgid; |
| 320 | event->fork.tid = pid; |
| 321 | event->fork.header.type = PERF_RECORD_FORK; |
David Miller | 4f8f382 | 2018-10-30 22:24:04 -0700 | [diff] [blame] | 322 | event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 323 | |
| 324 | event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); |
| 325 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 326 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 327 | return -1; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Jiri Olsa | a18382b | 2014-01-07 13:47:20 +0100 | [diff] [blame] | 332 | int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
| 333 | union perf_event *event, |
| 334 | pid_t pid, pid_t tgid, |
| 335 | perf_event__handler_t process, |
| 336 | struct machine *machine, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 337 | bool mmap_data) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 338 | { |
| 339 | char filename[PATH_MAX]; |
| 340 | FILE *fp; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 341 | unsigned long long t; |
| 342 | bool truncation = false; |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 343 | unsigned long long timeout = proc_map_timeout * 1000000ULL; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 344 | int rc = 0; |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 345 | const char *hugetlbfs_mnt = hugetlbfs__mountpoint(); |
| 346 | int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 347 | |
Dongsheng Yang | c239c25a | 2013-12-20 15:53:00 -0500 | [diff] [blame] | 348 | if (machine__is_default_guest(machine)) |
| 349 | return 0; |
| 350 | |
Stephane Eranian | 88b897a | 2017-03-15 10:17:13 -0700 | [diff] [blame] | 351 | snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps", |
| 352 | machine->root_dir, pid, pid); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 353 | |
| 354 | fp = fopen(filename, "r"); |
| 355 | if (fp == NULL) { |
| 356 | /* |
| 357 | * We raced with a task exiting - just return: |
| 358 | */ |
| 359 | pr_debug("couldn't open %s\n", filename); |
| 360 | return -1; |
| 361 | } |
| 362 | |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 363 | event->header.type = PERF_RECORD_MMAP2; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 364 | t = rdclock(); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 365 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 366 | while (1) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 367 | char bf[BUFSIZ]; |
| 368 | char prot[5]; |
| 369 | char execname[PATH_MAX]; |
| 370 | char anonstr[] = "//anon"; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 371 | unsigned int ino; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 372 | size_t size; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 373 | ssize_t n; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 374 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 375 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 376 | break; |
| 377 | |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 378 | if ((rdclock() - t) > timeout) { |
| 379 | pr_warning("Reading %s time out. " |
| 380 | "You may want to increase " |
| 381 | "the time limit by --proc-map-timeout\n", |
| 382 | filename); |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 383 | truncation = true; |
| 384 | goto out; |
| 385 | } |
| 386 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 387 | /* ensure null termination since stack will be reused. */ |
| 388 | strcpy(execname, ""); |
| 389 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 390 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
Jiri Olsa | b66ced1 | 2019-08-25 20:17:42 +0200 | [diff] [blame] | 391 | n = sscanf(bf, "%"PRI_lx64"-%"PRI_lx64" %s %"PRI_lx64" %x:%x %u %[^\n]\n", |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 392 | &event->mmap2.start, &event->mmap2.len, prot, |
| 393 | &event->mmap2.pgoff, &event->mmap2.maj, |
| 394 | &event->mmap2.min, |
| 395 | &ino, execname); |
| 396 | |
Don Zickus | 9d4ecc8 | 2013-11-13 15:32:06 -0300 | [diff] [blame] | 397 | /* |
| 398 | * Anon maps don't have the execname. |
| 399 | */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 400 | if (n < 7) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 401 | continue; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 402 | |
| 403 | event->mmap2.ino = (u64)ino; |
| 404 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 405 | /* |
| 406 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c |
| 407 | */ |
Dongsheng Yang | 73547aa | 2013-12-20 15:52:58 -0500 | [diff] [blame] | 408 | if (machine__is_host(machine)) |
| 409 | event->header.misc = PERF_RECORD_MISC_USER; |
| 410 | else |
| 411 | event->header.misc = PERF_RECORD_MISC_GUEST_USER; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 412 | |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 413 | /* map protection and flags bits */ |
| 414 | event->mmap2.prot = 0; |
| 415 | event->mmap2.flags = 0; |
| 416 | if (prot[0] == 'r') |
| 417 | event->mmap2.prot |= PROT_READ; |
| 418 | if (prot[1] == 'w') |
| 419 | event->mmap2.prot |= PROT_WRITE; |
| 420 | if (prot[2] == 'x') |
| 421 | event->mmap2.prot |= PROT_EXEC; |
| 422 | |
| 423 | if (prot[3] == 's') |
| 424 | event->mmap2.flags |= MAP_SHARED; |
| 425 | else |
| 426 | event->mmap2.flags |= MAP_PRIVATE; |
| 427 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 428 | if (prot[2] != 'x') { |
| 429 | if (!mmap_data || prot[0] != 'r') |
| 430 | continue; |
| 431 | |
| 432 | event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 433 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 434 | |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 435 | out: |
| 436 | if (truncation) |
| 437 | event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT; |
| 438 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 439 | if (!strcmp(execname, "")) |
| 440 | strcpy(execname, anonstr); |
Arnaldo Carvalho de Melo | fbef103 | 2016-09-12 16:47:57 -0300 | [diff] [blame] | 441 | |
Adrian Hunter | 973186c | 2016-09-23 17:38:34 +0300 | [diff] [blame] | 442 | if (hugetlbfs_mnt_len && |
| 443 | !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) { |
Wang Nan | d7e404a | 2016-09-06 04:58:29 +0000 | [diff] [blame] | 444 | strcpy(execname, anonstr); |
| 445 | event->mmap2.flags |= MAP_HUGETLB; |
| 446 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 447 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 448 | size = strlen(execname) + 1; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 449 | memcpy(event->mmap2.filename, execname, size); |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 450 | size = PERF_ALIGN(size, sizeof(u64)); |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 451 | event->mmap2.len -= event->mmap.start; |
| 452 | event->mmap2.header.size = (sizeof(event->mmap2) - |
| 453 | (sizeof(event->mmap2.filename) - size)); |
| 454 | memset(event->mmap2.filename + size, 0, machine->id_hdr_size); |
| 455 | event->mmap2.header.size += machine->id_hdr_size; |
| 456 | event->mmap2.pid = tgid; |
| 457 | event->mmap2.tid = pid; |
Anton Blanchard | 6a0e55d | 2011-08-30 09:15:06 +1000 | [diff] [blame] | 458 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 459 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 460 | rc = -1; |
| 461 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 462 | } |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 463 | |
| 464 | if (truncation) |
| 465 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | fclose(fp); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 469 | return rc; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 470 | } |
| 471 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 472 | int perf_event__synthesize_modules(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 473 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 474 | struct machine *machine) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 475 | { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 476 | int rc = 0; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 477 | struct map *pos; |
Arnaldo Carvalho de Melo | 68a7418 | 2018-04-24 17:06:25 -0300 | [diff] [blame] | 478 | struct maps *maps = machine__kernel_maps(machine); |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 479 | union perf_event *event = zalloc((sizeof(event->mmap) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 480 | machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 481 | if (event == NULL) { |
| 482 | pr_debug("Not enough memory synthesizing mmap event " |
| 483 | "for kernel modules\n"); |
| 484 | return -1; |
| 485 | } |
| 486 | |
| 487 | event->header.type = PERF_RECORD_MMAP; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 488 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 489 | /* |
| 490 | * kernel uses 0 for user space maps, see kernel/perf_event.c |
| 491 | * __perf_event_mmap |
| 492 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 493 | if (machine__is_host(machine)) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 494 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 495 | else |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 496 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 497 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 498 | for (pos = maps__first(maps); pos; pos = map__next(pos)) { |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 499 | size_t size; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 500 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 501 | if (!__map__is_kmodule(pos)) |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 502 | continue; |
| 503 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 504 | size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 505 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 506 | event->mmap.header.size = (sizeof(event->mmap) - |
| 507 | (sizeof(event->mmap.filename) - size)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 508 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
| 509 | event->mmap.header.size += machine->id_hdr_size; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 510 | event->mmap.start = pos->start; |
| 511 | event->mmap.len = pos->end - pos->start; |
| 512 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 513 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 514 | memcpy(event->mmap.filename, pos->dso->long_name, |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 515 | pos->dso->long_name_len + 1); |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 516 | if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 517 | rc = -1; |
| 518 | break; |
| 519 | } |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 520 | } |
| 521 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 522 | free(event); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 523 | return rc; |
Arnaldo Carvalho de Melo | b7cece76 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 524 | } |
| 525 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 526 | static int __event__synthesize_thread(union perf_event *comm_event, |
| 527 | union perf_event *mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 528 | union perf_event *fork_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 529 | union perf_event *namespaces_event, |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 530 | pid_t pid, int full, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 531 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 532 | struct perf_tool *tool, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 533 | struct machine *machine, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 534 | bool mmap_data) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 535 | { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 536 | char filename[PATH_MAX]; |
| 537 | DIR *tasks; |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 538 | struct dirent *dirent; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 539 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 540 | int rc = 0; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 541 | |
| 542 | /* special case: only send one comm event using passed in pid */ |
| 543 | if (!full) { |
| 544 | tgid = perf_event__synthesize_comm(tool, comm_event, pid, |
| 545 | process, machine); |
| 546 | |
| 547 | if (tgid == -1) |
| 548 | return -1; |
| 549 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 550 | if (perf_event__synthesize_namespaces(tool, namespaces_event, pid, |
| 551 | tgid, process, machine) < 0) |
| 552 | return -1; |
| 553 | |
Konstantin Khlebnikov | e5adfc3 | 2018-08-07 12:09:01 +0300 | [diff] [blame] | 554 | /* |
| 555 | * send mmap only for thread group leader |
| 556 | * see thread__init_map_groups |
| 557 | */ |
| 558 | if (pid == tgid && |
| 559 | perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 560 | process, machine, mmap_data)) |
Konstantin Khlebnikov | e5adfc3 | 2018-08-07 12:09:01 +0300 | [diff] [blame] | 561 | return -1; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 562 | |
Konstantin Khlebnikov | e5adfc3 | 2018-08-07 12:09:01 +0300 | [diff] [blame] | 563 | return 0; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | if (machine__is_default_guest(machine)) |
| 567 | return 0; |
| 568 | |
| 569 | snprintf(filename, sizeof(filename), "%s/proc/%d/task", |
| 570 | machine->root_dir, pid); |
| 571 | |
| 572 | tasks = opendir(filename); |
| 573 | if (tasks == NULL) { |
| 574 | pr_debug("couldn't open %s\n", filename); |
| 575 | return 0; |
| 576 | } |
| 577 | |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 578 | while ((dirent = readdir(tasks)) != NULL) { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 579 | char *end; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 580 | pid_t _pid; |
| 581 | |
Arnaldo Carvalho de Melo | 7093b4c | 2016-04-08 11:32:15 -0300 | [diff] [blame] | 582 | _pid = strtol(dirent->d_name, &end, 10); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 583 | if (*end) |
| 584 | continue; |
| 585 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 586 | rc = -1; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 587 | if (perf_event__prepare_comm(comm_event, _pid, machine, |
| 588 | &tgid, &ppid) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 589 | break; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 590 | |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 591 | if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 592 | ppid, process, machine) < 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 593 | break; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 594 | |
| 595 | if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid, |
| 596 | tgid, process, machine) < 0) |
| 597 | break; |
| 598 | |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 599 | /* |
| 600 | * Send the prepared comm event |
| 601 | */ |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 602 | if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 603 | break; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 604 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 605 | rc = 0; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 606 | if (_pid == pid) { |
| 607 | /* process the parent's maps too */ |
| 608 | rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 609 | process, machine, mmap_data); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 610 | if (rc) |
| 611 | break; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 612 | } |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | closedir(tasks); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 616 | return rc; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 617 | } |
| 618 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 619 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 620 | struct perf_thread_map *threads, |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 621 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 622 | struct machine *machine, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 623 | bool mmap_data) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 624 | { |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 625 | union perf_event *comm_event, *mmap_event, *fork_event; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 626 | union perf_event *namespaces_event; |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 627 | int err = -1, thread, j; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 628 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 629 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 630 | if (comm_event == NULL) |
| 631 | goto out; |
| 632 | |
Wang Nan | b0fb978 | 2016-01-12 10:12:04 +0000 | [diff] [blame] | 633 | mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 634 | if (mmap_event == NULL) |
| 635 | goto out_free_comm; |
| 636 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 637 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 638 | if (fork_event == NULL) |
| 639 | goto out_free_mmap; |
| 640 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 641 | namespaces_event = malloc(sizeof(namespaces_event->namespaces) + |
| 642 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 643 | machine->id_hdr_size); |
| 644 | if (namespaces_event == NULL) |
| 645 | goto out_free_fork; |
| 646 | |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 647 | err = 0; |
| 648 | for (thread = 0; thread < threads->nr; ++thread) { |
| 649 | if (__event__synthesize_thread(comm_event, mmap_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 650 | fork_event, namespaces_event, |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 651 | perf_thread_map__pid(threads, thread), 0, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 652 | process, tool, machine, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 653 | mmap_data)) { |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 654 | err = -1; |
| 655 | break; |
| 656 | } |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * comm.pid is set to thread group id by |
| 660 | * perf_event__synthesize_comm |
| 661 | */ |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 662 | if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 663 | bool need_leader = true; |
| 664 | |
| 665 | /* is thread group leader in thread_map? */ |
| 666 | for (j = 0; j < threads->nr; ++j) { |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 667 | if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 668 | need_leader = false; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /* if not, generate events for it */ |
| 674 | if (need_leader && |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 675 | __event__synthesize_thread(comm_event, mmap_event, |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 676 | fork_event, namespaces_event, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 677 | comm_event->comm.pid, 0, |
| 678 | process, tool, machine, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 679 | mmap_data)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 680 | err = -1; |
| 681 | break; |
| 682 | } |
| 683 | } |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 684 | } |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 685 | free(namespaces_event); |
| 686 | out_free_fork: |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 687 | free(fork_event); |
| 688 | out_free_mmap: |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 689 | free(mmap_event); |
| 690 | out_free_comm: |
| 691 | free(comm_event); |
| 692 | out: |
| 693 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 694 | } |
| 695 | |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 696 | static int __perf_event__synthesize_threads(struct perf_tool *tool, |
| 697 | perf_event__handler_t process, |
| 698 | struct machine *machine, |
| 699 | bool mmap_data, |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 700 | struct dirent **dirent, |
| 701 | int start, |
| 702 | int num) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 703 | { |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 704 | union perf_event *comm_event, *mmap_event, *fork_event; |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 705 | union perf_event *namespaces_event; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 706 | int err = -1; |
Kan Liang | ecdad24 | 2017-09-07 10:55:46 -0700 | [diff] [blame] | 707 | char *end; |
| 708 | pid_t pid; |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 709 | int i; |
Namhyung Kim | 574799b | 2014-03-17 10:45:49 +0900 | [diff] [blame] | 710 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 711 | comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 712 | if (comm_event == NULL) |
| 713 | goto out; |
| 714 | |
Wang Nan | b0fb978 | 2016-01-12 10:12:04 +0000 | [diff] [blame] | 715 | mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 716 | if (mmap_event == NULL) |
| 717 | goto out_free_comm; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 718 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 719 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 720 | if (fork_event == NULL) |
| 721 | goto out_free_mmap; |
| 722 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 723 | namespaces_event = malloc(sizeof(namespaces_event->namespaces) + |
| 724 | (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + |
| 725 | machine->id_hdr_size); |
| 726 | if (namespaces_event == NULL) |
| 727 | goto out_free_fork; |
| 728 | |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 729 | for (i = start; i < start + num; i++) { |
Kan Liang | ecdad24 | 2017-09-07 10:55:46 -0700 | [diff] [blame] | 730 | if (!isdigit(dirent[i]->d_name[0])) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 731 | continue; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 732 | |
Kan Liang | ecdad24 | 2017-09-07 10:55:46 -0700 | [diff] [blame] | 733 | pid = (pid_t)strtol(dirent[i]->d_name, &end, 10); |
| 734 | /* only interested in proper numerical dirents */ |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 735 | if (*end) |
| 736 | continue; |
| 737 | /* |
| 738 | * We may race with exiting thread, so don't stop just because |
| 739 | * one thread couldn't be synthesized. |
| 740 | */ |
| 741 | __event__synthesize_thread(comm_event, mmap_event, fork_event, |
| 742 | namespaces_event, pid, 1, process, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 743 | tool, machine, mmap_data); |
Kan Liang | ecdad24 | 2017-09-07 10:55:46 -0700 | [diff] [blame] | 744 | } |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 745 | err = 0; |
Kan Liang | ecdad24 | 2017-09-07 10:55:46 -0700 | [diff] [blame] | 746 | |
Hari Bathini | e907caf | 2017-03-08 02:11:51 +0530 | [diff] [blame] | 747 | free(namespaces_event); |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 748 | out_free_fork: |
| 749 | free(fork_event); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 750 | out_free_mmap: |
| 751 | free(mmap_event); |
| 752 | out_free_comm: |
| 753 | free(comm_event); |
| 754 | out: |
| 755 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 756 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 757 | |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 758 | struct synthesize_threads_arg { |
| 759 | struct perf_tool *tool; |
| 760 | perf_event__handler_t process; |
| 761 | struct machine *machine; |
| 762 | bool mmap_data; |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 763 | struct dirent **dirent; |
| 764 | int num; |
| 765 | int start; |
| 766 | }; |
| 767 | |
| 768 | static void *synthesize_threads_worker(void *arg) |
| 769 | { |
| 770 | struct synthesize_threads_arg *args = arg; |
| 771 | |
| 772 | __perf_event__synthesize_threads(args->tool, args->process, |
| 773 | args->machine, args->mmap_data, |
Mark Drayton | 3fcb10e | 2018-12-04 12:34:20 -0800 | [diff] [blame] | 774 | args->dirent, |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 775 | args->start, args->num); |
| 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | int perf_event__synthesize_threads(struct perf_tool *tool, |
| 780 | perf_event__handler_t process, |
| 781 | struct machine *machine, |
| 782 | bool mmap_data, |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 783 | unsigned int nr_threads_synthesize) |
| 784 | { |
| 785 | struct synthesize_threads_arg *args = NULL; |
| 786 | pthread_t *synthesize_threads = NULL; |
| 787 | char proc_path[PATH_MAX]; |
| 788 | struct dirent **dirent; |
| 789 | int num_per_thread; |
| 790 | int m, n, i, j; |
| 791 | int thread_nr; |
| 792 | int base = 0; |
| 793 | int err = -1; |
| 794 | |
| 795 | |
| 796 | if (machine__is_default_guest(machine)) |
| 797 | return 0; |
| 798 | |
| 799 | snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); |
| 800 | n = scandir(proc_path, &dirent, 0, alphasort); |
| 801 | if (n < 0) |
| 802 | return err; |
| 803 | |
Kan Liang | 0c6b499 | 2017-09-29 07:47:55 -0700 | [diff] [blame] | 804 | if (nr_threads_synthesize == UINT_MAX) |
| 805 | thread_nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 806 | else |
| 807 | thread_nr = nr_threads_synthesize; |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 808 | |
| 809 | if (thread_nr <= 1) { |
| 810 | err = __perf_event__synthesize_threads(tool, process, |
| 811 | machine, mmap_data, |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 812 | dirent, base, n); |
| 813 | goto free_dirent; |
| 814 | } |
| 815 | if (thread_nr > n) |
| 816 | thread_nr = n; |
| 817 | |
| 818 | synthesize_threads = calloc(sizeof(pthread_t), thread_nr); |
| 819 | if (synthesize_threads == NULL) |
| 820 | goto free_dirent; |
| 821 | |
| 822 | args = calloc(sizeof(*args), thread_nr); |
| 823 | if (args == NULL) |
| 824 | goto free_threads; |
| 825 | |
| 826 | num_per_thread = n / thread_nr; |
| 827 | m = n % thread_nr; |
| 828 | for (i = 0; i < thread_nr; i++) { |
| 829 | args[i].tool = tool; |
| 830 | args[i].process = process; |
| 831 | args[i].machine = machine; |
| 832 | args[i].mmap_data = mmap_data; |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 833 | args[i].dirent = dirent; |
| 834 | } |
| 835 | for (i = 0; i < m; i++) { |
| 836 | args[i].num = num_per_thread + 1; |
| 837 | args[i].start = i * args[i].num; |
| 838 | } |
| 839 | if (i != 0) |
| 840 | base = args[i-1].start + args[i-1].num; |
| 841 | for (j = i; j < thread_nr; j++) { |
| 842 | args[j].num = num_per_thread; |
| 843 | args[j].start = base + (j - i) * args[i].num; |
| 844 | } |
| 845 | |
| 846 | for (i = 0; i < thread_nr; i++) { |
| 847 | if (pthread_create(&synthesize_threads[i], NULL, |
| 848 | synthesize_threads_worker, &args[i])) |
| 849 | goto out_join; |
| 850 | } |
| 851 | err = 0; |
| 852 | out_join: |
| 853 | for (i = 0; i < thread_nr; i++) |
| 854 | pthread_join(synthesize_threads[i], NULL); |
| 855 | free(args); |
| 856 | free_threads: |
| 857 | free(synthesize_threads); |
| 858 | free_dirent: |
| 859 | for (i = 0; i < n; i++) |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 860 | zfree(&dirent[i]); |
Kan Liang | 340b47f | 2017-09-29 07:47:54 -0700 | [diff] [blame] | 861 | free(dirent); |
| 862 | |
| 863 | return err; |
| 864 | } |
| 865 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 866 | struct process_symbol_args { |
| 867 | const char *name; |
| 868 | u64 start; |
| 869 | }; |
| 870 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 871 | static int find_symbol_cb(void *arg, const char *name, char type, |
Cody P Schafer | 8215152 | 2012-08-10 15:22:48 -0700 | [diff] [blame] | 872 | u64 start) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 873 | { |
| 874 | struct process_symbol_args *args = arg; |
| 875 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 876 | /* |
| 877 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 878 | * an 'A' to the same address as "_stext". |
| 879 | */ |
Arnaldo Carvalho de Melo | e85e0e0 | 2018-04-25 17:16:31 -0300 | [diff] [blame] | 880 | if (!(kallsyms__is_function(type) || |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 881 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 882 | return 0; |
| 883 | |
| 884 | args->start = start; |
| 885 | return 1; |
| 886 | } |
| 887 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 888 | int kallsyms__get_function_start(const char *kallsyms_filename, |
| 889 | const char *symbol_name, u64 *addr) |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 890 | { |
| 891 | struct process_symbol_args args = { .name = symbol_name, }; |
| 892 | |
| 893 | if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 894 | return -1; |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 895 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 896 | *addr = args.start; |
| 897 | return 0; |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 900 | int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused, |
| 901 | perf_event__handler_t process __maybe_unused, |
| 902 | struct machine *machine __maybe_unused) |
| 903 | { |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
| 908 | perf_event__handler_t process, |
| 909 | struct machine *machine) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 910 | { |
| 911 | size_t size; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 912 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 913 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 914 | int err; |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 915 | union perf_event *event; |
| 916 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 917 | if (map == NULL) |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 918 | return -1; |
| 919 | |
Igor Lubashev | e9a6882 | 2019-08-26 21:39:12 -0400 | [diff] [blame] | 920 | kmap = map__kmap(map); |
| 921 | if (!kmap->ref_reloc_sym) |
| 922 | return -1; |
| 923 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 924 | /* |
| 925 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 926 | * available use this, and after it is use this as a fallback for older |
| 927 | * kernels. |
| 928 | */ |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 929 | event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 930 | if (event == NULL) { |
| 931 | pr_debug("Not enough memory synthesizing mmap event " |
| 932 | "for kernel modules\n"); |
| 933 | return -1; |
| 934 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 935 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 936 | if (machine__is_host(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 937 | /* |
| 938 | * kernel uses PERF_RECORD_MISC_USER for user space maps, |
| 939 | * see kernel/perf_event.c __perf_event_mmap |
| 940 | */ |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 941 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 942 | } else { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 943 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Thomas Jarosch | 0b9e01a | 2013-01-25 11:20:47 +0100 | [diff] [blame] | 944 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 945 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 946 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 947 | "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 948 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 949 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 950 | event->mmap.header.size = (sizeof(event->mmap) - |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 951 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 952 | event->mmap.pgoff = kmap->ref_reloc_sym->addr; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 953 | event->mmap.start = map->start; |
| 954 | event->mmap.len = map->end - event->mmap.start; |
| 955 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 956 | |
Arnaldo Carvalho de Melo | 3ea223a | 2016-03-29 18:46:04 -0300 | [diff] [blame] | 957 | err = perf_tool__process_synth_event(tool, event, machine, process); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 958 | free(event); |
| 959 | |
| 960 | return err; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 961 | } |
| 962 | |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 963 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
| 964 | perf_event__handler_t process, |
| 965 | struct machine *machine) |
| 966 | { |
| 967 | int err; |
| 968 | |
| 969 | err = __perf_event__synthesize_kernel_mmap(tool, process, machine); |
| 970 | if (err < 0) |
| 971 | return err; |
| 972 | |
| 973 | return perf_event__synthesize_extra_kmaps(tool, process, machine); |
| 974 | } |
| 975 | |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 976 | int perf_event__synthesize_thread_map2(struct perf_tool *tool, |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 977 | struct perf_thread_map *threads, |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 978 | perf_event__handler_t process, |
| 979 | struct machine *machine) |
| 980 | { |
| 981 | union perf_event *event; |
| 982 | int i, err, size; |
| 983 | |
| 984 | size = sizeof(event->thread_map); |
| 985 | size += threads->nr * sizeof(event->thread_map.entries[0]); |
| 986 | |
| 987 | event = zalloc(size); |
| 988 | if (!event) |
| 989 | return -ENOMEM; |
| 990 | |
| 991 | event->header.type = PERF_RECORD_THREAD_MAP; |
| 992 | event->header.size = size; |
| 993 | event->thread_map.nr = threads->nr; |
| 994 | |
| 995 | for (i = 0; i < threads->nr; i++) { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 996 | struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i]; |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 997 | char *comm = perf_thread_map__comm(threads, i); |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 998 | |
| 999 | if (!comm) |
| 1000 | comm = (char *) ""; |
| 1001 | |
Jiri Olsa | a2f354e | 2019-08-22 13:11:41 +0200 | [diff] [blame] | 1002 | entry->pid = perf_thread_map__pid(threads, i); |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 1003 | strncpy((char *) &entry->comm, comm, sizeof(entry->comm)); |
| 1004 | } |
| 1005 | |
| 1006 | err = process(tool, event, NULL, machine); |
| 1007 | |
| 1008 | free(event); |
| 1009 | return err; |
| 1010 | } |
| 1011 | |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1012 | static void synthesize_cpus(struct cpu_map_entries *cpus, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1013 | struct perf_cpu_map *map) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1014 | { |
| 1015 | int i; |
| 1016 | |
| 1017 | cpus->nr = map->nr; |
| 1018 | |
| 1019 | for (i = 0; i < map->nr; i++) |
| 1020 | cpus->cpu[i] = map->map[i]; |
| 1021 | } |
| 1022 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1023 | static void synthesize_mask(struct perf_record_record_cpu_map *mask, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1024 | struct perf_cpu_map *map, int max) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1025 | { |
| 1026 | int i; |
| 1027 | |
| 1028 | mask->nr = BITS_TO_LONGS(max); |
| 1029 | mask->long_size = sizeof(long); |
| 1030 | |
| 1031 | for (i = 0; i < map->nr; i++) |
| 1032 | set_bit(map->map[i], mask->mask); |
| 1033 | } |
| 1034 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1035 | static size_t cpus_size(struct perf_cpu_map *map) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1036 | { |
| 1037 | return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16); |
| 1038 | } |
| 1039 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1040 | static size_t mask_size(struct perf_cpu_map *map, int *max) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1041 | { |
| 1042 | int i; |
| 1043 | |
| 1044 | *max = 0; |
| 1045 | |
| 1046 | for (i = 0; i < map->nr; i++) { |
| 1047 | /* bit possition of the cpu is + 1 */ |
| 1048 | int bit = map->map[i] + 1; |
| 1049 | |
| 1050 | if (bit > *max) |
| 1051 | *max = bit; |
| 1052 | } |
| 1053 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1054 | return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1057 | void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1058 | { |
| 1059 | size_t size_cpus, size_mask; |
Jiri Olsa | 315c0a1 | 2019-08-22 13:11:39 +0200 | [diff] [blame] | 1060 | bool is_dummy = perf_cpu_map__empty(map); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1061 | |
| 1062 | /* |
| 1063 | * Both array and mask data have variable size based |
| 1064 | * on the number of cpus and their actual values. |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1065 | * The size of the 'struct perf_record_cpu_map_data' is: |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1066 | * |
| 1067 | * array = size of 'struct cpu_map_entries' + |
| 1068 | * number of cpus * sizeof(u64) |
| 1069 | * |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1070 | * mask = size of 'struct perf_record_record_cpu_map' + |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1071 | * maximum cpu bit converted to size of longs |
| 1072 | * |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1073 | * and finaly + the size of 'struct perf_record_cpu_map_data'. |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1074 | */ |
| 1075 | size_cpus = cpus_size(map); |
| 1076 | size_mask = mask_size(map, max); |
| 1077 | |
| 1078 | if (is_dummy || (size_cpus < size_mask)) { |
| 1079 | *size += size_cpus; |
| 1080 | *type = PERF_CPU_MAP__CPUS; |
| 1081 | } else { |
| 1082 | *size += size_mask; |
| 1083 | *type = PERF_CPU_MAP__MASK; |
| 1084 | } |
| 1085 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1086 | *size += sizeof(struct perf_record_cpu_map_data); |
David Miller | 0ed149c | 2018-10-11 22:46:55 -0700 | [diff] [blame] | 1087 | *size = PERF_ALIGN(*size, sizeof(u64)); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1088 | return zalloc(*size); |
| 1089 | } |
| 1090 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1091 | void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map, |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1092 | u16 type, int max) |
| 1093 | { |
| 1094 | data->type = type; |
| 1095 | |
| 1096 | switch (type) { |
| 1097 | case PERF_CPU_MAP__CPUS: |
| 1098 | synthesize_cpus((struct cpu_map_entries *) data->data, map); |
| 1099 | break; |
| 1100 | case PERF_CPU_MAP__MASK: |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1101 | synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max); |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1102 | default: |
| 1103 | break; |
| 1104 | }; |
| 1105 | } |
| 1106 | |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1107 | static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map) |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1108 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1109 | size_t size = sizeof(struct perf_record_cpu_map); |
| 1110 | struct perf_record_cpu_map *event; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1111 | int max; |
| 1112 | u16 type; |
| 1113 | |
| 1114 | event = cpu_map_data__alloc(map, &size, &type, &max); |
| 1115 | if (!event) |
| 1116 | return NULL; |
| 1117 | |
| 1118 | event->header.type = PERF_RECORD_CPU_MAP; |
| 1119 | event->header.size = size; |
| 1120 | event->data.type = type; |
| 1121 | |
| 1122 | cpu_map_data__synthesize(&event->data, map, type, max); |
| 1123 | return event; |
| 1124 | } |
| 1125 | |
| 1126 | int perf_event__synthesize_cpu_map(struct perf_tool *tool, |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1127 | struct perf_cpu_map *map, |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1128 | perf_event__handler_t process, |
| 1129 | struct machine *machine) |
| 1130 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1131 | struct perf_record_cpu_map *event; |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 1132 | int err; |
| 1133 | |
| 1134 | event = cpu_map_event__new(map); |
| 1135 | if (!event) |
| 1136 | return -ENOMEM; |
| 1137 | |
| 1138 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 1139 | |
| 1140 | free(event); |
| 1141 | return err; |
| 1142 | } |
| 1143 | |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 1144 | int perf_event__synthesize_stat_config(struct perf_tool *tool, |
| 1145 | struct perf_stat_config *config, |
| 1146 | perf_event__handler_t process, |
| 1147 | struct machine *machine) |
| 1148 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1149 | struct perf_record_stat_config *event; |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 1150 | int size, i = 0, err; |
| 1151 | |
| 1152 | size = sizeof(*event); |
| 1153 | size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0])); |
| 1154 | |
| 1155 | event = zalloc(size); |
| 1156 | if (!event) |
| 1157 | return -ENOMEM; |
| 1158 | |
| 1159 | event->header.type = PERF_RECORD_STAT_CONFIG; |
| 1160 | event->header.size = size; |
| 1161 | event->nr = PERF_STAT_CONFIG_TERM__MAX; |
| 1162 | |
| 1163 | #define ADD(__term, __val) \ |
| 1164 | event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \ |
| 1165 | event->data[i].val = __val; \ |
| 1166 | i++; |
| 1167 | |
| 1168 | ADD(AGGR_MODE, config->aggr_mode) |
| 1169 | ADD(INTERVAL, config->interval) |
| 1170 | ADD(SCALE, config->scale) |
| 1171 | |
| 1172 | WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX, |
| 1173 | "stat config terms unbalanced\n"); |
| 1174 | #undef ADD |
| 1175 | |
| 1176 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 1177 | |
| 1178 | free(event); |
| 1179 | return err; |
| 1180 | } |
| 1181 | |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 1182 | int perf_event__synthesize_stat(struct perf_tool *tool, |
| 1183 | u32 cpu, u32 thread, u64 id, |
| 1184 | struct perf_counts_values *count, |
| 1185 | perf_event__handler_t process, |
| 1186 | struct machine *machine) |
| 1187 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1188 | struct perf_record_stat event; |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 1189 | |
| 1190 | event.header.type = PERF_RECORD_STAT; |
| 1191 | event.header.size = sizeof(event); |
| 1192 | event.header.misc = 0; |
| 1193 | |
| 1194 | event.id = id; |
| 1195 | event.cpu = cpu; |
| 1196 | event.thread = thread; |
| 1197 | event.val = count->val; |
| 1198 | event.ena = count->ena; |
| 1199 | event.run = count->run; |
| 1200 | |
| 1201 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 1202 | } |
| 1203 | |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 1204 | int perf_event__synthesize_stat_round(struct perf_tool *tool, |
| 1205 | u64 evtime, u64 type, |
| 1206 | perf_event__handler_t process, |
| 1207 | struct machine *machine) |
| 1208 | { |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1209 | struct perf_record_stat_round event; |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 1210 | |
| 1211 | event.header.type = PERF_RECORD_STAT_ROUND; |
| 1212 | event.header.size = sizeof(event); |
| 1213 | event.header.misc = 0; |
| 1214 | |
| 1215 | event.time = evtime; |
| 1216 | event.type = type; |
| 1217 | |
| 1218 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 1219 | } |
| 1220 | |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 1221 | void perf_event__read_stat_config(struct perf_stat_config *config, |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 1222 | struct perf_record_stat_config *event) |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 1223 | { |
| 1224 | unsigned i; |
| 1225 | |
| 1226 | for (i = 0; i < event->nr; i++) { |
| 1227 | |
| 1228 | switch (event->data[i].tag) { |
| 1229 | #define CASE(__term, __val) \ |
| 1230 | case PERF_STAT_CONFIG_TERM__##__term: \ |
| 1231 | config->__val = event->data[i].val; \ |
| 1232 | break; |
| 1233 | |
| 1234 | CASE(AGGR_MODE, aggr_mode) |
| 1235 | CASE(SCALE, scale) |
| 1236 | CASE(INTERVAL, interval) |
| 1237 | #undef CASE |
| 1238 | default: |
Jiri Olsa | c5f416e | 2019-08-28 15:57:09 +0200 | [diff] [blame] | 1239 | pr_warning("unknown stat config term %" PRI_lu64 "\n", |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 1240 | event->data[i].tag); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1245 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
| 1246 | { |
Adrian Hunter | 022c50d | 2014-07-14 13:02:27 +0300 | [diff] [blame] | 1247 | const char *s; |
| 1248 | |
| 1249 | if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) |
| 1250 | s = " exec"; |
| 1251 | else |
| 1252 | s = ""; |
| 1253 | |
Arnaldo Carvalho de Melo | 5067406 | 2015-02-24 17:20:31 -0300 | [diff] [blame] | 1254 | return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1257 | size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp) |
| 1258 | { |
| 1259 | size_t ret = 0; |
| 1260 | struct perf_ns_link_info *ns_link_info; |
| 1261 | u32 nr_namespaces, idx; |
| 1262 | |
| 1263 | ns_link_info = event->namespaces.link_info; |
| 1264 | nr_namespaces = event->namespaces.nr_namespaces; |
| 1265 | |
| 1266 | ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[", |
| 1267 | event->namespaces.pid, |
| 1268 | event->namespaces.tid, |
| 1269 | nr_namespaces); |
| 1270 | |
| 1271 | for (idx = 0; idx < nr_namespaces; idx++) { |
| 1272 | if (idx && (idx % 4 == 0)) |
| 1273 | ret += fprintf(fp, "\n\t\t "); |
| 1274 | |
| 1275 | ret += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx, |
| 1276 | perf_ns__name(idx), (u64)ns_link_info[idx].dev, |
| 1277 | (u64)ns_link_info[idx].ino, |
| 1278 | ((idx + 1) != nr_namespaces) ? ", " : "]\n"); |
| 1279 | } |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1284 | int perf_event__process_comm(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 1285 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1286 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1287 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1288 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1289 | return machine__process_comm_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1290 | } |
| 1291 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1292 | int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused, |
| 1293 | union perf_event *event, |
| 1294 | struct perf_sample *sample, |
| 1295 | struct machine *machine) |
| 1296 | { |
| 1297 | return machine__process_namespaces_event(machine, event, sample); |
| 1298 | } |
| 1299 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1300 | int perf_event__process_lost(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 1301 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1302 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1303 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1304 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1305 | return machine__process_lost_event(machine, event, sample); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1306 | } |
| 1307 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1308 | int perf_event__process_aux(struct perf_tool *tool __maybe_unused, |
| 1309 | union perf_event *event, |
| 1310 | struct perf_sample *sample __maybe_unused, |
| 1311 | struct machine *machine) |
| 1312 | { |
| 1313 | return machine__process_aux_event(machine, event); |
| 1314 | } |
| 1315 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1316 | int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused, |
| 1317 | union perf_event *event, |
| 1318 | struct perf_sample *sample __maybe_unused, |
| 1319 | struct machine *machine) |
| 1320 | { |
| 1321 | return machine__process_itrace_start_event(machine, event); |
| 1322 | } |
| 1323 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 1324 | int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused, |
| 1325 | union perf_event *event, |
| 1326 | struct perf_sample *sample, |
| 1327 | struct machine *machine) |
| 1328 | { |
| 1329 | return machine__process_lost_samples_event(machine, event, sample); |
| 1330 | } |
| 1331 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1332 | int perf_event__process_switch(struct perf_tool *tool __maybe_unused, |
| 1333 | union perf_event *event, |
| 1334 | struct perf_sample *sample __maybe_unused, |
| 1335 | struct machine *machine) |
| 1336 | { |
| 1337 | return machine__process_switch_event(machine, event); |
| 1338 | } |
| 1339 | |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 1340 | int perf_event__process_ksymbol(struct perf_tool *tool __maybe_unused, |
| 1341 | union perf_event *event, |
| 1342 | struct perf_sample *sample __maybe_unused, |
| 1343 | struct machine *machine) |
| 1344 | { |
| 1345 | return machine__process_ksymbol(machine, event, sample); |
| 1346 | } |
| 1347 | |
Arnaldo Carvalho de Melo | 3f604b5 | 2019-08-26 19:28:13 -0300 | [diff] [blame] | 1348 | int perf_event__process_bpf(struct perf_tool *tool __maybe_unused, |
| 1349 | union perf_event *event, |
| 1350 | struct perf_sample *sample, |
| 1351 | struct machine *machine) |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1352 | { |
Arnaldo Carvalho de Melo | 3f604b5 | 2019-08-26 19:28:13 -0300 | [diff] [blame] | 1353 | return machine__process_bpf(machine, event, sample); |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1356 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
| 1357 | { |
Jiri Olsa | 1345e2e | 2019-08-25 20:17:41 +0200 | [diff] [blame] | 1358 | return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 "]: %c %s\n", |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1359 | event->mmap.pid, event->mmap.tid, event->mmap.start, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 1360 | event->mmap.len, event->mmap.pgoff, |
| 1361 | (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', |
| 1362 | event->mmap.filename); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1365 | size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) |
| 1366 | { |
Jiri Olsa | b66ced1 | 2019-08-25 20:17:42 +0200 | [diff] [blame] | 1367 | return fprintf(fp, " %d/%d: [%#" PRI_lx64 "(%#" PRI_lx64 ") @ %#" PRI_lx64 |
| 1368 | " %02x:%02x %"PRI_lu64" %"PRI_lu64"]: %c%c%c%c %s\n", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1369 | event->mmap2.pid, event->mmap2.tid, event->mmap2.start, |
| 1370 | event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, |
| 1371 | event->mmap2.min, event->mmap2.ino, |
| 1372 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1373 | (event->mmap2.prot & PROT_READ) ? 'r' : '-', |
| 1374 | (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', |
| 1375 | (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', |
| 1376 | (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1377 | event->mmap2.filename); |
| 1378 | } |
| 1379 | |
Jiri Olsa | ec7fa596 | 2015-10-25 15:51:22 +0100 | [diff] [blame] | 1380 | size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp) |
| 1381 | { |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 1382 | struct perf_thread_map *threads = thread_map__new_event(&event->thread_map); |
Jiri Olsa | ec7fa596 | 2015-10-25 15:51:22 +0100 | [diff] [blame] | 1383 | size_t ret; |
| 1384 | |
| 1385 | ret = fprintf(fp, " nr: "); |
| 1386 | |
| 1387 | if (threads) |
| 1388 | ret += thread_map__fprintf(threads, fp); |
| 1389 | else |
| 1390 | ret += fprintf(fp, "failed to get threads from event\n"); |
| 1391 | |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 1392 | perf_thread_map__put(threads); |
Jiri Olsa | ec7fa596 | 2015-10-25 15:51:22 +0100 | [diff] [blame] | 1393 | return ret; |
| 1394 | } |
| 1395 | |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1396 | size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp) |
| 1397 | { |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 1398 | struct perf_cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data); |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1399 | size_t ret; |
| 1400 | |
Jiri Olsa | a24020e | 2016-06-28 13:29:04 +0200 | [diff] [blame] | 1401 | ret = fprintf(fp, ": "); |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1402 | |
| 1403 | if (cpus) |
| 1404 | ret += cpu_map__fprintf(cpus, fp); |
| 1405 | else |
| 1406 | ret += fprintf(fp, "failed to get cpumap from event\n"); |
| 1407 | |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 1408 | perf_cpu_map__put(cpus); |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1409 | return ret; |
| 1410 | } |
| 1411 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1412 | int perf_event__process_mmap(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 1413 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1414 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1415 | struct machine *machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1416 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1417 | return machine__process_mmap_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1420 | int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, |
| 1421 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1422 | struct perf_sample *sample, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1423 | struct machine *machine) |
| 1424 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1425 | return machine__process_mmap2_event(machine, event, sample); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1428 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
| 1429 | { |
| 1430 | return fprintf(fp, "(%d:%d):(%d:%d)\n", |
| 1431 | event->fork.pid, event->fork.tid, |
| 1432 | event->fork.ppid, event->fork.ptid); |
| 1433 | } |
| 1434 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1435 | int perf_event__process_fork(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 1436 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1437 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1438 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1439 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1440 | return machine__process_fork_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1441 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1442 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1443 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
| 1444 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1445 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1446 | struct machine *machine) |
| 1447 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1448 | return machine__process_exit_event(machine, event, sample); |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1449 | } |
| 1450 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1451 | size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp) |
| 1452 | { |
Jiri Olsa | aedebdc | 2019-08-28 15:57:05 +0200 | [diff] [blame] | 1453 | return fprintf(fp, " offset: %#"PRI_lx64" size: %#"PRI_lx64" flags: %#"PRI_lx64" [%s%s%s]\n", |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1454 | event->aux.aux_offset, event->aux.aux_size, |
| 1455 | event->aux.flags, |
| 1456 | event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "", |
Alexander Shishkin | 05a1f47 | 2017-03-16 18:41:59 +0200 | [diff] [blame] | 1457 | event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "", |
| 1458 | event->aux.flags & PERF_AUX_FLAG_PARTIAL ? "P" : ""); |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1459 | } |
| 1460 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1461 | size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) |
| 1462 | { |
| 1463 | return fprintf(fp, " pid: %u tid: %u\n", |
| 1464 | event->itrace_start.pid, event->itrace_start.tid); |
| 1465 | } |
| 1466 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1467 | size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp) |
| 1468 | { |
| 1469 | bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; |
Alexey Budankov | b3f35b5 | 2018-04-09 10:26:05 +0300 | [diff] [blame] | 1470 | const char *in_out = !out ? "IN " : |
| 1471 | !(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT) ? |
| 1472 | "OUT " : "OUT preempt"; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1473 | |
| 1474 | if (event->header.type == PERF_RECORD_SWITCH) |
| 1475 | return fprintf(fp, " %s\n", in_out); |
| 1476 | |
| 1477 | return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n", |
| 1478 | in_out, out ? "next" : "prev", |
| 1479 | event->context_switch.next_prev_pid, |
| 1480 | event->context_switch.next_prev_tid); |
| 1481 | } |
| 1482 | |
Jiri Olsa | 3d7c27b | 2018-01-07 17:03:53 +0100 | [diff] [blame] | 1483 | static size_t perf_event__fprintf_lost(union perf_event *event, FILE *fp) |
| 1484 | { |
Jiri Olsa | 5290ed6 | 2019-08-25 20:17:46 +0200 | [diff] [blame] | 1485 | return fprintf(fp, " lost %" PRI_lu64 "\n", event->lost.lost); |
Jiri Olsa | 3d7c27b | 2018-01-07 17:03:53 +0100 | [diff] [blame] | 1486 | } |
| 1487 | |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 1488 | size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp) |
| 1489 | { |
Jiri Olsa | f15e3c2 | 2019-08-25 20:17:50 +0200 | [diff] [blame] | 1490 | return fprintf(fp, " addr %" PRI_lx64 " len %u type %u flags 0x%x name %s\n", |
Arnaldo Carvalho de Melo | ebdba16 | 2019-08-26 19:15:18 -0300 | [diff] [blame] | 1491 | event->ksymbol.addr, event->ksymbol.len, |
| 1492 | event->ksymbol.ksym_type, |
| 1493 | event->ksymbol.flags, event->ksymbol.name); |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Arnaldo Carvalho de Melo | 3f604b5 | 2019-08-26 19:28:13 -0300 | [diff] [blame] | 1496 | size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp) |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1497 | { |
Jiri Olsa | 8201787 | 2019-05-08 15:20:09 +0200 | [diff] [blame] | 1498 | return fprintf(fp, " type %u, flags %u, id %u\n", |
Arnaldo Carvalho de Melo | 6a1b359 | 2019-08-26 19:20:35 -0300 | [diff] [blame] | 1499 | event->bpf.type, event->bpf.flags, event->bpf.id); |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1502 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
| 1503 | { |
| 1504 | size_t ret = fprintf(fp, "PERF_RECORD_%s", |
| 1505 | perf_event__name(event->header.type)); |
| 1506 | |
| 1507 | switch (event->header.type) { |
| 1508 | case PERF_RECORD_COMM: |
| 1509 | ret += perf_event__fprintf_comm(event, fp); |
| 1510 | break; |
| 1511 | case PERF_RECORD_FORK: |
| 1512 | case PERF_RECORD_EXIT: |
| 1513 | ret += perf_event__fprintf_task(event, fp); |
| 1514 | break; |
| 1515 | case PERF_RECORD_MMAP: |
| 1516 | ret += perf_event__fprintf_mmap(event, fp); |
| 1517 | break; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1518 | case PERF_RECORD_NAMESPACES: |
| 1519 | ret += perf_event__fprintf_namespaces(event, fp); |
| 1520 | break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1521 | case PERF_RECORD_MMAP2: |
| 1522 | ret += perf_event__fprintf_mmap2(event, fp); |
| 1523 | break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1524 | case PERF_RECORD_AUX: |
| 1525 | ret += perf_event__fprintf_aux(event, fp); |
| 1526 | break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1527 | case PERF_RECORD_ITRACE_START: |
| 1528 | ret += perf_event__fprintf_itrace_start(event, fp); |
| 1529 | break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1530 | case PERF_RECORD_SWITCH: |
| 1531 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 1532 | ret += perf_event__fprintf_switch(event, fp); |
| 1533 | break; |
Jiri Olsa | 3d7c27b | 2018-01-07 17:03:53 +0100 | [diff] [blame] | 1534 | case PERF_RECORD_LOST: |
| 1535 | ret += perf_event__fprintf_lost(event, fp); |
| 1536 | break; |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 1537 | case PERF_RECORD_KSYMBOL: |
| 1538 | ret += perf_event__fprintf_ksymbol(event, fp); |
| 1539 | break; |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1540 | case PERF_RECORD_BPF_EVENT: |
Arnaldo Carvalho de Melo | 3f604b5 | 2019-08-26 19:28:13 -0300 | [diff] [blame] | 1541 | ret += perf_event__fprintf_bpf(event, fp); |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 1542 | break; |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1543 | default: |
| 1544 | ret += fprintf(fp, "\n"); |
| 1545 | } |
| 1546 | |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1550 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
| 1551 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1552 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1553 | struct machine *machine) |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1554 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1555 | return machine__process_event(machine, event, sample); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1556 | } |
| 1557 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1558 | struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr, |
| 1559 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1560 | { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 1561 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1562 | struct machine *machine = mg->machine; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1563 | bool load_map = false; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1564 | |
Arnaldo Carvalho de Melo | cc22e57 | 2013-12-19 17:20:06 -0300 | [diff] [blame] | 1565 | al->machine = machine; |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1566 | al->thread = thread; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1567 | al->addr = addr; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1568 | al->cpumode = cpumode; |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1569 | al->filtered = 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1570 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1571 | if (machine == NULL) { |
| 1572 | al->map = NULL; |
Arnaldo Carvalho de Melo | 71a84b5 | 2018-04-24 11:58:56 -0300 | [diff] [blame] | 1573 | return NULL; |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1576 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1577 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1578 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1579 | load_map = true; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1580 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1581 | al->level = '.'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1582 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
| 1583 | al->level = 'g'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1584 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1585 | load_map = true; |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1586 | } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { |
| 1587 | al->level = 'u'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1588 | } else { |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1589 | al->level = 'H'; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1590 | al->map = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1591 | |
| 1592 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || |
| 1593 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && |
| 1594 | !perf_guest) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1595 | al->filtered |= (1 << HIST_FILTER__GUEST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1596 | if ((cpumode == PERF_RECORD_MISC_USER || |
| 1597 | cpumode == PERF_RECORD_MISC_KERNEL) && |
| 1598 | !perf_host) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1599 | al->filtered |= (1 << HIST_FILTER__HOST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1600 | |
Arnaldo Carvalho de Melo | 71a84b5 | 2018-04-24 11:58:56 -0300 | [diff] [blame] | 1601 | return NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1602 | } |
Arnaldo Carvalho de Melo | edeb0c9 | 2018-10-16 17:08:29 -0300 | [diff] [blame] | 1603 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1604 | al->map = map_groups__find(mg, al->addr); |
Arnaldo Carvalho de Melo | edeb0c9 | 2018-10-16 17:08:29 -0300 | [diff] [blame] | 1605 | if (al->map != NULL) { |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1606 | /* |
| 1607 | * Kernel maps might be changed when loading symbols so loading |
| 1608 | * must be done prior to using kernel maps. |
| 1609 | */ |
| 1610 | if (load_map) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1611 | map__load(al->map); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1612 | al->addr = al->map->map_ip(al->map, al->addr); |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1613 | } |
Arnaldo Carvalho de Melo | 71a84b5 | 2018-04-24 11:58:56 -0300 | [diff] [blame] | 1614 | |
| 1615 | return al->map; |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1616 | } |
| 1617 | |
Adrian Hunter | 8e80ad9 | 2018-11-06 23:07:10 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * For branch stacks or branch samples, the sample cpumode might not be correct |
| 1620 | * because it applies only to the sample 'ip' and not necessary to 'addr' or |
| 1621 | * branch stack addresses. If possible, use a fallback to deal with those cases. |
| 1622 | */ |
| 1623 | struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr, |
| 1624 | struct addr_location *al) |
| 1625 | { |
| 1626 | struct map *map = thread__find_map(thread, cpumode, addr, al); |
| 1627 | struct machine *machine = thread->mg->machine; |
| 1628 | u8 addr_cpumode = machine__addr_cpumode(machine, cpumode, addr); |
| 1629 | |
| 1630 | if (map || addr_cpumode == cpumode) |
| 1631 | return map; |
| 1632 | |
| 1633 | return thread__find_map(thread, addr_cpumode, addr, al); |
| 1634 | } |
| 1635 | |
Arnaldo Carvalho de Melo | 117d3c2 | 2018-04-25 18:16:53 -0300 | [diff] [blame] | 1636 | struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode, |
| 1637 | u64 addr, struct addr_location *al) |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1638 | { |
Arnaldo Carvalho de Melo | d9a5f27 | 2018-04-24 12:05:48 -0300 | [diff] [blame] | 1639 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 404eb5a | 2018-04-26 09:34:37 -0300 | [diff] [blame] | 1640 | if (thread__find_map(thread, cpumode, addr, al)) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1641 | al->sym = map__find_symbol(al->map, al->addr); |
Arnaldo Carvalho de Melo | d9a5f27 | 2018-04-24 12:05:48 -0300 | [diff] [blame] | 1642 | return al->sym; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1643 | } |
| 1644 | |
Adrian Hunter | 8e80ad9 | 2018-11-06 23:07:10 +0200 | [diff] [blame] | 1645 | struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode, |
| 1646 | u64 addr, struct addr_location *al) |
| 1647 | { |
| 1648 | al->sym = NULL; |
| 1649 | if (thread__find_map_fb(thread, cpumode, addr, al)) |
| 1650 | al->sym = map__find_symbol(al->map, al->addr); |
| 1651 | return al->sym; |
| 1652 | } |
| 1653 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1654 | /* |
| 1655 | * Callers need to drop the reference to al->thread, obtained in |
| 1656 | * machine__findnew_thread() |
| 1657 | */ |
Arnaldo Carvalho de Melo | bb3eb56 | 2016-03-22 18:39:09 -0300 | [diff] [blame] | 1658 | int machine__resolve(struct machine *machine, struct addr_location *al, |
| 1659 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1660 | { |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 1661 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 1662 | sample->tid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1663 | |
| 1664 | if (thread == NULL) |
| 1665 | return -1; |
| 1666 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1667 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
Arnaldo Carvalho de Melo | f07a2d3 | 2018-04-24 10:49:50 -0300 | [diff] [blame] | 1668 | thread__find_map(thread, sample->cpumode, sample->ip, al); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1669 | dump_printf(" ...... dso: %s\n", |
| 1670 | al->map ? al->map->dso->long_name : |
| 1671 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Namhyung Kim | 466fa76 | 2014-03-17 17:12:27 -0300 | [diff] [blame] | 1672 | |
| 1673 | if (thread__is_filtered(thread)) |
| 1674 | al->filtered |= (1 << HIST_FILTER__THREAD); |
| 1675 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1676 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 1677 | al->cpu = sample->cpu; |
Kan Liang | 0c4c4deb | 2015-09-04 10:45:42 -0400 | [diff] [blame] | 1678 | al->socket = -1; |
Milian Wolff | 1fb7d06 | 2017-10-19 13:38:35 +0200 | [diff] [blame] | 1679 | al->srcline = NULL; |
Kan Liang | 0c4c4deb | 2015-09-04 10:45:42 -0400 | [diff] [blame] | 1680 | |
| 1681 | if (al->cpu >= 0) { |
| 1682 | struct perf_env *env = machine->env; |
| 1683 | |
| 1684 | if (env && env->cpu) |
| 1685 | al->socket = env->cpu[al->cpu].socket_id; |
| 1686 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1687 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1688 | if (al->map) { |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1689 | struct dso *dso = al->map->dso; |
| 1690 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1691 | if (symbol_conf.dso_list && |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1692 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
| 1693 | dso->short_name) || |
| 1694 | (dso->short_name != dso->long_name && |
| 1695 | strlist__has_entry(symbol_conf.dso_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1696 | dso->long_name))))) { |
| 1697 | al->filtered |= (1 << HIST_FILTER__DSO); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1698 | } |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1699 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1700 | al->sym = map__find_symbol(al->map, al->addr); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1701 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1702 | |
Feng Tang | 1500b93 | 2012-09-07 16:42:23 +0800 | [diff] [blame] | 1703 | if (symbol_conf.sym_list && |
| 1704 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1705 | al->sym->name))) { |
| 1706 | al->filtered |= (1 << HIST_FILTER__SYMBOL); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1707 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1708 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1709 | return 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1710 | } |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1711 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1712 | /* |
| 1713 | * The preprocess_sample method will return with reference counts for the |
| 1714 | * in it, when done using (and perhaps getting ref counts if needing to |
| 1715 | * keep a pointer to one of those entries) it must be paired with |
| 1716 | * addr_location__put(), so that the refcounts can be decremented. |
| 1717 | */ |
| 1718 | void addr_location__put(struct addr_location *al) |
| 1719 | { |
| 1720 | thread__zput(al->thread); |
| 1721 | } |
| 1722 | |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1723 | bool is_bts_event(struct perf_event_attr *attr) |
| 1724 | { |
| 1725 | return attr->type == PERF_TYPE_HARDWARE && |
| 1726 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
| 1727 | attr->sample_period == 1; |
| 1728 | } |
| 1729 | |
| 1730 | bool sample_addr_correlates_sym(struct perf_event_attr *attr) |
| 1731 | { |
| 1732 | if (attr->type == PERF_TYPE_SOFTWARE && |
| 1733 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS || |
| 1734 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || |
| 1735 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) |
| 1736 | return true; |
| 1737 | |
| 1738 | if (is_bts_event(attr)) |
| 1739 | return true; |
| 1740 | |
| 1741 | return false; |
| 1742 | } |
| 1743 | |
Arnaldo Carvalho de Melo | c2740a8 | 2016-03-22 18:44:46 -0300 | [diff] [blame] | 1744 | void thread__resolve(struct thread *thread, struct addr_location *al, |
| 1745 | struct perf_sample *sample) |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1746 | { |
Adrian Hunter | 225f99e | 2018-11-06 23:07:11 +0200 | [diff] [blame] | 1747 | thread__find_map_fb(thread, sample->cpumode, sample->addr, al); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1748 | |
| 1749 | al->cpu = sample->cpu; |
| 1750 | al->sym = NULL; |
| 1751 | |
| 1752 | if (al->map) |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1753 | al->sym = map__find_symbol(al->map, al->addr); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1754 | } |