Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf script: Simplify auxiliary event printing functions

This simplifies the print functions for the following perf script
options:

--show-task-events
--show-namespace-events
--show-cgroup-events
--show-mmap-events
--show-switch-events
--show-lost-events
--show-bpf-events

Example:
# perf record --switch-events -a -e cycles -c 10000 sleep 1
Before:
# perf script --show-task-events --show-namespace-events --show-cgroup-events --show-mmap-events --show-switch-events --show-lost-events --show-bpf-events > out-before.txt
After:
# perf script --show-task-events --show-namespace-events --show-cgroup-events --show-mmap-events --show-switch-events --show-lost-events --show-bpf-events > out-after.txt
# diff -s out-before.txt out-after.txt
Files out-before.txt and out-after.tx are identical

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Link: http://lore.kernel.org/lkml/20200402141548.21283-1-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Adrian Hunter and committed by
Arnaldo Carvalho de Melo
1a2725f3 025b16f8

+70 -242
+70 -242
tools/perf/builtin-script.c
··· 2040 2040 2041 2041 static bool filter_cpu(struct perf_sample *sample) 2042 2042 { 2043 - if (cpu_list) 2043 + if (cpu_list && sample->cpu != (u32)-1) 2044 2044 return !test_bit(sample->cpu, cpu_bitmap); 2045 2045 return false; 2046 2046 } ··· 2138 2138 return err; 2139 2139 } 2140 2140 2141 + static int print_event_with_time(struct perf_tool *tool, 2142 + union perf_event *event, 2143 + struct perf_sample *sample, 2144 + struct machine *machine, 2145 + pid_t pid, pid_t tid, u64 timestamp) 2146 + { 2147 + struct perf_script *script = container_of(tool, struct perf_script, tool); 2148 + struct perf_session *session = script->session; 2149 + struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2150 + struct thread *thread = NULL; 2151 + 2152 + if (evsel && !evsel->core.attr.sample_id_all) { 2153 + sample->cpu = 0; 2154 + sample->time = timestamp; 2155 + sample->pid = pid; 2156 + sample->tid = tid; 2157 + } 2158 + 2159 + if (filter_cpu(sample)) 2160 + return 0; 2161 + 2162 + if (tid != -1) 2163 + thread = machine__findnew_thread(machine, pid, tid); 2164 + 2165 + if (thread && evsel) { 2166 + perf_sample__fprintf_start(sample, thread, evsel, 2167 + event->header.type, stdout); 2168 + } 2169 + 2170 + perf_event__fprintf(event, stdout); 2171 + 2172 + thread__put(thread); 2173 + 2174 + return 0; 2175 + } 2176 + 2177 + static int print_event(struct perf_tool *tool, union perf_event *event, 2178 + struct perf_sample *sample, struct machine *machine, 2179 + pid_t pid, pid_t tid) 2180 + { 2181 + return print_event_with_time(tool, event, sample, machine, pid, tid, 0); 2182 + } 2183 + 2141 2184 static int process_comm_event(struct perf_tool *tool, 2142 2185 union perf_event *event, 2143 2186 struct perf_sample *sample, 2144 2187 struct machine *machine) 2145 2188 { 2146 - struct thread *thread; 2147 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2148 - struct perf_session *session = script->session; 2149 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2150 - int ret = -1; 2151 - 2152 - thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid); 2153 - if (thread == NULL) { 2154 - pr_debug("problem processing COMM event, skipping it.\n"); 2155 - return -1; 2156 - } 2157 - 2158 2189 if (perf_event__process_comm(tool, event, sample, machine) < 0) 2159 - goto out; 2190 + return -1; 2160 2191 2161 - if (!evsel->core.attr.sample_id_all) { 2162 - sample->cpu = 0; 2163 - sample->time = 0; 2164 - sample->tid = event->comm.tid; 2165 - sample->pid = event->comm.pid; 2166 - } 2167 - if (!filter_cpu(sample)) { 2168 - perf_sample__fprintf_start(sample, thread, evsel, 2169 - PERF_RECORD_COMM, stdout); 2170 - perf_event__fprintf(event, stdout); 2171 - } 2172 - ret = 0; 2173 - out: 2174 - thread__put(thread); 2175 - return ret; 2192 + return print_event(tool, event, sample, machine, event->comm.pid, 2193 + event->comm.tid); 2176 2194 } 2177 2195 2178 2196 static int process_namespaces_event(struct perf_tool *tool, ··· 2198 2180 struct perf_sample *sample, 2199 2181 struct machine *machine) 2200 2182 { 2201 - struct thread *thread; 2202 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2203 - struct perf_session *session = script->session; 2204 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2205 - int ret = -1; 2206 - 2207 - thread = machine__findnew_thread(machine, event->namespaces.pid, 2208 - event->namespaces.tid); 2209 - if (thread == NULL) { 2210 - pr_debug("problem processing NAMESPACES event, skipping it.\n"); 2211 - return -1; 2212 - } 2213 - 2214 2183 if (perf_event__process_namespaces(tool, event, sample, machine) < 0) 2215 - goto out; 2184 + return -1; 2216 2185 2217 - if (!evsel->core.attr.sample_id_all) { 2218 - sample->cpu = 0; 2219 - sample->time = 0; 2220 - sample->tid = event->namespaces.tid; 2221 - sample->pid = event->namespaces.pid; 2222 - } 2223 - if (!filter_cpu(sample)) { 2224 - perf_sample__fprintf_start(sample, thread, evsel, 2225 - PERF_RECORD_NAMESPACES, stdout); 2226 - perf_event__fprintf(event, stdout); 2227 - } 2228 - ret = 0; 2229 - out: 2230 - thread__put(thread); 2231 - return ret; 2186 + return print_event(tool, event, sample, machine, event->namespaces.pid, 2187 + event->namespaces.tid); 2232 2188 } 2233 2189 2234 2190 static int process_cgroup_event(struct perf_tool *tool, ··· 2210 2218 struct perf_sample *sample, 2211 2219 struct machine *machine) 2212 2220 { 2213 - struct thread *thread; 2214 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2215 - struct perf_session *session = script->session; 2216 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2217 - int ret = -1; 2218 - 2219 - thread = machine__findnew_thread(machine, sample->pid, sample->tid); 2220 - if (thread == NULL) { 2221 - pr_debug("problem processing CGROUP event, skipping it.\n"); 2222 - return -1; 2223 - } 2224 - 2225 2221 if (perf_event__process_cgroup(tool, event, sample, machine) < 0) 2226 - goto out; 2222 + return -1; 2227 2223 2228 - if (!evsel->core.attr.sample_id_all) { 2229 - sample->cpu = 0; 2230 - sample->time = 0; 2231 - } 2232 - if (!filter_cpu(sample)) { 2233 - perf_sample__fprintf_start(sample, thread, evsel, 2234 - PERF_RECORD_CGROUP, stdout); 2235 - perf_event__fprintf(event, stdout); 2236 - } 2237 - ret = 0; 2238 - out: 2239 - thread__put(thread); 2240 - return ret; 2224 + return print_event(tool, event, sample, machine, sample->pid, 2225 + sample->tid); 2241 2226 } 2242 2227 2243 2228 static int process_fork_event(struct perf_tool *tool, ··· 2222 2253 struct perf_sample *sample, 2223 2254 struct machine *machine) 2224 2255 { 2225 - struct thread *thread; 2226 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2227 - struct perf_session *session = script->session; 2228 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2229 - 2230 2256 if (perf_event__process_fork(tool, event, sample, machine) < 0) 2231 2257 return -1; 2232 2258 2233 - thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 2234 - if (thread == NULL) { 2235 - pr_debug("problem processing FORK event, skipping it.\n"); 2236 - return -1; 2237 - } 2238 - 2239 - if (!evsel->core.attr.sample_id_all) { 2240 - sample->cpu = 0; 2241 - sample->time = event->fork.time; 2242 - sample->tid = event->fork.tid; 2243 - sample->pid = event->fork.pid; 2244 - } 2245 - if (!filter_cpu(sample)) { 2246 - perf_sample__fprintf_start(sample, thread, evsel, 2247 - PERF_RECORD_FORK, stdout); 2248 - perf_event__fprintf(event, stdout); 2249 - } 2250 - thread__put(thread); 2251 - 2252 - return 0; 2259 + return print_event_with_time(tool, event, sample, machine, 2260 + event->fork.pid, event->fork.tid, 2261 + event->fork.time); 2253 2262 } 2254 2263 static int process_exit_event(struct perf_tool *tool, 2255 2264 union perf_event *event, 2256 2265 struct perf_sample *sample, 2257 2266 struct machine *machine) 2258 2267 { 2259 - int err = 0; 2260 - struct thread *thread; 2261 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2262 - struct perf_session *session = script->session; 2263 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2264 - 2265 - thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); 2266 - if (thread == NULL) { 2267 - pr_debug("problem processing EXIT event, skipping it.\n"); 2268 + /* Print before 'exit' deletes anything */ 2269 + if (print_event_with_time(tool, event, sample, machine, event->fork.pid, 2270 + event->fork.tid, event->fork.time)) 2268 2271 return -1; 2269 - } 2270 2272 2271 - if (!evsel->core.attr.sample_id_all) { 2272 - sample->cpu = 0; 2273 - sample->time = 0; 2274 - sample->tid = event->fork.tid; 2275 - sample->pid = event->fork.pid; 2276 - } 2277 - if (!filter_cpu(sample)) { 2278 - perf_sample__fprintf_start(sample, thread, evsel, 2279 - PERF_RECORD_EXIT, stdout); 2280 - perf_event__fprintf(event, stdout); 2281 - } 2282 - 2283 - if (perf_event__process_exit(tool, event, sample, machine) < 0) 2284 - err = -1; 2285 - 2286 - thread__put(thread); 2287 - return err; 2273 + return perf_event__process_exit(tool, event, sample, machine); 2288 2274 } 2289 2275 2290 2276 static int process_mmap_event(struct perf_tool *tool, ··· 2247 2323 struct perf_sample *sample, 2248 2324 struct machine *machine) 2249 2325 { 2250 - struct thread *thread; 2251 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2252 - struct perf_session *session = script->session; 2253 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2254 - 2255 2326 if (perf_event__process_mmap(tool, event, sample, machine) < 0) 2256 2327 return -1; 2257 2328 2258 - thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid); 2259 - if (thread == NULL) { 2260 - pr_debug("problem processing MMAP event, skipping it.\n"); 2261 - return -1; 2262 - } 2263 - 2264 - if (!evsel->core.attr.sample_id_all) { 2265 - sample->cpu = 0; 2266 - sample->time = 0; 2267 - sample->tid = event->mmap.tid; 2268 - sample->pid = event->mmap.pid; 2269 - } 2270 - if (!filter_cpu(sample)) { 2271 - perf_sample__fprintf_start(sample, thread, evsel, 2272 - PERF_RECORD_MMAP, stdout); 2273 - perf_event__fprintf(event, stdout); 2274 - } 2275 - thread__put(thread); 2276 - return 0; 2329 + return print_event(tool, event, sample, machine, event->mmap.pid, 2330 + event->mmap.tid); 2277 2331 } 2278 2332 2279 2333 static int process_mmap2_event(struct perf_tool *tool, ··· 2259 2357 struct perf_sample *sample, 2260 2358 struct machine *machine) 2261 2359 { 2262 - struct thread *thread; 2263 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2264 - struct perf_session *session = script->session; 2265 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2266 - 2267 2360 if (perf_event__process_mmap2(tool, event, sample, machine) < 0) 2268 2361 return -1; 2269 2362 2270 - thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid); 2271 - if (thread == NULL) { 2272 - pr_debug("problem processing MMAP2 event, skipping it.\n"); 2273 - return -1; 2274 - } 2275 - 2276 - if (!evsel->core.attr.sample_id_all) { 2277 - sample->cpu = 0; 2278 - sample->time = 0; 2279 - sample->tid = event->mmap2.tid; 2280 - sample->pid = event->mmap2.pid; 2281 - } 2282 - if (!filter_cpu(sample)) { 2283 - perf_sample__fprintf_start(sample, thread, evsel, 2284 - PERF_RECORD_MMAP2, stdout); 2285 - perf_event__fprintf(event, stdout); 2286 - } 2287 - thread__put(thread); 2288 - return 0; 2363 + return print_event(tool, event, sample, machine, event->mmap2.pid, 2364 + event->mmap2.tid); 2289 2365 } 2290 2366 2291 2367 static int process_switch_event(struct perf_tool *tool, ··· 2271 2391 struct perf_sample *sample, 2272 2392 struct machine *machine) 2273 2393 { 2274 - struct thread *thread; 2275 2394 struct perf_script *script = container_of(tool, struct perf_script, tool); 2276 - struct perf_session *session = script->session; 2277 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2278 2395 2279 2396 if (perf_event__process_switch(tool, event, sample, machine) < 0) 2280 2397 return -1; ··· 2282 2405 if (!script->show_switch_events) 2283 2406 return 0; 2284 2407 2285 - thread = machine__findnew_thread(machine, sample->pid, 2286 - sample->tid); 2287 - if (thread == NULL) { 2288 - pr_debug("problem processing SWITCH event, skipping it.\n"); 2289 - return -1; 2290 - } 2291 - 2292 - if (!filter_cpu(sample)) { 2293 - perf_sample__fprintf_start(sample, thread, evsel, 2294 - PERF_RECORD_SWITCH, stdout); 2295 - perf_event__fprintf(event, stdout); 2296 - } 2297 - thread__put(thread); 2298 - return 0; 2408 + return print_event(tool, event, sample, machine, sample->pid, 2409 + sample->tid); 2299 2410 } 2300 2411 2301 2412 static int ··· 2292 2427 struct perf_sample *sample, 2293 2428 struct machine *machine) 2294 2429 { 2295 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2296 - struct perf_session *session = script->session; 2297 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2298 - struct thread *thread; 2299 - 2300 - thread = machine__findnew_thread(machine, sample->pid, 2301 - sample->tid); 2302 - if (thread == NULL) 2303 - return -1; 2304 - 2305 - if (!filter_cpu(sample)) { 2306 - perf_sample__fprintf_start(sample, thread, evsel, 2307 - PERF_RECORD_LOST, stdout); 2308 - perf_event__fprintf(event, stdout); 2309 - } 2310 - thread__put(thread); 2311 - return 0; 2430 + return print_event(tool, event, sample, machine, sample->pid, 2431 + sample->tid); 2312 2432 } 2313 2433 2314 2434 static int ··· 2312 2462 struct perf_sample *sample, 2313 2463 struct machine *machine) 2314 2464 { 2315 - struct thread *thread; 2316 - struct perf_script *script = container_of(tool, struct perf_script, tool); 2317 - struct perf_session *session = script->session; 2318 - struct evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id); 2319 - 2320 2465 if (machine__process_ksymbol(machine, event, sample) < 0) 2321 2466 return -1; 2322 2467 2323 - if (!evsel->core.attr.sample_id_all) { 2324 - perf_event__fprintf(event, stdout); 2325 - return 0; 2326 - } 2327 - 2328 - thread = machine__findnew_thread(machine, sample->pid, sample->tid); 2329 - if (thread == NULL) { 2330 - pr_debug("problem processing MMAP event, skipping it.\n"); 2331 - return -1; 2332 - } 2333 - 2334 - if (!filter_cpu(sample)) { 2335 - perf_sample__fprintf_start(sample, thread, evsel, 2336 - event->header.type, stdout); 2337 - perf_event__fprintf(event, stdout); 2338 - } 2339 - 2340 - thread__put(thread); 2341 - return 0; 2468 + return print_event(tool, event, sample, machine, sample->pid, 2469 + sample->tid); 2342 2470 } 2343 2471 2344 2472 static void sig_handler(int sig __maybe_unused)