blob: 49eaf1d7d89d0554a146a272e923cd139e6cbac7 [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 Weisbecker1902efe2013-09-11 16:56:44 +02009#include "comm.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020010
Adrian Hunter99d725f2013-08-26 16:00:19 +030011struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020012{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020013 char *comm_str;
14 struct comm *comm;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030015 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020016
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030017 if (thread != NULL) {
18 map_groups__init(&thread->mg);
19 thread->pid_ = pid;
20 thread->tid = tid;
21 thread->ppid = -1;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020022 INIT_LIST_HEAD(&thread->comm_list);
23
24 comm_str = malloc(32);
25 if (!comm_str)
26 goto err_thread;
27
28 snprintf(comm_str, 32, ":%d", tid);
29 comm = comm__new(comm_str, 0);
30 free(comm_str);
31 if (!comm)
32 goto err_thread;
33
34 list_add(&comm->list, &thread->comm_list);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020035 }
36
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030037 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020038
39err_thread:
40 free(thread);
41 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020042}
43
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030044void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030045{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020046 struct comm *comm, *tmp;
47
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030048 map_groups__exit(&thread->mg);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020049 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
50 list_del(&comm->list);
51 comm__free(comm);
52 }
53
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030054 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030055}
56
Namhyung Kim4dfced32013-09-13 16:28:57 +090057struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020058{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020059 if (list_empty(&thread->comm_list))
60 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -030061
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020062 return list_first_entry(&thread->comm_list, struct comm, list);
63}
64
65/* CHECKME: time should always be 0 if event aren't ordered */
66int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
67{
68 struct comm *new, *curr = thread__comm(thread);
69
70 /* Override latest entry if it had no specific time coverage */
71 if (!curr->start) {
Namhyung Kim4dfced32013-09-13 16:28:57 +090072 comm__override(curr, str, timestamp);
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +010073 } else {
74 new = comm__new(str, timestamp);
75 if (!new)
76 return -ENOMEM;
77 list_add(&new->list, &thread->comm_list);
David S. Miller4385d582010-02-26 12:08:34 -030078 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020079
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020080 thread->comm_set = true;
81
82 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020083}
84
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020085const char *thread__comm_str(const struct thread *thread)
86{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020087 const struct comm *comm = thread__comm(thread);
88
89 if (!comm)
90 return NULL;
91
92 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020093}
94
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020095/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030096int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020097{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030098 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020099 const char *comm = thread__comm_str(thread);
100 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200101 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200102 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200103 }
104
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300105 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200106}
107
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300108size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200109{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200110 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300111 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200112}
113
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300114void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300115{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300116 map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
117 map_groups__insert(&thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200118}
119
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200120int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200121{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200122 int i, err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200123
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200124 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200125 const char *comm = thread__comm_str(parent);
126 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200127 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200128 err = thread__set_comm(thread, comm, timestamp);
129 if (!err)
130 return err;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300131 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200132 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200133
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200134 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300135 if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200136 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -0600137
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300138 thread->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -0600139
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200140 return 0;
141}