perf hists: Resort hist entries using group members for output
[deliverable/linux.git] / tools / perf / util / thread.c
CommitLineData
6baa0a5a
FW
1#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
b3165f41 5#include "session.h"
6baa0a5a
FW
6#include "thread.h"
7#include "util.h"
6e086437 8#include "debug.h"
6baa0a5a 9
9d2f8e22 10struct thread *thread__new(pid_t pid)
6baa0a5a 11{
36479484 12 struct thread *self = zalloc(sizeof(*self));
6baa0a5a
FW
13
14 if (self != NULL) {
9958e1f0
ACM
15 map_groups__init(&self->mg);
16 self->pid = pid;
97ea1a7f
FW
17 self->comm = malloc(32);
18 if (self->comm)
19 snprintf(self->comm, 32, ":%d", self->pid);
6baa0a5a
FW
20 }
21
22 return self;
23}
24
591765fd
ACM
25void thread__delete(struct thread *self)
26{
27 map_groups__exit(&self->mg);
28 free(self->comm);
29 free(self);
30}
31
6baa0a5a
FW
32int thread__set_comm(struct thread *self, const char *comm)
33{
4385d580
DM
34 int err;
35
6baa0a5a
FW
36 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
4385d580
DM
39 err = self->comm == NULL ? -ENOMEM : 0;
40 if (!err) {
41 self->comm_set = true;
4385d580
DM
42 }
43 return err;
6baa0a5a
FW
44}
45
a4fb581b
FW
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
3f067dca 57size_t thread__fprintf(struct thread *thread, FILE *fp)
9958e1f0 58{
3f067dca
ACM
59 return fprintf(fp, "Thread %d %s\n", thread->pid, thread->comm) +
60 map_groups__fprintf(&thread->mg, verbose, fp);
6baa0a5a
FW
61}
62
1b46cddf
ACM
63void thread__insert_map(struct thread *self, struct map *map)
64{
c6e718ff 65 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
9958e1f0 66 map_groups__insert(&self->mg, map);
6baa0a5a
FW
67}
68
95011c60
ACM
69int thread__fork(struct thread *self, struct thread *parent)
70{
71 int i;
6baa0a5a 72
faa5c5c3
ACM
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 }
6baa0a5a 81
95011c60 82 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 83 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
6baa0a5a 84 return -ENOMEM;
6baa0a5a
FW
85 return 0;
86}
This page took 0.356717 seconds and 5 git commands to generate.