commit b24a199dcb45ae5389a88e779061ccb3691fca52
parent 9cd4f26d6c41e612c29645d66275962d9debf0ef
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Mon, 26 Oct 2020 15:28:27 +0100
Implement the device API
Diffstat:
| A | cmake/CMakeLists.txt | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrtp.c | | | 122 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrtp_c.h | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrtp_log.c | | | 125 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | src/atrtp_log.h | | | 69 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 455 insertions(+), 0 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -0,0 +1,99 @@
+# Copyright (C) CNRS
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+cmake_minimum_required(VERSION 2.8)
+project(atrtp C)
+enable_testing()
+
+set(ATRTP_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src)
+option(NO_TEST "Do not build tests" OFF)
+
+################################################################################
+# Check dependencies
+################################################################################
+find_package(RCMake 0.4 REQUIRED)
+find_package(RSys 0.10 REQUIRED)
+
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR})
+include(rcmake)
+include(rcmake_runtime)
+
+include_directories(${RSys_INCLUDE_DIR})
+
+################################################################################
+# Configure and define targets
+################################################################################
+set(VERSION_MAJOR 0)
+set(VERSION_MINOR 0)
+set(VERSION_PATCH 0)
+set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
+
+set(ATRTP_FILES_SRC
+ atrtp.c
+ atrtp_log.c)
+set(ATRTP_FILES_INC
+ atrtp_c.h
+ atrtp_log.h)
+set(ATRTP_FILES_INC_API
+ atrtp.h)
+
+set(ATRTP_FILES_DOC COPYING README.md)
+
+# Prepend each file in the `ATRTP_FILES_<SRC|INC>' list by `ATRTP_SOURCE_DIR'
+rcmake_prepend_path(ATRTP_FILES_SRC ${ATRTP_SOURCE_DIR})
+rcmake_prepend_path(ATRTP_FILES_INC ${ATRTP_SOURCE_DIR})
+rcmake_prepend_path(ATRTP_FILES_INC_API ${ATRTP_SOURCE_DIR})
+rcmake_prepend_path(ATRTP_FILES_DOC ${PROJECT_SOURCE_DIR}/../)
+
+add_library(atrtp SHARED ${ATRTP_FILES_SRC} ${ATRTP_FILES_INC} ${ATRTP_FILES_INC_API})
+target_link_libraries(atrtp RSys)
+
+set_target_properties(atrtp PROPERTIES
+ DEFINE_SYMBOL ATRTP_SHARED_BUILD
+ VERSION ${VERSION}
+ SOVERSION ${VERSION_MAJOR})
+
+rcmake_setup_devel(atrtp AtrTP ${VERSION} astoria/atrtp_version.h)
+
+################################################################################
+# Add tests
+################################################################################
+if(NOT NO_TEST)
+ # function(build_test _name)
+ # add_executable(${_name}
+ # ${ATRTP_SOURCE_DIR}/${_name}.c
+ # ${ATRTP_SOURCE_DIR}/test_atrtp_utils.h)
+ # target_link_libraries(${_name} atrtp RSys ${ARGN})
+ # endfunction()
+ #
+ # function(new_test _name)
+ # build_test(${_name} ${ARGN})
+ # add_test(${_name} ${_name})
+ # endfunction()
+ #
+ # new_test(test_atrtp)
+ # new_test(test_atrtp_load)
+endif()
+
+################################################################################
+# Define output & install directories
+################################################################################
+install(TARGETS atrtp
+ ARCHIVE DESTINATION bin
+ LIBRARY DESTINATION lib
+ RUNTIME DESTINATION bin)
+install(FILES ${ATRTP_FILES_INC_API} DESTINATION include/astoria)
+install(FILES ${ATRTP_FILES_DOC} DESTINATION share/doc/atrtp)
+
diff --git a/src/atrtp.c b/src/atrtp.c
@@ -0,0 +1,122 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#define _POSIX_C_SOURCE 200809L /* mmap support */
+#define _DEFAULT_SOURCE 1 /* MAP_POPULATE support */
+#define _BSD_SOURCE 1 /* MAP_POPULATE for glibc < 2.19 */
+
+#include "atrtp.h"
+#include "atrtp_c.h"
+#include "atrtp_log.h"
+
+#include <unistd.h>
+
+#include <sys/mman.h> /* mmap */
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+reset_atrtp(struct atrtp* atrtp)
+{
+ ASSERT(atrtp);
+ atrtp->pagesize = 0;
+ atrtp->nnodes = 0;
+ if(atrtp->props) munmap(atrtp->props, atrtp->map_len);
+ atrtp->props = NULL;
+ atrtp->map_len = 0;
+}
+
+static void
+release_atrtp(ref_T* ref)
+{
+ struct atrtp* atrtp;
+ ASSERT(ref);
+ atrtp = CONTAINER_OF(ref, struct atrtp, ref);
+ reset_atrtp(atrtp);
+ if(atrtp->logger == &atrtp->logger__) logger_release(&atrtp->logger__);
+ MEM_RM(atrtp->allocator, atrtp);
+}
+
+/*******************************************************************************
+ * Exported functions
+ ******************************************************************************/
+res_T
+atrtp_create
+ (struct logger* logger, /* NULL <=> use default logger */
+ struct mem_allocator* mem_allocator, /* NULL <=> use default allocator */
+ const int verbose, /* Verbosity level */
+ struct atrtp** out_atrtp)
+{
+ struct atrtp* atrtp = NULL;
+ struct mem_allocator* allocator = NULL;
+ res_T res = RES_OK;
+
+ if(!out_atrtp) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
+ atrtp = MEM_CALLOC(allocator, 1, sizeof(*atrtp));
+ if(!atrtp) {
+ if(verbose) {
+ #define ERR_STR "Could not allocate the AtrTP device.\n"
+ if(logger) {
+ logger_print(logger, LOG_ERROR, ERR_STR);
+ } else {
+ fprintf(stderr, MSG_ERROR_PREFIX ERR_STR);
+ }
+ #undef ERR_STR
+ }
+ res = RES_MEM_ERR;
+ goto error;
+ }
+ ref_init(&atrtp->ref);
+ atrtp->allocator = allocator;
+ atrtp->verbose = verbose;
+ atrtp->pagesize_os = (size_t)sysconf(_SC_PAGESIZE);
+ if(logger) {
+ atrtp->logger = logger;
+ } else {
+ setup_log_default(atrtp);
+ }
+
+exit:
+ if(out_atrtp) *out_atrtp = atrtp;
+ return res;
+error:
+ if(atrtp) {
+ ATRTP(ref_put(atrtp));
+ atrtp = NULL;
+ }
+ goto exit;
+}
+
+res_T
+atrtp_ref_get(struct atrtp* atrtp)
+{
+ if(!atrtp) return RES_BAD_ARG;
+ ref_get(&atrtp->ref);
+ return RES_OK;
+}
+
+res_T
+atrtp_ref_put(struct atrtp* atrtp)
+{
+ if(!atrtp) return RES_BAD_ARG;
+ ref_put(&atrtp->ref, release_atrtp);
+ return RES_OK;
+}
diff --git a/src/atrtp_c.h b/src/atrtp_c.h
@@ -0,0 +1,40 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ATRTP_C_H
+#define ATRTP_C_H
+
+#include <rsys/logger.h>
+#include <rsys/ref_count.h>
+
+struct mem_allocator;
+
+struct atrtp {
+ uint64_t pagesize;
+ uint64_t nnodes;
+
+ double* props;
+ size_t map_len;
+
+ size_t pagesize_os;
+
+ struct mem_allocator* allocator;
+ struct logger* logger;
+ struct logger logger__; /* Default logger */
+ int verbose;
+ ref_T ref;
+};
+
+#endif /* ATRTP_C_H */
diff --git a/src/atrtp_log.c b/src/atrtp_log.c
@@ -0,0 +1,125 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "atrtp_c.h"
+#include "atrtp_log.h"
+
+#include <rsys/cstr.h>
+#include <rsys/logger.h>
+
+#include <stdarg.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static INLINE void
+log_msg
+ (const struct atrtp* atrtp,
+ const enum log_type stream,
+ const char* msg,
+ va_list vargs)
+{
+ ASSERT(atrtp && msg);
+ if(atrtp->verbose) {
+ res_T res; (void)res;
+ res = logger_vprint(atrtp->logger, stream, msg, vargs);
+ ASSERT(res == RES_OK);
+ }
+}
+
+static void
+print_info(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_INFO_PREFIX"%s", msg);
+}
+
+static void
+print_err(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_ERROR_PREFIX"%s", msg);
+}
+
+static void
+print_warn(const char* msg, void* ctx)
+{
+ (void)ctx;
+ fprintf(stderr, MSG_WARNING_PREFIX"%s", msg);
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+setup_log_default(struct atrtp* atrtp)
+{
+ res_T res = RES_OK;
+ ASSERT(atrtp);
+
+ res = logger_init(atrtp->allocator, &atrtp->logger__);
+ if(res != RES_OK) {
+ if(atrtp->verbose) {
+ fprintf(stderr,
+ MSG_ERROR_PREFIX
+ "Could not setup the AtrTP default logger -- %s.\n",
+ res_to_cstr(res));
+ }
+ goto error;
+ }
+ logger_set_stream(&atrtp->logger__, LOG_OUTPUT, print_info, NULL);
+ logger_set_stream(&atrtp->logger__, LOG_ERROR, print_err, NULL);
+ logger_set_stream(&atrtp->logger__, LOG_WARNING, print_warn, NULL);
+ atrtp->logger = &atrtp->logger__;
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+void
+log_info(const struct atrtp* atrtp, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrtp && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrtp, LOG_OUTPUT, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_err(const struct atrtp* atrtp, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrtp && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrtp, LOG_ERROR, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_warn(const struct atrtp* atrtp, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(atrtp && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(atrtp, LOG_WARNING, msg, vargs_list);
+ va_end(vargs_list);
+}
+
diff --git a/src/atrtp_log.h b/src/atrtp_log.h
@@ -0,0 +1,69 @@
+/* Copyright (C) 2020 CNRS
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef ATRTP_LOG_H
+#define ATRTP_LOG_H
+
+#include <rsys/rsys.h>
+
+#define MSG_INFO_PREFIX "AtrTP:\x1b[1m\x1b[32minfo\x1b[0m: "
+#define MSG_ERROR_PREFIX "AtrTP:\x1b[1m\x1b[31merror\x1b[0m: "
+#define MSG_WARNING_PREFIX "AtrTP:\x1b[1m\x1b[33mwarning\x1b[0m: "
+
+struct atrtp;
+struct logger;
+
+extern LOCAL_SYM res_T
+setup_log_default
+ (struct atrtp* atrtp);
+
+/* Conditionally log a message on the LOG_OUTPUT stream of the atrtp logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_info
+ (const struct atrtp* atrtp,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_ERROR stream of the atrtp logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_err
+ (const struct atrtp* atrtp,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_WARNING stream of the atrtp logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_warn
+ (const struct atrtp* atrtp,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+#endif /* ATRTP_LOG_H */
+