From 2f23ad0de08f53ec680ba8d7361accd129d0cbe9 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Wed, 20 Jan 2021 12:47:39 +0900 Subject: [PATCH] memory: add new halapi Change-Id: I52f89b125ea4982461e5c66f85662187624b6cf5 Signed-off-by: Youngjae Cho --- include/backend/hal-memory-interface.h | 42 ++++++++++++++++ include/hal-memory.h | 37 ++++++++++++++ src/memory.c | 88 ++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 include/backend/hal-memory-interface.h create mode 100644 include/hal-memory.h create mode 100644 src/memory.c diff --git a/include/backend/hal-memory-interface.h b/include/backend/hal-memory-interface.h new file mode 100644 index 0000000..b29bffe --- /dev/null +++ b/include/backend/hal-memory-interface.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef __HAL_MEMORY_INTERFACE_H__ +#define __HAL_MEMORY_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct gpu_info { + int used_pages; +}; + +struct gem_info { + int rss; + int pss; +}; + +typedef struct __hal_backend_memory_funcs { + int (*get_gpu_info)(const int pid, struct gpu_info *info); + int (*get_gem_info)(const int pid, struct gem_info *info); +} hal_backend_memory_funcs; + +#ifdef __cplusplus +} +#endif +#endif /* __HAL_MEMORY_INTERFACE_H__ */ diff --git a/include/hal-memory.h b/include/hal-memory.h new file mode 100644 index 0000000..ffcccb1 --- /dev/null +++ b/include/hal-memory.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef __HAL_MEMORY_H__ +#define __HAL_MEMORY_H__ + +#include +#include "hal-memory-interface.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int hal_device_memory_get_backend(void); +int hal_device_memory_put_backend(void); +int hal_device_memory_get_gpu_info(int pid, struct gpu_info *info); +int hal_device_memory_get_gem_info(int pid, struct gem_info *info); + +#ifdef __cplusplus +} +#endif + +#endif /* __HAL_MEMORY_H__ */ diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..9acd608 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "hal-memory-interface.h" +#include "common.h" + +static hal_backend_memory_funcs *hal_memory_funcs = NULL; +/* +-1 : failed to initialize +0 : not initialized +1 : succeeded to initialize +*/ +static int hal_initialized = 0; + +int hal_device_memory_get_backend(void) +{ + int ret; + + if (hal_memory_funcs) + return 0; + + ret = hal_common_get_backend(HAL_MODULE_DEVICE_MEMORY, (void **)&hal_memory_funcs); + if (ret < 0) { + _E("Failed to get memory backend"); + hal_initialized = -1; + return -EINVAL; + } + + hal_initialized = 1; + return 0; +} + +int hal_device_memory_put_backend(void) +{ + if (!hal_memory_funcs) + return 0; + + hal_common_put_backend(HAL_MODULE_DEVICE_MEMORY, (void *)hal_memory_funcs); + hal_memory_funcs = NULL; + hal_initialized = 0; + + return 0; +} + +int hal_device_memory_get_gpu_info(int pid, struct gpu_info *info) +{ + int ret; + + if (!hal_memory_funcs && !hal_initialized) { + if ((ret = hal_device_memory_get_backend()) < 0) + return ret; + } + + if (!hal_memory_funcs || !hal_memory_funcs->get_gpu_info) + return -ENODEV; + + return hal_memory_funcs->get_gpu_info(pid, info); +} + +int hal_device_memory_get_gem_info(int pid, struct gem_info *info) +{ + int ret; + + if (!hal_memory_funcs && !hal_initialized) { + if ((ret = hal_device_memory_get_backend()) < 0) + return ret; + } + + if (!hal_memory_funcs || !hal_memory_funcs->get_gem_info) + return -ENODEV; + + return hal_memory_funcs->get_gem_info(pid, info); +} -- 2.7.4