blob: 1f7ecd47f49942809d19b0be3f65d9fb1f904ebc [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02005#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02006#include "thread.h"
7#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02008#include "debug.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02009
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030010int find_all_tid(int pid, pid_t ** all_tid)
11{
12 char name[256];
13 int items;
14 struct dirent **namelist = NULL;
15 int ret = 0;
16 int i;
17
18 sprintf(name, "/proc/%d/task", pid);
19 items = scandir(name, &namelist, NULL, NULL);
20 if (items <= 0)
21 return -ENOENT;
22 *all_tid = malloc(sizeof(pid_t) * items);
23 if (!*all_tid) {
24 ret = -ENOMEM;
25 goto failure;
26 }
27
28 for (i = 0; i < items; i++)
29 (*all_tid)[i] = atoi(namelist[i]->d_name);
30
31 ret = items;
32
33failure:
34 for (i=0; i<items; i++)
35 free(namelist[i]);
36 free(namelist);
37
38 return ret;
39}
40
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020041static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020042{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020043 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020044
45 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020046 map_groups__init(&self->mg);
47 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020048 self->comm = malloc(32);
49 if (self->comm)
50 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020051 }
52
53 return self;
54}
55
56int thread__set_comm(struct thread *self, const char *comm)
57{
David S. Miller4385d582010-02-26 12:08:34 -030058 int err;
59
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020060 if (self->comm)
61 free(self->comm);
62 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030063 err = self->comm == NULL ? -ENOMEM : 0;
64 if (!err) {
65 self->comm_set = true;
66 map_groups__flush(&self->mg);
67 }
68 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020069}
70
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020071int thread__comm_len(struct thread *self)
72{
73 if (!self->comm_len) {
74 if (!self->comm)
75 return 0;
76 self->comm_len = strlen(self->comm);
77 }
78
79 return self->comm_len;
80}
81
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020082static size_t thread__fprintf(struct thread *self, FILE *fp)
83{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020084 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030085 map_groups__fprintf(&self->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020086}
87
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020088struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020089{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020090 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020091 struct rb_node *parent = NULL;
92 struct thread *th;
93
94 /*
95 * Font-end cache - PID lookups come in blocks,
96 * so most of the time we dont have to look up
97 * the full rbtree:
98 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020099 if (self->last_match && self->last_match->pid == pid)
100 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200101
102 while (*p != NULL) {
103 parent = *p;
104 th = rb_entry(parent, struct thread, rb_node);
105
106 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200107 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200108 return th;
109 }
110
111 if (pid < th->pid)
112 p = &(*p)->rb_left;
113 else
114 p = &(*p)->rb_right;
115 }
116
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200117 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200118 if (th != NULL) {
119 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200120 rb_insert_color(&th->rb_node, &self->threads);
121 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200122 }
123
124 return th;
125}
126
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300127void thread__insert_map(struct thread *self, struct map *map)
128{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300129 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200130 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200131}
132
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200133int thread__fork(struct thread *self, struct thread *parent)
134{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200135 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200136
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200137 if (parent->comm_set) {
138 if (self->comm)
139 free(self->comm);
140 self->comm = strdup(parent->comm);
141 if (!self->comm)
142 return -ENOMEM;
143 self->comm_set = true;
144 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200145
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200146 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200147 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200148 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200149 return 0;
150}
151
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200152size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200153{
154 size_t ret = 0;
155 struct rb_node *nd;
156
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200157 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200158 struct thread *pos = rb_entry(nd, struct thread, rb_node);
159
160 ret += thread__fprintf(pos, fp);
161 }
162
163 return ret;
164}