Commit c13c0a3f authored by Pedro Gonnet's avatar Pedro Gonnet
Browse files

keep a separate log buffer per thread id. this avoids having to protect...

keep a separate log buffer per thread id. this avoids having to protect against concurrent access to the logs.
parent b32c270b
......@@ -45,33 +45,28 @@
*/
void threadpool_log(struct threadpool *tp, int tid, size_t chunk_size,
ticks tic, ticks toc) {
/* Only one cell logs at a time. */
pthread_mutex_lock(&tp->log_mutex);
struct mapper_log *log = &tp->logs[tid];
/* Check if we need to re-allocate the log buffer. */
if (tp->log_count == tp->log_size) {
tp->log_size *= 2;
if (log->count == log->size) {
log->size *= 2;
struct mapper_log_entry *new_log;
if ((new_log = (struct mapper_log_entry *)malloc(
sizeof(struct mapper_log_entry) * tp->log_size)) == NULL)
sizeof(struct mapper_log_entry) * log->size)) == NULL)
error("Failed to re-allocate mapper log.");
memcpy(new_log, tp->log, sizeof(struct mapper_log_entry) * tp->log_count);
free(tp->log);
tp->log = new_log;
memcpy(new_log, log->log, sizeof(struct mapper_log_entry) * log->count);
free(log->log);
log->log = new_log;
}
/* Store the new entry. */
struct mapper_log_entry *entry = &tp->log[tp->log_count];
struct mapper_log_entry *entry = &log->log[log->count];
entry->tid = tid;
entry->chunk_size = chunk_size;
entry->tic = tic;
entry->toc = toc;
entry->map_function = tp->map_function;
tp->log_count++;
/* Release the logging mutex. */
pthread_mutex_unlock(&tp->log_mutex);
log->count++;
}
void threadpool_dump_log(struct threadpool *tp, const char *filename,
......@@ -96,10 +91,14 @@ void threadpool_dump_log(struct threadpool *tp, const char *filename,
fprintf(fd, "# {'num_threads': %i, 'cpufreq': %lli}\n", tp->num_threads,
clocks_get_cpufreq());
/* Loop over the per-tid logs and dump them. */
for (int k = 0; k < tp->num_threads; k++) {
struct mapper_log *log = &tp->logs[k];
/* Loop over the log entries and dump them. */
for (int i = 0; i < tp->log_count; i++) {
for (int i = 0; i < log->count; i++) {
struct mapper_log_entry *entry = &tp->log[i];
struct mapper_log_entry *entry = &log->log[i];
/* Look for the function pointer in the buffer. */
int nid = 0;
......@@ -122,7 +121,8 @@ void threadpool_dump_log(struct threadpool *tp, const char *filename,
}
/* Clear the log if requested. */
if (reset) tp->log_count = 0;
if (reset) log->count = 0;
}
/* Close the file. */
fclose(fd);
......@@ -214,13 +214,16 @@ void threadpool_init(struct threadpool *tp, int num_threads) {
tp->map_function = NULL;
#ifdef SWIFT_DEBUG_THREADPOOL
tp->log_size = threadpool_log_initial_size;
tp->log_count = 0;
if (pthread_mutex_init(&tp->log_mutex, NULL) != 0)
error("Failed to initialize log mutex.");
if ((tp->log = (struct mapper_log_entry *)malloc(
sizeof(struct mapper_log_entry) * tp->log_size)) == NULL)
if ((tp->logs = (struct mapper_log *)malloc(sizeof(struct mapper_log) *
num_threads)) == NULL)
error("Failed to allocate mapper logs.");
for (int k = 0; k < num_threads; k++) {
tp->logs[k].size = threadpool_log_initial_size;
tp->logs[k].count = 0;
if ((tp->logs[k].log = (struct mapper_log_entry *)malloc(
sizeof(struct mapper_log_entry) * tp->logs[k].size)) == NULL)
error("Failed to allocate mapper log.");
}
#endif
/* Allocate the threads. */
......@@ -300,7 +303,9 @@ void threadpool_map(struct threadpool *tp, threadpool_map_function map_function,
* @brief Re-sets the log for this #threadpool.
*/
#ifdef SWIFT_DEBUG_THREADPOOL
void threadpool_reset_log(struct threadpool *tp) { tp->log_count = 0; }
void threadpool_reset_log(struct threadpool *tp) {
for (int k = 0; k < tp->num_threads; k++) tp->logs[k].count = 0;
}
#endif
/**
......@@ -309,6 +314,9 @@ void threadpool_reset_log(struct threadpool *tp) { tp->log_count = 0; }
void threadpool_clean(struct threadpool *tp) {
free(tp->threads);
#ifdef SWIFT_DEBUG_THREADPOOL
free(tp->log);
for (int k = 0; k < tp->num_threads; k++) {
free(tp->logs[k].log);
}
free(tp->logs);
#endif
}
......@@ -52,6 +52,17 @@ struct mapper_log_entry {
ticks tic, toc;
};
struct mapper_log {
/* Log of threadpool mapper calls. */
struct mapper_log_entry *log;
/* Size of the allocated log. */
int size;
/* Number of entries in the log. */
int count;
};
/* Data of a threadpool. */
struct threadpool {
......@@ -75,17 +86,7 @@ struct threadpool {
volatile int num_threads_waiting, num_threads_running;
#ifdef SWIFT_DEBUG_THREADPOOL
/* Log of threadpool mapper calls. */
struct mapper_log_entry *log;
/* Size of the allocated log. */
int log_size;
/* Number of entries in the log. */
int log_count;
/* Mutex for log access/reallocation. */
pthread_mutex_t log_mutex;
struct mapper_log *logs;
#endif
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment