ARGOBOTS  1.1
log.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include "abti.h"
9 
10 #ifdef ABT_CONFIG_USE_DEBUG_LOG
11 
12 void ABTI_log_debug(FILE *fh, const char *format, ...)
13 {
15  if (!p_global || p_global->use_logging == ABT_FALSE)
16  return;
18 
19  const char *prefix_fmt = NULL, *prefix = NULL;
20  char static_buffer[256], *newfmt;
21  uint64_t tid;
22  int rank;
23  size_t newfmt_len;
24 
25  ABTI_xstream *p_local_xstream = ABTI_local_get_xstream_or_null(p_local);
26  if (p_local_xstream) {
27  ABTI_ythread *p_ythread =
28  ABTI_thread_get_ythread(p_local_xstream->p_thread);
29  if (p_ythread == NULL) {
30  if (p_local_xstream->type != ABTI_XSTREAM_TYPE_PRIMARY) {
31  prefix_fmt = "<U%" PRIu64 ":E%d> %s";
32  rank = p_local_xstream->rank;
33  tid = 0;
34  } else {
35  prefix = "<U0:E0> ";
36  prefix_fmt = "%s%s";
37  }
38  } else {
39  rank = p_local_xstream->rank;
40  prefix_fmt = "<U%" PRIu64 ":E%d> %s";
41  tid = ABTI_thread_get_id(&p_ythread->thread);
42  }
43  } else {
44  prefix = "<EXT> ";
45  prefix_fmt = "%s%s";
46  }
47 
48  if (prefix == NULL) {
49  /* Both tid and rank are less than 42 characters in total. */
50  const int len_tid_rank = 50;
51  newfmt_len = 6 + len_tid_rank + strlen(format);
52  if (sizeof(static_buffer) >= newfmt_len + 1) {
53  newfmt = static_buffer;
54  } else {
55  int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
56  if (abt_errno != ABT_SUCCESS)
57  return;
58  }
59  sprintf(newfmt, prefix_fmt, tid, rank, format);
60  } else {
61  newfmt_len = strlen(prefix) + strlen(format);
62  if (sizeof(static_buffer) >= newfmt_len + 1) {
63  newfmt = static_buffer;
64  } else {
65  int abt_errno = ABTU_malloc(newfmt_len + 1, (void **)&newfmt);
66  if (abt_errno != ABT_SUCCESS)
67  return;
68  }
69  sprintf(newfmt, prefix_fmt, prefix, format);
70  }
71 
72 #ifndef ABT_CONFIG_USE_DEBUG_LOG_DISCARD
73  va_list list;
74  va_start(list, format);
75  vfprintf(fh, newfmt, list);
76  va_end(list);
77  fflush(fh);
78 #else
79  /* Discard the log message. This option is used to check if the logging
80  * function works correct (i.e., without any SEGV) but a tester does not
81  * need an actual log since the output can be extremely large. */
82 #endif
83  if (newfmt != static_buffer) {
84  ABTU_free(newfmt);
85  }
86 }
87 
88 void ABTI_log_pool_push(ABTI_pool *p_pool, ABT_unit unit)
89 {
91  if (!p_global || p_global->use_logging == ABT_FALSE)
92  return;
93  if (unit == ABT_UNIT_NULL)
94  return;
95 
96  ABTI_thread *p_thread = ABTI_unit_get_thread(p_global, unit);
97  char unit_type = (p_thread->type & ABTI_THREAD_TYPE_YIELDABLE) ? 'U' : 'T';
98  if (p_thread->p_last_xstream) {
99  LOG_DEBUG("[%c%" PRIu64 ":E%d] pushed to P%" PRIu64 "\n", unit_type,
100  ABTI_thread_get_id(p_thread), p_thread->p_last_xstream->rank,
101  p_pool->id);
102  } else {
103  LOG_DEBUG("[%c%" PRIu64 "] pushed to P%" PRIu64 "\n", unit_type,
104  ABTI_thread_get_id(p_thread), p_pool->id);
105  }
106 }
107 
108 void ABTI_log_pool_remove(ABTI_pool *p_pool, ABT_unit unit)
109 {
111  if (!p_global || p_global->use_logging == ABT_FALSE)
112  return;
113  if (unit == ABT_UNIT_NULL)
114  return;
115 
116  ABTI_thread *p_thread = ABTI_unit_get_thread(p_global, unit);
117  char unit_type = (p_thread->type & ABTI_THREAD_TYPE_YIELDABLE) ? 'U' : 'T';
118  if (p_thread->p_last_xstream) {
119  LOG_DEBUG("[%c%" PRIu64 ":E%d] removed from P%" PRIu64 "\n", unit_type,
120  ABTI_thread_get_id(p_thread), p_thread->p_last_xstream->rank,
121  p_pool->id);
122  } else {
123  LOG_DEBUG("[%c%" PRIu64 "] removed from P%" PRIu64 "\n", unit_type,
124  ABTI_thread_get_id(p_thread), p_pool->id);
125  }
126 }
127 
128 void ABTI_log_pool_pop(ABTI_pool *p_pool, ABT_unit unit)
129 {
131  if (!p_global || p_global->use_logging == ABT_FALSE)
132  return;
133  if (unit == ABT_UNIT_NULL)
134  return;
135 
136  ABTI_thread *p_thread = ABTI_unit_get_thread(p_global, unit);
137  char unit_type = (p_thread->type & ABTI_THREAD_TYPE_YIELDABLE) ? 'U' : 'T';
138  if (p_thread->p_last_xstream) {
139  LOG_DEBUG("[%c%" PRIu64 ":E%d] popped from P%" PRIu64 "\n", unit_type,
140  ABTI_thread_get_id(p_thread), p_thread->p_last_xstream->rank,
141  p_pool->id);
142  } else {
143  LOG_DEBUG("[%c%" PRIu64 "] popped from P%" PRIu64 "\n", unit_type,
144  ABTI_thread_get_id(p_thread), p_pool->id);
145  }
146 }
147 
148 #endif /* ABT_CONFIG_USE_DEBUG_LOG */
ABTI_XSTREAM_TYPE_PRIMARY
@ ABTI_XSTREAM_TYPE_PRIMARY
Definition: abti.h:71
ABTI_thread::type
ABTI_thread_type type
Definition: abti.h:375
ABTI_xstream::rank
int rank
Definition: abti.h:269
ABTI_xstream::type
ABTI_xstream_type type
Definition: abti.h:270
ABTI_THREAD_TYPE_YIELDABLE
#define ABTI_THREAD_TYPE_YIELDABLE
Definition: abti.h:86
ABTI_thread_get_ythread
static ABTI_ythread * ABTI_thread_get_ythread(ABTI_thread *p_thread)
Definition: abti_thread.h:52
ABTI_thread_get_id
ABT_unit_id ABTI_thread_get_id(ABTI_thread *p_thread)
Definition: thread.c:2561
ABTI_thread
Definition: abti.h:371
ABTI_xstream
Definition: abti.h:264
ABTI_pool
Definition: abti.h:327
abti.h
ABTI_global::use_logging
ABT_bool use_logging
Definition: abti.h:206
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:262
LOG_DEBUG
#define LOG_DEBUG(fmt,...)
Definition: abti_log.h:26
ABT_unit
struct ABT_unit_opaque * ABT_unit
Work unit handle type for scheduling.
Definition: abt.h:869
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTI_local_get_local_uninlined
static ABTI_local * ABTI_local_get_local_uninlined(void)
Definition: abti_local.h:51
ABTI_local_get_xstream_or_null
static ABTI_xstream * ABTI_local_get_xstream_or_null(ABTI_local *p_local)
Definition: abti_local.h:77
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:750
ABTI_ythread
Definition: abti.h:406
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:217
ABTI_unit_get_thread
static ABTI_thread * ABTI_unit_get_thread(ABTI_global *p_global, ABT_unit unit)
Definition: abti_unit.h:43
ABTI_thread::p_last_xstream
ABTI_xstream * p_last_xstream
Definition: abti.h:377
ABTI_ythread::thread
ABTI_thread thread
Definition: abti.h:407
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
ABTI_pool::id
uint64_t id
Definition: abti.h:335
ABTI_global
Definition: abti.h:196
ABT_UNIT_NULL
#define ABT_UNIT_NULL
Definition: abt.h:1061
ABTI_global_get_global_or_null
static ABTI_global * ABTI_global_get_global_or_null(void)
Definition: abti_global.h:15