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

perf data: Add global path holder

Add a 'path' member to 'struct perf_data'. It will keep the configured
path for the data (const char *). The path in struct perf_data_file is
now dynamically allocated (duped) from it.

This scheme is useful/used in following patches where struct
perf_data::path holds the 'configure' directory path and struct
perf_data_file::path holds the allocated path for specific files.

Also it actually makes the code little simpler.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20190221094145.9151-3-jolsa@kernel.org
[ Fixup data-convert-bt.c missing conversion ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
2d4f2799 45112e89

+87 -93
+2 -2
tools/perf/builtin-annotate.c
··· 441 441 } 442 442 443 443 if (total_nr_samples == 0) { 444 - ui__error("The %s file has no samples!\n", session->data->file.path); 444 + ui__error("The %s data has no samples!\n", session->data->path); 445 445 goto out; 446 446 } 447 447 ··· 578 578 if (quiet) 579 579 perf_quiet_option(); 580 580 581 - data.file.path = input_name; 581 + data.path = input_name; 582 582 583 583 annotate.session = perf_session__new(&data, false, &annotate.tool); 584 584 if (annotate.session == NULL)
+2 -2
tools/perf/builtin-buildid-cache.c
··· 416 416 nsi = nsinfo__new(ns_id); 417 417 418 418 if (missing_filename) { 419 - data.file.path = missing_filename; 420 - data.force = force; 419 + data.path = missing_filename; 420 + data.force = force; 421 421 422 422 session = perf_session__new(&data, false, NULL); 423 423 if (session == NULL)
+3 -5
tools/perf/builtin-buildid-list.c
··· 52 52 { 53 53 struct perf_session *session; 54 54 struct perf_data data = { 55 - .file = { 56 - .path = input_name, 57 - }, 58 - .mode = PERF_DATA_MODE_READ, 59 - .force = force, 55 + .path = input_name, 56 + .mode = PERF_DATA_MODE_READ, 57 + .force = force, 60 58 }; 61 59 62 60 symbol__elf_init();
+2 -2
tools/perf/builtin-c2c.c
··· 2750 2750 if (!input_name || !strlen(input_name)) 2751 2751 input_name = "perf.data"; 2752 2752 2753 - data.file.path = input_name; 2754 - data.force = symbol_conf.force; 2753 + data.path = input_name; 2754 + data.force = symbol_conf.force; 2755 2755 2756 2756 err = setup_display(display); 2757 2757 if (err)
+6 -6
tools/perf/builtin-diff.c
··· 708 708 709 709 data__for_each_file(i, d) 710 710 fprintf(stdout, "# [%d] %s %s\n", 711 - d->idx, d->data.file.path, 711 + d->idx, d->data.path, 712 712 !d->idx ? "(Baseline)" : ""); 713 713 714 714 fprintf(stdout, "#\n"); ··· 779 779 data__for_each_file(i, d) { 780 780 d->session = perf_session__new(&d->data, false, &tool); 781 781 if (!d->session) { 782 - pr_err("Failed to open %s\n", d->data.file.path); 782 + pr_err("Failed to open %s\n", d->data.path); 783 783 ret = -1; 784 784 goto out_delete; 785 785 } 786 786 787 787 ret = perf_session__process_events(d->session); 788 788 if (ret) { 789 - pr_err("Failed to process %s\n", d->data.file.path); 789 + pr_err("Failed to process %s\n", d->data.path); 790 790 goto out_delete; 791 791 } 792 792 ··· 1289 1289 data__for_each_file(i, d) { 1290 1290 struct perf_data *data = &d->data; 1291 1291 1292 - data->file.path = use_default ? defaults[i] : argv[i]; 1293 - data->mode = PERF_DATA_MODE_READ, 1294 - data->force = force, 1292 + data->path = use_default ? defaults[i] : argv[i]; 1293 + data->mode = PERF_DATA_MODE_READ, 1294 + data->force = force, 1295 1295 1296 1296 d->idx = i; 1297 1297 }
+1 -3
tools/perf/builtin-evlist.c
··· 23 23 struct perf_session *session; 24 24 struct perf_evsel *pos; 25 25 struct perf_data data = { 26 - .file = { 27 - .path = file_name, 28 - }, 26 + .path = file_name, 29 27 .mode = PERF_DATA_MODE_READ, 30 28 .force = details->force, 31 29 };
+4 -6
tools/perf/builtin-inject.c
··· 770 770 .input_name = "-", 771 771 .samples = LIST_HEAD_INIT(inject.samples), 772 772 .output = { 773 - .file = { 774 - .path = "-", 775 - }, 776 - .mode = PERF_DATA_MODE_WRITE, 773 + .path = "-", 774 + .mode = PERF_DATA_MODE_WRITE, 777 775 }, 778 776 }; 779 777 struct perf_data data = { ··· 784 786 "Inject build-ids into the output stream"), 785 787 OPT_STRING('i', "input", &inject.input_name, "file", 786 788 "input file name"), 787 - OPT_STRING('o', "output", &inject.output.file.path, "file", 789 + OPT_STRING('o', "output", &inject.output.path, "file", 788 790 "output file name"), 789 791 OPT_BOOLEAN('s', "sched-stat", &inject.sched_stat, 790 792 "Merge sched-stat and sched-switch for getting events " ··· 832 834 833 835 inject.tool.ordered_events = inject.sched_stat; 834 836 835 - data.file.path = inject.input_name; 837 + data.path = inject.input_name; 836 838 inject.session = perf_session__new(&data, true, &inject.tool); 837 839 if (inject.session == NULL) 838 840 return -1;
+1 -1
tools/perf/builtin-kmem.c
··· 1949 1949 return __cmd_record(argc, argv); 1950 1950 } 1951 1951 1952 - data.file.path = input_name; 1952 + data.path = input_name; 1953 1953 1954 1954 kmem_session = session = perf_session__new(&data, false, &perf_kmem); 1955 1955 if (session == NULL)
+3 -5
tools/perf/builtin-kvm.c
··· 1080 1080 .ordered_events = true, 1081 1081 }; 1082 1082 struct perf_data file = { 1083 - .file = { 1084 - .path = kvm->file_name, 1085 - }, 1086 - .mode = PERF_DATA_MODE_READ, 1087 - .force = kvm->force, 1083 + .path = kvm->file_name, 1084 + .mode = PERF_DATA_MODE_READ, 1085 + .force = kvm->force, 1088 1086 }; 1089 1087 1090 1088 kvm->tool = eops;
+3 -5
tools/perf/builtin-lock.c
··· 866 866 .ordered_events = true, 867 867 }; 868 868 struct perf_data data = { 869 - .file = { 870 - .path = input_name, 871 - }, 872 - .mode = PERF_DATA_MODE_READ, 873 - .force = force, 869 + .path = input_name, 870 + .mode = PERF_DATA_MODE_READ, 871 + .force = force, 874 872 }; 875 873 876 874 session = perf_session__new(&data, false, &eops);
+3 -5
tools/perf/builtin-mem.c
··· 239 239 static int report_raw_events(struct perf_mem *mem) 240 240 { 241 241 struct perf_data data = { 242 - .file = { 243 - .path = input_name, 244 - }, 245 - .mode = PERF_DATA_MODE_READ, 246 - .force = mem->force, 242 + .path = input_name, 243 + .mode = PERF_DATA_MODE_READ, 244 + .force = mem->force, 247 245 }; 248 246 int ret; 249 247 struct perf_session *session = perf_session__new(&data, false,
+3 -3
tools/perf/builtin-record.c
··· 918 918 919 919 if (!quiet) 920 920 fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 921 - data->file.path, timestamp); 921 + data->path, timestamp); 922 922 923 923 /* Output tracking events */ 924 924 if (!at_exit) { ··· 1461 1461 1462 1462 fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n", 1463 1463 perf_data__size(data) / 1024.0 / 1024.0, 1464 - data->file.path, postfix, samples); 1464 + data->path, postfix, samples); 1465 1465 } 1466 1466 1467 1467 out_delete_session: ··· 1862 1862 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 1863 1863 "list of cpus to monitor"), 1864 1864 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 1865 - OPT_STRING('o', "output", &record.data.file.path, "file", 1865 + OPT_STRING('o', "output", &record.data.path, "file", 1866 1866 "output file name"), 1867 1867 OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 1868 1868 &record.opts.no_inherit_set,
+3 -3
tools/perf/builtin-report.c
··· 899 899 rep->nr_entries += evsel__hists(pos)->nr_entries; 900 900 901 901 if (rep->nr_entries == 0) { 902 - ui__error("The %s file has no samples!\n", data->file.path); 902 + ui__error("The %s data has no samples!\n", data->path); 903 903 return 0; 904 904 } 905 905 ··· 1207 1207 input_name = "perf.data"; 1208 1208 } 1209 1209 1210 - data.file.path = input_name; 1211 - data.force = symbol_conf.force; 1210 + data.path = input_name; 1211 + data.force = symbol_conf.force; 1212 1212 1213 1213 repeat: 1214 1214 session = perf_session__new(&data, false, &report.tool);
+6 -10
tools/perf/builtin-sched.c
··· 1785 1785 }; 1786 1786 struct perf_session *session; 1787 1787 struct perf_data data = { 1788 - .file = { 1789 - .path = input_name, 1790 - }, 1791 - .mode = PERF_DATA_MODE_READ, 1792 - .force = sched->force, 1788 + .path = input_name, 1789 + .mode = PERF_DATA_MODE_READ, 1790 + .force = sched->force, 1793 1791 }; 1794 1792 int rc = -1; 1795 1793 ··· 2956 2958 { "sched:sched_migrate_task", timehist_migrate_task_event, }, 2957 2959 }; 2958 2960 struct perf_data data = { 2959 - .file = { 2960 - .path = input_name, 2961 - }, 2962 - .mode = PERF_DATA_MODE_READ, 2963 - .force = sched->force, 2961 + .path = input_name, 2962 + .mode = PERF_DATA_MODE_READ, 2963 + .force = sched->force, 2964 2964 }; 2965 2965 2966 2966 struct perf_session *session;
+5 -7
tools/perf/builtin-script.c
··· 2951 2951 DIR *scripts_dir, *lang_dir; 2952 2952 struct perf_session *session; 2953 2953 struct perf_data data = { 2954 - .file = { 2955 - .path = input_name, 2956 - }, 2957 - .mode = PERF_DATA_MODE_READ, 2954 + .path = input_name, 2955 + .mode = PERF_DATA_MODE_READ, 2958 2956 }; 2959 2957 char *temp; 2960 2958 int i = 0; ··· 3425 3427 argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage, 3426 3428 PARSE_OPT_STOP_AT_NON_OPTION); 3427 3429 3428 - data.file.path = input_name; 3429 - data.force = symbol_conf.force; 3430 + data.path = input_name; 3431 + data.force = symbol_conf.force; 3430 3432 3431 3433 if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { 3432 3434 rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); ··· 3652 3654 goto out_delete; 3653 3655 } 3654 3656 3655 - input = open(data.file.path, O_RDONLY); /* input_name */ 3657 + input = open(data.path, O_RDONLY); /* input_name */ 3656 3658 if (input < 0) { 3657 3659 err = -errno; 3658 3660 perror("failed to open file");
+3 -3
tools/perf/builtin-stat.c
··· 1322 1322 PARSE_OPT_STOP_AT_NON_OPTION); 1323 1323 1324 1324 if (output_name) 1325 - data->file.path = output_name; 1325 + data->path = output_name; 1326 1326 1327 1327 if (stat_config.run_count != 1 || forever) { 1328 1328 pr_err("Cannot use -r option with perf stat record.\n"); ··· 1523 1523 input_name = "perf.data"; 1524 1524 } 1525 1525 1526 - perf_stat.data.file.path = input_name; 1527 - perf_stat.data.mode = PERF_DATA_MODE_READ; 1526 + perf_stat.data.path = input_name; 1527 + perf_stat.data.mode = PERF_DATA_MODE_READ; 1528 1528 1529 1529 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool); 1530 1530 if (session == NULL)
+3 -5
tools/perf/builtin-timechart.c
··· 1602 1602 { "syscalls:sys_exit_select", process_exit_poll }, 1603 1603 }; 1604 1604 struct perf_data data = { 1605 - .file = { 1606 - .path = input_name, 1607 - }, 1608 - .mode = PERF_DATA_MODE_READ, 1609 - .force = tchart->force, 1605 + .path = input_name, 1606 + .mode = PERF_DATA_MODE_READ, 1607 + .force = tchart->force, 1610 1608 }; 1611 1609 1612 1610 struct perf_session *session = perf_session__new(&data, false,
+3 -5
tools/perf/builtin-trace.c
··· 3154 3154 { "probe:vfs_getname", trace__vfs_getname, }, 3155 3155 }; 3156 3156 struct perf_data data = { 3157 - .file = { 3158 - .path = input_name, 3159 - }, 3160 - .mode = PERF_DATA_MODE_READ, 3161 - .force = trace->force, 3157 + .path = input_name, 3158 + .mode = PERF_DATA_MODE_READ, 3159 + .force = trace->force, 3162 3160 }; 3163 3161 struct perf_session *session; 3164 3162 struct perf_evsel *evsel;
+2 -2
tools/perf/util/data-convert-bt.c
··· 1578 1578 { 1579 1579 struct perf_session *session; 1580 1580 struct perf_data data = { 1581 - .file = { .path = input, .fd = -1 }, 1581 + .path = input, 1582 1582 .mode = PERF_DATA_MODE_READ, 1583 1583 .force = opts->force, 1584 1584 }; ··· 1650 1650 1651 1651 fprintf(stderr, 1652 1652 "[ perf data convert: Converted '%s' into CTF data '%s' ]\n", 1653 - data.file.path, path); 1653 + data.path, path); 1654 1654 1655 1655 fprintf(stderr, 1656 1656 "[ perf data convert: Converted and wrote %.3f MB (%" PRIu64 " samples",
+27 -12
tools/perf/util/data.c
··· 19 19 int fd = perf_data__is_read(data) ? 20 20 STDIN_FILENO : STDOUT_FILENO; 21 21 22 - if (!data->file.path) { 22 + if (!data->path) { 23 23 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode)) 24 24 is_pipe = true; 25 25 } else { 26 - if (!strcmp(data->file.path, "-")) 26 + if (!strcmp(data->path, "-")) 27 27 is_pipe = true; 28 28 } 29 29 ··· 37 37 { 38 38 struct stat st; 39 39 40 - if (!stat(data->file.path, &st) && st.st_size) { 40 + if (!stat(data->path, &st) && st.st_size) { 41 41 /* TODO check errors properly */ 42 42 char oldname[PATH_MAX]; 43 43 snprintf(oldname, sizeof(oldname), "%s.old", 44 - data->file.path); 44 + data->path); 45 45 unlink(oldname); 46 - rename(data->file.path, oldname); 46 + rename(data->path, oldname); 47 47 } 48 48 49 49 return 0; ··· 115 115 fd = perf_data__is_read(data) ? 116 116 open_file_read(data) : open_file_write(data); 117 117 118 + if (fd < 0) { 119 + free(data->file.path); 120 + return -1; 121 + } 122 + 118 123 data->file.fd = fd; 119 - return fd < 0 ? -1 : 0; 124 + return 0; 125 + } 126 + 127 + static int open_file_dup(struct perf_data *data) 128 + { 129 + data->file.path = strdup(data->path); 130 + if (!data->file.path) 131 + return -ENOMEM; 132 + 133 + return open_file(data); 120 134 } 121 135 122 136 int perf_data__open(struct perf_data *data) ··· 138 124 if (check_pipe(data)) 139 125 return 0; 140 126 141 - if (!data->file.path) 142 - data->file.path = "perf.data"; 127 + if (!data->path) 128 + data->path = "perf.data"; 143 129 144 - return open_file(data); 130 + return open_file_dup(data); 145 131 } 146 132 147 133 void perf_data__close(struct perf_data *data) 148 134 { 135 + free(data->file.path); 149 136 close(data->file.fd); 150 137 } 151 138 ··· 174 159 if (perf_data__is_read(data)) 175 160 return -EINVAL; 176 161 177 - if (asprintf(&new_filepath, "%s.%s", data->file.path, postfix) < 0) 162 + if (asprintf(&new_filepath, "%s.%s", data->path, postfix) < 0) 178 163 return -ENOMEM; 179 164 180 165 /* 181 166 * Only fire a warning, don't return error, continue fill 182 167 * original file. 183 168 */ 184 - if (rename(data->file.path, new_filepath)) 185 - pr_warning("Failed to rename %s to %s\n", data->file.path, new_filepath); 169 + if (rename(data->path, new_filepath)) 170 + pr_warning("Failed to rename %s to %s\n", data->path, new_filepath); 186 171 187 172 if (!at_exit) { 188 173 close(data->file.fd);
+2 -1
tools/perf/util/data.h
··· 10 10 }; 11 11 12 12 struct perf_data_file { 13 - const char *path; 13 + char *path; 14 14 int fd; 15 15 unsigned long size; 16 16 }; 17 17 18 18 struct perf_data { 19 + const char *path; 19 20 struct perf_data_file file; 20 21 bool is_pipe; 21 22 bool force;