blob: 0ea73fe383f5f9ebc6b537aa299c6dadb02d36fb [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
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020034int thread__set_comm(struct thread *thread, const char *comm,
35 u64 timestamp __maybe_unused)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020036{
David S. Miller4385d582010-02-26 12:08:34 -030037 int err;
38
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030039 if (thread->comm)
40 free(thread->comm);
41 thread->comm = strdup(comm);
42 err = thread->comm == NULL ? -ENOMEM : 0;
David S. Miller4385d582010-02-26 12:08:34 -030043 if (!err) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030044 thread->comm_set = true;
David S. Miller4385d582010-02-26 12:08:34 -030045 }
46 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020047}
48
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020049const char *thread__comm_str(const struct thread *thread)
50{
51 return thread->comm;
52}
53
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030054int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020055{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030056 if (!thread->comm_len) {
57 if (!thread->comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020058 return 0;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030059 thread->comm_len = strlen(thread->comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020060 }
61
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030062 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020063}
64
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030065size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020066{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020067 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030068 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020069}
70
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030071void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030072{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030073 map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
74 map_groups__insert(&thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020075}
76
Frederic Weisbecker162f0be2013-09-11 16:18:24 +020077int thread__fork(struct thread *thread, struct thread *parent,
78 u64 timestamp __maybe_unused)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020079{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020080 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020081
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020082 if (parent->comm_set) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030083 if (thread->comm)
84 free(thread->comm);
85 thread->comm = strdup(parent->comm);
86 if (!thread->comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020087 return -ENOMEM;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030088 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020089 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020090
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020091 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030092 if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020093 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -060094
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030095 thread->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -060096
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020097 return 0;
98}