Skip to content

Commit 93bb5b0

Browse files
captain5050namhyung
authored andcommitted
perf threads: Move threads to its own files
Move threads out of machine and into its own file. Signed-off-by: Ian Rogers <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Yang Jihong <[email protected]> Cc: Oliver Upton <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d436f90 commit 93bb5b0

File tree

5 files changed

+285
-273
lines changed

5 files changed

+285
-273
lines changed

tools/perf/util/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ perf-y += ordered-events.o
7272
perf-y += namespaces.o
7373
perf-y += comm.o
7474
perf-y += thread.o
75+
perf-y += threads.o
7576
perf-y += thread_map.o
7677
perf-y += parse-events-flex.o
7778
perf-y += parse-events-bison.o

tools/perf/util/machine.c

Lines changed: 0 additions & 248 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@
4343
#include <linux/string.h>
4444
#include <linux/zalloc.h>
4545

46-
struct thread_rb_node {
47-
struct rb_node rb_node;
48-
struct thread *thread;
49-
};
50-
51-
static struct threads_table_entry *threads__table(struct threads *threads, pid_t tid)
52-
{
53-
/* Cast it to handle tid == -1 */
54-
return &threads->table[(unsigned int)tid % THREADS__TABLE_SIZE];
55-
}
56-
5746
static struct dso *machine__kernel_dso(struct machine *machine)
5847
{
5948
return map__dso(machine->vmlinux_map);
@@ -66,18 +55,6 @@ static void dsos__init(struct dsos *dsos)
6655
init_rwsem(&dsos->lock);
6756
}
6857

69-
void threads__init(struct threads *threads)
70-
{
71-
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
72-
struct threads_table_entry *table = &threads->table[i];
73-
74-
table->entries = RB_ROOT_CACHED;
75-
init_rwsem(&table->lock);
76-
table->nr = 0;
77-
table->last_match = NULL;
78-
}
79-
}
80-
8158
static int machine__set_mmap_name(struct machine *machine)
8259
{
8360
if (machine__is_host(machine))
@@ -210,49 +187,11 @@ static void dsos__exit(struct dsos *dsos)
210187
exit_rwsem(&dsos->lock);
211188
}
212189

213-
static void __threads_table_entry__set_last_match(struct threads_table_entry *table,
214-
struct thread *th);
215-
216-
void threads__remove_all_threads(struct threads *threads)
217-
{
218-
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
219-
struct threads_table_entry *table = &threads->table[i];
220-
struct rb_node *nd;
221-
222-
down_write(&table->lock);
223-
__threads_table_entry__set_last_match(table, NULL);
224-
nd = rb_first_cached(&table->entries);
225-
while (nd) {
226-
struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node);
227-
228-
nd = rb_next(nd);
229-
thread__put(trb->thread);
230-
rb_erase_cached(&trb->rb_node, &table->entries);
231-
RB_CLEAR_NODE(&trb->rb_node);
232-
--table->nr;
233-
234-
free(trb);
235-
}
236-
assert(table->nr == 0);
237-
up_write(&table->lock);
238-
}
239-
}
240-
241190
void machine__delete_threads(struct machine *machine)
242191
{
243192
threads__remove_all_threads(&machine->threads);
244193
}
245194

