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
|
2011-01-19 01:15:24 +08:00
|
|
|
#include <dirent.h>
|
2017-04-18 21:46:11 +08:00
|
|
|
#include <errno.h>
|
2012-01-20 00:08:15 +08:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2011-01-19 01:15:24 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2012-01-20 00:08:15 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2017-04-18 03:51:59 +08:00
|
|
|
#include "string2.h"
|
2012-02-09 00:32:52 +08:00
|
|
|
#include "strlist.h"
|
|
|
|
#include <string.h>
|
2015-06-26 17:29:07 +08:00
|
|
|
#include <api/fs/fs.h>
|
2015-06-23 06:36:05 +08:00
|
|
|
#include "asm/bug.h"
|
2011-01-19 01:15:24 +08:00
|
|
|
#include "thread_map.h"
|
2013-12-27 04:41:15 +08:00
|
|
|
#include "util.h"
|
2015-06-26 17:29:07 +08:00
|
|
|
#include "debug.h"
|
2015-10-25 22:51:21 +08:00
|
|
|
#include "event.h"
|
2011-01-19 01:15:24 +08:00
|
|
|
|
|
|
|
/* Skip "." and ".." directories */
|
|
|
|
static int filter(const struct dirent *dir)
|
|
|
|
{
|
|
|
|
if (dir->d_name[0] == '.')
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-26 17:29:06 +08:00
|
|
|
static void thread_map__reset(struct thread_map *map, int start, int nr)
|
|
|
|
{
|
|
|
|
size_t size = (nr - start) * sizeof(map->map[0]);
|
|
|
|
|
|
|
|
memset(&map->map[start], 0, size);
|
perf stat: Ignore error thread when enabling system-wide --per-thread
If we execute 'perf stat --per-thread' with non-root account (even set
kernel.perf_event_paranoid = -1 yet), it reports the error:
jinyao@skl:~$ perf stat --per-thread
Error:
You may not have permission to collect system-wide stats.
Consider tweaking /proc/sys/kernel/perf_event_paranoid,
which controls use of the performance events system by
unprivileged users (without CAP_SYS_ADMIN).
The current value is 2:
-1: Allow use of (almost) all events by all users
Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK
>= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN
Disallow raw tracepoint access by users without CAP_SYS_ADMIN
>= 1: Disallow CPU event access by users without CAP_SYS_ADMIN
>= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN
To make this setting permanent, edit /etc/sysctl.conf too, e.g.:
kernel.perf_event_paranoid = -1
Perhaps the ptrace rule doesn't allow to trace some processes. But anyway
the global --per-thread mode had better ignore such errors and continue
working on other threads.
This patch will record the index of error thread in perf_evsel__open()
and remove this thread before retrying.
For example (run with non-root, kernel.perf_event_paranoid isn't set):
jinyao@skl:~$ perf stat --per-thread
^C
Performance counter stats for 'system wide':
vmstat-3458 6.171984 cpu-clock:u (msec) # 0.000 CPUs utilized
perf-3670 0.515599 cpu-clock:u (msec) # 0.000 CPUs utilized
vmstat-3458 1,163,643 cycles:u # 0.189 GHz
perf-3670 40,881 cycles:u # 0.079 GHz
vmstat-3458 1,410,238 instructions:u # 1.21 insn per cycle
perf-3670 3,536 instructions:u # 0.09 insn per cycle
vmstat-3458 288,937 branches:u # 46.814 M/sec
perf-3670 936 branches:u # 1.815 M/sec
vmstat-3458 15,195 branch-misses:u # 5.26% of all branches
perf-3670 76 branch-misses:u # 8.12% of all branches
12.651675247 seconds time elapsed
Signed-off-by: Jin Yao <yao.jin@linux.intel.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Kan Liang <kan.liang@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1516117388-10120-1-git-send-email-yao.jin@linux.intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2018-01-16 23:43:08 +08:00
|
|
|
map->err_thread = -1;
|
2015-06-26 17:29:06 +08:00
|
|
|
}
|
|
|
|
|
2015-06-14 16:19:17 +08:00
|
|
|
static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
|
|
|
|
{
|
2015-06-26 01:48:49 +08:00
|
|
|
size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
|
2015-06-26 17:29:06 +08:00
|
|
|
int start = map ? map->nr : 0;
|
|
|
|
|
|
|
|
map = realloc(map, size);
|
|
|
|
/*
|
|
|
|
* We only realloc to add more items, let's reset new items.
|
|
|
|
*/
|
|
|
|
if (map)
|
|
|
|
thread_map__reset(map, start, nr);
|
2015-06-14 16:19:17 +08:00
|
|
|
|
2015-06-26 17:29:06 +08:00
|
|
|
return map;
|
2015-06-14 16:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
|
|
|
|
|
2011-01-19 01:15:24 +08:00
|
|
|
struct thread_map *thread_map__new_by_pid(pid_t pid)
|
|
|
|
{
|
|
|
|
struct thread_map *threads;
|
|
|
|
char name[256];
|
|
|
|
int items;
|
|
|
|
struct dirent **namelist = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sprintf(name, "/proc/%d/task", pid);
|
|
|
|
items = scandir(name, &namelist, filter, NULL);
|
|
|
|
if (items <= 0)
|
2012-01-20 00:08:15 +08:00
|
|
|
return NULL;
|
2011-01-19 01:15:24 +08:00
|
|
|
|
2015-06-14 16:19:17 +08:00
|
|
|
threads = thread_map__alloc(items);
|
2011-01-19 01:15:24 +08:00
|
|
|
if (threads != NULL) {
|
|
|
|
for (i = 0; i < items; i++)
|
2015-06-23 06:36:02 +08:00
|
|
|
thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
|
2011-01-19 01:15:24 +08:00
|
|
|
threads->nr = items;
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2011-01-19 01:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i<items; i++)
|
2013-12-28 03:55:14 +08:00
|
|
|
zfree(&namelist[i]);
|
2011-01-19 01:15:24 +08:00
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_map *thread_map__new_by_tid(pid_t tid)
|
|
|
|
{
|
2015-06-14 16:19:17 +08:00
|
|
|
struct thread_map *threads = thread_map__alloc(1);
|
2011-01-19 01:15:24 +08:00
|
|
|
|
|
|
|
if (threads != NULL) {
|
2015-06-23 06:36:02 +08:00
|
|
|
thread_map__set_pid(threads, 0, tid);
|
|
|
|
threads->nr = 1;
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2011-01-19 01:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:44:30 +08:00
|
|
|
static struct thread_map *__thread_map__new_all_cpus(uid_t uid)
|
2012-01-20 00:08:15 +08:00
|
|
|
{
|
|
|
|
DIR *proc;
|
|
|
|
int max_threads = 32, items, i;
|
perf thread_map: Correctly size buffer used with dirent->dt_name
The size of dirent->dt_name is NAME_MAX + 1, but the size for the 'path'
buffer is hard coded at 256, which may truncate it because we also
prepend "/proc/", so that all that into account and thank gcc 7 for this
warning:
/git/linux/tools/perf/util/thread_map.c: In function 'thread_map__new_by_uid':
/git/linux/tools/perf/util/thread_map.c:119:39: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size 250 [-Werror=format-truncation=]
snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
^~
In file included from /usr/include/stdio.h:939:0,
from /git/linux/tools/perf/util/thread_map.c:5:
/usr/include/bits/stdio2.h:64:10: note: '__builtin___snprintf_chk' output between 7 and 262 bytes into a destination of size 256
return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__bos (__s), __fmt, __va_arg_pack ());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-csy0r8zrvz5efccgd4k12c82@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2017-02-09 04:01:46 +08:00
|
|
|
char path[NAME_MAX + 1 + 6];
|
2016-04-08 22:31:24 +08:00
|
|
|
struct dirent *dirent, **namelist = NULL;
|
2015-06-14 16:19:17 +08:00
|
|
|
struct thread_map *threads = thread_map__alloc(max_threads);
|
|
|
|
|
2012-01-20 00:08:15 +08:00
|
|
|
if (threads == NULL)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
proc = opendir("/proc");
|
|
|
|
if (proc == NULL)
|
|
|
|
goto out_free_threads;
|
|
|
|
|
|
|
|
threads->nr = 0;
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2012-01-20 00:08:15 +08:00
|
|
|
|
2016-04-08 22:31:24 +08:00
|
|
|
while ((dirent = readdir(proc)) != NULL) {
|
2012-01-20 00:08:15 +08:00
|
|
|
char *end;
|
|
|
|
bool grow = false;
|
2016-04-08 22:31:24 +08:00
|
|
|
pid_t pid = strtol(dirent->d_name, &end, 10);
|
2012-01-20 00:08:15 +08:00
|
|
|
|
|
|
|
if (*end) /* only interested in proper numerical dirents */
|
|
|
|
continue;
|
|
|
|
|
2016-04-08 22:31:24 +08:00
|
|
|
snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
|
2012-01-20 00:08:15 +08:00
|
|
|
|
2017-12-01 22:44:30 +08:00
|
|
|
if (uid != UINT_MAX) {
|
|
|
|
struct stat st;
|
2012-01-20 00:08:15 +08:00
|
|
|
|
2017-12-01 22:44:30 +08:00
|
|
|
if (stat(path, &st) != 0 || st.st_uid != uid)
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-20 00:08:15 +08:00
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "/proc/%d/task", pid);
|
|
|
|
items = scandir(path, &namelist, filter, NULL);
|
|
|
|
if (items <= 0)
|
|
|
|
goto out_free_closedir;
|
|
|
|
|
|
|
|
while (threads->nr + items >= max_threads) {
|
|
|
|
max_threads *= 2;
|
|
|
|
grow = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grow) {
|
|
|
|
struct thread_map *tmp;
|
|
|
|
|
perf thread_map: Fix the sizeof() calculation for map entries
When we started adding extra stuff per array entry, growing the size of
those entries to more than sizeof(pid_t), we had to convert those sizeof
operations to the more robust sizeof(map->map[0]) idiom, that is future
proof, i.e. if/when we add more stuff to those entries, that expression
will produce the new per-entry size.
And besides that, we need to zero out those extra fields, that sometimes
may not get filled, like when we couldn't care less about the comms,
since we don't need those, but since we will try freeing it at
thread_map__delete(), we better fix it.
That is why a thread_map__realloc() was provided.
But that method wasn't used in thread_map__new_by_uid(), fix it.
Reported-by: Ingo Molnar <mingo@kernel.org>
Fixes: 792402fd5c0a ("perf thrad_map: Add comm string into array")
Fixes: 9d7e8c3a96e5 ("perf tools: Add thread_map__(alloc|realloc) helpers")
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-6a0swlm6m8lnu3wpjv284hkb@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-07-09 23:14:43 +08:00
|
|
|
tmp = thread_map__realloc(threads, max_threads);
|
2012-01-20 00:08:15 +08:00
|
|
|
if (tmp == NULL)
|
|
|
|
goto out_free_namelist;
|
|
|
|
|
|
|
|
threads = tmp;
|
|
|
|
}
|
|
|
|
|
2015-06-23 06:36:02 +08:00
|
|
|
for (i = 0; i < items; i++) {
|
|
|
|
thread_map__set_pid(threads, threads->nr + i,
|
|
|
|
atoi(namelist[i]->d_name));
|
|
|
|
}
|
2012-01-20 00:08:15 +08:00
|
|
|
|
|
|
|
for (i = 0; i < items; i++)
|
2013-12-28 03:55:14 +08:00
|
|
|
zfree(&namelist[i]);
|
2012-01-20 00:08:15 +08:00
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
threads->nr += items;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_closedir:
|
|
|
|
closedir(proc);
|
|
|
|
out:
|
|
|
|
return threads;
|
|
|
|
|
|
|
|
out_free_threads:
|
|
|
|
free(threads);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
out_free_namelist:
|
|
|
|
for (i = 0; i < items; i++)
|
2013-12-28 03:55:14 +08:00
|
|
|
zfree(&namelist[i]);
|
2012-01-20 00:08:15 +08:00
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
out_free_closedir:
|
2013-12-27 04:41:15 +08:00
|
|
|
zfree(&threads);
|
2012-01-20 00:08:15 +08:00
|
|
|
goto out_closedir;
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:44:30 +08:00
|
|
|
struct thread_map *thread_map__new_all_cpus(void)
|
|
|
|
{
|
|
|
|
return __thread_map__new_all_cpus(UINT_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_map *thread_map__new_by_uid(uid_t uid)
|
|
|
|
{
|
|
|
|
return __thread_map__new_all_cpus(uid);
|
|
|
|
}
|
|
|
|
|
2012-01-20 00:08:15 +08:00
|
|
|
struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
|
2011-01-19 01:15:24 +08:00
|
|
|
{
|
|
|
|
if (pid != -1)
|
|
|
|
return thread_map__new_by_pid(pid);
|
2012-01-20 00:08:15 +08:00
|
|
|
|
|
|
|
if (tid == -1 && uid != UINT_MAX)
|
|
|
|
return thread_map__new_by_uid(uid);
|
|
|
|
|
2011-01-19 01:15:24 +08:00
|
|
|
return thread_map__new_by_tid(tid);
|
|
|
|
}
|
|
|
|
|
2012-02-09 00:32:52 +08:00
|
|
|
static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
|
|
|
|
{
|
|
|
|
struct thread_map *threads = NULL, *nt;
|
|
|
|
char name[256];
|
|
|
|
int items, total_tasks = 0;
|
|
|
|
struct dirent **namelist = NULL;
|
|
|
|
int i, j = 0;
|
|
|
|
pid_t pid, prev_pid = INT_MAX;
|
|
|
|
char *end_ptr;
|
|
|
|
struct str_node *pos;
|
2015-07-20 23:13:34 +08:00
|
|
|
struct strlist_config slist_config = { .dont_dupstr = true, };
|
|
|
|
struct strlist *slist = strlist__new(pid_str, &slist_config);
|
2012-02-09 00:32:52 +08:00
|
|
|
|
|
|
|
if (!slist)
|
|
|
|
return NULL;
|
|
|
|
|
2016-06-23 22:31:20 +08:00
|
|
|
strlist__for_each_entry(pos, slist) {
|
2012-02-09 00:32:52 +08:00
|
|
|
pid = strtol(pos->s, &end_ptr, 10);
|
|
|
|
|
|
|
|
if (pid == INT_MIN || pid == INT_MAX ||
|
|
|
|
(*end_ptr != '\0' && *end_ptr != ','))
|
|
|
|
goto out_free_threads;
|
|
|
|
|
|
|
|
if (pid == prev_pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sprintf(name, "/proc/%d/task", pid);
|
|
|
|
items = scandir(name, &namelist, filter, NULL);
|
|
|
|
if (items <= 0)
|
|
|
|
goto out_free_threads;
|
|
|
|
|
|
|
|
total_tasks += items;
|
2015-06-14 16:19:17 +08:00
|
|
|
nt = thread_map__realloc(threads, total_tasks);
|
2012-02-09 00:32:52 +08:00
|
|
|
if (nt == NULL)
|
2012-05-25 21:21:49 +08:00
|
|
|
goto out_free_namelist;
|
2012-02-09 00:32:52 +08:00
|
|
|
|
|
|
|
threads = nt;
|
|
|
|
|
2012-05-25 21:21:49 +08:00
|
|
|
for (i = 0; i < items; i++) {
|
2015-06-23 06:36:02 +08:00
|
|
|
thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
|
2013-12-28 03:55:14 +08:00
|
|
|
zfree(&namelist[i]);
|
2012-05-25 21:21:49 +08:00
|
|
|
}
|
|
|
|
threads->nr = total_tasks;
|
2012-02-09 00:32:52 +08:00
|
|
|
free(namelist);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
strlist__delete(slist);
|
2015-06-23 06:36:05 +08:00
|
|
|
if (threads)
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2012-02-09 00:32:52 +08:00
|
|
|
return threads;
|
|
|
|
|
2012-05-25 21:21:49 +08:00
|
|
|
out_free_namelist:
|
|
|
|
for (i = 0; i < items; i++)
|
2013-12-28 03:55:14 +08:00
|
|
|
zfree(&namelist[i]);
|
2012-05-25 21:21:49 +08:00
|
|
|
free(namelist);
|
|
|
|
|
2012-02-09 00:32:52 +08:00
|
|
|
out_free_threads:
|
2013-12-27 04:41:15 +08:00
|
|
|
zfree(&threads);
|
2012-02-09 00:32:52 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2014-10-10 23:03:46 +08:00
|
|
|
struct thread_map *thread_map__new_dummy(void)
|
|
|
|
{
|
2015-06-14 16:19:17 +08:00
|
|
|
struct thread_map *threads = thread_map__alloc(1);
|
2014-10-10 23:03:46 +08:00
|
|
|
|
|
|
|
if (threads != NULL) {
|
2015-06-23 06:36:02 +08:00
|
|
|
thread_map__set_pid(threads, 0, -1);
|
|
|
|
threads->nr = 1;
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2014-10-10 23:03:46 +08:00
|
|
|
}
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
2016-04-12 21:29:28 +08:00
|
|
|
struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
|
2012-02-09 00:32:52 +08:00
|
|
|
{
|
|
|
|
struct thread_map *threads = NULL, *nt;
|
|
|
|
int ntasks = 0;
|
|
|
|
pid_t tid, prev_tid = INT_MAX;
|
|
|
|
char *end_ptr;
|
|
|
|
struct str_node *pos;
|
2015-07-20 23:13:34 +08:00
|
|
|
struct strlist_config slist_config = { .dont_dupstr = true, };
|
2012-02-09 00:32:52 +08:00
|
|
|
struct strlist *slist;
|
|
|
|
|
|
|
|
/* perf-stat expects threads to be generated even if tid not given */
|
2014-10-10 23:03:46 +08:00
|
|
|
if (!tid_str)
|
|
|
|
return thread_map__new_dummy();
|
2012-02-09 00:32:52 +08:00
|
|
|
|
2015-07-20 23:13:34 +08:00
|
|
|
slist = strlist__new(tid_str, &slist_config);
|
2012-02-09 00:32:52 +08:00
|
|
|
if (!slist)
|
|
|
|
return NULL;
|
|
|
|
|
2016-06-23 22:31:20 +08:00
|
|
|
strlist__for_each_entry(pos, slist) {
|
2012-02-09 00:32:52 +08:00
|
|
|
tid = strtol(pos->s, &end_ptr, 10);
|
|
|
|
|
|
|
|
if (tid == INT_MIN || tid == INT_MAX ||
|
|
|
|
(*end_ptr != '\0' && *end_ptr != ','))
|
|
|
|
goto out_free_threads;
|
|
|
|
|
|
|
|
if (tid == prev_tid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ntasks++;
|
2015-06-14 16:19:17 +08:00
|
|
|
nt = thread_map__realloc(threads, ntasks);
|
2012-02-09 00:32:52 +08:00
|
|
|
|
|
|
|
if (nt == NULL)
|
|
|
|
goto out_free_threads;
|
|
|
|
|
|
|
|
threads = nt;
|
2015-06-23 06:36:02 +08:00
|
|
|
thread_map__set_pid(threads, ntasks - 1, tid);
|
|
|
|
threads->nr = ntasks;
|
2012-02-09 00:32:52 +08:00
|
|
|
}
|
|
|
|
out:
|
2015-06-23 06:36:05 +08:00
|
|
|
if (threads)
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2012-02-09 00:32:52 +08:00
|
|
|
return threads;
|
|
|
|
|
|
|
|
out_free_threads:
|
2013-12-27 04:41:15 +08:00
|
|
|
zfree(&threads);
|
2015-12-10 11:00:58 +08:00
|
|
|
strlist__delete(slist);
|
2012-02-09 00:32:52 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_map *thread_map__new_str(const char *pid, const char *tid,
|
2018-02-13 04:32:36 +08:00
|
|
|
uid_t uid, bool all_threads)
|
2012-02-09 00:32:52 +08:00
|
|
|
{
|
|
|
|
if (pid)
|
|
|
|
return thread_map__new_by_pid_str(pid);
|
|
|
|
|
|
|
|
if (!tid && uid != UINT_MAX)
|
|
|
|
return thread_map__new_by_uid(uid);
|
|
|
|
|
2018-02-13 04:32:36 +08:00
|
|
|
if (all_threads)
|
2017-12-05 22:03:09 +08:00
|
|
|
return thread_map__new_all_cpus();
|
|
|
|
|
2012-02-09 00:32:52 +08:00
|
|
|
return thread_map__new_by_tid_str(tid);
|
|
|
|
}
|
|
|
|
|
2015-06-23 06:36:05 +08:00
|
|
|
static void thread_map__delete(struct thread_map *threads)
|
2011-01-19 01:15:24 +08:00
|
|
|
{
|
2015-06-23 06:36:05 +08:00
|
|
|
if (threads) {
|
2015-06-26 17:29:07 +08:00
|
|
|
int i;
|
|
|
|
|
2017-02-21 23:35:03 +08:00
|
|
|
WARN_ONCE(refcount_read(&threads->refcnt) != 0,
|
2015-06-23 06:36:05 +08:00
|
|
|
"thread map refcnt unbalanced\n");
|
2015-06-26 17:29:07 +08:00
|
|
|
for (i = 0; i < threads->nr; i++)
|
|
|
|
free(thread_map__comm(threads, i));
|
2015-06-23 06:36:05 +08:00
|
|
|
free(threads);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_map *thread_map__get(struct thread_map *map)
|
|
|
|
{
|
|
|
|
if (map)
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_inc(&map->refcnt);
|
2015-06-23 06:36:05 +08:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_map__put(struct thread_map *map)
|
|
|
|
{
|
2017-02-21 23:35:03 +08:00
|
|
|
if (map && refcount_dec_and_test(&map->refcnt))
|
2015-06-23 06:36:05 +08:00
|
|
|
thread_map__delete(map);
|
2011-01-19 01:15:24 +08:00
|
|
|
}
|
2012-01-20 00:07:23 +08:00
|
|
|
|
|
|
|
size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t printed = fprintf(fp, "%d thread%s: ",
|
|
|
|
threads->nr, threads->nr > 1 ? "s" : "");
|
|
|
|
for (i = 0; i < threads->nr; ++i)
|
2015-06-23 06:36:02 +08:00
|
|
|
printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
|
2012-01-20 00:07:23 +08:00
|
|
|
|
|
|
|
return printed + fprintf(fp, "\n");
|
|
|
|
}
|
2015-06-26 17:29:07 +08:00
|
|
|
|
|
|
|
static int get_comm(char **comm, pid_t pid)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
size_t size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
err = filename__read_str(path, comm, &size);
|
|
|
|
if (!err) {
|
|
|
|
/*
|
|
|
|
* We're reading 16 bytes, while filename__read_str
|
|
|
|
* allocates data per BUFSIZ bytes, so we can safely
|
|
|
|
* mark the end of the string.
|
|
|
|
*/
|
|
|
|
(*comm)[size] = 0;
|
|
|
|
rtrim(*comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void comm_init(struct thread_map *map, int i)
|
|
|
|
{
|
|
|
|
pid_t pid = thread_map__pid(map, i);
|
|
|
|
char *comm = NULL;
|
|
|
|
|
|
|
|
/* dummy pid comm initialization */
|
|
|
|
if (pid == -1) {
|
|
|
|
map->map[i].comm = strdup("dummy");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The comm name is like extra bonus ;-),
|
|
|
|
* so just warn if we fail for any reason.
|
|
|
|
*/
|
|
|
|
if (get_comm(&comm, pid))
|
|
|
|
pr_warning("Couldn't resolve comm name for pid %d\n", pid);
|
|
|
|
|
|
|
|
map->map[i].comm = comm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_map__read_comms(struct thread_map *threads)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < threads->nr; ++i)
|
|
|
|
comm_init(threads, i);
|
|
|
|
}
|
2015-10-25 22:51:21 +08:00
|
|
|
|
|
|
|
static void thread_map__copy_event(struct thread_map *threads,
|
|
|
|
struct thread_map_event *event)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
threads->nr = (int) event->nr;
|
|
|
|
|
|
|
|
for (i = 0; i < event->nr; i++) {
|
|
|
|
thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
|
|
|
|
threads->map[i].comm = strndup(event->entries[i].comm, 16);
|
|
|
|
}
|
|
|
|
|
2017-02-21 23:35:03 +08:00
|
|
|
refcount_set(&threads->refcnt, 1);
|
2015-10-25 22:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct thread_map *thread_map__new_event(struct thread_map_event *event)
|
|
|
|
{
|
|
|
|
struct thread_map *threads;
|
|
|
|
|
|
|
|
threads = thread_map__alloc(event->nr);
|
|
|
|
if (threads)
|
|
|
|
thread_map__copy_event(threads, event);
|
|
|
|
|
|
|
|
return threads;
|
|
|
|
}
|
2016-04-12 21:29:24 +08:00
|
|
|
|
|
|
|
bool thread_map__has(struct thread_map *threads, pid_t pid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < threads->nr; ++i) {
|
|
|
|
if (threads->map[i].pid == pid)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-12 18:35:41 +08:00
|
|
|
|
|
|
|
int thread_map__remove(struct thread_map *threads, int idx)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (threads->nr < 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (idx >= threads->nr)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the 'idx' item and shift the rest up.
|
|
|
|
*/
|
|
|
|
free(threads->map[idx].comm);
|
|
|
|
|
|
|
|
for (i = idx; i < threads->nr - 1; i++)
|
|
|
|
threads->map[i] = threads->map[i + 1];
|
|
|
|
|
|
|
|
threads->nr--;
|
|
|
|
return 0;
|
|
|
|
}
|