2013-09-11 21:56:44 +07:00
|
|
|
#include "comm.h"
|
|
|
|
#include "util.h"
|
2017-04-18 20:46:11 +07:00
|
|
|
#include <errno.h>
|
2013-09-11 21:56:44 +07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-04-20 05:06:30 +07:00
|
|
|
#include <string.h>
|
2017-02-21 22:34:57 +07:00
|
|
|
#include <linux/refcount.h>
|
2013-09-11 21:56:44 +07:00
|
|
|
|
|
|
|
struct comm_str {
|
|
|
|
char *str;
|
|
|
|
struct rb_node rb_node;
|
2017-02-21 22:34:57 +07:00
|
|
|
refcount_t refcnt;
|
2013-09-11 21:56:44 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Should perhaps be moved to struct machine */
|
|
|
|
static struct rb_root comm_str_root;
|
|
|
|
|
2015-05-20 05:07:42 +07:00
|
|
|
static struct comm_str *comm_str__get(struct comm_str *cs)
|
2013-09-11 21:56:44 +07:00
|
|
|
{
|
2015-05-20 05:07:42 +07:00
|
|
|
if (cs)
|
2017-02-21 22:34:57 +07:00
|
|
|
refcount_inc(&cs->refcnt);
|
2015-05-20 05:07:42 +07:00
|
|
|
return cs;
|
2013-09-11 21:56:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void comm_str__put(struct comm_str *cs)
|
|
|
|
{
|
2017-02-21 22:34:57 +07:00
|
|
|
if (cs && refcount_dec_and_test(&cs->refcnt)) {
|
2013-09-11 21:56:44 +07:00
|
|
|
rb_erase(&cs->rb_node, &comm_str_root);
|
2013-12-28 02:55:14 +07:00
|
|
|
zfree(&cs->str);
|
2013-09-11 21:56:44 +07:00
|
|
|
free(cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct comm_str *comm_str__alloc(const char *str)
|
|
|
|
{
|
|
|
|
struct comm_str *cs;
|
|
|
|
|
|
|
|
cs = zalloc(sizeof(*cs));
|
|
|
|
if (!cs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cs->str = strdup(str);
|
|
|
|
if (!cs->str) {
|
|
|
|
free(cs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-21 22:34:57 +07:00
|
|
|
refcount_set(&cs->refcnt, 1);
|
2015-05-20 05:07:42 +07:00
|
|
|
|
2013-09-11 21:56:44 +07:00
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct comm_str *comm_str__findnew(const char *str, struct rb_root *root)
|
|
|
|
{
|
|
|
|
struct rb_node **p = &root->rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
struct comm_str *iter, *new;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
while (*p != NULL) {
|
|
|
|
parent = *p;
|
|
|
|
iter = rb_entry(parent, struct comm_str, rb_node);
|
|
|
|
|
|
|
|
cmp = strcmp(str, iter->str);
|
|
|
|
if (!cmp)
|
2017-02-21 22:34:57 +07:00
|
|
|
return comm_str__get(iter);
|
2013-09-11 21:56:44 +07:00
|
|
|
|
|
|
|
if (cmp < 0)
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
else
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
new = comm_str__alloc(str);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rb_link_node(&new->rb_node, parent, p);
|
|
|
|
rb_insert_color(&new->rb_node, root);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2014-07-31 13:00:44 +07:00
|
|
|
struct comm *comm__new(const char *str, u64 timestamp, bool exec)
|
2013-09-11 21:56:44 +07:00
|
|
|
{
|
|
|
|
struct comm *comm = zalloc(sizeof(*comm));
|
|
|
|
|
|
|
|
if (!comm)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
comm->start = timestamp;
|
2014-07-31 13:00:44 +07:00
|
|
|
comm->exec = exec;
|
2013-09-11 21:56:44 +07:00
|
|
|
|
|
|
|
comm->comm_str = comm_str__findnew(str, &comm_str_root);
|
|
|
|
if (!comm->comm_str) {
|
|
|
|
free(comm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return comm;
|
|
|
|
}
|
|
|
|
|
2014-07-31 13:00:44 +07:00
|
|
|
int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
|
2013-09-13 14:28:57 +07:00
|
|
|
{
|
2014-01-14 22:37:14 +07:00
|
|
|
struct comm_str *new, *old = comm->comm_str;
|
2013-09-13 14:28:57 +07:00
|
|
|
|
2014-01-14 22:37:14 +07:00
|
|
|
new = comm_str__findnew(str, &comm_str_root);
|
|
|
|
if (!new)
|
|
|
|
return -ENOMEM;
|
2013-09-13 14:28:57 +07:00
|
|
|
|
|
|
|
comm_str__put(old);
|
2014-01-14 22:37:14 +07:00
|
|
|
comm->comm_str = new;
|
|
|
|
comm->start = timestamp;
|
2014-07-31 13:00:44 +07:00
|
|
|
if (exec)
|
|
|
|
comm->exec = true;
|
2014-01-14 22:37:14 +07:00
|
|
|
|
|
|
|
return 0;
|
2013-09-13 14:28:57 +07:00
|
|
|
}
|
|
|
|
|
2013-09-11 21:56:44 +07:00
|
|
|
void comm__free(struct comm *comm)
|
|
|
|
{
|
|
|
|
comm_str__put(comm->comm_str);
|
|
|
|
free(comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *comm__str(const struct comm *comm)
|
|
|
|
{
|
|
|
|
return comm->comm_str->str;
|
|
|
|
}
|