<commit_before>/* Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp_cunit_common.h>
#include "shmem.h"
#include <stdlib.h>

#define ALIGN_SIZE  (128)
#define MEM_NAME "test_shmem"
#define NAME_LEN (sizeof(MEM_NAME) + 20)
#define TEST_SHARE_FOO (0xf0f0f0f0)
#define TEST_SHARE_BAR (0xf0f0f0f)
#define SMALL_MEM 10
#define MEDIUM_MEM 4096
#define BIG_MEM 65536
#define STRESS_SIZE 32		/* power of 2 and <=256 */
#define STRESS_RANDOM_SZ 5
#define STRESS_ITERATION 5000

typedef enum {
	STRESS_FREE, /* entry is free and can be allocated */
	STRESS_BUSY, /* entry is being processed: don't touch */
	STRESS_ALLOC /* entry is allocated  and can be freed */
} stress_state_t;

typedef struct {
	stress_state_t state;
	odp_shm_t shm;
	char name[NAME_LEN];
	void *address;
	uint32_t flags;
	uint32_t size;
	uint64_t align;
	uint8_t data_val;
} stress_data_t;

typedef struct {
	odp_barrier_t test_barrier1;
	odp_barrier_t test_barrier2;
	odp_barrier_t test_barrier3;
	odp_barrier_t test_barrier4;
	uint32_t foo;
	uint32_t bar;
	odp_atomic_u32_t index;
	uint32_t nb_threads;
	odp_shm_t shm[MAX_WORKERS];
	void *address[MAX_WORKERS];
	char name[MAX_WORKERS][NAME_LEN];
	odp_spinlock_t  stress_lock;
	stress_data_t stress[STRESS_SIZE];
} shared_test_data_t;

/* memory stuff expected to fit in a single page */
typedef struct {
	int data[SMALL_MEM];
} shared_test_data_small_t;

/* memory stuff expected to fit in a huge page */
typedef struct {
	int data[MEDIUM_MEM];
} shared_test_data_medium_t;

/* memory stuff expected to fit in many huge pages */
typedef struct {
	int data[BIG_MEM];
} shared_test_data_big_t;

/*
 * thread part for the shmem_test_basic test
 */
static int run_test_basic_thread(void *arg ODP_UNUSED)
{
	odp_shm_info_t  info;
	odp_shm_t shm;
	shared_test_data_t *shared_test_data;
	int thr;

	thr = odp_thread_id();
	printf("Thread %i starts\n", thr);

	shm = odp_shm_lookup(MEM_NAME);
	CU_ASSERT(ODP_SHM_INVALID != shm);
	shared_test_data = odp_shm_addr(shm);
	CU_ASSERT(NULL != shared_test_data);

	odp_barrier_wait(&shared_test_data->test_barrier1);
	odp_shm_print_all();
	CU_ASSERT(TEST_SHARE_FOO == shared_test_data->foo);
	CU_ASSERT(TEST_SHARE_BAR == shared_test_data->bar);
	CU_ASSERT(0 == odp_shm_info(shm, &info));
	CU_ASSERT(0 == strcmp(MEM_NAME, info.name));
	CU_ASSERT(0 == info.flags);
	CU_ASSERT(shared_test_data == info.addr);
	CU_ASSERT(sizeof(shared_test_data_t) <= info.size);
	CU_ASSERT((info.page_size == odp_sys_huge_page_size()) ||
		  (info.page_size == odp_sys_page_size()))
	odp_shm_print_all();

	fflush(stdout);
	return CU_get_number_of_failures();
}

/*
 * test basic things: shmem creation, info, share, and free
 */
