linux/tools/perf/tests/pmu.c

453 lines
12 KiB
C
Raw Normal View History

License cleanup: add SPDX GPL-2.0 license identifier to files with no license Many source files in the tree are missing licensing information, which makes it harder for compliance tools to determine the correct license. By default all files without license information are under the default license of the kernel, which is GPL version 2. Update the files which contain no license information with the 'GPL-2.0' SPDX license identifier. The SPDX identifier is a legally binding shorthand, which can be used instead of the full boiler plate text. This patch is based on work done by Thomas Gleixner and Kate Stewart and Philippe Ombredanne. How this work was done: Patches were generated and checked against linux-4.14-rc6 for a subset of the use cases: - file had no licensing information it it. - file was a */uapi/* one with no licensing information in it, - file was a */uapi/* one with existing licensing information, Further patches will be generated in subsequent months to fix up cases where non-standard license headers were used, and references to license had to be inferred by heuristics based on keywords. The analysis to determine which SPDX License Identifier to be applied to a file was done in a spreadsheet of side by side results from of the output of two independent scanners (ScanCode & Windriver) producing SPDX tag:value files created by Philippe Ombredanne. Philippe prepared the base worksheet, and did an initial spot review of a few 1000 files. The 4.13 kernel was the starting point of the analysis with 60,537 files assessed. Kate Stewart did a file by file comparison of the scanner results in the spreadsheet to determine which SPDX license identifier(s) to be applied to the file. She confirmed any determination that was not immediately clear with lawyers working with the Linux Foundation. Criteria used to select files for SPDX license identifier tagging was: - Files considered eligible had to be source code files. - Make and config files were included as candidates if they contained >5 lines of source - File already had some variant of a license header in it (even if <5 lines). All documentation files were explicitly excluded. The following heuristics were used to determine which SPDX license identifiers to apply. - when both scanners couldn't find any license traces, file was considered to have no license information in it, and the top level COPYING file license applied. For non */uapi/* files that summary was: SPDX license identifier # files ---------------------------------------------------|------- GPL-2.0 11139 and resulted in the first patch in this series. If that file was a */uapi/* path one, it was "GPL-2.0 WITH Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was: SPDX license identifier # files ---------------------------------------------------|------- GPL-2.0 WITH Linux-syscall-note 930 and resulted in the second patch in this series. - if a file had some form of licensing information in it, and was one of the */uapi/* ones, it was denoted with the Linux-syscall-note if any GPL family license was found in the file or had no licensing in it (per prior point). Results summary: SPDX license identifier # files ---------------------------------------------------|------ GPL-2.0 WITH Linux-syscall-note 270 GPL-2.0+ WITH Linux-syscall-note 169 ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21 ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17 LGPL-2.1+ WITH Linux-syscall-note 15 GPL-1.0+ WITH Linux-syscall-note 14 ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5 LGPL-2.0+ WITH Linux-syscall-note 4 LGPL-2.1 WITH Linux-syscall-note 3 ((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3 ((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1 and that resulted in the third patch in this series. - when the two scanners agreed on the detected license(s), that became the concluded license(s). - when there was disagreement between the two scanners (one detected a license but the other didn't, or they both detected different licenses) a manual inspection of the file occurred. - In most cases a manual inspection of the information in the file resulted in a clear resolution of the license that should apply (and which scanner probably needed to revisit its heuristics). - When it was not immediately clear, the license identifier was confirmed with lawyers working with the Linux Foundation. - If there was any question as to the appropriate license identifier, the file was flagged for further research and to be revisited later in time. In total, over 70 hours of logged manual review was done on the spreadsheet to determine the SPDX license identifiers to apply to the source files by Kate, Philippe, Thomas and, in some cases, confirmation by lawyers working with the Linux Foundation. Kate also obtained a third independent scan of the 4.13 code base from FOSSology, and compared selected files where the other two scanners disagreed against that SPDX file, to see if there was new insights. The Windriver scanner is based on an older version of FOSSology in part, so they are related. Thomas did random spot checks in about 500 files from the spreadsheets for the uapi headers and agreed with SPDX license identifier in the files he inspected. For the non-uapi files Thomas did random spot checks in about 15000 files. In initial set of patches against 4.14-rc6, 3 files were found to have copy/paste license identifier errors, and have been fixed to reflect the correct identifier. Additionally Philippe spent 10 hours this week doing a detailed manual inspection and review of the 12,461 patched files from the initial patch version early this week with: - a full scancode scan run, collecting the matched texts, detected license ids and scores - reviewing anything where there was a license detected (about 500+ files) to ensure that the applied SPDX license was correct - reviewing anything where there was no detection but the patch license was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied SPDX license was correct This produced a worksheet with 20 files needing minor correction. This worksheet was then exported into 3 different .csv files for the different types of files to be modified. These .csv files were then reviewed by Greg. Thomas wrote a script to parse the csv files and add the proper SPDX tag to the file, in the format that the file expected. This script was further refined by Greg based on the output to detect more types of files automatically and to distinguish between header and source .c files (which need different comment types.) Finally Greg ran the script using the .csv files to generate the patches. Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org> Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
// SPDX-License-Identifier: GPL-2.0
#include "evlist.h"
#include "evsel.h"
#include "parse-events.h"
#include "pmu.h"
#include "pmus.h"
#include "tests.h"
#include "debug.h"
#include "fncache.h"
#include <api/fs/fs.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
perf pmu: Use relative path for sysfs scan The PMU information is in the kernel sysfs so it needs to scan the directory to get the whole information like event aliases, formats and so on. During the traversal, it opens a lot of files and directories like below: dir = opendir("/sys/bus/event_source/devices"); while (dentry = readdir(dir)) { char buf[PATH_MAX]; snprintf(buf, sizeof(buf), "%s/%s", "/sys/bus/event_source/devices", dentry->d_name); fd = open(buf, O_RDONLY); ... } But this is not good since it needs to copy the string to build the absolute pathname, and it makes redundant pathname walk (from the /sys) unnecessarily. We can use openat(2) to open the file in the given directory. While it's not a problem ususally, it can be a problem when the kernel has contentions on the sysfs. Add a couple of new helper to return the file descriptor of PMU directory so that it can use it with relative paths. * perf_pmu__event_source_devices_fd() - returns a fd for the PMU root ("/sys/bus/event_source/devices") * perf_pmu__pathname_fd() - returns a fd for "<pmu>/<file>" under the PMU root Now the above code can be converted something like below: dirfd = perf_pmu__event_source_devices_fd(); dir = fdopendir(dirfd); while (dentry = readdir(dir)) { fd = openat(dirfd, dentry->d_name, O_RDONLY); ... } Signed-off-by: Namhyung Kim <namhyung@kernel.org> Acked-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Leo Yan <leo.yan@linaro.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20230331202949.810326-2-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-03-31 20:29:45 +00:00
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
/* Fake PMUs created in temp directory. */
static LIST_HEAD(test_pmus);
/* Cleanup test PMU directory. */
static int test_pmu_put(const char *dir, struct perf_pmu *pmu)
{
char buf[PATH_MAX + 20];
int ret;
if (scnprintf(buf, sizeof(buf), "rm -fr %s", dir) < 0) {
pr_err("Failure to set up buffer for \"%s\"\n", dir);
return -EINVAL;
}
ret = system(buf);
if (ret)
pr_err("Failure to \"%s\"\n", buf);
list_del(&pmu->list);
perf_pmu__delete(pmu);
return ret;
}
/*
* Prepare test PMU directory data, normally exported by kernel at
* /sys/bus/event_source/devices/<pmu>/. Give as input a buffer to hold the file
* path, the result is PMU loaded using that directory.
*/
static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
{
/* Simulated format definitions. */
const struct test_format {
const char *name;
const char *value;
} test_formats[] = {
{ "krava01", "config:0-1,62-63\n", },
{ "krava02", "config:10-17\n", },
{ "krava03", "config:5\n", },
{ "krava11", "config1:0,2,4,6,8,20-28\n", },
{ "krava12", "config1:63\n", },
{ "krava13", "config1:45-47\n", },
{ "krava21", "config2:0-3,10-13,20-23,30-33,40-43,50-53,60-63\n", },
{ "krava22", "config2:8,18,48,58\n", },
{ "krava23", "config2:28-29,38\n", },
};
const char *test_event = "krava01=15,krava02=170,krava03=1,krava11=27,krava12=1,"
"krava13=2,krava21=119,krava22=11,krava23=2\n";
char name[PATH_MAX];
int dirfd, file;
struct perf_pmu *pmu = NULL;
ssize_t len;
/* Create equivalent of sysfs mount point. */
scnprintf(dir, sz, "/tmp/perf-pmu-test-XXXXXX");
if (!mkdtemp(dir)) {
pr_err("mkdtemp failed\n");
dir[0] = '\0';
return NULL;
}
dirfd = open(dir, O_DIRECTORY);
if (dirfd < 0) {
pr_err("Failed to open test directory \"%s\"\n", dir);
goto err_out;
}
/* Create the test PMU directory and give it a perf_event_attr type number. */
if (mkdirat(dirfd, "perf-pmu-test", 0755) < 0) {
pr_err("Failed to mkdir PMU directory\n");
goto err_out;
}
file = openat(dirfd, "perf-pmu-test/type", O_WRONLY | O_CREAT, 0600);
if (!file) {
pr_err("Failed to open for writing file \"type\"\n");
goto err_out;
}
len = strlen("9999");
if (write(file, "9999\n", len) < len) {
close(file);
pr_err("Failed to write to 'type' file\n");
goto err_out;
}
close(file);
/* Create format directory and files. */
if (mkdirat(dirfd, "perf-pmu-test/format", 0755) < 0) {
pr_err("Failed to mkdir PMU format directory\n)");
goto err_out;
}
for (size_t i = 0; i < ARRAY_SIZE(test_formats); i++) {
const struct test_format *format = &test_formats[i];
if (scnprintf(name, PATH_MAX, "perf-pmu-test/format/%s", format->name) < 0) {
pr_err("Failure to set up path for \"%s\"\n", format->name);
goto err_out;
}
file = openat(dirfd, name, O_WRONLY | O_CREAT, 0600);
if (!file) {
pr_err("Failed to open for writing file \"%s\"\n", name);
goto err_out;
}
if (write(file, format->value, strlen(format->value)) < 0) {
pr_err("Failed to write to file \"%s\"\n", name);
close(file);
goto err_out;
}
close(file);
}
/* Create test event. */
if (mkdirat(dirfd, "perf-pmu-test/events", 0755) < 0) {
pr_err("Failed to mkdir PMU events directory\n");
goto err_out;
}
file = openat(dirfd, "perf-pmu-test/events/test-event", O_WRONLY | O_CREAT, 0600);
if (!file) {
pr_err("Failed to open for writing file \"type\"\n");
goto err_out;
}
len = strlen(test_event);
if (write(file, test_event, len) < len) {
close(file);
pr_err("Failed to write to 'test-event' file\n");
goto err_out;
}
close(file);
/* Make the PMU reading the files created above. */
pmu = perf_pmus__add_test_pmu(dirfd, "perf-pmu-test");
if (!pmu)
pr_err("Test PMU creation failed\n");
err_out:
if (!pmu)
test_pmu_put(dir, pmu);
if (dirfd >= 0)
close(dirfd);
return pmu;
}
static int test__pmu_format(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
char dir[PATH_MAX];
struct perf_event_attr attr;
struct parse_events_terms terms;
int ret = TEST_FAIL;
struct perf_pmu *pmu = test_pmu_get(dir, sizeof(dir));
if (!pmu)
return TEST_FAIL;
parse_events_terms__init(&terms);
if (parse_events_terms(&terms,
"krava01=15,krava02=170,krava03=1,krava11=27,krava12=1,"
"krava13=2,krava21=119,krava22=11,krava23=2",
NULL)) {
pr_err("Term parsing failed\n");
goto err_out;
}
memset(&attr, 0, sizeof(attr));
ret = perf_pmu__config_terms(pmu, &attr, &terms, /*zero=*/false, /*err=*/NULL);
if (ret) {
pr_err("perf_pmu__config_terms failed");
goto err_out;
}
if (attr.config != 0xc00000000002a823) {
pr_err("Unexpected config value %llx\n", attr.config);
goto err_out;
}
if (attr.config1 != 0x8000400000000145) {
pr_err("Unexpected config1 value %llx\n", attr.config1);
goto err_out;
}
if (attr.config2 != 0x0400000020041d07) {
pr_err("Unexpected config2 value %llx\n", attr.config2);
goto err_out;
}
ret = TEST_OK;
err_out:
parse_events_terms__exit(&terms);
test_pmu_put(dir, pmu);
return ret;
}
static int test__pmu_events(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
char dir[PATH_MAX];
struct parse_events_error err;
struct evlist *evlist;
struct evsel *evsel;
struct perf_event_attr *attr;
int ret = TEST_FAIL;
struct perf_pmu *pmu = test_pmu_get(dir, sizeof(dir));
const char *event = "perf-pmu-test/test-event/";
if (!pmu)
return TEST_FAIL;
evlist = evlist__new();
if (evlist == NULL) {
pr_err("Failed allocation");
goto err_out;
}
parse_events_error__init(&err);
ret = parse_events(evlist, event, &err);
if (ret) {
pr_debug("failed to parse event '%s', err %d\n", event, ret);
parse_events_error__print(&err, event);
if (parse_events_error__contains(&err, "can't access trace events"))
ret = TEST_SKIP;
goto err_out;
}
evsel = evlist__first(evlist);
attr = &evsel->core.attr;
if (attr->config != 0xc00000000002a823) {
pr_err("Unexpected config value %llx\n", attr->config);
goto err_out;
}
if (attr->config1 != 0x8000400000000145) {
pr_err("Unexpected config1 value %llx\n", attr->config1);
goto err_out;
}
if (attr->config2 != 0x0400000020041d07) {
pr_err("Unexpected config2 value %llx\n", attr->config2);
goto err_out;
}
ret = TEST_OK;
err_out:
parse_events_error__exit(&err);
evlist__delete(evlist);
test_pmu_put(dir, pmu);
return ret;
}
static bool permitted_event_name(const char *name)
{
bool has_lower = false, has_upper = false;
for (size_t i = 0; i < strlen(name); i++) {
char c = name[i];
if (islower(c)) {
if (has_upper)
return false;
has_lower = true;
continue;
}
if (isupper(c)) {
if (has_lower)
return false;
has_upper = true;
continue;
}
if (!isdigit(c) && c != '.' && c != '_' && c != '-')
return false;
}
return true;
}
static int test__pmu_event_names(struct test_suite *test __maybe_unused,
int subtest __maybe_unused)
{
char path[PATH_MAX];
DIR *pmu_dir, *event_dir;
struct dirent *pmu_dent, *event_dent;
const char *sysfs = sysfs__mountpoint();
int ret = TEST_OK;
if (!sysfs) {
pr_err("Sysfs not mounted\n");
return TEST_FAIL;
}
snprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs);
pmu_dir = opendir(path);
if (!pmu_dir) {
pr_err("Error opening \"%s\"\n", path);
return TEST_FAIL;
}
while ((pmu_dent = readdir(pmu_dir))) {
if (!strcmp(pmu_dent->d_name, ".") ||
!strcmp(pmu_dent->d_name, ".."))
continue;
snprintf(path, sizeof(path), "%s/bus/event_source/devices/%s/type",
sysfs, pmu_dent->d_name);
/* Does it look like a PMU? */
if (!file_available(path))
continue;
/* Process events. */
snprintf(path, sizeof(path), "%s/bus/event_source/devices/%s/events",
sysfs, pmu_dent->d_name);
event_dir = opendir(path);
if (!event_dir) {
pr_debug("Skipping as no event directory \"%s\"\n", path);
continue;
}
while ((event_dent = readdir(event_dir))) {
const char *event_name = event_dent->d_name;
if (!strcmp(event_name, ".") || !strcmp(event_name, ".."))
continue;
if (!permitted_event_name(event_name)) {
pr_err("Invalid sysfs event name: %s/%s\n",
pmu_dent->d_name, event_name);
ret = TEST_FAIL;
}
}
closedir(event_dir);
}
closedir(pmu_dir);
return ret;
}
static const char * const uncore_chas[] = {
"uncore_cha_0",
"uncore_cha_1",
"uncore_cha_2",
"uncore_cha_3",
"uncore_cha_4",
"uncore_cha_5",
"uncore_cha_6",
"uncore_cha_7",
"uncore_cha_8",
"uncore_cha_9",
"uncore_cha_10",
"uncore_cha_11",
"uncore_cha_12",
"uncore_cha_13",
"uncore_cha_14",
"uncore_cha_15",
"uncore_cha_16",
"uncore_cha_17",
"uncore_cha_18",
"uncore_cha_19",
"uncore_cha_20",
"uncore_cha_21",
"uncore_cha_22",
"uncore_cha_23",
"uncore_cha_24",
"uncore_cha_25",
"uncore_cha_26",
"uncore_cha_27",
"uncore_cha_28",
"uncore_cha_29",
"uncore_cha_30",
"uncore_cha_31",
};
static const char * const mrvl_ddrs[] = {
"mrvl_ddr_pmu_87e1b0000000",
"mrvl_ddr_pmu_87e1b1000000",
"mrvl_ddr_pmu_87e1b2000000",
"mrvl_ddr_pmu_87e1b3000000",
"mrvl_ddr_pmu_87e1b4000000",
"mrvl_ddr_pmu_87e1b5000000",
"mrvl_ddr_pmu_87e1b6000000",
"mrvl_ddr_pmu_87e1b7000000",
"mrvl_ddr_pmu_87e1b8000000",
"mrvl_ddr_pmu_87e1b9000000",
"mrvl_ddr_pmu_87e1ba000000",
"mrvl_ddr_pmu_87e1bb000000",
"mrvl_ddr_pmu_87e1bc000000",
"mrvl_ddr_pmu_87e1bd000000",
"mrvl_ddr_pmu_87e1be000000",
"mrvl_ddr_pmu_87e1bf000000",
};
static int test__name_len(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
TEST_ASSERT_VAL("cpu", pmu_name_len_no_suffix("cpu") == strlen("cpu"));
TEST_ASSERT_VAL("i915", pmu_name_len_no_suffix("i915") == strlen("i915"));
TEST_ASSERT_VAL("cpum_cf", pmu_name_len_no_suffix("cpum_cf") == strlen("cpum_cf"));
for (size_t i = 0; i < ARRAY_SIZE(uncore_chas); i++) {
TEST_ASSERT_VAL("Strips uncore_cha suffix",
pmu_name_len_no_suffix(uncore_chas[i]) ==
strlen("uncore_cha"));
}
for (size_t i = 0; i < ARRAY_SIZE(mrvl_ddrs); i++) {
TEST_ASSERT_VAL("Strips mrvl_ddr_pmu suffix",
pmu_name_len_no_suffix(mrvl_ddrs[i]) ==
strlen("mrvl_ddr_pmu"));
}
return TEST_OK;
}
static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
TEST_ASSERT_EQUAL("cpu", pmu_name_cmp("cpu", "cpu"), 0);
TEST_ASSERT_EQUAL("i915", pmu_name_cmp("i915", "i915"), 0);
TEST_ASSERT_EQUAL("cpum_cf", pmu_name_cmp("cpum_cf", "cpum_cf"), 0);
TEST_ASSERT_VAL("i915", pmu_name_cmp("cpu", "i915") < 0);
TEST_ASSERT_VAL("i915", pmu_name_cmp("i915", "cpu") > 0);
TEST_ASSERT_VAL("cpum_cf", pmu_name_cmp("cpum_cf", "cpum_ce") > 0);
TEST_ASSERT_VAL("cpum_cf", pmu_name_cmp("cpum_cf", "cpum_d0") < 0);
for (size_t i = 1; i < ARRAY_SIZE(uncore_chas); i++) {
TEST_ASSERT_VAL("uncore_cha suffixes ordered lt",
pmu_name_cmp(uncore_chas[i-1], uncore_chas[i]) < 0);
TEST_ASSERT_VAL("uncore_cha suffixes ordered gt",
pmu_name_cmp(uncore_chas[i], uncore_chas[i-1]) > 0);
}
for (size_t i = 1; i < ARRAY_SIZE(mrvl_ddrs); i++) {
TEST_ASSERT_VAL("mrvl_ddr_pmu suffixes ordered lt",
pmu_name_cmp(mrvl_ddrs[i-1], mrvl_ddrs[i]) < 0);
TEST_ASSERT_VAL("mrvl_ddr_pmu suffixes ordered gt",
pmu_name_cmp(mrvl_ddrs[i], mrvl_ddrs[i-1]) > 0);
}
return TEST_OK;
}
static struct test_case tests__pmu[] = {
TEST_CASE("Parsing with PMU format directory", pmu_format),
TEST_CASE("Parsing with PMU event", pmu_events),
TEST_CASE("PMU event names", pmu_event_names),
TEST_CASE("PMU name combining", name_len),
TEST_CASE("PMU name comparison", name_cmp),
{ .name = NULL, }
};
struct test_suite suite__pmu = {
.desc = "Sysfs PMU tests",
.test_cases = tests__pmu,
};