blob: 56760079565bfa02a8f7e6aa7a7999f99e7d545a [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
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020048const char *thread__comm_str(const struct thread *thread)
49{
50 return thread->comm;
51}
52
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030053int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020054{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030055 if (!thread->comm_len) {
56 if (!thread->comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020057 return 0;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030058 thread->comm_len = strlen(thread->comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020059 }
60
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030061 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020062}
63
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030064size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020065{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020066 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030067 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020068}
69
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030070void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030071{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030072 map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
73 map_groups__insert(&thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020074}
75
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030076int thread__fork(struct thread *thread, struct thread *parent)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020077{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020078 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020079
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020080 if (parent->comm_set) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030081 if (thread->comm)
82 free(thread->comm);
83 thread->comm = strdup(parent->comm);
84 if (!thread->comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020085 return -ENOMEM;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030086 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020087 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020088
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020089 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030090 if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020091 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -060092
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030093 thread->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -060094
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020095 return 0;
96}