2009-08-14 12:21:53 +02:00
|
|
|
#include "../perf.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2009-12-13 19:50:28 -02:00
|
|
|
#include "session.h"
|
2009-08-14 12:21:53 +02:00
|
|
|
#include "thread.h"
|
|
|
|
|
#include "util.h"
|
2009-08-18 17:04:03 +02:00
|
|
|
#include "debug.h"
|
2009-08-14 12:21:53 +02:00
|
|
|
|
2012-10-06 15:43:20 -03:00
|
|
|
struct thread *thread__new(pid_t pid)
|
2009-08-14 12:21:53 +02:00
|
|
|
{
|
2009-11-24 12:05:16 -02:00
|
|
|
struct thread *self = zalloc(sizeof(*self));
|
2009-08-14 12:21:53 +02:00
|
|
|
|
|
|
|
|
if (self != NULL) {
|
2009-12-11 14:50:36 -02:00
|
|
|
map_groups__init(&self->mg);
|
|
|
|
|
self->pid = pid;
|
2009-10-08 21:04:17 +02:00
|
|
|
self->comm = malloc(32);
|
|
|
|
|
if (self->comm)
|
|
|
|
|
snprintf(self->comm, 32, ":%d", self->pid);
|
2009-08-14 12:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-30 18:28:42 -03:00
|
|
|
void thread__delete(struct thread *self)
|
|
|
|
|
{
|
|
|
|
|
map_groups__exit(&self->mg);
|
|
|
|
|
free(self->comm);
|
|
|
|
|
free(self);
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-14 12:21:53 +02:00
|
|
|
int thread__set_comm(struct thread *self, const char *comm)
|
|
|
|
|
{
|
2010-02-26 12:08:34 -03:00
|
|
|
int err;
|
|
|
|
|
|
2009-08-14 12:21:53 +02:00
|
|
|
if (self->comm)
|
|
|
|
|
free(self->comm);
|
|
|
|
|
self->comm = strdup(comm);
|
2010-02-26 12:08:34 -03:00
|
|
|
err = self->comm == NULL ? -ENOMEM : 0;
|
|
|
|
|
if (!err) {
|
|
|
|
|
self->comm_set = true;
|
|
|
|
|
}
|
|
|
|
|
return err;
|
2009-08-14 12:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
2009-10-22 23:23:23 +02:00
|
|
|
int thread__comm_len(struct thread *self)
|
|
|
|
|
{
|
|
|
|
|
if (!self->comm_len) {
|
|
|
|
|
if (!self->comm)
|
|
|
|
|
return 0;
|
|
|
|
|
self->comm_len = strlen(self->comm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self->comm_len;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-27 16:29:20 -02:00
|
|
|
static size_t thread__fprintf(struct thread *self, FILE *fp)
|
|
|
|
|
{
|
2009-12-11 14:50:36 -02:00
|
|
|
return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
|
2010-03-26 12:11:06 -03:00
|
|
|
map_groups__fprintf(&self->mg, verbose, fp);
|
2009-08-14 12:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-28 14:48:46 -03:00
|
|
|
void thread__insert_map(struct thread *self, struct map *map)
|
|
|
|
|
{
|
2010-03-26 12:11:06 -03:00
|
|
|
map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
|
2009-12-11 14:50:36 -02:00
|
|
|
map_groups__insert(&self->mg, map);
|
2009-11-27 16:29:20 -02:00
|
|
|
}
|
|
|
|
|
|
2009-08-14 12:21:53 +02:00
|
|
|
int thread__fork(struct thread *self, struct thread *parent)
|
|
|
|
|
{
|
2009-11-27 16:29:20 -02:00
|
|
|
int i;
|
2009-08-14 12:21:53 +02:00
|
|
|
|
2010-02-19 23:02:07 -02:00
|
|
|
if (parent->comm_set) {
|
|
|
|
|
if (self->comm)
|
|
|
|
|
free(self->comm);
|
|
|
|
|
self->comm = strdup(parent->comm);
|
|
|
|
|
if (!self->comm)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
self->comm_set = true;
|
|
|
|
|
}
|
2009-08-14 12:21:53 +02:00
|
|
|
|
2009-11-27 16:29:20 -02:00
|
|
|
for (i = 0; i < MAP__NR_TYPES; ++i)
|
2009-12-11 14:50:36 -02:00
|
|
|
if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
|
2009-08-14 12:21:53 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-09 13:24:25 -02:00
|
|
|
size_t machine__fprintf(struct machine *machine, FILE *fp)
|
2009-08-14 12:21:53 +02:00
|
|
|
{
|
|
|
|
|
size_t ret = 0;
|
|
|
|
|
struct rb_node *nd;
|
|
|
|
|
|
2011-11-09 13:24:25 -02:00
|
|
|
for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
|
2009-08-14 12:21:53 +02:00
|
|
|
struct thread *pos = rb_entry(nd, struct thread, rb_node);
|
|
|
|
|
|
|
|
|
|
ret += thread__fprintf(pos, fp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|