Load thread tid from metadata

This commit is contained in:
Rodrigo Arias 2024-09-10 09:58:43 +02:00
parent 4ec966cb67
commit aafaf6e954
5 changed files with 31 additions and 101 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC) /* Copyright (c) 2021-2024 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */ * SPDX-License-Identifier: GPL-3.0-or-later */
#include "metadata.h" #include "metadata.h"
@ -135,25 +135,3 @@ metadata_load_proc(const char *path, struct loom *loom, struct proc *proc)
return 0; return 0;
} }
int
metadata_load_thread(const char *path, struct thread *thread)
{
JSON_Object *meta = load_json(path);
if (meta == NULL) {
err("cannot load thread metadata from file %s", path);
return -1;
}
if (check_version(meta) != 0) {
err("version check failed");
return -1;
}
if (thread_load_metadata(thread, meta) != 0) {
err("cannot load thread attributes");
return -1;
}
return 0;
}

View File

@ -1,4 +1,4 @@
/* Copyright (c) 2021-2023 Barcelona Supercomputing Center (BSC) /* Copyright (c) 2021-2024 Barcelona Supercomputing Center (BSC)
* SPDX-License-Identifier: GPL-3.0-or-later */ * SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef METADATA_H #ifndef METADATA_H
@ -10,6 +10,5 @@ struct proc;
struct thread; struct thread;
USE_RET int metadata_load_proc(const char *path, struct loom *loom, struct proc *proc); USE_RET int metadata_load_proc(const char *path, struct loom *loom, struct proc *proc);
USE_RET int metadata_load_thread(const char *path, struct thread *thread);
#endif /* METADATA_H */ #endif /* METADATA_H */

View File

