Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 1 | #include <linux/types.h> |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 2 | #include <sys/mman.h> |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 3 | #include "event.h" |
| 4 | #include "debug.h" |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 5 | #include "hist.h" |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 6 | #include "machine.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 7 | #include "sort.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 8 | #include "string.h" |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 9 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 10 | #include "thread.h" |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 11 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 12 | #include "symbol/kallsyms.h" |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 13 | #include "asm/bug.h" |
| 14 | #include "stat.h" |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 16 | static const char *perf_event__names[] = { |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 17 | [0] = "TOTAL", |
| 18 | [PERF_RECORD_MMAP] = "MMAP", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 19 | [PERF_RECORD_MMAP2] = "MMAP2", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 20 | [PERF_RECORD_LOST] = "LOST", |
| 21 | [PERF_RECORD_COMM] = "COMM", |
| 22 | [PERF_RECORD_EXIT] = "EXIT", |
| 23 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 24 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 25 | [PERF_RECORD_FORK] = "FORK", |
| 26 | [PERF_RECORD_READ] = "READ", |
| 27 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 28 | [PERF_RECORD_AUX] = "AUX", |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 29 | [PERF_RECORD_ITRACE_START] = "ITRACE_START", |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 30 | [PERF_RECORD_LOST_SAMPLES] = "LOST_SAMPLES", |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 31 | [PERF_RECORD_SWITCH] = "SWITCH", |
| 32 | [PERF_RECORD_SWITCH_CPU_WIDE] = "SWITCH_CPU_WIDE", |
Frederic Weisbecker | 3cb6d15 | 2011-05-23 13:06:27 +0200 | [diff] [blame] | 33 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
| 34 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
| 35 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
| 36 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
| 37 | [PERF_RECORD_FINISHED_ROUND] = "FINISHED_ROUND", |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 38 | [PERF_RECORD_ID_INDEX] = "ID_INDEX", |
Adrian Hunter | a16ac02 | 2015-04-09 18:53:43 +0300 | [diff] [blame] | 39 | [PERF_RECORD_AUXTRACE_INFO] = "AUXTRACE_INFO", |
| 40 | [PERF_RECORD_AUXTRACE] = "AUXTRACE", |
Adrian Hunter | e9bf54d | 2015-04-09 18:53:47 +0300 | [diff] [blame] | 41 | [PERF_RECORD_AUXTRACE_ERROR] = "AUXTRACE_ERROR", |
Jiri Olsa | 5f3339d | 2015-10-25 15:51:19 +0100 | [diff] [blame] | 42 | [PERF_RECORD_THREAD_MAP] = "THREAD_MAP", |
Jiri Olsa | 6640b6c | 2015-10-25 15:51:23 +0100 | [diff] [blame] | 43 | [PERF_RECORD_CPU_MAP] = "CPU_MAP", |
Jiri Olsa | 374fb9e | 2015-10-25 15:51:27 +0100 | [diff] [blame] | 44 | [PERF_RECORD_STAT_CONFIG] = "STAT_CONFIG", |
Jiri Olsa | d80518c | 2015-10-25 15:51:30 +0100 | [diff] [blame] | 45 | [PERF_RECORD_STAT] = "STAT", |
Jiri Olsa | 2d8f0f1 | 2015-10-25 15:51:33 +0100 | [diff] [blame] | 46 | [PERF_RECORD_STAT_ROUND] = "STAT_ROUND", |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 47 | [PERF_RECORD_EVENT_UPDATE] = "EVENT_UPDATE", |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 48 | }; |
| 49 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 50 | const char *perf_event__name(unsigned int id) |
Thomas Gleixner | 3835bc0 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 51 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 52 | if (id >= ARRAY_SIZE(perf_event__names)) |
Thomas Gleixner | 3835bc0 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 53 | return "INVALID"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 54 | if (!perf_event__names[id]) |
Thomas Gleixner | 3835bc0 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 55 | return "UNKNOWN"; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 56 | return perf_event__names[id]; |
Thomas Gleixner | 3835bc0 | 2010-12-07 12:48:42 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 59 | static struct perf_sample synth_sample = { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 60 | .pid = -1, |
| 61 | .tid = -1, |
| 62 | .time = -1, |
| 63 | .stream_id = -1, |
| 64 | .cpu = -1, |
| 65 | .period = 1, |
| 66 | }; |
| 67 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 68 | /* |
| 69 | * Assumes that the first 4095 bytes of /proc/pid/stat contains |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 70 | * the comm, tgid and ppid. |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 71 | */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 72 | static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len, |
| 73 | pid_t *tgid, pid_t *ppid) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 74 | { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 75 | char filename[PATH_MAX]; |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 76 | char bf[4096]; |
| 77 | int fd; |
Andrzej Hajda | 3834966 | 2015-10-06 11:00:17 +0200 | [diff] [blame] | 78 | size_t size = 0; |
| 79 | ssize_t n; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 80 | char *nl, *name, *tgids, *ppids; |
| 81 | |
| 82 | *tgid = -1; |
| 83 | *ppid = -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 84 | |
| 85 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
| 86 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 87 | fd = open(filename, O_RDONLY); |
| 88 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 89 | pr_debug("couldn't open %s\n", filename); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 90 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 91 | } |
| 92 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 93 | n = read(fd, bf, sizeof(bf) - 1); |
| 94 | close(fd); |
| 95 | if (n <= 0) { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 96 | 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] | 97 | pid); |
| 98 | return -1; |
| 99 | } |
| 100 | bf[n] = '\0'; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 101 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 102 | name = strstr(bf, "Name:"); |
| 103 | tgids = strstr(bf, "Tgid:"); |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 104 | ppids = strstr(bf, "PPid:"); |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 105 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 106 | if (name) { |
| 107 | name += 5; /* strlen("Name:") */ |
| 108 | |
| 109 | while (*name && isspace(*name)) |
| 110 | ++name; |
| 111 | |
| 112 | nl = strchr(name, '\n'); |
| 113 | if (nl) |
| 114 | *nl = '\0'; |
| 115 | |
| 116 | size = strlen(name); |
| 117 | if (size >= len) |
| 118 | size = len - 1; |
| 119 | memcpy(comm, name, size); |
| 120 | comm[size] = '\0'; |
| 121 | } else { |
| 122 | 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] | 123 | } |
| 124 | |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 125 | if (tgids) { |
| 126 | tgids += 5; /* strlen("Tgid:") */ |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 127 | *tgid = atoi(tgids); |
David Ahern | 5aa0b03 | 2015-03-30 14:35:57 -0600 | [diff] [blame] | 128 | } else { |
| 129 | pr_debug("Tgid: string not found for pid %d\n", pid); |
| 130 | } |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 131 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 132 | if (ppids) { |
| 133 | ppids += 5; /* strlen("PPid:") */ |
| 134 | *ppid = atoi(ppids); |
| 135 | } else { |
| 136 | pr_debug("PPid: string not found for pid %d\n", pid); |
| 137 | } |
| 138 | |
| 139 | return 0; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 140 | } |
| 141 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 142 | static int perf_event__prepare_comm(union perf_event *event, pid_t pid, |
| 143 | struct machine *machine, |
| 144 | pid_t *tgid, pid_t *ppid) |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 145 | { |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 146 | size_t size; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 147 | |
| 148 | *ppid = -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 149 | |
| 150 | memset(&event->comm, 0, sizeof(event->comm)); |
| 151 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 152 | if (machine__is_host(machine)) { |
| 153 | if (perf_event__get_comm_ids(pid, event->comm.comm, |
| 154 | sizeof(event->comm.comm), |
| 155 | tgid, ppid) != 0) { |
| 156 | return -1; |
| 157 | } |
| 158 | } else { |
| 159 | *tgid = machine->pid; |
| 160 | } |
Dongsheng Yang | f5db57c4 | 2013-12-20 15:52:59 -0500 | [diff] [blame] | 161 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 162 | if (*tgid < 0) |
| 163 | return -1; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 164 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 165 | event->comm.pid = *tgid; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 166 | event->comm.header.type = PERF_RECORD_COMM; |
David Ahern | f5faf72 | 2011-12-22 11:30:02 -0700 | [diff] [blame] | 167 | |
| 168 | size = strlen(event->comm.comm) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 169 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 170 | memset(event->comm.comm + size, 0, machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 171 | event->comm.header.size = (sizeof(event->comm) - |
| 172 | (sizeof(event->comm.comm) - size) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 173 | machine->id_hdr_size); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 174 | event->comm.tid = pid; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 175 | |
| 176 | return 0; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 177 | } |
| 178 | |
Namhyung Kim | e803cf9 | 2015-09-22 09:24:55 +0900 | [diff] [blame] | 179 | pid_t perf_event__synthesize_comm(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 180 | union perf_event *event, pid_t pid, |
| 181 | perf_event__handler_t process, |
| 182 | struct machine *machine) |
| 183 | { |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 184 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 185 | |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 186 | if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0) |
| 187 | return -1; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 188 | |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 189 | if (process(tool, event, &synth_sample, machine) != 0) |
| 190 | return -1; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 191 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 192 | return tgid; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 193 | } |
| 194 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 195 | static int perf_event__synthesize_fork(struct perf_tool *tool, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 196 | union perf_event *event, |
| 197 | pid_t pid, pid_t tgid, pid_t ppid, |
| 198 | perf_event__handler_t process, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 199 | struct machine *machine) |
| 200 | { |
| 201 | memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size); |
| 202 | |
David Ahern | 7764a38 | 2015-04-09 12:48:27 -0400 | [diff] [blame] | 203 | /* |
| 204 | * for main thread set parent to ppid from status file. For other |
| 205 | * threads set parent pid to main thread. ie., assume main thread |
| 206 | * spawns all threads in a process |
| 207 | */ |
| 208 | if (tgid == pid) { |
| 209 | event->fork.ppid = ppid; |
| 210 | event->fork.ptid = ppid; |
| 211 | } else { |
| 212 | event->fork.ppid = tgid; |
| 213 | event->fork.ptid = tgid; |
| 214 | } |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 215 | event->fork.pid = tgid; |
| 216 | event->fork.tid = pid; |
| 217 | event->fork.header.type = PERF_RECORD_FORK; |
| 218 | |
| 219 | event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size); |
| 220 | |
| 221 | if (process(tool, event, &synth_sample, machine) != 0) |
| 222 | return -1; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Jiri Olsa | a18382b | 2014-01-07 13:47:20 +0100 | [diff] [blame] | 227 | int perf_event__synthesize_mmap_events(struct perf_tool *tool, |
| 228 | union perf_event *event, |
| 229 | pid_t pid, pid_t tgid, |
| 230 | perf_event__handler_t process, |
| 231 | struct machine *machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 232 | bool mmap_data, |
| 233 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 234 | { |
| 235 | char filename[PATH_MAX]; |
| 236 | FILE *fp; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 237 | unsigned long long t; |
| 238 | bool truncation = false; |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 239 | unsigned long long timeout = proc_map_timeout * 1000000ULL; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 240 | int rc = 0; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 241 | |
Dongsheng Yang | c239c25a | 2013-12-20 15:53:00 -0500 | [diff] [blame] | 242 | if (machine__is_default_guest(machine)) |
| 243 | return 0; |
| 244 | |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 245 | snprintf(filename, sizeof(filename), "%s/proc/%d/maps", |
| 246 | machine->root_dir, pid); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 247 | |
| 248 | fp = fopen(filename, "r"); |
| 249 | if (fp == NULL) { |
| 250 | /* |
| 251 | * We raced with a task exiting - just return: |
| 252 | */ |
| 253 | pr_debug("couldn't open %s\n", filename); |
| 254 | return -1; |
| 255 | } |
| 256 | |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 257 | event->header.type = PERF_RECORD_MMAP2; |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 258 | t = rdclock(); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 259 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 260 | while (1) { |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 261 | char bf[BUFSIZ]; |
| 262 | char prot[5]; |
| 263 | char execname[PATH_MAX]; |
| 264 | char anonstr[] = "//anon"; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 265 | unsigned int ino; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 266 | size_t size; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 267 | ssize_t n; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 268 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 269 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 270 | break; |
| 271 | |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 272 | if ((rdclock() - t) > timeout) { |
| 273 | pr_warning("Reading %s time out. " |
| 274 | "You may want to increase " |
| 275 | "the time limit by --proc-map-timeout\n", |
| 276 | filename); |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 277 | truncation = true; |
| 278 | goto out; |
| 279 | } |
| 280 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 281 | /* ensure null termination since stack will be reused. */ |
| 282 | strcpy(execname, ""); |
| 283 | |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 284 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 285 | n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %s\n", |
| 286 | &event->mmap2.start, &event->mmap2.len, prot, |
| 287 | &event->mmap2.pgoff, &event->mmap2.maj, |
| 288 | &event->mmap2.min, |
| 289 | &ino, execname); |
| 290 | |
Don Zickus | 9d4ecc8 | 2013-11-13 15:32:06 -0300 | [diff] [blame] | 291 | /* |
| 292 | * Anon maps don't have the execname. |
| 293 | */ |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 294 | if (n < 7) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 295 | continue; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 296 | |
| 297 | event->mmap2.ino = (u64)ino; |
| 298 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 299 | /* |
| 300 | * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c |
| 301 | */ |
Dongsheng Yang | 73547aa | 2013-12-20 15:52:58 -0500 | [diff] [blame] | 302 | if (machine__is_host(machine)) |
| 303 | event->header.misc = PERF_RECORD_MISC_USER; |
| 304 | else |
| 305 | event->header.misc = PERF_RECORD_MISC_GUEST_USER; |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 306 | |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 307 | /* map protection and flags bits */ |
| 308 | event->mmap2.prot = 0; |
| 309 | event->mmap2.flags = 0; |
| 310 | if (prot[0] == 'r') |
| 311 | event->mmap2.prot |= PROT_READ; |
| 312 | if (prot[1] == 'w') |
| 313 | event->mmap2.prot |= PROT_WRITE; |
| 314 | if (prot[2] == 'x') |
| 315 | event->mmap2.prot |= PROT_EXEC; |
| 316 | |
| 317 | if (prot[3] == 's') |
| 318 | event->mmap2.flags |= MAP_SHARED; |
| 319 | else |
| 320 | event->mmap2.flags |= MAP_PRIVATE; |
| 321 | |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 322 | if (prot[2] != 'x') { |
| 323 | if (!mmap_data || prot[0] != 'r') |
| 324 | continue; |
| 325 | |
| 326 | event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; |
| 327 | } |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 328 | |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 329 | out: |
| 330 | if (truncation) |
| 331 | event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT; |
| 332 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 333 | if (!strcmp(execname, "")) |
| 334 | strcpy(execname, anonstr); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 335 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 336 | size = strlen(execname) + 1; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 337 | memcpy(event->mmap2.filename, execname, size); |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 338 | size = PERF_ALIGN(size, sizeof(u64)); |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 339 | event->mmap2.len -= event->mmap.start; |
| 340 | event->mmap2.header.size = (sizeof(event->mmap2) - |
| 341 | (sizeof(event->mmap2.filename) - size)); |
| 342 | memset(event->mmap2.filename + size, 0, machine->id_hdr_size); |
| 343 | event->mmap2.header.size += machine->id_hdr_size; |
| 344 | event->mmap2.pid = tgid; |
| 345 | event->mmap2.tid = pid; |
Anton Blanchard | 6a0e55d | 2011-08-30 09:15:06 +1000 | [diff] [blame] | 346 | |
Namhyung Kim | 6064803 | 2012-11-11 23:20:50 +0900 | [diff] [blame] | 347 | if (process(tool, event, &synth_sample, machine) != 0) { |
| 348 | rc = -1; |
| 349 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 350 | } |
Kan Liang | 930e6fc | 2015-06-17 09:51:10 -0400 | [diff] [blame] | 351 | |
| 352 | if (truncation) |
| 353 | break; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | fclose(fp); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 357 | return rc; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 358 | } |
| 359 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 360 | int perf_event__synthesize_modules(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 361 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 362 | struct machine *machine) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 363 | { |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 364 | int rc = 0; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 365 | struct map *pos; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 366 | struct map_groups *kmaps = &machine->kmaps; |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 367 | struct maps *maps = &kmaps->maps[MAP__FUNCTION]; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 368 | union perf_event *event = zalloc((sizeof(event->mmap) + |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 369 | machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 370 | if (event == NULL) { |
| 371 | pr_debug("Not enough memory synthesizing mmap event " |
| 372 | "for kernel modules\n"); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | event->header.type = PERF_RECORD_MMAP; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 377 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 378 | /* |
| 379 | * kernel uses 0 for user space maps, see kernel/perf_event.c |
| 380 | * __perf_event_mmap |
| 381 | */ |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 382 | if (machine__is_host(machine)) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 383 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 384 | else |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 385 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 386 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 387 | for (pos = maps__first(maps); pos; pos = map__next(pos)) { |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 388 | size_t size; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 389 | |
Arnaldo Carvalho de Melo | ab9c2bd | 2015-09-23 15:45:20 -0300 | [diff] [blame] | 390 | if (__map__is_kernel(pos)) |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 391 | continue; |
| 392 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 393 | 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] | 394 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 395 | event->mmap.header.size = (sizeof(event->mmap) - |
| 396 | (sizeof(event->mmap.filename) - size)); |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 397 | memset(event->mmap.filename + size, 0, machine->id_hdr_size); |
| 398 | event->mmap.header.size += machine->id_hdr_size; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 399 | event->mmap.start = pos->start; |
| 400 | event->mmap.len = pos->end - pos->start; |
| 401 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 402 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 403 | memcpy(event->mmap.filename, pos->dso->long_name, |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 404 | pos->dso->long_name_len + 1); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 405 | if (process(tool, event, &synth_sample, machine) != 0) { |
| 406 | rc = -1; |
| 407 | break; |
| 408 | } |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 409 | } |
| 410 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 411 | free(event); |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 412 | return rc; |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 413 | } |
| 414 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 415 | static int __event__synthesize_thread(union perf_event *comm_event, |
| 416 | union perf_event *mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 417 | union perf_event *fork_event, |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 418 | pid_t pid, int full, |
| 419 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 420 | struct perf_tool *tool, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 421 | struct machine *machine, |
| 422 | bool mmap_data, |
| 423 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 424 | { |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 425 | char filename[PATH_MAX]; |
| 426 | DIR *tasks; |
| 427 | struct dirent dirent, *next; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 428 | pid_t tgid, ppid; |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 429 | int rc = 0; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 430 | |
| 431 | /* special case: only send one comm event using passed in pid */ |
| 432 | if (!full) { |
| 433 | tgid = perf_event__synthesize_comm(tool, comm_event, pid, |
| 434 | process, machine); |
| 435 | |
| 436 | if (tgid == -1) |
| 437 | return -1; |
| 438 | |
| 439 | return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 440 | process, machine, mmap_data, |
| 441 | proc_map_timeout); |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | if (machine__is_default_guest(machine)) |
| 445 | return 0; |
| 446 | |
| 447 | snprintf(filename, sizeof(filename), "%s/proc/%d/task", |
| 448 | machine->root_dir, pid); |
| 449 | |
| 450 | tasks = opendir(filename); |
| 451 | if (tasks == NULL) { |
| 452 | pr_debug("couldn't open %s\n", filename); |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | while (!readdir_r(tasks, &dirent, &next) && next) { |
| 457 | char *end; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 458 | pid_t _pid; |
| 459 | |
| 460 | _pid = strtol(dirent.d_name, &end, 10); |
| 461 | if (*end) |
| 462 | continue; |
| 463 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 464 | rc = -1; |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 465 | if (perf_event__prepare_comm(comm_event, _pid, machine, |
| 466 | &tgid, &ppid) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 467 | break; |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 468 | |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 469 | if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid, |
David Ahern | ca6c41c | 2015-03-30 14:35:58 -0600 | [diff] [blame] | 470 | ppid, process, machine) < 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 471 | break; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 472 | /* |
| 473 | * Send the prepared comm event |
| 474 | */ |
| 475 | if (process(tool, comm_event, &synth_sample, machine) != 0) |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 476 | break; |
Arnaldo Carvalho de Melo | 4aa5f4f | 2015-02-27 19:52:10 -0300 | [diff] [blame] | 477 | |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 478 | rc = 0; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 479 | if (_pid == pid) { |
| 480 | /* process the parent's maps too */ |
| 481 | rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 482 | process, machine, mmap_data, proc_map_timeout); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 483 | if (rc) |
| 484 | break; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 485 | } |
Don Zickus | bfd66cc | 2014-02-26 10:45:26 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | closedir(tasks); |
Arnaldo Carvalho de Melo | d998b73 | 2015-04-08 11:57:03 -0300 | [diff] [blame] | 489 | return rc; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 490 | } |
| 491 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 492 | int perf_event__synthesize_thread_map(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 493 | struct thread_map *threads, |
Arnaldo Carvalho de Melo | 7c940c1 | 2011-02-11 11:45:54 -0200 | [diff] [blame] | 494 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 495 | struct machine *machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 496 | bool mmap_data, |
| 497 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 498 | { |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 499 | union perf_event *comm_event, *mmap_event, *fork_event; |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 500 | int err = -1, thread, j; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 501 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 502 | 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] | 503 | if (comm_event == NULL) |
| 504 | goto out; |
| 505 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 506 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 507 | if (mmap_event == NULL) |
| 508 | goto out_free_comm; |
| 509 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 510 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 511 | if (fork_event == NULL) |
| 512 | goto out_free_mmap; |
| 513 | |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 514 | err = 0; |
| 515 | for (thread = 0; thread < threads->nr; ++thread) { |
| 516 | if (__event__synthesize_thread(comm_event, mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 517 | fork_event, |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 518 | thread_map__pid(threads, thread), 0, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 519 | process, tool, machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 520 | mmap_data, proc_map_timeout)) { |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 521 | err = -1; |
| 522 | break; |
| 523 | } |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * comm.pid is set to thread group id by |
| 527 | * perf_event__synthesize_comm |
| 528 | */ |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 529 | if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 530 | bool need_leader = true; |
| 531 | |
| 532 | /* is thread group leader in thread_map? */ |
| 533 | for (j = 0; j < threads->nr; ++j) { |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 534 | if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 535 | need_leader = false; |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* if not, generate events for it */ |
| 541 | if (need_leader && |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 542 | __event__synthesize_thread(comm_event, mmap_event, |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 543 | fork_event, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 544 | comm_event->comm.pid, 0, |
| 545 | process, tool, machine, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 546 | mmap_data, proc_map_timeout)) { |
David Ahern | defd8d3 | 2011-12-22 11:30:01 -0700 | [diff] [blame] | 547 | err = -1; |
| 548 | break; |
| 549 | } |
| 550 | } |
Arnaldo Carvalho de Melo | 401b8e1 | 2011-02-10 12:52:47 -0200 | [diff] [blame] | 551 | } |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 552 | free(fork_event); |
| 553 | out_free_mmap: |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 554 | free(mmap_event); |
| 555 | out_free_comm: |
| 556 | free(comm_event); |
| 557 | out: |
| 558 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 559 | } |
| 560 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 561 | int perf_event__synthesize_threads(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 562 | perf_event__handler_t process, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 563 | struct machine *machine, |
| 564 | bool mmap_data, |
| 565 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 566 | { |
| 567 | DIR *proc; |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 568 | char proc_path[PATH_MAX]; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 569 | struct dirent dirent, *next; |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 570 | union perf_event *comm_event, *mmap_event, *fork_event; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 571 | int err = -1; |
| 572 | |
Namhyung Kim | 574799b | 2014-03-17 10:45:49 +0900 | [diff] [blame] | 573 | if (machine__is_default_guest(machine)) |
| 574 | return 0; |
| 575 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 576 | 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] | 577 | if (comm_event == NULL) |
| 578 | goto out; |
| 579 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 580 | mmap_event = malloc(sizeof(mmap_event->mmap) + machine->id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 581 | if (mmap_event == NULL) |
| 582 | goto out_free_comm; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 583 | |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 584 | fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size); |
| 585 | if (fork_event == NULL) |
| 586 | goto out_free_mmap; |
| 587 | |
Dongsheng Yang | 9956346 | 2013-12-20 15:52:57 -0500 | [diff] [blame] | 588 | snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); |
| 589 | proc = opendir(proc_path); |
| 590 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 591 | if (proc == NULL) |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 592 | goto out_free_fork; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 593 | |
| 594 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 595 | char *end; |
| 596 | pid_t pid = strtol(dirent.d_name, &end, 10); |
| 597 | |
| 598 | if (*end) /* only interested in proper numerical dirents */ |
| 599 | continue; |
Arnaldo Carvalho de Melo | ba361c9 | 2012-10-17 18:50:13 -0300 | [diff] [blame] | 600 | /* |
| 601 | * We may race with exiting thread, so don't stop just because |
| 602 | * one thread couldn't be synthesized. |
| 603 | */ |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 604 | __event__synthesize_thread(comm_event, mmap_event, fork_event, pid, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 605 | 1, process, tool, machine, mmap_data, |
| 606 | proc_map_timeout); |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 607 | } |
| 608 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 609 | err = 0; |
David Ahern | 1e6d532 | 2012-08-26 12:24:42 -0600 | [diff] [blame] | 610 | closedir(proc); |
Don Zickus | 363b785 | 2014-03-14 10:43:44 -0400 | [diff] [blame] | 611 | out_free_fork: |
| 612 | free(fork_event); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 613 | out_free_mmap: |
| 614 | free(mmap_event); |
| 615 | out_free_comm: |
| 616 | free(comm_event); |
| 617 | out: |
| 618 | return err; |
Arnaldo Carvalho de Melo | 234fbbf | 2009-10-26 19:23:18 -0200 | [diff] [blame] | 619 | } |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 620 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 621 | struct process_symbol_args { |
| 622 | const char *name; |
| 623 | u64 start; |
| 624 | }; |
| 625 | |
Arnaldo Carvalho de Melo | 3b01a41 | 2010-12-22 01:08:36 -0200 | [diff] [blame] | 626 | 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] | 627 | u64 start) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 628 | { |
| 629 | struct process_symbol_args *args = arg; |
| 630 | |
Arnaldo Carvalho de Melo | 881516e | 2010-01-15 18:08:27 -0200 | [diff] [blame] | 631 | /* |
| 632 | * Must be a function or at least an alias, as in PARISC64, where "_text" is |
| 633 | * an 'A' to the same address as "_stext". |
| 634 | */ |
| 635 | if (!(symbol_type__is_a(type, MAP__FUNCTION) || |
| 636 | type == 'A') || strcmp(name, args->name)) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 637 | return 0; |
| 638 | |
| 639 | args->start = start; |
| 640 | return 1; |
| 641 | } |
| 642 | |
Adrian Hunter | 29b596b | 2014-01-29 16:14:37 +0200 | [diff] [blame] | 643 | u64 kallsyms__get_function_start(const char *kallsyms_filename, |
| 644 | const char *symbol_name) |
| 645 | { |
| 646 | struct process_symbol_args args = { .name = symbol_name, }; |
| 647 | |
| 648 | if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0) |
| 649 | return 0; |
| 650 | |
| 651 | return args.start; |
| 652 | } |
| 653 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 654 | int perf_event__synthesize_kernel_mmap(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 655 | perf_event__handler_t process, |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 656 | struct machine *machine) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 657 | { |
| 658 | size_t size; |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 659 | const char *mmap_name; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 660 | char name_buff[PATH_MAX]; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 661 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 662 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 663 | int err; |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 664 | union perf_event *event; |
| 665 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 666 | if (map == NULL) |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 667 | return -1; |
| 668 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 669 | /* |
| 670 | * We should get this from /sys/kernel/sections/.text, but till that is |
| 671 | * available use this, and after it is use this as a fallback for older |
| 672 | * kernels. |
| 673 | */ |
Andi Kleen | a5c2a4c | 2014-09-24 14:39:54 -0700 | [diff] [blame] | 674 | event = zalloc((sizeof(event->mmap) + machine->id_hdr_size)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 675 | if (event == NULL) { |
| 676 | pr_debug("Not enough memory synthesizing mmap event " |
| 677 | "for kernel modules\n"); |
| 678 | return -1; |
| 679 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 680 | |
Arnaldo Carvalho de Melo | 48ea8f5 | 2010-04-27 21:19:05 -0300 | [diff] [blame] | 681 | mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff)); |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 682 | if (machine__is_host(machine)) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 683 | /* |
| 684 | * kernel uses PERF_RECORD_MISC_USER for user space maps, |
| 685 | * see kernel/perf_event.c __perf_event_mmap |
| 686 | */ |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 687 | event->header.misc = PERF_RECORD_MISC_KERNEL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 688 | } else { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 689 | event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; |
Thomas Jarosch | 0b9e01a | 2013-01-25 11:20:47 +0100 | [diff] [blame] | 690 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 691 | |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 692 | kmap = map__kmap(map); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 693 | size = snprintf(event->mmap.filename, sizeof(event->mmap.filename), |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 694 | "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 695 | size = PERF_ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 696 | event->mmap.header.type = PERF_RECORD_MMAP; |
| 697 | event->mmap.header.size = (sizeof(event->mmap) - |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 698 | (sizeof(event->mmap.filename) - size) + machine->id_hdr_size); |
Adrian Hunter | 0ae617b | 2014-01-29 16:14:40 +0200 | [diff] [blame] | 699 | event->mmap.pgoff = kmap->ref_reloc_sym->addr; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 700 | event->mmap.start = map->start; |
| 701 | event->mmap.len = map->end - event->mmap.start; |
| 702 | event->mmap.pid = machine->pid; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 703 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 704 | err = process(tool, event, &synth_sample, machine); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 705 | free(event); |
| 706 | |
| 707 | return err; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 708 | } |
| 709 | |
Jiri Olsa | 99471c9 | 2015-10-25 15:51:20 +0100 | [diff] [blame] | 710 | int perf_event__synthesize_thread_map2(struct perf_tool *tool, |
| 711 | struct thread_map *threads, |
| 712 | perf_event__handler_t process, |
| 713 | struct machine *machine) |
| 714 | { |
| 715 | union perf_event *event; |
| 716 | int i, err, size; |
| 717 | |
| 718 | size = sizeof(event->thread_map); |
| 719 | size += threads->nr * sizeof(event->thread_map.entries[0]); |
| 720 | |
| 721 | event = zalloc(size); |
| 722 | if (!event) |
| 723 | return -ENOMEM; |
| 724 | |
| 725 | event->header.type = PERF_RECORD_THREAD_MAP; |
| 726 | event->header.size = size; |
| 727 | event->thread_map.nr = threads->nr; |
| 728 | |
| 729 | for (i = 0; i < threads->nr; i++) { |
| 730 | struct thread_map_event_entry *entry = &event->thread_map.entries[i]; |
| 731 | char *comm = thread_map__comm(threads, i); |
| 732 | |
| 733 | if (!comm) |
| 734 | comm = (char *) ""; |
| 735 | |
| 736 | entry->pid = thread_map__pid(threads, i); |
| 737 | strncpy((char *) &entry->comm, comm, sizeof(entry->comm)); |
| 738 | } |
| 739 | |
| 740 | err = process(tool, event, NULL, machine); |
| 741 | |
| 742 | free(event); |
| 743 | return err; |
| 744 | } |
| 745 | |
Jiri Olsa | 6c87290 | 2015-10-25 15:51:24 +0100 | [diff] [blame] | 746 | static void synthesize_cpus(struct cpu_map_entries *cpus, |
| 747 | struct cpu_map *map) |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | cpus->nr = map->nr; |
| 752 | |
| 753 | for (i = 0; i < map->nr; i++) |
| 754 | cpus->cpu[i] = map->map[i]; |
| 755 | } |
| 756 | |
| 757 | static void synthesize_mask(struct cpu_map_mask *mask, |
| 758 | struct cpu_map *map, int max) |
| 759 | { |
| 760 | int i; |
| 761 | |
| 762 | mask->nr = BITS_TO_LONGS(max); |
| 763 | mask->long_size = sizeof(long); |
| 764 | |
| 765 | for (i = 0; i < map->nr; i++) |
| 766 | set_bit(map->map[i], mask->mask); |
| 767 | } |
| 768 | |
| 769 | static size_t cpus_size(struct cpu_map *map) |
| 770 | { |
| 771 | return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16); |
| 772 | } |
| 773 | |
| 774 | static size_t mask_size(struct cpu_map *map, int *max) |
| 775 | { |
| 776 | int i; |
| 777 | |
| 778 | *max = 0; |
| 779 | |
| 780 | for (i = 0; i < map->nr; i++) { |
| 781 | /* bit possition of the cpu is + 1 */ |
| 782 | int bit = map->map[i] + 1; |
| 783 | |
| 784 | if (bit > *max) |
| 785 | *max = bit; |
| 786 | } |
| 787 | |
| 788 | return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long); |
| 789 | } |
| 790 | |
| 791 | void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max) |
| 792 | { |
| 793 | size_t size_cpus, size_mask; |
| 794 | bool is_dummy = cpu_map__empty(map); |
| 795 | |
| 796 | /* |
| 797 | * Both array and mask data have variable size based |
| 798 | * on the number of cpus and their actual values. |
| 799 | * The size of the 'struct cpu_map_data' is: |
| 800 | * |
| 801 | * array = size of 'struct cpu_map_entries' + |
| 802 | * number of cpus * sizeof(u64) |
| 803 | * |
| 804 | * mask = size of 'struct cpu_map_mask' + |
| 805 | * maximum cpu bit converted to size of longs |
| 806 | * |
| 807 | * and finaly + the size of 'struct cpu_map_data'. |
| 808 | */ |
| 809 | size_cpus = cpus_size(map); |
| 810 | size_mask = mask_size(map, max); |
| 811 | |
| 812 | if (is_dummy || (size_cpus < size_mask)) { |
| 813 | *size += size_cpus; |
| 814 | *type = PERF_CPU_MAP__CPUS; |
| 815 | } else { |
| 816 | *size += size_mask; |
| 817 | *type = PERF_CPU_MAP__MASK; |
| 818 | } |
| 819 | |
| 820 | *size += sizeof(struct cpu_map_data); |
| 821 | return zalloc(*size); |
| 822 | } |
| 823 | |
| 824 | void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map, |
| 825 | u16 type, int max) |
| 826 | { |
| 827 | data->type = type; |
| 828 | |
| 829 | switch (type) { |
| 830 | case PERF_CPU_MAP__CPUS: |
| 831 | synthesize_cpus((struct cpu_map_entries *) data->data, map); |
| 832 | break; |
| 833 | case PERF_CPU_MAP__MASK: |
| 834 | synthesize_mask((struct cpu_map_mask *) data->data, map, max); |
| 835 | default: |
| 836 | break; |
| 837 | }; |
| 838 | } |
| 839 | |
| 840 | static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map) |
| 841 | { |
| 842 | size_t size = sizeof(struct cpu_map_event); |
| 843 | struct cpu_map_event *event; |
| 844 | int max; |
| 845 | u16 type; |
| 846 | |
| 847 | event = cpu_map_data__alloc(map, &size, &type, &max); |
| 848 | if (!event) |
| 849 | return NULL; |
| 850 | |
| 851 | event->header.type = PERF_RECORD_CPU_MAP; |
| 852 | event->header.size = size; |
| 853 | event->data.type = type; |
| 854 | |
| 855 | cpu_map_data__synthesize(&event->data, map, type, max); |
| 856 | return event; |
| 857 | } |
| 858 | |
| 859 | int perf_event__synthesize_cpu_map(struct perf_tool *tool, |
| 860 | struct cpu_map *map, |
| 861 | perf_event__handler_t process, |
| 862 | struct machine *machine) |
| 863 | { |
| 864 | struct cpu_map_event *event; |
| 865 | int err; |
| 866 | |
| 867 | event = cpu_map_event__new(map); |
| 868 | if (!event) |
| 869 | return -ENOMEM; |
| 870 | |
| 871 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 872 | |
| 873 | free(event); |
| 874 | return err; |
| 875 | } |
| 876 | |
Jiri Olsa | 6742434 | 2015-10-25 15:51:28 +0100 | [diff] [blame] | 877 | int perf_event__synthesize_stat_config(struct perf_tool *tool, |
| 878 | struct perf_stat_config *config, |
| 879 | perf_event__handler_t process, |
| 880 | struct machine *machine) |
| 881 | { |
| 882 | struct stat_config_event *event; |
| 883 | int size, i = 0, err; |
| 884 | |
| 885 | size = sizeof(*event); |
| 886 | size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0])); |
| 887 | |
| 888 | event = zalloc(size); |
| 889 | if (!event) |
| 890 | return -ENOMEM; |
| 891 | |
| 892 | event->header.type = PERF_RECORD_STAT_CONFIG; |
| 893 | event->header.size = size; |
| 894 | event->nr = PERF_STAT_CONFIG_TERM__MAX; |
| 895 | |
| 896 | #define ADD(__term, __val) \ |
| 897 | event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \ |
| 898 | event->data[i].val = __val; \ |
| 899 | i++; |
| 900 | |
| 901 | ADD(AGGR_MODE, config->aggr_mode) |
| 902 | ADD(INTERVAL, config->interval) |
| 903 | ADD(SCALE, config->scale) |
| 904 | |
| 905 | WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX, |
| 906 | "stat config terms unbalanced\n"); |
| 907 | #undef ADD |
| 908 | |
| 909 | err = process(tool, (union perf_event *) event, NULL, machine); |
| 910 | |
| 911 | free(event); |
| 912 | return err; |
| 913 | } |
| 914 | |
Jiri Olsa | 5796f8f | 2015-10-25 15:51:31 +0100 | [diff] [blame] | 915 | int perf_event__synthesize_stat(struct perf_tool *tool, |
| 916 | u32 cpu, u32 thread, u64 id, |
| 917 | struct perf_counts_values *count, |
| 918 | perf_event__handler_t process, |
| 919 | struct machine *machine) |
| 920 | { |
| 921 | struct stat_event event; |
| 922 | |
| 923 | event.header.type = PERF_RECORD_STAT; |
| 924 | event.header.size = sizeof(event); |
| 925 | event.header.misc = 0; |
| 926 | |
| 927 | event.id = id; |
| 928 | event.cpu = cpu; |
| 929 | event.thread = thread; |
| 930 | event.val = count->val; |
| 931 | event.ena = count->ena; |
| 932 | event.run = count->run; |
| 933 | |
| 934 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 935 | } |
| 936 | |
Jiri Olsa | d4c2259 | 2015-10-25 15:51:34 +0100 | [diff] [blame] | 937 | int perf_event__synthesize_stat_round(struct perf_tool *tool, |
| 938 | u64 evtime, u64 type, |
| 939 | perf_event__handler_t process, |
| 940 | struct machine *machine) |
| 941 | { |
| 942 | struct stat_round_event event; |
| 943 | |
| 944 | event.header.type = PERF_RECORD_STAT_ROUND; |
| 945 | event.header.size = sizeof(event); |
| 946 | event.header.misc = 0; |
| 947 | |
| 948 | event.time = evtime; |
| 949 | event.type = type; |
| 950 | |
| 951 | return process(tool, (union perf_event *) &event, NULL, machine); |
| 952 | } |
| 953 | |
Jiri Olsa | 8e38159 | 2015-10-25 15:51:29 +0100 | [diff] [blame] | 954 | void perf_event__read_stat_config(struct perf_stat_config *config, |
| 955 | struct stat_config_event *event) |
| 956 | { |
| 957 | unsigned i; |
| 958 | |
| 959 | for (i = 0; i < event->nr; i++) { |
| 960 | |
| 961 | switch (event->data[i].tag) { |
| 962 | #define CASE(__term, __val) \ |
| 963 | case PERF_STAT_CONFIG_TERM__##__term: \ |
| 964 | config->__val = event->data[i].val; \ |
| 965 | break; |
| 966 | |
| 967 | CASE(AGGR_MODE, aggr_mode) |
| 968 | CASE(SCALE, scale) |
| 969 | CASE(INTERVAL, interval) |
| 970 | #undef CASE |
| 971 | default: |
| 972 | pr_warning("unknown stat config term %" PRIu64 "\n", |
| 973 | event->data[i].tag); |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 978 | size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp) |
| 979 | { |
Adrian Hunter | 022c50d | 2014-07-14 13:02:27 +0300 | [diff] [blame] | 980 | const char *s; |
| 981 | |
| 982 | if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC) |
| 983 | s = " exec"; |
| 984 | else |
| 985 | s = ""; |
| 986 | |
Arnaldo Carvalho de Melo | 5067406 | 2015-02-24 17:20:31 -0300 | [diff] [blame] | 987 | 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] | 988 | } |
| 989 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 990 | 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] | 991 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 992 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 993 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 994 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 995 | return machine__process_comm_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 996 | } |
| 997 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 998 | 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] | 999 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1000 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1001 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1002 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1003 | return machine__process_lost_event(machine, event, sample); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1006 | int perf_event__process_aux(struct perf_tool *tool __maybe_unused, |
| 1007 | union perf_event *event, |
| 1008 | struct perf_sample *sample __maybe_unused, |
| 1009 | struct machine *machine) |
| 1010 | { |
| 1011 | return machine__process_aux_event(machine, event); |
| 1012 | } |
| 1013 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1014 | int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused, |
| 1015 | union perf_event *event, |
| 1016 | struct perf_sample *sample __maybe_unused, |
| 1017 | struct machine *machine) |
| 1018 | { |
| 1019 | return machine__process_itrace_start_event(machine, event); |
| 1020 | } |
| 1021 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 1022 | int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused, |
| 1023 | union perf_event *event, |
| 1024 | struct perf_sample *sample, |
| 1025 | struct machine *machine) |
| 1026 | { |
| 1027 | return machine__process_lost_samples_event(machine, event, sample); |
| 1028 | } |
| 1029 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1030 | int perf_event__process_switch(struct perf_tool *tool __maybe_unused, |
| 1031 | union perf_event *event, |
| 1032 | struct perf_sample *sample __maybe_unused, |
| 1033 | struct machine *machine) |
| 1034 | { |
| 1035 | return machine__process_switch_event(machine, event); |
| 1036 | } |
| 1037 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1038 | size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp) |
| 1039 | { |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 1040 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n", |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1041 | event->mmap.pid, event->mmap.tid, event->mmap.start, |
Arnaldo Carvalho de Melo | 62605dc50 | 2013-11-11 09:44:09 -0300 | [diff] [blame] | 1042 | event->mmap.len, event->mmap.pgoff, |
| 1043 | (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x', |
| 1044 | event->mmap.filename); |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1047 | size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp) |
| 1048 | { |
| 1049 | return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1050 | " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n", |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1051 | event->mmap2.pid, event->mmap2.tid, event->mmap2.start, |
| 1052 | event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj, |
| 1053 | event->mmap2.min, event->mmap2.ino, |
| 1054 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1055 | (event->mmap2.prot & PROT_READ) ? 'r' : '-', |
| 1056 | (event->mmap2.prot & PROT_WRITE) ? 'w' : '-', |
| 1057 | (event->mmap2.prot & PROT_EXEC) ? 'x' : '-', |
| 1058 | (event->mmap2.flags & MAP_SHARED) ? 's' : 'p', |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1059 | event->mmap2.filename); |
| 1060 | } |
| 1061 | |
Jiri Olsa | ec7fa596 | 2015-10-25 15:51:22 +0100 | [diff] [blame] | 1062 | size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp) |
| 1063 | { |
| 1064 | struct thread_map *threads = thread_map__new_event(&event->thread_map); |
| 1065 | size_t ret; |
| 1066 | |
| 1067 | ret = fprintf(fp, " nr: "); |
| 1068 | |
| 1069 | if (threads) |
| 1070 | ret += thread_map__fprintf(threads, fp); |
| 1071 | else |
| 1072 | ret += fprintf(fp, "failed to get threads from event\n"); |
| 1073 | |
| 1074 | thread_map__put(threads); |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Jiri Olsa | eb12a1a | 2015-10-25 15:51:26 +0100 | [diff] [blame] | 1078 | size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp) |
| 1079 | { |
| 1080 | struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data); |
| 1081 | size_t ret; |
| 1082 | |
| 1083 | ret = fprintf(fp, " nr: "); |
| 1084 | |
| 1085 | if (cpus) |
| 1086 | ret += cpu_map__fprintf(cpus, fp); |
| 1087 | else |
| 1088 | ret += fprintf(fp, "failed to get cpumap from event\n"); |
| 1089 | |
| 1090 | cpu_map__put(cpus); |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1094 | 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] | 1095 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1096 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1097 | struct machine *machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1098 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1099 | return machine__process_mmap_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1102 | int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused, |
| 1103 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1104 | struct perf_sample *sample, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1105 | struct machine *machine) |
| 1106 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1107 | return machine__process_mmap2_event(machine, event, sample); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1110 | size_t perf_event__fprintf_task(union perf_event *event, FILE *fp) |
| 1111 | { |
| 1112 | return fprintf(fp, "(%d:%d):(%d:%d)\n", |
| 1113 | event->fork.pid, event->fork.tid, |
| 1114 | event->fork.ppid, event->fork.ptid); |
| 1115 | } |
| 1116 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1117 | 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] | 1118 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1119 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1120 | struct machine *machine) |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1121 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1122 | return machine__process_fork_event(machine, event, sample); |
Arnaldo Carvalho de Melo | 62daacb | 2009-11-27 16:29:22 -0200 | [diff] [blame] | 1123 | } |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1124 | |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1125 | int perf_event__process_exit(struct perf_tool *tool __maybe_unused, |
| 1126 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1127 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1128 | struct machine *machine) |
| 1129 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1130 | return machine__process_exit_event(machine, event, sample); |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 1131 | } |
| 1132 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1133 | size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp) |
| 1134 | { |
| 1135 | return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s]\n", |
| 1136 | event->aux.aux_offset, event->aux.aux_size, |
| 1137 | event->aux.flags, |
| 1138 | event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "", |
| 1139 | event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : ""); |
| 1140 | } |
| 1141 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1142 | size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp) |
| 1143 | { |
| 1144 | return fprintf(fp, " pid: %u tid: %u\n", |
| 1145 | event->itrace_start.pid, event->itrace_start.tid); |
| 1146 | } |
| 1147 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1148 | size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp) |
| 1149 | { |
| 1150 | bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; |
| 1151 | const char *in_out = out ? "OUT" : "IN "; |
| 1152 | |
| 1153 | if (event->header.type == PERF_RECORD_SWITCH) |
| 1154 | return fprintf(fp, " %s\n", in_out); |
| 1155 | |
| 1156 | return fprintf(fp, " %s %s pid/tid: %5u/%-5u\n", |
| 1157 | in_out, out ? "next" : "prev", |
| 1158 | event->context_switch.next_prev_pid, |
| 1159 | event->context_switch.next_prev_tid); |
| 1160 | } |
| 1161 | |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1162 | size_t perf_event__fprintf(union perf_event *event, FILE *fp) |
| 1163 | { |
| 1164 | size_t ret = fprintf(fp, "PERF_RECORD_%s", |
| 1165 | perf_event__name(event->header.type)); |
| 1166 | |
| 1167 | switch (event->header.type) { |
| 1168 | case PERF_RECORD_COMM: |
| 1169 | ret += perf_event__fprintf_comm(event, fp); |
| 1170 | break; |
| 1171 | case PERF_RECORD_FORK: |
| 1172 | case PERF_RECORD_EXIT: |
| 1173 | ret += perf_event__fprintf_task(event, fp); |
| 1174 | break; |
| 1175 | case PERF_RECORD_MMAP: |
| 1176 | ret += perf_event__fprintf_mmap(event, fp); |
| 1177 | break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1178 | case PERF_RECORD_MMAP2: |
| 1179 | ret += perf_event__fprintf_mmap2(event, fp); |
| 1180 | break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1181 | case PERF_RECORD_AUX: |
| 1182 | ret += perf_event__fprintf_aux(event, fp); |
| 1183 | break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1184 | case PERF_RECORD_ITRACE_START: |
| 1185 | ret += perf_event__fprintf_itrace_start(event, fp); |
| 1186 | break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1187 | case PERF_RECORD_SWITCH: |
| 1188 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 1189 | ret += perf_event__fprintf_switch(event, fp); |
| 1190 | break; |
Arnaldo Carvalho de Melo | 482ad89 | 2011-12-02 11:06:37 -0200 | [diff] [blame] | 1191 | default: |
| 1192 | ret += fprintf(fp, "\n"); |
| 1193 | } |
| 1194 | |
| 1195 | return ret; |
| 1196 | } |
| 1197 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1198 | int perf_event__process(struct perf_tool *tool __maybe_unused, |
| 1199 | union perf_event *event, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1200 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1201 | struct machine *machine) |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1202 | { |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1203 | return machine__process_event(machine, event, sample); |
Srikar Dronamraju | b83f920 | 2010-08-02 18:08:51 +0530 | [diff] [blame] | 1204 | } |
| 1205 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1206 | void thread__find_addr_map(struct thread *thread, u8 cpumode, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1207 | enum map_type type, u64 addr, |
Adrian Hunter | 326f59b | 2013-08-08 14:32:27 +0300 | [diff] [blame] | 1208 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1209 | { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 1210 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1211 | struct machine *machine = mg->machine; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1212 | bool load_map = false; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1213 | |
Arnaldo Carvalho de Melo | cc22e57 | 2013-12-19 17:20:06 -0300 | [diff] [blame] | 1214 | al->machine = machine; |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 1215 | al->thread = thread; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1216 | al->addr = addr; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1217 | al->cpumode = cpumode; |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1218 | al->filtered = 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1219 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1220 | if (machine == NULL) { |
| 1221 | al->map = NULL; |
| 1222 | return; |
| 1223 | } |
| 1224 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1225 | if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1226 | al->level = 'k'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1227 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1228 | load_map = true; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1229 | } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) { |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1230 | al->level = '.'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1231 | } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) { |
| 1232 | al->level = 'g'; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1233 | mg = &machine->kmaps; |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1234 | load_map = true; |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1235 | } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) { |
| 1236 | al->level = 'u'; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1237 | } else { |
Dongsheng Yang | fb50bb4 | 2013-12-20 15:52:56 -0500 | [diff] [blame] | 1238 | al->level = 'H'; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1239 | al->map = NULL; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1240 | |
| 1241 | if ((cpumode == PERF_RECORD_MISC_GUEST_USER || |
| 1242 | cpumode == PERF_RECORD_MISC_GUEST_KERNEL) && |
| 1243 | !perf_guest) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1244 | al->filtered |= (1 << HIST_FILTER__GUEST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1245 | if ((cpumode == PERF_RECORD_MISC_USER || |
| 1246 | cpumode == PERF_RECORD_MISC_KERNEL) && |
| 1247 | !perf_host) |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1248 | al->filtered |= (1 << HIST_FILTER__HOST); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1249 | |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1250 | return; |
| 1251 | } |
| 1252 | try_again: |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 1253 | al->map = map_groups__find(mg, type, al->addr); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1254 | if (al->map == NULL) { |
| 1255 | /* |
| 1256 | * If this is outside of all known maps, and is a negative |
| 1257 | * address, try to look it up in the kernel dso, as it might be |
| 1258 | * a vsyscall or vdso (which executes in user-mode). |
| 1259 | * |
| 1260 | * XXX This is nasty, we should have a symbol list in the |
| 1261 | * "[vdso]" dso, but for now lets use the old trick of looking |
| 1262 | * in the whole kernel symbol list. |
| 1263 | */ |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 1264 | if (cpumode == PERF_RECORD_MISC_USER && machine && |
| 1265 | mg != &machine->kmaps && |
| 1266 | machine__kernel_ip(machine, al->addr)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1267 | mg = &machine->kmaps; |
Adrian Hunter | 1f2a706 | 2014-07-14 13:02:31 +0300 | [diff] [blame] | 1268 | load_map = true; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1269 | goto try_again; |
| 1270 | } |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1271 | } else { |
| 1272 | /* |
| 1273 | * Kernel maps might be changed when loading symbols so loading |
| 1274 | * must be done prior to using kernel maps. |
| 1275 | */ |
| 1276 | if (load_map) |
Adrian Hunter | 326f59b | 2013-08-08 14:32:27 +0300 | [diff] [blame] | 1277 | map__load(al->map, machine->symbol_filter); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1278 | al->addr = al->map->map_ip(al->map, al->addr); |
Adrian Hunter | 5b7ba82 | 2013-08-07 14:38:46 +0300 | [diff] [blame] | 1279 | } |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1282 | void thread__find_addr_location(struct thread *thread, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1283 | u8 cpumode, enum map_type type, u64 addr, |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 1284 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1285 | { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1286 | thread__find_addr_map(thread, cpumode, type, addr, al); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1287 | if (al->map != NULL) |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 1288 | al->sym = map__find_symbol(al->map, al->addr, |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1289 | thread->mg->machine->symbol_filter); |
Arnaldo Carvalho de Melo | 59ee68e | 2010-01-14 23:45:29 -0200 | [diff] [blame] | 1290 | else |
| 1291 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1294 | /* |
| 1295 | * Callers need to drop the reference to al->thread, obtained in |
| 1296 | * machine__findnew_thread() |
| 1297 | */ |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1298 | int perf_event__preprocess_sample(const union perf_event *event, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1299 | struct machine *machine, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1300 | struct addr_location *al, |
Adrian Hunter | e44baa3 | 2013-08-08 14:32:25 +0300 | [diff] [blame] | 1301 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1302 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1303 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 1304 | struct thread *thread = machine__findnew_thread(machine, sample->pid, |
Namhyung Kim | 13ce34d | 2014-05-12 09:56:42 +0900 | [diff] [blame] | 1305 | sample->tid); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1306 | |
| 1307 | if (thread == NULL) |
| 1308 | return -1; |
| 1309 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 1310 | dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid); |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1311 | /* |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1312 | * Have we already created the kernel maps for this machine? |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1313 | * |
| 1314 | * This should have happened earlier, when we processed the kernel MMAP |
| 1315 | * events, but for older perf.data files there was no such thing, so do |
| 1316 | * it now. |
| 1317 | */ |
| 1318 | if (cpumode == PERF_RECORD_MISC_KERNEL && |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1319 | machine__kernel_map(machine) == NULL) |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1320 | machine__create_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1321 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1322 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, al); |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1323 | dump_printf(" ...... dso: %s\n", |
| 1324 | al->map ? al->map->dso->long_name : |
| 1325 | al->level == 'H' ? "[hypervisor]" : "<not found>"); |
Namhyung Kim | 466fa76 | 2014-03-17 17:12:27 -0300 | [diff] [blame] | 1326 | |
| 1327 | if (thread__is_filtered(thread)) |
| 1328 | al->filtered |= (1 << HIST_FILTER__THREAD); |
| 1329 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1330 | al->sym = NULL; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 1331 | al->cpu = sample->cpu; |
Kan Liang | 0c4c4de | 2015-09-04 10:45:42 -0400 | [diff] [blame] | 1332 | al->socket = -1; |
| 1333 | |
| 1334 | if (al->cpu >= 0) { |
| 1335 | struct perf_env *env = machine->env; |
| 1336 | |
| 1337 | if (env && env->cpu) |
| 1338 | al->socket = env->cpu[al->cpu].socket_id; |
| 1339 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1340 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1341 | if (al->map) { |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1342 | struct dso *dso = al->map->dso; |
| 1343 | |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1344 | if (symbol_conf.dso_list && |
Namhyung Kim | cb8f4e9 | 2011-12-13 00:16:55 +0900 | [diff] [blame] | 1345 | (!dso || !(strlist__has_entry(symbol_conf.dso_list, |
| 1346 | dso->short_name) || |
| 1347 | (dso->short_name != dso->long_name && |
| 1348 | strlist__has_entry(symbol_conf.dso_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1349 | dso->long_name))))) { |
| 1350 | al->filtered |= (1 << HIST_FILTER__DSO); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1351 | } |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1352 | |
Adrian Hunter | e44baa3 | 2013-08-08 14:32:25 +0300 | [diff] [blame] | 1353 | al->sym = map__find_symbol(al->map, al->addr, |
| 1354 | machine->symbol_filter); |
Arnaldo Carvalho de Melo | 96415e4 | 2010-03-24 16:40:15 -0300 | [diff] [blame] | 1355 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1356 | |
Feng Tang | 1500b93 | 2012-09-07 16:42:23 +0800 | [diff] [blame] | 1357 | if (symbol_conf.sym_list && |
| 1358 | (!al->sym || !strlist__has_entry(symbol_conf.sym_list, |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1359 | al->sym->name))) { |
| 1360 | al->filtered |= (1 << HIST_FILTER__SYMBOL); |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1361 | } |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1362 | |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 1363 | return 0; |
Arnaldo Carvalho de Melo | 1ed091c | 2009-11-27 16:29:23 -0200 | [diff] [blame] | 1364 | } |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1365 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1366 | /* |
| 1367 | * The preprocess_sample method will return with reference counts for the |
| 1368 | * in it, when done using (and perhaps getting ref counts if needing to |
| 1369 | * keep a pointer to one of those entries) it must be paired with |
| 1370 | * addr_location__put(), so that the refcounts can be decremented. |
| 1371 | */ |
| 1372 | void addr_location__put(struct addr_location *al) |
| 1373 | { |
| 1374 | thread__zput(al->thread); |
| 1375 | } |
| 1376 | |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1377 | bool is_bts_event(struct perf_event_attr *attr) |
| 1378 | { |
| 1379 | return attr->type == PERF_TYPE_HARDWARE && |
| 1380 | (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && |
| 1381 | attr->sample_period == 1; |
| 1382 | } |
| 1383 | |
| 1384 | bool sample_addr_correlates_sym(struct perf_event_attr *attr) |
| 1385 | { |
| 1386 | if (attr->type == PERF_TYPE_SOFTWARE && |
| 1387 | (attr->config == PERF_COUNT_SW_PAGE_FAULTS || |
| 1388 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN || |
| 1389 | attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ)) |
| 1390 | return true; |
| 1391 | |
| 1392 | if (is_bts_event(attr)) |
| 1393 | return true; |
| 1394 | |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | void perf_event__preprocess_sample_addr(union perf_event *event, |
| 1399 | struct perf_sample *sample, |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1400 | struct thread *thread, |
| 1401 | struct addr_location *al) |
| 1402 | { |
| 1403 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1404 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1405 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->addr, al); |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1406 | if (!al->map) |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1407 | thread__find_addr_map(thread, cpumode, MAP__VARIABLE, |
Adrian Hunter | 9b0d2d8 | 2014-07-22 16:17:39 +0300 | [diff] [blame] | 1408 | sample->addr, al); |
| 1409 | |
| 1410 | al->cpu = sample->cpu; |
| 1411 | al->sym = NULL; |
| 1412 | |
| 1413 | if (al->map) |
| 1414 | al->sym = map__find_symbol(al->map, al->addr, NULL); |
| 1415 | } |