246-
void threads__exit(struct threads *threads)
247-
{
248-
threads__remove_all_threads(threads);
249-
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
250-
struct threads_table_entry *table = &threads->table[i];
251-
252-
exit_rwsem(&table->lock);
253-
}
254-
}
255-
256195
void machine__exit(struct machine *machine)
257196
{
258197
if (machine == NULL)
@@ -568,121 +507,6 @@ static void machine__update_thread_pid(struct machine *machine,
568507
goto out_put;
569508
}
570509

571-
/*
572-
* Front-end cache - TID lookups come in blocks,
573-
* so most of the time we dont have to look up
574-
* the full rbtree:
575-
*/
576-
static struct thread *__threads_table_entry__get_last_match(struct threads_table_entry *table,
577-
pid_t tid)
578-
{
579-
struct thread *th, *res = NULL;
580-
581-
th = table->last_match;
582-
if (th != NULL) {
583-
if (thread__tid(th) == tid)
584-
res = thread__get(th);
585-
}
586-
return res;
587-
}
588-
589-
static void __threads_table_entry__set_last_match(struct threads_table_entry *table,
590-
struct thread *th)
591-
{
592-
thread__put(table->last_match);
593-
table->last_match = thread__get(th);
594-
}
595-
596-
static void threads_table_entry__set_last_match(struct threads_table_entry *table,
597-
struct thread *th)
598-
{
599-
down_write(&table->lock);
600-
__threads_table_entry__set_last_match(table, th);
601-
up_write(&table->lock);
602-
}
603-
604-
struct thread *threads__find(struct threads *threads, pid_t tid)
605-
{
606-
struct threads_table_entry *table = threads__table(threads, tid);
607-
struct rb_node **p;
608-
struct thread *res = NULL;
609-
610-
down_read(&table->lock);
611-
res = __threads_table_entry__get_last_match(table, tid);
612-
if (res)
613-
return res;
614-
615-
p = &table->entries.rb_root.rb_node;
616-
while (*p != NULL) {
617-
struct rb_node *parent = *p;
618-
struct thread *th = rb_entry(parent, struct thread_rb_node, rb_node)->thread;
619-
620-
if (thread__tid(th) == tid) {
621-
res = thread__get(th);
622-
break;
623-
}
624-
625-
if (tid < thread__tid(th))
626-
p = &(*p)->rb_left;
627-
else
628-
p = &(*p)->rb_right;
629-
}
630-
up_read(&table->lock);
631-
if (res)
632-
threads_table_entry__set_last_match(table, res);
633-
return res;
634-
}
635-
636-
struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created)
637-
{
638-
struct threads_table_entry *table = threads__table(threads, tid);
639-
struct rb_node **p;
640-
struct rb_node *parent = NULL;
641-
struct thread *res = NULL;
642-
struct thread_rb_node *nd;
643-
bool leftmost = true;
644-
645-
*created = false;
646-
down_write(&table->lock);
647-
p = &table->entries.rb_root.rb_node;
648-
while (*p != NULL) {
649-
struct thread *th;
650-
651-
parent = *p;
652-
th = rb_entry(parent, struct thread_rb_node, rb_node)->thread;
653-
654-
if (thread__tid(th) == tid) {
655-
__threads_table_entry__set_last_match(table, th);
656-
res = thread__get(th);
657-
goto out_unlock;
658-
}
659-
660-
if (tid < thread__tid(th))
661-
p = &(*p)->rb_left;
662-
else {
663-
p = &(*p)->rb_right;
664-
leftmost = false;
665-
}
666-
}
667-
nd = malloc(sizeof(*nd));
668-
if (nd == NULL)
669-
goto out_unlock;
670-
res = thread__new(pid, tid);
671-
if (!res)
672-
free(nd);
673-
else {
674-
*created = true;
675-
nd->thread = thread__get(res);
676-
rb_link_node(&nd->rb_node, parent, p);
677-
rb_insert_color_cached(&nd->rb_node, &table->entries, leftmost);
678-
++table->nr;
679-
__threads_table_entry__set_last_match(table, res);
680-
}
681-
out_unlock:
682-
up_write(&table->lock);
683-
return res;
684-
}
685-
686510
/*
687511
* Caller must eventually drop thread->refcnt returned with a successful
688512
* lookup/new thread inserted.
@@ -699,7 +523,6 @@ static struct thread *__machine__findnew_thread(struct machine *machine,
699523
machine__update_thread_pid(machine, th, pid);
700524
return th;
701525
}
702-
703526
if (!create)
704527
return NULL;
705528

@@ -1147,20 +970,6 @@ static int machine_fprintf_cb(struct thread *thread, void *data)
1147970
return 0;
1148971
}
1149972

1150-
size_t threads__nr(struct threads *threads)
1151-
{
1152-
size_t nr = 0;
1153-
1154-
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
1155-
struct threads_table_entry *table = &threads->table[i];
1156-
1157-
down_read(&table->lock);
1158-
nr += table->nr;
1159-
up_read(&table->lock);
1160-
}
1161-
return nr;
1162-
}
1163-
1164973
size_t machine__fprintf(struct machine *machine, FILE *fp)
1165974
{
1166975
struct machine_fprintf_cb_args args = {
@@ -2093,39 +1902,6 @@ int machine__process_mmap_event(struct machine *machine, union perf_event *event
20931902
return 0;
20941903
}
20951904

2096-
void threads__remove(struct threads *threads, struct thread *thread)
2097-
{
2098-
struct rb_node **p;
2099-
struct threads_table_entry *table = threads__table(threads, thread__tid(thread));
2100-
pid_t tid = thread__tid(thread);
2101-
2102-
down_write(&table->lock);
2103-
if (table->last_match && RC_CHK_EQUAL(table->last_match, thread))
2104-
__threads_table_entry__set_last_match(table, NULL);
2105-
2106-
p = &table->entries.rb_root.rb_node;
2107-
while (*p != NULL) {
2108-
struct rb_node *parent = *p;
2109-
struct thread_rb_node *nd = rb_entry(parent, struct thread_rb_node, rb_node);
2110-
struct thread *th = nd->thread;
2111-
2112-
if (RC_CHK_EQUAL(th, thread)) {
2113-
thread__put(nd->thread);
2114-
rb_erase_cached(&nd->rb_node, &table->entries);
2115-
RB_CLEAR_NODE(&nd->rb_node);
2116-
--table->nr;
2117-
free(nd);
2118-
break;
2119-
}
2120-
2121-
if (tid < thread__tid(th))
2122-
p = &(*p)->rb_left;
2123-
else
2124-
p = &(*p)->rb_right;
2125-
}
2126-
up_write(&table->lock);
2127-
}
2128-
21291905
void machine__remove_thread(struct machine *machine, struct thread *th)
21301906
{
21311907
return threads__remove(&machine->threads, th);
@@ -3258,30 +3034,6 @@ int thread__resolve_callchain(struct thread *thread,
32583034
return ret;
32593035
}
32603036

3261-
int threads__for_each_thread(struct threads *threads,
3262-
int (*fn)(struct thread *thread, void *data),
3263-
void *data)
3264-
{
3265-
for (int i = 0; i < THREADS__TABLE_SIZE; i++) {
3266-
struct threads_table_entry *table = &threads->table[i];
3267-
struct rb_node *nd;
3268-
3269-
down_read(&table->lock);
3270-
for (nd = rb_first_cached(&table->entries); nd; nd = rb_next(nd)) {
3271-
struct thread_rb_node *trb = rb_entry(nd, struct thread_rb_node, rb_node);
3272-
int rc = fn(trb->thread, data);
3273-
3274-
if (rc != 0) {
3275-
up_read(&table->lock);
3276-
return rc;
3277-
}
3278-
}
3279-
up_read(&table->lock);
3280-
}
3281-
return 0;
3282-
3283-
}
3284-
32853037
int machine__for_each_thread(struct machine *machine,
32863038
int (*fn)(struct thread *thread, void *p),
32873039
void *priv)

tools/perf/util/machine.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "maps.h"
88
#include "dsos.h"
99
#include "rwsem.h"
10+
#include "threads.h"
1011

1112
struct addr_location;
1213
struct branch_stack;
@@ -28,31 +29,6 @@ extern const char *ref_reloc_sym_names[];
2829

2930
struct vdso_info;
3031

31-
#define THREADS__TABLE_BITS 8
32-
#define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS)
33-
34-
struct threads_table_entry {
35-
struct rb_root_cached entries;
36-
struct rw_semaphore lock;
37-
unsigned int nr;
38-
struct thread *last_match;
39-
};
40-
41-
struct threads {
42-
struct threads_table_entry table[THREADS__TABLE_SIZE];
43-
};
44-
45-
void threads__init(struct threads *threads);
46-
void threads__exit(struct threads *threads);
47-
size_t threads__nr(struct threads *threads);
48-
struct thread *threads__find(struct threads *threads, pid_t tid);
49-
struct thread *threads__findnew(struct threads *threads, pid_t pid, pid_t tid, bool *created);
50-
void threads__remove_all_threads(struct threads *threads);
51-
void threads__remove(struct threads *threads, struct thread *thread);
52-
int threads__for_each_thread(struct threads *threads,
53-
int (*fn)(struct thread *thread, void *data),
54-
void *data);
55-
5632
struct machine {
5733
struct rb_node rb_node;
5834
pid_t pid;

0 commit comments

Comments
 (0)