Skip to content

Commit

Permalink
Merge pull request #41 from adanalis/sde_shutdown
Browse files Browse the repository at this point in the history
Sde shutdown
  • Loading branch information
gcongiu authored Jul 3, 2023
2 parents 1109043 + b32dc46 commit 517b23a
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 79 deletions.
48 changes: 24 additions & 24 deletions src/sde_lib/sde_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,30 @@ papi_sde_enable( papi_handle_t handle ){
int
papi_sde_shutdown( papi_handle_t handle ){
papisde_library_desc_t *lib_handle, *tmp_lib, *next_lib, *prev_lib;
papisde_list_entry_t *list_head, *curr;
int i, ret_val=SDE_OK;
int i;

lib_handle = (papisde_library_desc_t *) handle;
papisde_control_t *gctl = _papisde_global_control;
if( (NULL==lib_handle) || lib_handle->disabled || (NULL==gctl) || gctl->disabled)
return SDE_OK;

SDEDBG("papi_sde_shutdown(): for library '%s'.\n", lib_handle->libraryName);

sde_lock();
for(i=0; i<PAPISDE_HT_SIZE; i++){
list_head = &(lib_handle->lib_counters[i]);
if( NULL == list_head )
continue;

if(NULL != list_head->item){
sdei_free_counter(list_head->item);
}
sde_counter_t *all_lib_counters;
int item_cnt = ht_to_array(lib_handle->lib_counters, &all_lib_counters);

for(curr = list_head->next; NULL != curr; curr=list_head->next){
if(NULL == curr->item){ // This can only legally happen for the head of the list.
SDE_ERROR("papi_sde_shutdown(): the counter hash table is clobbered.");
ret_val = SDE_EINVAL;
goto fn_exit;
}
sdei_free_counter(curr->item);
list_head->next = curr->next;
free(curr);
}
for(i=0; i<item_cnt; i++){
char *cntr_name = all_lib_counters[i].name;
sdei_delete_counter(lib_handle, cntr_name);
}

// We don't need the serialized array any more. Besides, the pointers inside
// its elements have _not_ been copied, so they are junk by now, since we
// deleted the counters.
free(all_lib_counters);

// Keep the `gctl` struct consistent
// 1. If the lib head is this one, just set to next (could be NULL)
// 2. Otherwise, find the prev_lib and set prev_lib->next = next_lib
Expand All @@ -263,9 +257,8 @@ papi_sde_shutdown( papi_handle_t handle ){
free(lib_handle->libraryName);
free(lib_handle);

fn_exit:
sde_unlock();
return ret_val;
return SDE_OK;
}


Expand Down Expand Up @@ -353,7 +346,7 @@ papi_sde_unregister_counter( papi_handle_t handle, const char *event_name)
char *full_event_name;
int ret_val;

SDEDBG("Preparing to unregister counter\n");
SDEDBG("Preparing to unregister counter: '%s'.\n",event_name);

lib_handle = (papisde_library_desc_t *) handle;
papisde_control_t *gctl = _papisde_global_control;
Expand Down Expand Up @@ -525,9 +518,13 @@ papi_sde_add_counter_to_group(papi_handle_t handle, const char *event_name, cons
(void)ht_insert(gctl->all_reg_counters, ht_hash_id(cntr_group_uniq_id), tmp_group);

}else{
// should the following branch ever be true? Why do we already have a group registered if it's empty?
if( NULL == tmp_group->u.cntr_group.group_head ){
SDE_ERROR("papi_sde_add_counter_to_group(): Found an empty counter group: '%s'. This might indicate that a cleanup routine is not doing its job.", group_name);
if( CNTR_CLASS_PLACEHOLDER == tmp_group->cntr_class ){
tmp_group->cntr_class = CNTR_CLASS_GROUP;
}else{
SDE_ERROR("papi_sde_add_counter_to_group(): Found an empty counter group: '%s'. This might indicate that a cleanup routine is not doing its job.", group_name);
}

}

// make sure the caller is not trying to change the flags of the group after it has been created.
Expand All @@ -544,6 +541,9 @@ papi_sde_add_counter_to_group(papi_handle_t handle, const char *event_name, cons
new_head->item = tmp_item;
new_head->next = tmp_group->u.cntr_group.group_head;
tmp_group->u.cntr_group.group_head = new_head;
if( SDE_OK != sdei_inc_ref_count(tmp_item) ){
SDE_ERROR("papi_sde_add_counter_to_group(): Error while adding counter '%s' to counter group: '%s'.", tmp_item->name, group_name);
}

free(full_group_name);
ret_val = SDE_OK;
Expand Down
1 change: 1 addition & 0 deletions src/sde_lib/sde_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define SDE_ENOMEM -2 /**< Insufficient memory */
#define SDE_ECMP -4 /**< Not supported by component */
#define SDE_ENOEVNT -7 /**< Event does not exist */
#define SDE_EMISC -14 /**< Unknown error code */

#define register_fp_counter register_counter_cb
#define papi_sde_register_fp_counter papi_sde_register_counter_cb
Expand Down
62 changes: 61 additions & 1 deletion src/sde_lib/sde_lib_datastructures.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,63 @@ void ht_insert(papisde_list_entry_t *hash_table, int ht_key, sde_counter_t *sde_
return;
}

// This function serializes the items contained in the hash-table. A pointer
// to the resulting serialized array is put into the parameter "rslt_array".
// The return value indicates the size of the array.
// The array items are copies of the hash-table item into newly allocated
// memory. They do not reference the original items in the hash table. However,
// this is a shallow copy. If the items contain pointers, then the pointed
// elements are _NOT_ copied.
// The caller is responsible for freeing the resulting array memory.
int ht_to_array(papisde_list_entry_t *hash_table, sde_counter_t **rslt_array)
{
int i, item_cnt = 0, index=0;
papisde_list_entry_t *list_head, *curr;

// First pass counts how many items have been inserted in the hash table.

// Traverse all the elements of the hash-table.
for(i=0; i<PAPISDE_HT_SIZE; i++){
// For each element traverse the linked-list starting there (if any).
list_head = &(hash_table[i]);

if(NULL != list_head->item){
item_cnt++;
}
for(curr = list_head->next; NULL != curr; curr=curr->next){
if(NULL == curr->item){ // This can only legally happen for the head of the list.
SDE_ERROR("ht_to_array(): the hash table is clobbered.");
}else{
item_cnt++;
}
}
}

// Allocate a contiguous array to store the items.
sde_counter_t *array = (sde_counter_t *)malloc( item_cnt * sizeof(sde_counter_t));

// Traverse the hash-table again and copy all the items to the array we just allocated.
for(i=0; i<PAPISDE_HT_SIZE; i++){
list_head = &(hash_table[i]);

if(NULL != list_head->item){
memcpy( &array[index], list_head->item, sizeof(sde_counter_t) );
index++;
}
for(curr = list_head->next; NULL != curr; curr=curr->next){
if(NULL == curr->item){ // This can only legally happen for the head of the list.
SDE_ERROR("ht_to_array(): the hash table is clobbered.");
}else{
memcpy( &array[index], curr->item, sizeof(sde_counter_t) );
index++;
}
}
}
*rslt_array = array;

return item_cnt;
}

sde_counter_t *ht_delete(papisde_list_entry_t *hash_table, int ht_key, uint32_t uniq_id)
{
papisde_list_entry_t *list_head, *curr, *prev;
Expand All @@ -68,8 +125,11 @@ sde_counter_t *ht_delete(papisde_list_entry_t *hash_table, int ht_key, uint32_t
// If the head contains the element to be deleted, free the space of the counter and pull the list up.
if( list_head->item->glb_uniq_id == uniq_id ){
item = list_head->item;
if( NULL != list_head->next)
if( NULL != list_head->next){
*list_head = *(list_head->next);
}else{
memset(list_head, 0, sizeof(papisde_list_entry_t));
}
return item;
}

Expand Down
4 changes: 3 additions & 1 deletion src/sde_lib/sde_lib_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ struct sde_counter_s {
int overflow;
int cntr_type;
int cntr_mode;
int ref_count;
papisde_library_desc_t *which_lib;
};

Expand Down Expand Up @@ -187,7 +188,7 @@ struct papisde_control_s {

int sdei_setup_counter_internals( papi_handle_t handle, const char *event_name, int cntr_mode, int cntr_type, enum CNTR_CLASS cntr_class, cntr_class_specific_t cntr_union );
int sdei_delete_counter(papisde_library_desc_t* lib_handle, const char *name);
int sdei_free_counter(sde_counter_t *counter);
int sdei_inc_ref_count(sde_counter_t *counter);
int sdei_read_counter_group( sde_counter_t *counter, long long int *rslt_ptr );
void sdei_counting_set_to_list( void *cset_handle, cset_list_object_t **list_head );
int sdei_read_and_update_data_value( sde_counter_t *counter, long long int previous_value, long long int *rslt_ptr );
Expand All @@ -199,6 +200,7 @@ sde_counter_t *ht_lookup_by_id(papisde_list_entry_t *hash_table, uint32_t uniq_i
sde_counter_t *ht_lookup_by_name(papisde_list_entry_t *hash_table, const char *name);
sde_counter_t *ht_delete(papisde_list_entry_t *hash_table, int ht_key, uint32_t uniq_id);
void ht_insert(papisde_list_entry_t *hash_table, int ht_key, sde_counter_t *sde_counter);
int ht_to_array(papisde_list_entry_t *hash_table, sde_counter_t **rslt_array);
uint32_t ht_hash_name(const char *str);
uint32_t ht_hash_id(uint32_t uniq_id);
papi_handle_t do_sde_init(const char *name_of_library, papisde_control_t *gctl);
Expand Down
Loading

0 comments on commit 517b23a

Please sign in to comment.