@ -24,11 +24,11 @@
struct bay; struct bay;
static struct thread * static struct thread *
create_thread(struct proc *proc, const char *tracedir, const char *relpath) create_thread(struct proc *proc, struct stream *s)
{ {
int tid; int tid;
if (thread_relpath_get_tid(relpath, &tid) != 0) { if ((tid = thread_stream_get_tid(s)) < 0) {
err("cannot get thread tid from %s", relpath); err("cannot get thread tid from stream: %s", s->relpath);
return NULL; return NULL;
} }
@ -45,21 +45,13 @@ create_thread(struct proc *proc, const char *tracedir, const char *relpath)
return NULL; return NULL;
} }
if (thread_init_begin(thread, relpath) != 0) { if (thread_init_begin(thread, tid) != 0) {
err("cannot init thread"); err("thread_init_begin failed: %s", s->relpath);
return NULL; return NULL;
} }
/* Build metadata path */ if (thread_load_metadata(thread, s) != 0) {
char mpath[PATH_MAX]; err("thread_load_metadata failed: %s", s->relpath);
if (snprintf(mpath, PATH_MAX, "%s/%s/thread.%d.json",
tracedir, proc->id, tid) >= PATH_MAX) {
err("path too long");
return NULL;
}
if (metadata_load_thread(mpath, thread) != 0) {
err("cannot load metadata from %s", mpath);
return NULL; return NULL;
} }
@ -276,7 +268,7 @@ create_system(struct system *sys, struct trace *trace)
return -1; return -1;
} }
struct thread *thread = create_thread(proc, dir, s->relpath); struct thread *thread = create_thread(proc, s);
if (thread == NULL) { if (thread == NULL) {
err("create_thread failed"); err("create_thread failed");
return -1; return -1;

View File

@ -15,6 +15,7 @@
#include "pv/prv.h" #include "pv/prv.h"
#include "pv/pvt.h" #include "pv/pvt.h"
#include "recorder.h" #include "recorder.h"
#include "stream.h"
#include "value.h" #include "value.h"
struct proc; struct proc;
@ -59,75 +60,37 @@ static const struct pcf_value_label (*pcf_labels[TH_CHAN_MAX])[] = {
[TH_CHAN_STATE] = &state_name, [TH_CHAN_STATE] = &state_name,
}; };
static int int
get_tid(const char *id, int *tid) thread_stream_get_tid(struct stream *s)
{ {
/* The id must be like "loom.host01.123/proc.345/thread.567" */ JSON_Object *meta = stream_metadata(s);
if (path_count(id, '/') != 2) {
err("proc id can only contain two '/': %s", id); double tid = json_object_dotget_number(meta, "ovni.tid");
/* Zero is used for errors, so forbidden for tid too */
if (tid == 0) {
err("cannot get attribute ovni.tid for stream: %s",
s->relpath);
return -1; return -1;
} }
/* Get the thread.567 part */ return (int) tid;
const char *thname;
if (path_strip(id, 2, &thname) != 0) {
err("cannot get thread name");
return -1;
}
/* Ensure the prefix is ok */
const char prefix[] = "thread.";
if (!path_has_prefix(thname, prefix)) {
err("thread name must start with '%s': %s", prefix, thname);
return -1;
}
/* Get the 567 part */
const char *tidstr;
if (path_next(thname, '.', &tidstr) != 0) {
err("cannot find thread dot in '%s'", id);
return -1;
}
char *endptr;
errno = 0;
*tid = (int) strtol(tidstr, &endptr, 10);
if (errno != 0) {
err("strtol failed for '%s':", tidstr);
return -1;
}
if (endptr == tidstr) {
err("no digits in tid string '%s'", tidstr);
return -1;
}
return 0;
} }
int int
thread_relpath_get_tid(const char *relpath, int *tid) thread_init_begin(struct thread *thread, int tid)
{
return get_tid(relpath, tid);
}
int
thread_init_begin(struct thread *thread, const char *relpath)
{ {
memset(thread, 0, sizeof(struct thread)); memset(thread, 0, sizeof(struct thread));
thread->state = TH_ST_UNKNOWN; thread->state = TH_ST_UNKNOWN;
thread->gindex = -1; thread->gindex = -1;
thread->tid = tid;
if (snprintf(thread->id, PATH_MAX, "%s", relpath) >= PATH_MAX) { if (snprintf(thread->id, PATH_MAX, "thread.%d", tid) >= PATH_MAX) {
err("relpath too long"); err("relpath too long");
return -1; return -1;
} }
if (get_tid(thread->id, &thread->tid) != 0) {
err("cannot parse thread tid");
return -1;
}
return 0; return 0;
} }
@ -445,12 +408,9 @@ thread_migrate_cpu(struct thread *th, struct cpu *cpu)
} }
int int
thread_load_metadata(struct thread *thread, JSON_Object *meta) thread_load_metadata(struct thread *thread, struct stream *s)
{ {
if (meta == NULL) { JSON_Object *meta = stream_metadata(s);
err("metadata is null");
return -1;
}
if (thread->meta != NULL) { if (thread->meta != NULL) {
err("thread %s already loaded metadata", thread->id); err("thread %s already loaded metadata", thread->id);

View File

@ -20,6 +20,7 @@ struct pcf;
struct proc; struct proc;
struct recorder; struct recorder;
struct value; struct value;
struct stream;
/* Emulated thread runtime status */ /* Emulated thread runtime status */
enum thread_state { enum thread_state {
@ -82,10 +83,10 @@ struct thread {
UT_hash_handle hh; /* threads in the process */ UT_hash_handle hh; /* threads in the process */
}; };
USE_RET int thread_relpath_get_tid(const char *relpath, int *tid); USE_RET int thread_stream_get_tid(struct stream *s);
USE_RET int thread_init_begin(struct thread *thread, const char *relpath); USE_RET int thread_init_begin(struct thread *thread, int tid);
USE_RET int thread_init_end(struct thread *thread); USE_RET int thread_init_end(struct thread *thread);
USE_RET int thread_load_metadata(struct thread *thread, JSON_Object *meta); USE_RET int thread_load_metadata(struct thread *thread, struct stream *s);
USE_RET int thread_set_state(struct thread *th, enum thread_state state); USE_RET int thread_set_state(struct thread *th, enum thread_state state);
USE_RET int thread_set_cpu(struct thread *th, struct cpu *cpu); USE_RET int thread_set_cpu(struct thread *th, struct cpu *cpu);
USE_RET int thread_unset_cpu(struct thread *th); USE_RET int thread_unset_cpu(struct thread *th);