tools/perf/util/thread_map.c
Source file repositories/reference/linux-study-clean/tools/perf/util/thread_map.c
File Facts
- System
- Linux kernel
- Corpus path
tools/perf/util/thread_map.c- Extension
.c- Size
- 8207 bytes
- Lines
- 398
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
dirent.herrno.hlimits.hstdbool.hstdlib.hstdio.hsys/types.hsys/stat.hunistd.hstring2.hstrlist.hstring.hapi/fs/fs.hlinux/string.hlinux/zalloc.hasm/bug.hthread_map.hdebug.hevent.hinternal/threadmap.h
Detected Declarations
function filterfunction strlist__for_each_entryfunction strlist__for_each_entryfunction thread_map__fprintffunction get_commfunction comm_initfunction thread_map__read_commsfunction thread_map__copy_eventfunction thread_map__hasfunction thread_map__remove
Annotated Snippet
if (items <= 0) {
pr_debug("scandir for %d returned empty, skipping\n", pid);
continue;
}
while (threads->nr + items >= max_threads) {
max_threads *= 2;
grow = true;
}
if (grow) {
struct perf_thread_map *tmp;
tmp = perf_thread_map__realloc(threads, max_threads);
if (tmp == NULL)
goto out_free_namelist;
threads = tmp;
}
for (i = 0; i < items; i++) {
perf_thread_map__set_pid(threads, threads->nr + i,
atoi(namelist[i]->d_name));
}
for (i = 0; i < items; i++)
zfree(&namelist[i]);
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++)
zfree(&namelist[i]);
free(namelist);
zfree(&threads);
goto out_closedir;
}
struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid)
{
if (pid != -1)
return thread_map__new_by_pid(pid);
return thread_map__new_by_tid(tid);
}
static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
{
struct perf_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;
struct str_node *pos;
struct strlist *slist = strlist__new(pid_str, NULL);
if (!slist)
return NULL;
strlist__for_each_entry(pos, slist) {
pid = strtol(pos->s, NULL, 10);
if (pid == INT_MIN || pid == INT_MAX)
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;
nt = perf_thread_map__realloc(threads, total_tasks);
if (nt == NULL)
goto out_free_namelist;
threads = nt;
Annotation
- Immediate include surface: `dirent.h`, `errno.h`, `limits.h`, `stdbool.h`, `stdlib.h`, `stdio.h`, `sys/types.h`, `sys/stat.h`.
- Detected declarations: `function filter`, `function strlist__for_each_entry`, `function strlist__for_each_entry`, `function thread_map__fprintf`, `function get_comm`, `function comm_init`, `function thread_map__read_comms`, `function thread_map__copy_event`, `function thread_map__has`, `function thread_map__remove`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.