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

perf record: Put a copy of kcore into the perf.data directory

Add a new 'perf record' option '--kcore' which will put a copy of
/proc/kcore, kallsyms and modules into a perf.data directory. Note, that
without the --kcore option, output goes to a file as previously. The
tools' -o and -i options work with either a file name or directory name.

Example:

$ sudo perf record --kcore uname

$ sudo tree perf.data
perf.data
├── kcore_dir
│   ├── kallsyms
│   ├── kcore
│   └── modules
└── data

$ sudo perf script -v
build id event received for vmlinux: 1eaa285996affce2d74d8e66dcea09a80c9941de
build id event received for [vdso]: 8bbaf5dc62a9b644b4d4e4539737e104e4a84541
Samples for 'cycles' event do not have CPU attribute set. Skipping 'cpu' field.
Using CPUID GenuineIntel-6-8E-A
Using perf.data/kcore_dir/kcore for kernel data
Using perf.data/kcore_dir/kallsyms for symbols
perf 19058 506778.423729: 1 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux)
perf 19058 506778.423733: 1 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux)
perf 19058 506778.423734: 7 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux)
perf 19058 506778.423736: 117 cycles: ffffffffa2caa54a native_write_msr+0xa (vmlinux)
perf 19058 506778.423738: 2092 cycles: ffffffffa2c9b7b0 native_apic_msr_write+0x0 (vmlinux)
perf 19058 506778.423740: 37380 cycles: ffffffffa2f121d0 perf_event_addr_filters_exec+0x0 (vmlinux)
uname 19058 506778.423751: 582673 cycles: ffffffffa303a407 propagate_protected_usage+0x147 (vmlinux)
uname 19058 506778.423892: 2241841 cycles: ffffffffa2cae0c9 unwind_next_frame.part.5+0x79 (vmlinux)
uname 19058 506778.424430: 2457397 cycles: ffffffffa3019232 check_memory_region+0x52 (vmlinux)

Committer testing:

