From 247fb089781f35344ecba0fcea979585f8ba9f11 Mon Sep 17 00:00:00 2001 From: Petri Savolainen Date: Fri, 20 Oct 2017 13:55:12 +0300 Subject: [PATCH 1/4] api: shm: style clean up Clean up shm header file. Remove [in] Doxygen tags as function parameters are by default input. Move @note text to the main text block, as it's usual API specification text. Signed-off-by: Petri Savolainen --- include/odp/api/spec/shared_memory.h | 63 ++++++++++++++++------------ 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/include/odp/api/spec/shared_memory.h b/include/odp/api/spec/shared_memory.h index 1a9c1299e5..2c0db10213 100644 --- a/include/odp/api/spec/shared_memory.h +++ b/include/odp/api/spec/shared_memory.h @@ -50,6 +50,7 @@ extern "C" { */ #define ODP_SHM_SW_ONLY 0x1 /**< Application SW only, no HW access */ #define ODP_SHM_PROC 0x2 /**< Share with external processes */ + /** * Single virtual address * @@ -58,6 +59,7 @@ extern "C" { * of ODP thread type (e.g. pthread vs. process (or fork process time)). */ #define ODP_SHM_SINGLE_VA 0x4 + /** * Export memory * @@ -70,11 +72,20 @@ extern "C" { * Shared memory block info */ typedef struct odp_shm_info_t { - const char *name; /**< Block name */ - void *addr; /**< Block address */ - uint64_t size; /**< Block size in bytes */ - uint64_t page_size; /**< Memory page size */ - uint32_t flags; /**< ODP_SHM_* flags */ + /** Block name */ + const char *name; + + /** Block address */ + void *addr; + + /** Block size in bytes */ + uint64_t size; + + /** Memory page size */ + uint64_t page_size; + + /** ODP_SHM_* flags */ + uint32_t flags; } odp_shm_info_t; /** @@ -116,11 +127,10 @@ int odp_shm_capability(odp_shm_capability_t *capa); /** * Reserve a contiguous block of shared memory * - * @param[in] name Name of the block (maximum ODP_SHM_NAME_LEN - 1 chars) - * @param[in] size Block size in bytes - * @param[in] align Block alignment in bytes - * @param[in] flags Shared memory parameter flags (ODP_SHM_*). - * Default value is 0. + * @param name Name of the block (maximum ODP_SHM_NAME_LEN - 1 chars) + * @param size Block size in bytes + * @param align Block alignment in bytes + * @param flags Shared memory parameter flags (ODP_SHM_*). Default value is 0. * * @return Handle of the reserved block * @retval ODP_SHM_INVALID on failure @@ -131,10 +141,10 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align, /** * Free a contiguous block of shared memory * - * Frees a previously reserved block of shared memory. - * @note Freeing memory that is in use will result in UNDEFINED behavior + * Frees a previously reserved block of shared memory. Freeing memory that is + * in use will result in UNDEFINED behavior * - * @param[in] shm Block handle + * @param shm Block handle * * @retval 0 on success * @retval <0 on failure @@ -144,7 +154,7 @@ int odp_shm_free(odp_shm_t shm); /** * Lookup for a block of shared memory * - * @param[in] name Name of the block + * @param name Name of the block * * @return A handle to the block if it is found by name * @retval ODP_SHM_INVALID on failure @@ -177,20 +187,21 @@ odp_shm_t odp_shm_import(const char *remote_name, /** * Shared memory block address * - * @param[in] shm Block handle + * @param shm Block handle * * @return Memory block address * @retval NULL on failure */ void *odp_shm_addr(odp_shm_t shm); - /** * Shared memory block info - * @note This is the only shared memory API function which accepts invalid - * shm handles (any bit value) without causing undefined behavior. * - * @param[in] shm Block handle + * Get information about the specified shared memory block. This is the only + * shared memory API function which accepts invalid shm handles (any bit value) + * without causing undefined behavior. + * + * @param shm Block handle * @param[out] info Block info pointer for output * * @retval 0 on success @@ -207,15 +218,15 @@ void odp_shm_print_all(void); /** * Get printable value for an odp_shm_t * - * @param hdl odp_shm_t handle to be printed - * @return uint64_t value that can be used to print/display this - * handle + * This routine is intended to be used for diagnostic purposes to enable + * applications to generate a printable value that represents an odp_shm_t + * handle. + * + * @param shm Block handle * - * @note This routine is intended to be used for diagnostic purposes - * to enable applications to generate a printable value that represents - * an odp_shm_t handle. + * @return uint64_t value that can be used to print this handle */ -uint64_t odp_shm_to_u64(odp_shm_t hdl); +uint64_t odp_shm_to_u64(odp_shm_t shm); /** * @} From 30ba6f2946d6ccbe7b7c14acc8ba7aadc25d4275 Mon Sep 17 00:00:00 2001 From: Petri Savolainen Date: Fri, 20 Oct 2017 13:42:03 +0300 Subject: [PATCH 2/4] api: shm: print shm block info Added function to print debugging information about the specified shared memory block. Info is implementation defined but should include information about allocated memory addresses, pages, etc. Signed-off-by: Petri Savolainen --- include/odp/api/spec/shared_memory.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/include/odp/api/spec/shared_memory.h b/include/odp/api/spec/shared_memory.h index 2c0db10213..60a0cdde34 100644 --- a/include/odp/api/spec/shared_memory.h +++ b/include/odp/api/spec/shared_memory.h @@ -209,12 +209,21 @@ void *odp_shm_addr(odp_shm_t shm); */ int odp_shm_info(odp_shm_t shm, odp_shm_info_t *info); - /** * Print all shared memory blocks */ void odp_shm_print_all(void); +/** + * Print shared memory block info + * + * Print implementation defined information about the specified shared memory + * block to the ODP log. The information is intended to be used for debugging. + * + * @param shm Block handle + */ +void odp_shm_print(odp_shm_t shm); + /** * Get printable value for an odp_shm_t * From 4c8aa301bcb9542d0699853c1bc62682da41a193 Mon Sep 17 00:00:00 2001 From: Petri Savolainen Date: Fri, 20 Oct 2017 15:06:55 +0300 Subject: [PATCH 3/4] linux-gen: shm: make odp_shm_print_all readable again Use ODP_PRINT instead of ODP_DBG in the print function. The idea of a print function is that it prints information when application calls it. ODP_DBG does not print anything by default. Format printted output to be a readable table of shm blocks. Signed-off-by: Petri Savolainen --- platform/linux-generic/_ishm.c | 69 +++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c index 6e5bc61fe1..e7a6c23572 100644 --- a/platform/linux-generic/_ishm.c +++ b/platform/linux-generic/_ishm.c @@ -1708,11 +1708,28 @@ int _odp_ishm_status(const char *title) int nb_allocated_frgments = 0; /* nb frag describing an allocated VA */ int nb_blocks = 0; int single_va_blocks = 0; + int max_name_len = 0; odp_spinlock_lock(&ishm_tbl->lock); procsync(); - ODP_DBG("ishm blocks allocated at: %s\n", title); + /* find longest block name */ + for (i = 0; i < ISHM_MAX_NB_BLOCKS; i++) { + int str_len; + + if (ishm_tbl->block[i].len <= 0) + continue; + + str_len = strlen(ishm_tbl->block[i].name); + + if (max_name_len < str_len) + max_name_len = str_len; + } + + ODP_PRINT("ishm blocks allocated at: %s\n", title); + + ODP_PRINT(" %-*s flag len user_len seq ref start fd" + " file\n", max_name_len, "name"); /* display block table: 1 line per entry +1 extra line if mapped here */ for (i = 0; i < ISHM_MAX_NB_BLOCKS; i++) { @@ -1742,41 +1759,39 @@ int _odp_ishm_status(const char *title) huge = '?'; } proc_index = procfind_block(i); - ODP_DBG("%-3d: name:%-.24s file:%-.24s" - " flags:%s,%c len:0x%-08lx" - " user_len:%-8ld seq:%-3ld refcnt:%-4d\n", - i, - ishm_tbl->block[i].name, - ishm_tbl->block[i].filename, - flags, huge, - ishm_tbl->block[i].len, - ishm_tbl->block[i].user_len, - ishm_tbl->block[i].seq, - ishm_tbl->block[i].refcnt); + ODP_PRINT("%2i %-*s %s%c 0x%-08lx %-8lu %-3lu %-3lu", + i, max_name_len, ishm_tbl->block[i].name, + flags, huge, + ishm_tbl->block[i].len, + ishm_tbl->block[i].user_len, + ishm_tbl->block[i].seq, + ishm_tbl->block[i].refcnt); if (proc_index < 0) continue; - ODP_DBG(" start:%-08lx fd:%-3d\n", - ishm_proctable->entry[proc_index].start, - ishm_proctable->entry[proc_index].fd); + ODP_PRINT("%-08lx %-3d", + ishm_proctable->entry[proc_index].start, + ishm_proctable->entry[proc_index].fd); + + ODP_PRINT("%s\n", ishm_tbl->block[i].filename); } /* display the virtual space allocations... : */ - ODP_DBG("ishm virtual space:\n"); + ODP_PRINT("\nishm virtual space:\n"); for (fragmnt = ishm_ftbl->used_fragmnts; fragmnt; fragmnt = fragmnt->next) { if (fragmnt->block_index >= 0) { nb_allocated_frgments++; - ODP_DBG(" %08p - %08p: ALLOCATED by block:%d\n", - (uintptr_t)fragmnt->start, - (uintptr_t)fragmnt->start + fragmnt->len - 1, - fragmnt->block_index); + ODP_PRINT(" %08p - %08p: ALLOCATED by block:%d\n", + (uintptr_t)fragmnt->start, + (uintptr_t)fragmnt->start + fragmnt->len - 1, + fragmnt->block_index); consecutive_unallocated = 0; } else { - ODP_DBG(" %08p - %08p: NOT ALLOCATED\n", - (uintptr_t)fragmnt->start, - (uintptr_t)fragmnt->start + fragmnt->len - 1); + ODP_PRINT(" %08p - %08p: NOT ALLOCATED\n", + (uintptr_t)fragmnt->start, + (uintptr_t)fragmnt->start + fragmnt->len - 1); if (consecutive_unallocated++) ODP_ERR("defragmentation error\n"); } @@ -1807,9 +1822,9 @@ int _odp_ishm_status(const char *title) fragmnt; fragmnt = fragmnt->next) nb_unused_frgments++; - ODP_DBG("ishm: %d fragment used. %d fragments unused. (total=%d)\n", - nb_used_frgments, nb_unused_frgments, - nb_used_frgments + nb_unused_frgments); + ODP_PRINT("ishm: %d fragment used. %d fragments unused. (total=%d)\n", + nb_used_frgments, nb_unused_frgments, + nb_used_frgments + nb_unused_frgments); if ((nb_used_frgments + nb_unused_frgments) != ISHM_NB_FRAGMNTS) ODP_ERR("lost fragments!\n"); @@ -1817,7 +1832,7 @@ int _odp_ishm_status(const char *title) if (nb_blocks < ishm_proctable->nb_entries) ODP_ERR("process known block cannot exceed main total sum!\n"); - ODP_DBG("\n"); + ODP_PRINT("\n"); odp_spinlock_unlock(&ishm_tbl->lock); return nb_blocks; From 8fe69d217d58dc20097b71108274ff54bf197074 Mon Sep 17 00:00:00 2001 From: Petri Savolainen Date: Fri, 20 Oct 2017 15:48:24 +0300 Subject: [PATCH 4/4] linux-gen: shm: implement odp_shm_print Implement the new block level debug print function. Signed-off-by: Petri Savolainen --- platform/linux-generic/_ishm.c | 49 +++++++++++++++++++ .../linux-generic/include/_ishm_internal.h | 1 + platform/linux-generic/odp_shared_memory.c | 5 ++ test/validation/api/shmem/shmem.c | 2 + 4 files changed, 57 insertions(+) diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c index e7a6c23572..238efb81a2 100644 --- a/platform/linux-generic/_ishm.c +++ b/platform/linux-generic/_ishm.c @@ -1837,3 +1837,52 @@ int _odp_ishm_status(const char *title) odp_spinlock_unlock(&ishm_tbl->lock); return nb_blocks; } + +void _odp_ishm_print(int block_index) +{ + ishm_block_t *block; + const char *str; + + odp_spinlock_lock(&ishm_tbl->lock); + + if ((block_index < 0) || + (block_index >= ISHM_MAX_NB_BLOCKS) || + (ishm_tbl->block[block_index].len == 0)) { + odp_spinlock_unlock(&ishm_tbl->lock); + ODP_ERR("Request for info on an invalid block\n"); + return; + } + + block = &ishm_tbl->block[block_index]; + + ODP_PRINT("\nSHM block info\n--------------\n"); + ODP_PRINT(" name: %s\n", block->name); + ODP_PRINT(" file: %s\n", block->filename); + ODP_PRINT(" expt: %s\n", block->exptname); + ODP_PRINT(" user_flags: 0x%x\n", block->user_flags); + ODP_PRINT(" flags: 0x%x\n", block->flags); + ODP_PRINT(" user_len: %lu\n", block->user_len); + ODP_PRINT(" start: %p\n", block->start); + ODP_PRINT(" len: %lu\n", block->len); + + switch (block->huge) { + case HUGE: + str = "huge"; + break; + case NORMAL: + str = "normal"; + break; + case EXTERNAL: + str = "external"; + break; + default: + str = "??"; + } + + ODP_PRINT(" page type: %s\n", str); + ODP_PRINT(" seq: %lu\n", block->seq); + ODP_PRINT(" refcnt: %lu\n", block->refcnt); + ODP_PRINT("\n"); + + odp_spinlock_unlock(&ishm_tbl->lock); +} diff --git a/platform/linux-generic/include/_ishm_internal.h b/platform/linux-generic/include/_ishm_internal.h index 005d6b5511..34068bc0c5 100644 --- a/platform/linux-generic/include/_ishm_internal.h +++ b/platform/linux-generic/include/_ishm_internal.h @@ -45,6 +45,7 @@ void *_odp_ishm_address(int block_index); int _odp_ishm_info(int block_index, _odp_ishm_info_t *info); int _odp_ishm_status(const char *title); int _odp_ishm_cleanup_files(const char *dirpath); +void _odp_ishm_print(int block_index); #ifdef __cplusplus } diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c index ccd25c8c85..aabecd9b00 100644 --- a/platform/linux-generic/odp_shared_memory.c +++ b/platform/linux-generic/odp_shared_memory.c @@ -116,6 +116,11 @@ void odp_shm_print_all(void) _odp_ishm_status("Memory allocation status:"); } +void odp_shm_print(odp_shm_t shm) +{ + _odp_ishm_print(from_handle(shm)); +} + uint64_t odp_shm_to_u64(odp_shm_t hdl) { return _odp_pri(hdl); diff --git a/test/validation/api/shmem/shmem.c b/test/validation/api/shmem/shmem.c index 08587940c6..d5335afa9e 100644 --- a/test/validation/api/shmem/shmem.c +++ b/test/validation/api/shmem/shmem.c @@ -152,6 +152,8 @@ void shmem_test_basic(void) odp_cunit_thread_create(run_test_basic_thread, &thrdarg); CU_ASSERT(odp_cunit_thread_exit(&thrdarg) >= 0); + odp_shm_print(shm); + CU_ASSERT(0 == odp_shm_free(shm)); }