xref: /openbmc/linux/tools/perf/util/thread.c (revision 05bcf503)
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
9 
10 static struct thread *thread__new(pid_t pid)
11 {
12 	struct thread *self = zalloc(sizeof(*self));
13 
14 	if (self != NULL) {
15 		map_groups__init(&self->mg);
16 		self->pid = pid;
17 		self->comm = malloc(32);
18 		if (self->comm)
19 			snprintf(self->comm, 32, ":%d", self->pid);
20 	}
21 
22 	return self;
23 }
24 
25 void thread__delete(struct thread *self)
26 {
27 	map_groups__exit(&self->mg);
28 	free(self->comm);
29 	free(self);
30 }
31 
32 int thread__set_comm(struct thread *self, const char *comm)
33 {
34 	int err;
35 
36 	if (self->comm)
37 		free(self->comm);
38 	self->comm = strdup(comm);
39 	err = self->comm == NULL ? -ENOMEM : 0;
40 	if (!err) {
41 		self->comm_set = true;
42 	}
43 	return err;
44 }
45 
46 int thread__comm_len(struct thread *self)
47 {
48 	if (!self->comm_len) {
49 		if (!self->comm)
50 			return 0;
51 		self->comm_len = strlen(self->comm);
52 	}
53 
54 	return self->comm_len;
55 }
56 
57 static size_t thread__fprintf(struct thread *self, FILE *fp)
58 {
59 	return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
60 	       map_groups__fprintf(&self->mg, verbose, fp);
61 }
62 
63 struct thread *machine__findnew_thread(struct machine *self, pid_t pid)
64 {
65 	struct rb_node **p = &self->threads.rb_node;
66 	struct rb_node *parent = NULL;
67 	struct thread *th;
68 
69 	/*
70 	 * Font-end cache - PID lookups come in blocks,
71 	 * so most of the time we dont have to look up
72 	 * the full rbtree:
73 	 */
74 	if (self->last_match && self->last_match->pid == pid)
75 		return self->last_match;
76 
77 	while (*p != NULL) {
78 		parent = *p;
79 		th = rb_entry(parent, struct thread, rb_node);
80 
81 		if (th->pid == pid) {
82 			self->last_match = th;
83 			return th;
84 		}
85 
86 		if (pid < th->pid)
87 			p = &(*p)->rb_left;
88 		else
89 			p = &(*p)->rb_right;
90 	}
91 
92 	th = thread__new(pid);
93 	if (th != NULL) {
94 		rb_link_node(&th->rb_node, parent, p);
95 		rb_insert_color(&th->rb_node, &self->threads);
96 		self->last_match = th;
97 	}
98 
99 	return th;
100 }
101 
102 void thread__insert_map(struct thread *self, struct map *map)
103 {
104 	map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
105 	map_groups__insert(&self->mg, map);
106 }
107 
108 int thread__fork(struct thread *self, struct thread *parent)
109 {
110 	int i;
111 
112 	if (parent->comm_set) {
113 		if (self->comm)
114 			free(self->comm);
115 		self->comm = strdup(parent->comm);
116 		if (!self->comm)
117 			return -ENOMEM;
118 		self->comm_set = true;
119 	}
120 
121 	for (i = 0; i < MAP__NR_TYPES; ++i)
122 		if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
123 			return -ENOMEM;
124 	return 0;
125 }
126 
127 size_t machine__fprintf(struct machine *machine, FILE *fp)
128 {
129 	size_t ret = 0;
130 	struct rb_node *nd;
131 
132 	for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
133 		struct thread *pos = rb_entry(nd, struct thread, rb_node);
134 
135 		ret += thread__fprintf(pos, fp);
136 	}
137 
138 	return ret;
139 }
140