# rm -rf perf.data*
# perf record sleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.024 MB perf.data (7 samples) ]
# ls -l perf.data
-rw-------. 1 root root 34772 Oct 21 11:08 perf.data
# perf record --kcore uname
Linux
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.024 MB perf.data (7 samples) ]
ls[root@quaco ~]# ls -lad perf.data*
drwx------. 3 root root 4096 Oct 21 11:08 perf.data
-rw-------. 1 root root 34772 Oct 21 11:08 perf.data.old
# perf evlist -v
cycles: size: 112, { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|PERIOD, read_format: ID, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1, precise_ip: 3, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
# perf evlist -v -i perf.data/data
cycles: size: 112, { sample_period, sample_freq }: 4000, sample_type: IP|TID|TIME|PERIOD, read_format: ID, disabled: 1, inherit: 1, mmap: 1, comm: 1, freq: 1, enable_on_exec: 1, task: 1, precise_ip: 3, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, bpf_event: 1
#

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Link: http://lore.kernel.org/lkml/20191004083121.12182-6-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
eeb399b5 46e201ef

+147
+3
tools/perf/Documentation/perf-record.txt
··· 571 571 572 572 Implies --tail-synthesize. 573 573 574 + --kcore:: 575 + Make a copy of /proc/kcore and place it into a directory with the perf data file. 576 + 574 577 SEE ALSO 575 578 -------- 576 579 linkperf:perf-stat[1], linkperf:perf-list[1]
+35
tools/perf/Documentation/perf.data-directory-format.txt
··· 26 26 27 27 Future versions are expected to describe different data files 28 28 layout according to special needs. 29 + 30 + Currently the only 'perf record' option to output to a directory is 31 + the --kcore option which puts a copy of /proc/kcore into the directory. 32 + e.g. 33 + 34 + $ sudo perf record --kcore uname 35 + Linux 36 + [ perf record: Woken up 1 times to write data ] 37 + [ perf record: Captured and wrote 0.015 MB perf.data (9 samples) ] 38 + $ sudo tree -ps perf.data 39 + perf.data 40 + ├── [-rw------- 23744] data 41 + └── [drwx------ 4096] kcore_dir 42 + ├── [-r-------- 6731125] kallsyms 43 + ├── [-r-------- 40230912] kcore 44 + └── [-r-------- 5419] modules 45 + 46 + 1 directory, 4 files 47 + $ sudo perf script -v 48 + build id event received for vmlinux: 1eaa285996affce2d74d8e66dcea09a80c9941de 49 + build id event received for [vdso]: 8bbaf5dc62a9b644b4d4e4539737e104e4a84541 50 + build id event received for /lib/x86_64-linux-gnu/libc-2.28.so: 5b157f49586a3ca84d55837f97ff466767dd3445 51 + Samples for 'cycles' event do not have CPU attribute set. Skipping 'cpu' field. 52 + Using CPUID GenuineIntel-6-8E-A 53 + Using perf.data/kcore_dir/kcore for kernel data 54 + Using perf.data/kcore_dir/kallsyms for symbols 55 + perf 15316 2060795.480902: 1 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux) 56 + perf 15316 2060795.480906: 1 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux) 57 + perf 15316 2060795.480908: 7 cycles: ffffffffa2caa548 native_write_msr+0x8 (vmlinux) 58 + perf 15316 2060795.480910: 119 cycles: ffffffffa2caa54a native_write_msr+0xa (vmlinux) 59 + perf 15316 2060795.480912: 2109 cycles: ffffffffa2c9b7b0 native_apic_msr_write+0x0 (vmlinux) 60 + perf 15316 2060795.480914: 37606 cycles: ffffffffa2f121fe perf_event_addr_filters_exec+0x2e (vmlinux) 61 + uname 15316 2060795.480924: 588287 cycles: ffffffffa303a56d page_counter_try_charge+0x6d (vmlinux) 62 + uname 15316 2060795.481067: 2261945 cycles: ffffffffa301438f kmem_cache_free+0x4f (vmlinux) 63 + uname 15316 2060795.481643: 2172167 cycles: 7f1a48c393c0 _IO_un_link+0x0 (/lib/x86_64-linux-gnu/libc-2.28.so)
+52
tools/perf/builtin-record.c
··· 55 55 #include <signal.h> 56 56 #include <sys/mman.h> 57 57 #include <sys/wait.h> 58 + #include <sys/types.h> 59 + #include <sys/stat.h> 60 + #include <fcntl.h> 58 61 #include <linux/err.h> 59 62 #include <linux/string.h> 60 63 #include <linux/time64.h> ··· 701 698 } 702 699 703 700 #endif 701 + 702 + static bool record__kcore_readable(struct machine *machine) 703 + { 704 + char kcore[PATH_MAX]; 705 + int fd; 706 + 707 + scnprintf(kcore, sizeof(kcore), "%s/proc/kcore", machine->root_dir); 708 + 709 + fd = open(kcore, O_RDONLY); 710 + if (fd < 0) 711 + return false; 712 + 713 + close(fd); 714 + 715 + return true; 716 + } 717 + 718 + static int record__kcore_copy(struct machine *machine, struct perf_data *data) 719 + { 720 + char from_dir[PATH_MAX]; 721 + char kcore_dir[PATH_MAX]; 722 + int ret; 723 + 724 + snprintf(from_dir, sizeof(from_dir), "%s/proc", machine->root_dir); 725 + 726 + ret = perf_data__make_kcore_dir(data, kcore_dir, sizeof(kcore_dir)); 727 + if (ret) 728 + return ret; 729 + 730 + return kcore_copy(from_dir, kcore_dir); 731 + } 704 732 705 733 static int record__mmap_evlist(struct record *rec, 706 734 struct evlist *evlist) ··· 1417 1383 session->header.env.comp_type = PERF_COMP_ZSTD; 1418 1384 session->header.env.comp_level = rec->opts.comp_level; 1419 1385 1386 + if (rec->opts.kcore && 1387 + !record__kcore_readable(&session->machines.host)) { 1388 + pr_err("ERROR: kcore is not readable.\n"); 1389 + return -1; 1390 + } 1391 + 1420 1392 record__init_features(rec); 1421 1393 1422 1394 if (rec->opts.use_clockid && rec->opts.clockid_res_ns) ··· 1453 1413 goto out_child; 1454 1414 } 1455 1415 session->header.env.comp_mmap_len = session->evlist->core.mmap_len; 1416 + 1417 + if (rec->opts.kcore) { 1418 + err = record__kcore_copy(&session->machines.host, data); 1419 + if (err) { 1420 + pr_err("ERROR: Failed to copy kcore\n"); 1421 + goto out_child; 1422 + } 1423 + } 1456 1424 1457 1425 err = bpf__apply_obj_config(); 1458 1426 if (err) { ··· 2232 2184 parse_cgroups), 2233 2185 OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 2234 2186 "ms to wait before starting measurement after program start"), 2187 + OPT_BOOLEAN(0, "kcore", &record.opts.kcore, "copy /proc/kcore"), 2235 2188 OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 2236 2189 "user to profile"), 2237 2190 ··· 2370 2321 "cgroup monitoring only available in system-wide mode"); 2371 2322 2372 2323 } 2324 + 2325 + if (rec->opts.kcore) 2326 + rec->data.is_dir = true; 2373 2327 2374 2328 if (rec->opts.comp_level != 0) { 2375 2329 pr_debug("Compression enabled, disabling build id collection at the end of the session.\n");
+33
tools/perf/util/data.c
··· 424 424 425 425 return size; 426 426 } 427 + 428 + int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz) 429 + { 430 + int ret; 431 + 432 + if (!data->is_dir) 433 + return -1; 434 + 435 + ret = snprintf(buf, buf_sz, "%s/kcore_dir", data->path); 436 + if (ret < 0 || (size_t)ret >= buf_sz) 437 + return -1; 438 + 439 + return mkdir(buf, S_IRWXU); 440 + } 441 + 442 + char *perf_data__kallsyms_name(struct perf_data *data) 443 + { 444 + char *kallsyms_name; 445 + struct stat st; 446 + 447 + if (!data->is_dir) 448 + return NULL; 449 + 450 + if (asprintf(&kallsyms_name, "%s/kcore_dir/kallsyms", data->path) < 0) 451 + return NULL; 452 + 453 + if (stat(kallsyms_name, &st)) { 454 + free(kallsyms_name); 455 + return NULL; 456 + } 457 + 458 + return kallsyms_name; 459 + }
+2
tools/perf/util/data.h
··· 87 87 void perf_data__close_dir(struct perf_data *data); 88 88 int perf_data__update_dir(struct perf_data *data); 89 89 unsigned long perf_data__size(struct perf_data *data); 90 + int perf_data__make_kcore_dir(struct perf_data *data, char *buf, size_t buf_sz); 91 + char *perf_data__kallsyms_name(struct perf_data *data); 90 92 #endif /* __PERF_DATA_H */
+1
tools/perf/util/record.h
··· 44 44 bool strict_freq; 45 45 bool sample_id; 46 46 bool no_bpf_event; 47 + bool kcore; 47 48 unsigned int freq; 48 49 unsigned int mmap_pages; 49 50 unsigned int auxtrace_mmap_pages;
+4
tools/perf/util/session.c
··· 230 230 if (ret) 231 231 goto out_delete; 232 232 } 233 + 234 + if (!symbol_conf.kallsyms_name && 235 + !symbol_conf.vmlinux_name) 236 + symbol_conf.kallsyms_name = perf_data__kallsyms_name(data); 233 237 } 234 238 } else { 235 239 session->machines.host.env = &perf_env;
+17
tools/perf/util/util.c
··· 182 182 return rmdir(path); 183 183 } 184 184 185 + static int rm_rf_kcore_dir(const char *path) 186 + { 187 + char kcore_dir_path[PATH_MAX]; 188 + const char *pat[] = { 189 + "kcore", 190 + "kallsyms", 191 + "modules", 192 + NULL, 193 + }; 194 + 195 + snprintf(kcore_dir_path, sizeof(kcore_dir_path), "%s/kcore_dir", path); 196 + 197 + return rm_rf_depth_pat(kcore_dir_path, 0, pat); 198 + } 199 + 185 200 int rm_rf_perf_data(const char *path) 186 201 { 187 202 const char *pat[] = { ··· 204 189 "data.*", 205 190 NULL, 206 191 }; 192 + 193 + rm_rf_kcore_dir(path); 207 194 208 195 return rm_rf_depth_pat(path, 0, pat); 209 196 }