void shmem_test_basic(void)
{
	pthrd_arg thrdarg;
	odp_shm_t shm;
	shared_test_data_t *shared_test_data;
	odp_cpumask_t unused;

	shm = odp_shm_reserve(MEM_NAME,
			      sizeof(shared_test_data_t), ALIGN_SIZE, 0);
	CU_ASSERT(ODP_SHM_INVALID != shm);
	CU_ASSERT(odp_shm_to_u64(shm) !=
					odp_shm_to_u64(ODP_SHM_INVALID));

	CU_ASSERT(0 == odp_shm_free(shm));
	CU_ASSERT(ODP_SHM_INVALID == odp_shm_lookup(MEM_NAME));

	shm = odp_shm_reserve(MEM_NAME,
			      sizeof(shared_test_data_t), ALIGN_SIZE, 0);
	CU_ASSERT(ODP_SHM_INVALID != shm);

	shared_test_data = odp_shm_addr(shm);
	CU_ASSERT_FATAL(NULL != shared_test_data);
	shared_test_data->foo = TEST_SHARE_FOO;<commit_msg>test: api: shm: test using the same block name multiple times<commit_after>/* Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp_cunit_common.h>
#include "shmem.h"
#include <stdlib.h>

#define ALIGN_SIZE  (128)
#define MEM_NAME "test_shmem"
#define NAME_LEN (sizeof(MEM_NAME) + 20)
#define TEST_SHARE_FOO (0xf0f0f0f0)
#define TEST_SHARE_BAR (0xf0f0f0f)
#define SMALL_MEM 10
#define MEDIUM_MEM 4096
#define BIG_MEM 65536
#define STRESS_SIZE 32		/* power of 2 and <=256 */
#define STRESS_RANDOM_SZ 5
#define STRESS_ITERATION 5000

typedef enum {
	STRESS_FREE, /* entry is free and can be allocated */
	STRESS_BUSY, /* entry is being processed: don't touch */
	STRESS_ALLOC /* entry is allocated  and can be freed */
} stress_state_t;

typedef struct {
	stress_state_t state;
	odp_shm_t shm;
	char name[NAME_LEN];
	void *address;
	uint32_t flags;
	uint32_t size;
	uint64_t align;
	uint8_t data_val;
} stress_data_t;

typedef struct {
	odp_barrier_t test_barrier1;
	odp_barrier_t test_barrier2;
	odp_barrier_t test_barrier3;
	odp_barrier_t test_barrier4;
	uint32_t foo;
	uint32_t bar;
	odp_atomic_u32_t index;
	uint32_t nb_threads;
	odp_shm_t shm[MAX_WORKERS];
	void *address[MAX_WORKERS];
	char name[MAX_WORKERS][NAME_LEN];
	odp_spinlock_t  stress_lock;
	stress_data_t stress[STRESS_SIZE];
} shared_test_data_t;

/* memory stuff expected to fit in a single page */
typedef struct {
	int data[SMALL_MEM];
} shared_test_data_small_t;

/* memory stuff expected to fit in a huge page */
typedef struct {
	int data[MEDIUM_MEM];
} shared_test_data_medium_t;

/* memory stuff expected to fit in many huge pages */
typedef struct {
	int data[BIG_MEM];
} shared_test_data_big_t;

/*
 * thread part for the shmem_test_basic test
 */
static int run_test_basic_thread(void *arg ODP_UNUSED)
{
	odp_shm_info_t  info;
	odp_shm_t shm;
	shared_test_data_t *shared_test_data;
	int thr;

	thr = odp_thread_id();
	printf("Thread %i starts\n", thr);

	shm = odp_shm_lookup(MEM_NAME);
	CU_ASSERT(ODP_SHM_INVALID != shm);
	shared_test_data = odp_shm_addr(shm);
	CU_ASSERT(NULL != shared_test_data);

	odp_barrier_wait(&shared_test_data->test_barrier1);
	odp_shm_print_all();
	CU_ASSERT(TEST_SHARE_FOO == shared_test_data->foo);
	CU_ASSERT(TEST_SHARE_BAR == shared_test_data->bar);
	CU_ASSERT(0 == odp_shm_info(shm, &info));
	CU_ASSERT(0 == strcmp(MEM_NAME, info.name));
	CU_ASSERT(0 == info.flags);
	CU_ASSERT(shared_test_data == info.addr);
	CU_ASSERT(sizeof(shared_test_data_t) <= info.size);
	CU_ASSERT((info.page_size == odp_sys_huge_page_size()) ||
		  (info.page_size == odp_sys_page_size()))
	odp_shm_print_all();

	fflush(stdout);
	return CU_get_number_of_failures();
}

/*
 * test basic things: shmem creation, info, share, and free
 */
void shmem_test_basic(void)
{
	pthrd_arg thrdarg;
	odp_shm_t shm;
	odp_shm_t shm2;
	shared_test_data_t *shared_test_data;
	odp_cpumask_t unused;

	shm = odp_shm_reserve(MEM_NAME,
			      sizeof(shared_test_data_t), ALIGN_SIZE, 0);
	CU_ASSERT(ODP_SHM_INVALID != shm);
	CU_ASSERT(odp_shm_to_u64(shm) !=
					odp_shm_to_u64(ODP_SHM_INVALID));

	/* also check that another reserve with same name is accepted: */
	shm2 = odp_shm_reserve(MEM_NAME,
			       sizeof(shared_test_data_t), ALIGN_SIZE, 0);
	CU_ASSERT(ODP_SHM_INVALID != shm2);
	CU_ASSERT(odp_shm_to_u64(shm2) !=
					odp_shm_to_u64(ODP_SHM_INVALID));

	CU_ASSERT(0 == odp_shm_free(shm));
	CU_ASSERT(0 == odp_shm_free(shm2));
	CU_ASSERT(ODP_SHM_INVALID == odp_shm_lookup(MEM_NAME));

	shm = odp_shm_reserve(MEM_NAME,
			      sizeof(shared_test_data_t), ALIGN_SIZE, 0);
	CU_ASSERT(ODP_SHM_INVALID != shm);

	shared_test_data = odp_shm_addr(shm);
	CU_ASSERT_FATAL(NULL != shared_test_data);
	shared_test_data->foo = TEST_SHARE_FOO;
{'fraction_non_alphanumeric': 0.10739460953697305, 'fraction_numerical': 0.021147201105736004, 'mean_word_length': 5.7, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '519829', 'n_tokens_mistral': 3211, 'n_tokens_neox': 3038, 'n_words': 588}