]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/util/thread.c
0ea73fe383f5f9ebc6b537aa299c6dadb02d36fb
[mirror_ubuntu-artful-kernel.git] / tools / perf / util / thread.c
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
9
10 struct thread *thread__new(pid_t pid, pid_t tid)
11 {
12 struct thread *thread = zalloc(sizeof(*thread));
13
14 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);
22 }
23
24 return thread;
25 }
26
27 void thread__delete(struct thread *thread)
28 {
29 map_groups__exit(&thread->mg);
30 free(thread->comm);
31 free(thread);
32 }
33
34 int thread__set_comm(struct thread *thread, const char *comm,
35 u64 timestamp __maybe_unused)
36 {
37 int err;
38
39 if (thread->comm)
40 free(thread->comm);
41 thread->comm = strdup(comm);
42 err = thread->comm == NULL ? -ENOMEM : 0;
43 if (!err) {
44 thread->comm_set = true;
45 }
46 return err;
47 }
48
49 const char *thread__comm_str(const struct thread *thread)
50 {
51 return thread->comm;
52 }
53
54 int thread__comm_len(struct thread *thread)
55 {
56 if (!thread->comm_len) {
57 if (!thread->comm)
58 return 0;
59 thread->comm_len = strlen(thread->comm);
60 }
61
62 return thread->comm_len;
63 }
64
65 size_t thread__fprintf(struct thread *thread, FILE *fp)
66 {
67 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
68 map_groups__fprintf(&thread->mg, verbose, fp);
69 }
70
71 void thread__insert_map(struct thread *thread, struct map *map)
72 {
73 map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
74 map_groups__insert(&thread->mg, map);
75 }
76
77 int thread__fork(struct thread *thread, struct thread *parent,
78 u64 timestamp __maybe_unused)
79 {
80 int i;
81
82 if (parent->comm_set) {
83 if (thread->comm)
84 free(thread->comm);
85 thread->comm = strdup(parent->comm);
86 if (!thread->comm)
87 return -ENOMEM;
88 thread->comm_set = true;
89 }
90
91 for (i = 0; i < MAP__NR_TYPES; ++i)
92 if (map_groups__clone(&thread->mg, &parent->mg, i) < 0)
93 return -ENOMEM;
94
95 thread->ppid = parent->tid;
96
97 return 0;
98 }