Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
10struct thread *thread__new(pid_t pid)
11{
12 struct thread *self = zalloc(sizeof(*self));
13
14 if (self != NULL) {
15 map_groups__init(&self->mg);
16 self->pid = pid;
17 self->comm = malloc(32);
18 if (self->comm)
19 snprintf(self->comm, 32, ":%d", self->pid);
20 }
21
22 return self;
23}
24
25void thread__delete(struct thread *self)
26{
27 map_groups__exit(&self->mg);
28 free(self->comm);
29 free(self);
30}
31
32int thread__set_comm(struct thread *self, const char *comm)
33{
34 int err;
35
36 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
39 err = self->comm == NULL ? -ENOMEM : 0;
40 if (!err) {
41 self->comm_set = true;
42 }
43 return err;
44}
45
46int thread__comm_len(struct thread *self)
47{
48 if (!self->comm_len) {
49 if (!self->comm)
50 return 0;
51 self->comm_len = strlen(self->comm);
52 }
53
54 return self->comm_len;
55}
56
57static size_t thread__fprintf(struct thread *self, FILE *fp)
58{
59 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
60 map_groups__fprintf(&self->mg, verbose, fp);
61}
62
63void thread__insert_map(struct thread *self, struct map *map)
64{
65 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
66 map_groups__insert(&self->mg, map);
67}
68
69int thread__fork(struct thread *self, struct thread *parent)
70{
71 int i;
72
73 if (parent->comm_set) {
74 if (self->comm)
75 free(self->comm);
76 self->comm = strdup(parent->comm);
77 if (!self->comm)
78 return -ENOMEM;
79 self->comm_set = true;
80 }
81
82 for (i = 0; i < MAP__NR_TYPES; ++i)
83 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
84 return -ENOMEM;
85 return 0;
86}
87
88size_t machine__fprintf(struct machine *machine, FILE *fp)
89{
90 size_t ret = 0;
91 struct rb_node *nd;
92
93 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
94 struct thread *pos = rb_entry(nd, struct thread, rb_node);
95
96 ret += thread__fprintf(pos, fp);
97 }
98
99 return ret;
100}