OpenCloudOS-Kernel/tools/perf/util/block-range.c

330 lines
6.8 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 22:07:57 +08:00
// SPDX-License-Identifier: GPL-2.0
perf annotate: Add branch stack / basic block I wanted to know the hottest path through a function and figured the branch-stack (LBR) information should be able to help out with that. The below uses the branch-stack to create basic blocks and generate statistics from them. from to branch_i * ----> * | | block v * ----> * from to branch_i+1 The blocks are broken down into non-overlapping ranges, while tracking if the start of each range is an entry point and/or the end of a range is a branch. Each block iterates all ranges it covers (while splitting where required to exactly match the block) and increments the 'coverage' count. For the range including the branch we increment the taken counter, as well as the pred counter if flags.predicted. Using these number we can find if an instruction: - had coverage; given by: br->coverage / br->sym->max_coverage This metric ensures each symbol has a 100% spot, which reflects the observation that each symbol must have a most covered/hottest block. - is a branch target: br->is_target && br->start == add - for targets, how much of a branch's coverages comes from it: target->entry / branch->coverage - is a branch: br->is_branch && br->end == addr - for branches, how often it was taken: br->taken / br->coverage after all, all execution that didn't take the branch would have incremented the coverage and continued onward to a later branch. - for branches, how often it was predicted: br->pred / br->taken The coverage percentage is used to color the address and asm sections; for low (<1%) coverage we use NORMAL (uncolored), indicating that these instructions are not 'important'. For high coverage (>75%) we color the address RED. For each branch, we add an asm comment after the instruction with information on how often it was taken and predicted. Output looks like (sans color, which does loose a lot of the information :/) $ perf record --branch-filter u,any -e cycles:p ./branches 27 $ perf annotate branches Percent | Source code & Disassembly of branches for cycles:pu (217 samples) --------------------------------------------------------------------------------- : branches(): 0.00 : 40057a: push %rbp 0.00 : 40057b: mov %rsp,%rbp 0.00 : 40057e: sub $0x20,%rsp 0.00 : 400582: mov %rdi,-0x18(%rbp) 0.00 : 400586: mov %rsi,-0x20(%rbp) 0.00 : 40058a: mov -0x18(%rbp),%rax 0.00 : 40058e: mov %rax,-0x10(%rbp) 0.00 : 400592: movq $0x0,-0x8(%rbp) 0.00 : 40059a: jmpq 400656 <branches+0xdc> 1.84 : 40059f: mov -0x10(%rbp),%rax # +100.00% 3.23 : 4005a3: and $0x1,%eax 1.84 : 4005a6: test %rax,%rax 0.00 : 4005a9: je 4005bf <branches+0x45> # -54.50% (p:42.00%) 0.46 : 4005ab: mov 0x200bbe(%rip),%rax # 601170 <acc> 12.90 : 4005b2: add $0x1,%rax 2.30 : 4005b6: mov %rax,0x200bb3(%rip) # 601170 <acc> 0.46 : 4005bd: jmp 4005d1 <branches+0x57> # -100.00% (p:100.00%) 0.92 : 4005bf: mov 0x200baa(%rip),%rax # 601170 <acc> # +49.54% 13.82 : 4005c6: sub $0x1,%rax 0.46 : 4005ca: mov %rax,0x200b9f(%rip) # 601170 <acc> 2.30 : 4005d1: mov -0x10(%rbp),%rax # +50.46% 0.46 : 4005d5: mov %rax,%rdi 0.46 : 4005d8: callq 400526 <lfsr> # -100.00% (p:100.00%) 0.00 : 4005dd: mov %rax,-0x10(%rbp) # +100.00% 0.92 : 4005e1: mov -0x18(%rbp),%rax 0.00 : 4005e5: and $0x1,%eax 0.00 : 4005e8: test %rax,%rax 0.00 : 4005eb: je 4005ff <branches+0x85> # -100.00% (p:100.00%) 0.00 : 4005ed: mov 0x200b7c(%rip),%rax # 601170 <acc> 0.00 : 4005f4: shr $0x2,%rax 0.00 : 4005f8: mov %rax,0x200b71(%rip) # 601170 <acc> 0.00 : 4005ff: mov -0x10(%rbp),%rax # +100.00% 7.37 : 400603: and $0x1,%eax 3.69 : 400606: test %rax,%rax 0.00 : 400609: jne 400612 <branches+0x98> # -59.25% (p:42.99%) 1.84 : 40060b: mov $0x1,%eax 14.29 : 400610: jmp 400617 <branches+0x9d> # -100.00% (p:100.00%) 1.38 : 400612: mov $0x0,%eax # +57.65% 10.14 : 400617: test %al,%al # +42.35% 0.00 : 400619: je 40062f <branches+0xb5> # -57.65% (p:100.00%) 0.46 : 40061b: mov 0x200b4e(%rip),%rax # 601170 <acc> 2.76 : 400622: sub $0x1,%rax 0.00 : 400626: mov %rax,0x200b43(%rip) # 601170 <acc> 0.46 : 40062d: jmp 400641 <branches+0xc7> # -100.00% (p:100.00%) 0.92 : 40062f: mov 0x200b3a(%rip),%rax # 601170 <acc> # +56.13% 2.30 : 400636: add $0x1,%rax 0.92 : 40063a: mov %rax,0x200b2f(%rip) # 601170 <acc> 0.92 : 400641: mov -0x10(%rbp),%rax # +43.87% 2.30 : 400645: mov %rax,%rdi 0.00 : 400648: callq 400526 <lfsr> # -100.00% (p:100.00%) 0.00 : 40064d: mov %rax,-0x10(%rbp) # +100.00% 1.84 : 400651: addq $0x1,-0x8(%rbp) 0.92 : 400656: mov -0x8(%rbp),%rax 5.07 : 40065a: cmp -0x20(%rbp),%rax 0.00 : 40065e: jb 40059f <branches+0x25> # -100.00% (p:100.00%) 0.00 : 400664: nop 0.00 : 400665: leaveq 0.00 : 400666: retq (Note: the --branch-filter u,any was used to avoid spurious target and branch points due to interrupts/faults, they show up as very small -/+ annotations on 'weird' locations) Committer note: Please take a look at: http://vger.kernel.org/~acme/perf/annotate_basic_blocks.png To see the colors. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <andi@firstfloor.org> Cc: Anshuman Khandual <khandual@linux.vnet.ibm.com> Cc: David Carrillo-Cisneros <davidcc@google.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kan Liang <kan.liang@intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Stephane Eranian <eranian@google.com> [ Moved sym->max_coverage to 'struct annotate', aka symbol__annotate(sym) ] Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-09-06 03:08:12 +08:00
#include "block-range.h"
#include "annotate.h"
struct {
struct rb_root root;
u64 blocks;
} block_ranges;
static void block_range__debug(void)
{
/*
* XXX still paranoid for now; see if we can make this depend on
* DEBUG=1 builds.
*/
#if 1
struct rb_node *rb;
u64 old = 0; /* NULL isn't executable */
for (rb = rb_first(&block_ranges.root); rb; rb = rb_next(rb)) {
struct block_range *entry = rb_entry(rb, struct block_range, node);
assert(old < entry->start);
assert(entry->start <= entry->end); /* single instruction block; jump to a jump */
old = entry->end;
}
#endif
}
struct block_range *block_range__find(u64 addr)
{
struct rb_node **p = &block_ranges.root.rb_node;
struct rb_node *parent = NULL;
struct block_range *entry;
while (*p != NULL) {
parent = *p;
entry = rb_entry(parent, struct block_range, node);
if (addr < entry->start)
p = &parent->rb_left;
else if (addr > entry->end)
p = &parent->rb_right;
else
return entry;
}
return NULL;
}
static inline void rb_link_left_of_node(struct rb_node *left, struct rb_node *node)
{
struct rb_node **p = &node->rb_left;
while (*p) {
node = *p;
p = &node->rb_right;
}
rb_link_node(left, node, p);
}
static inline void rb_link_right_of_node(struct rb_node *right, struct rb_node *node)
{
struct rb_node **p = &node->rb_right;
while (*p) {
node = *p;
p = &node->rb_left;
}
rb_link_node(right, node, p);
}
/**
* block_range__create
* @start: branch target starting this basic block
* @end: branch ending this basic block
*
* Create all the required block ranges to precisely span the given range.
*/
struct block_range_iter block_range__create(u64 start, u64 end)
{
struct rb_node **p = &block_ranges.root.rb_node;
struct rb_node *n, *parent = NULL;
struct block_range *next, *entry = NULL;
struct block_range_iter iter = { NULL, NULL };
while (*p != NULL) {
parent = *p;
entry = rb_entry(parent, struct block_range, node);
if (start < entry->start)
p = &parent->rb_left;
else if (start > entry->end)
p = &parent->rb_right;
else
break;
}
/*
* Didn't find anything.. there's a hole at @start, however @end might
* be inside/behind the next range.
*/
if (!*p) {
if (!entry) /* tree empty */
goto do_whole;
/*
* If the last node is before, advance one to find the next.
*/
n = parent;
if (entry->end < start) {
n = rb_next(n);
if (!n)
goto do_whole;
}
next = rb_entry(n, struct block_range, node);
if (next->start <= end) { /* add head: [start...][n->start...] */
struct block_range *head = malloc(sizeof(struct block_range));
if (!head)
return iter;
*head = (struct block_range){
.start = start,
.end = next->start - 1,
.is_target = 1,
.is_branch = 0,
};
rb_link_left_of_node(&head->node, &next->node);
rb_insert_color(&head->node, &block_ranges.root);
block_range__debug();
iter.start = head;
goto do_tail;
}
do_whole:
/*
* The whole [start..end] range is non-overlapping.
*/
entry = malloc(sizeof(struct block_range));
if (!entry)
return iter;
*entry = (struct block_range){
.start = start,
.end = end,
.is_target = 1,
.is_branch = 1,
};
rb_link_node(&entry->node, parent, p);
rb_insert_color(&entry->node, &block_ranges.root);
block_range__debug();
iter.start = entry;
iter.end = entry;
goto done;
}
/*
* We found a range that overlapped with ours, split if needed.
*/
if (entry->start < start) { /* split: [e->start...][start...] */
struct block_range *head = malloc(sizeof(struct block_range));
if (!head)
return iter;
*head = (struct block_range){
.start = entry->start,
.end = start - 1,
.is_target = entry->is_target,
.is_branch = 0,
.coverage = entry->coverage,
.entry = entry->entry,
};
entry->start = start;
entry->is_target = 1;
entry->entry = 0;
rb_link_left_of_node(&head->node, &entry->node);
rb_insert_color(&head->node, &block_ranges.root);
block_range__debug();
} else if (entry->start == start)
entry->is_target = 1;
iter.start = entry;
do_tail:
/*
* At this point we've got: @iter.start = [@start...] but @end can still be
* inside or beyond it.
*/
entry = iter.start;
for (;;) {
/*
* If @end is inside @entry, split.
*/
if (end < entry->end) { /* split: [...end][...e->end] */
struct block_range *tail = malloc(sizeof(struct block_range));
if (!tail)
return iter;
*tail = (struct block_range){
.start = end + 1,
.end = entry->end,
.is_target = 0,
.is_branch = entry->is_branch,
.coverage = entry->coverage,
.taken = entry->taken,
.pred = entry->pred,
};
entry->end = end;
entry->is_branch = 1;
entry->taken = 0;
entry->pred = 0;
rb_link_right_of_node(&tail->node, &entry->node);
rb_insert_color(&tail->node, &block_ranges.root);
block_range__debug();
iter.end = entry;
goto done;
}
/*
* If @end matches @entry, done
*/
if (end == entry->end) {
entry->is_branch = 1;
iter.end = entry;
goto done;
}
next = block_range__next(entry);
if (!next)
goto add_tail;
/*
* If @end is in beyond @entry but not inside @next, add tail.
*/
if (end < next->start) { /* add tail: [...e->end][...end] */
struct block_range *tail;
add_tail:
tail = malloc(sizeof(struct block_range));
if (!tail)
return iter;
*tail = (struct block_range){
.start = entry->end + 1,
.end = end,
.is_target = 0,
.is_branch = 1,
};
rb_link_right_of_node(&tail->node, &entry->node);
rb_insert_color(&tail->node, &block_ranges.root);
block_range__debug();
iter.end = tail;
goto done;
}
/*
* If there is a hole between @entry and @next, fill it.
*/
if (entry->end + 1 != next->start) {
struct block_range *hole = malloc(sizeof(struct block_range));
if (!hole)
return iter;
*hole = (struct block_range){
.start = entry->end + 1,
.end = next->start - 1,
.is_target = 0,
.is_branch = 0,
};
rb_link_left_of_node(&hole->node, &next->node);
rb_insert_color(&hole->node, &block_ranges.root);
block_range__debug();
}
entry = next;
}
done:
assert(iter.start->start == start && iter.start->is_target);
assert(iter.end->end == end && iter.end->is_branch);
block_ranges.blocks++;
return iter;
}
/*
* Compute coverage as:
*
* br->coverage / br->sym->max_coverage
*
* This ensures each symbol has a 100% spot, to reflect that each symbol has a
* most covered section.
*
* Returns [0-1] for coverage and -1 if we had no data what so ever or the
* symbol does not exist.
*/
double block_range__coverage(struct block_range *br)
{
struct symbol *sym;
if (!br) {
if (block_ranges.blocks)
return 0;
return -1;
}
sym = br->sym;
if (!sym)
return -1;
return (double)br->coverage / symbol__annotation(sym)->max_coverage;
}