Skip to content

Commit 0948b0f

Browse files
committed
Add atomics and remove all global variables
1 parent a7e5988 commit 0948b0f

File tree

4 files changed

+42
-20
lines changed

4 files changed

+42
-20
lines changed

src/mercury.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
/* PVAR profiling support */
2626
#include "mercury_prof_pvar_impl.h"
27-
HG_PROF_PVAR_UINT_COUNTER_DECL_EXTERN(hg_pvar_hg_forward_count);
2827

2928
/****************/
3029
/* Local Macros */
@@ -963,7 +962,6 @@ HG_Init_opt(const char *na_info_string, hg_bool_t na_listen,
963962
const struct hg_init_info *hg_init_info)
964963
{
965964
struct hg_private_class *hg_class = NULL;
966-
int ret;
967965
#ifdef HG_HAS_VERBOSE_ERROR
968966
const char *log_level = NULL;
969967

@@ -994,7 +992,7 @@ HG_Init_opt(const char *na_info_string, hg_bool_t na_listen,
994992

995993

996994
/* Initialize PVAR profiling data structures */
997-
ret = hg_prof_pvar_init();
995+
int ret = hg_prof_pvar_init();
998996
assert(ret == HG_SUCCESS);
999997

1000998
return (hg_class_t *) hg_class;
@@ -1893,6 +1891,8 @@ HG_Forward(hg_handle_t handle, hg_cb_t callback, void *arg, void *in_struct)
18931891
hg_uint8_t flags = 0;
18941892
hg_return_t ret = HG_SUCCESS;
18951893

1894+
HG_PROF_PVAR_UINT_COUNTER(hg_pvar_hg_forward_count);
1895+
18961896
HG_CHECK_ERROR(handle == HG_HANDLE_NULL, done, ret, HG_INVALID_ARG,
18971897
"NULL HG handle");
18981898

src/mercury_prof_interface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ HG_Prof_pvar_read(hg_prof_pvar_session_t session, hg_prof_pvar_handle_t handle,
230230
/*for(int i = 0; i < h.count; h++)*/ /* Need to support PVAR arrays, just a placeholder that assumes PVAR count is 1 */
231231
*((unsigned int *)buf) = *((unsigned int *)h.addr);
232232
break;
233+
case HG_DOUBLE:
234+
*((double *)buf) = *((double *)h.addr);
235+
break;
233236
}
234237
return HG_SUCCESS;
235238
}

src/mercury_prof_pvar_impl.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
/* Local Variables */
2626
/*******************/
2727

28-
static hg_hash_table_t *pvar_table; /* Internal hash table containing PVAR info */
28+
static hg_hash_table_t *pvar_table; /* Internal hash table containing all PVAR info */
2929

30-
HG_PROF_PVAR_UINT_COUNTER_DECL(hg_pvar_hg_forward_count); /* Declaring a PVAR */
31-
32-
/* Internal routines for the pvar_hash_table data structure */
30+
/* Internal routines for the hash tables */
3331
static HG_INLINE int
3432
hg_prof_uint_equal(void *vlocation1, void *vlocation2)
3533
{
@@ -44,14 +42,34 @@ hg_prof_uint_hash(void *vlocation)
4442
}
4543

4644
/*---------------------------------------------------------------------------*/
47-
4845
hg_prof_pvar_data_t *
4946
hg_prof_pvar_table_lookup(unsigned int key)
5047
{
5148
return hg_hash_table_lookup(pvar_table, (hg_hash_table_key_t)(&key));
5249
}
50+
5351
/*---------------------------------------------------------------------------*/
52+
hg_atomic_int32_t *
53+
hg_prof_get_pvar_addr_from_name(const char* name)
54+
{
55+
hg_prof_pvar_data_t * value = NULL;
56+
57+
for(unsigned int i = 0; i < hg_hash_table_num_entries(pvar_table); i++) {
58+
value = hg_hash_table_lookup(pvar_table, (hg_hash_table_key_t)(&i));
59+
if(strcmp(value->name, name)==0) {
60+
break;
61+
}
62+
value = NULL;
63+
}
64+
65+
if(value != NULL) {
66+
return (hg_atomic_int32_t *)value->addr;
67+
} else {
68+
return NULL;
69+
}
70+
}
5471

72+
/*---------------------------------------------------------------------------*/
5573
void
5674
HG_PROF_PVAR_REGISTER_impl(hg_prof_class_t varclass, hg_prof_datatype_t dtype, const char* name, void *addr, int count,
5775
hg_prof_bind_t bind, int continuous, const char * desc) {
@@ -61,6 +79,7 @@ HG_PROF_PVAR_REGISTER_impl(hg_prof_class_t varclass, hg_prof_datatype_t dtype, c
6179
*key = hg_hash_table_num_entries(pvar_table);
6280
hg_prof_pvar_data_t * pvar_info = NULL;
6381
pvar_info = (hg_prof_pvar_data_t *)malloc(sizeof(hg_prof_pvar_data_t));
82+
6483
(*pvar_info).pvar_class = varclass;
6584
(*pvar_info).pvar_datatype = dtype;
6685
(*pvar_info).pvar_bind = bind;
@@ -69,6 +88,7 @@ HG_PROF_PVAR_REGISTER_impl(hg_prof_class_t varclass, hg_prof_datatype_t dtype, c
6988
strcpy((*pvar_info).name, name);
7089
strcpy((*pvar_info).description, desc);
7190
(*pvar_info).continuous = continuous;
91+
7292
hg_hash_table_insert(pvar_table, (hg_hash_table_key_t)key, (hg_hash_table_value_t)(pvar_info));
7393
}
7494

@@ -80,7 +100,6 @@ hg_prof_pvar_init() {
80100
pvar_table = hg_hash_table_new(hg_prof_uint_hash, hg_prof_uint_equal);
81101
/* Register available PVARs */
82102
HG_PROF_PVAR_UINT_COUNTER_REGISTER(HG_UINT, HG_PROF_BIND_NO_OBJECT, hg_pvar_hg_forward_count, "Number of times HG_Forward has been invoked");
83-
HG_PROF_PVAR_UINT_COUNTER_REGISTER(HG_UINT, HG_PROF_BIND_NO_OBJECT, hg_pvar_dummy, "Dummy");
84103

85104
return HG_SUCCESS;
86105
}

src/mercury_prof_pvar_impl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,23 @@ typedef struct hg_prof_pvar_data_t hg_prof_pvar_data_t;
4040

4141
#define NUM_PVARS 1 /* Number of PVARs currently exported. PVAR indices go from 0......(NUM_PVARS - 1). */
4242

43-
/* PVAR declaration and registration macros */
44-
#define HG_PROF_PVAR_UINT_COUNTER_DECL(name) \
45-
hg_atomic_int32_t PVAR_COUNTER_##name;
46-
47-
#define HG_PROF_PVAR_UINT_COUNTER_DECL_EXTERN(name) \
48-
extern hg_atomic_int32_t PVAR_COUNTER_##name;
43+
/* PVAR handle declaration and registration macros */
44+
#define HG_PROF_PVAR_UINT_COUNTER(name) \
45+
static hg_atomic_int32_t * addr_##name = NULL;
4946

5047
#define HG_PROF_PVAR_UINT_COUNTER_REGISTER(dtype, bind,\
5148
name, desc) \
52-
void *addr_##name; \
49+
hg_atomic_int32_t *addr_##name = (hg_atomic_int32_t *)malloc(sizeof(hg_atomic_int32_t)); \
5350
/* Set initial value */ \
54-
hg_atomic_init32((&PVAR_COUNTER_##name), 0); \
55-
addr = &PVAR_COUNTER_##name; \
51+
hg_atomic_init32(addr_##name, 0); \
5652
HG_PROF_PVAR_REGISTER_impl(HG_PVAR_CLASS_COUNTER, dtype, #name, \
57-
addr, 1, bind, 1, desc);
53+
(void *)addr_##name, 1, bind, 1, desc);
5854

5955
/* Increment the value of a PVAR */
6056
#define HG_PROF_PVAR_COUNTER_INC(name, val) \
57+
addr_##name = (addr_##name == NULL ? hg_prof_get_pvar_addr_from_name(#name): addr_##name); \
6158
for(int i=0; i < val; i++) \
62-
hg_atomic_incr32(&PVAR_COUNTER_##name);
59+
hg_atomic_incr32(addr_##name);
6360

6461
/**
6562
* Internal routine that gets invoked during Mercury's own initialization routine.
@@ -70,6 +67,8 @@ typedef struct hg_prof_pvar_data_t hg_prof_pvar_data_t;
7067
hg_return_t
7168
hg_prof_pvar_init();
7269

70+
hg_atomic_int32_t *
71+
hg_prof_get_pvar_addr_from_name(const char* name);
7372

7473
/**
7574
* Internal routine that returns the PVAR data associated with a key representing index.
@@ -80,6 +79,7 @@ hg_prof_pvar_init();
8079
hg_prof_pvar_data_t *
8180
hg_prof_pvar_table_lookup(unsigned int key);
8281

82+
8383
/**
8484
* PVAR registration function. Used by internal Mercury modules to register any PVARs that they export.
8585
* \param varclass [IN] PVAR class

0 commit comments

Comments
 (0)