blob: 80d19a0860721b60dbcf57578261537ebd5f7af5 [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
Adrian Hunter99d725f2013-08-26 16:00:19 +030010struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020011{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030012 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020013
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030014 if (thread != NULL) {
15 map_groups__init(&thread->mg);
16 thread->pid_ = pid;
17 thread->tid = tid;
18 thread->ppid = -1;
19 thread->comm = malloc(32);
20 if (thread->comm)
21 snprintf(thread->comm, 32, ":%d", thread->tid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020022 }
23
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030024 return thread;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020025}
26
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030027void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030028{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030029 map_groups__exit(&thread->mg);
30 free(thread->comm);
31 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030032}
33
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030034int thread__set_comm(struct thread *thread, const char *comm)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020035{
David S. Miller4385d582010-02-26 12:08:34 -030036 int err;
37
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030038 if (thread->comm)
39 free(thread->comm);
40 thread->comm = strdup(comm);
41 err = thread->comm == NULL ? -ENOMEM : 0;
David S. Miller4385d582010-02-26 12:08:34 -030042 if (!err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030043 thread->comm_set = true;
David S. Miller4385d582010-02-26 12:08:34 -030044 }
45 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020046}
47
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030048int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020049{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030050 if (!thread->comm_len) {
51 if (!thread->comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020052 return 0;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030053 thread->comm_len = strlen(thread->comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020054 }
55
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030056 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020057}
58
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030059size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020060{
Adrian Hunter38051232013-07-04 16:20:31 +030061 return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030062 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020063}
64
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030065void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030066{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030067 map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
68 map_groups__insert(&thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020069}
70
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030071int thread__fork(struct thread *thread, struct thread *parent)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020072{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020073 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020074
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020075 if (parent->comm_set) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030076 if (thread->comm)
77 free(thread->comm);
78 thread->comm = strdup(parent->comm);
79 if (!thread->comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020080 return -ENOMEM;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030081 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020082 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020083
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020084 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030085 if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020086 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -060087
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030088 thread->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -060089
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020090 return 0;
91}