diff options
author | qchang <qing.chang1@huawei.com> | 2018-03-08 17:39:22 -0800 |
---|---|---|
committer | qchang <qing.chang1@huawei.com> | 2018-03-08 17:39:22 -0800 |
commit | 697ade6190b23c80e7f60963983786e679759393 (patch) | |
tree | dd9782d1e936b8342163b26795e23571d4b1b415 /src/framework/common/mem_mgr/nsfw_mem_stat.c | |
parent | 71a4e2f34afa8018426f0e830050e50a1de6d375 (diff) |
dmm initial commit
Change-Id: I049ee277cf4efdb83f9c2ac439365fcd421c159b
Signed-off-by: qchang <qing.chang1@huawei.com>
Diffstat (limited to 'src/framework/common/mem_mgr/nsfw_mem_stat.c')
-rw-r--r-- | src/framework/common/mem_mgr/nsfw_mem_stat.c | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/src/framework/common/mem_mgr/nsfw_mem_stat.c b/src/framework/common/mem_mgr/nsfw_mem_stat.c new file mode 100644 index 0000000..f7a1f41 --- /dev/null +++ b/src/framework/common/mem_mgr/nsfw_mem_stat.c @@ -0,0 +1,292 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* 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 <stdlib.h> +#include "types.h" +#include "nstack_securec.h" +#include "nsfw_init.h" + +#include "nstack_log.h" +#include "nsfw_maintain_api.h" +#include "nsfw_mem_api.h" + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif /* __cplusplus */ + +#define NSFW_MEM_STAT_NUM 512 + +#define NSFW_MEM_STAT_MODULE "nsfw_mem_stat_module" + +typedef struct _nsfw_mem_stat +{ + u8 mem_type; + u8 alloc_flag; + char module[NSFW_MEM_MODULE_LEN]; + char mem_name[NSFW_MEM_NAME_LEN]; + u32 mem_size; +} nsfw_mem_stat_t; + +nsfw_mem_stat_t g_mem_stat[NSFW_MEM_STAT_NUM]; + +#ifdef SYS_MEM_RES_STAT +#define MAX_STAT_ITEM_NUM 20 +typedef struct _mem_stat_item_t +{ + char name[32]; + u64 size; +} mem_stat_item_t; + +typedef struct _mem_stat_mgr_t +{ + u32 item_num; + mem_stat_item_t item[MAX_STAT_ITEM_NUM]; +} mem_stat_mgr; + +mem_stat_mgr g_max_mem_list; +#endif + +/***************************************************************************** +* Prototype : nsfw_mem_stat +* Description : add memory stat +* Input : char *module +* char *mem_name +* u8 mem_type +* u32 mem_size +* Output : None +* Return Value : void +* Calls : +* Called By : +* +*****************************************************************************/ +void +nsfw_mem_stat (char *module, char *mem_name, u8 mem_type, u32 mem_size) +{ + if (NULL == module || NULL == mem_name) + { + NSFW_LOGERR ("argv err]module=%p,mem_name=%p", module, mem_name); + return; + } + + int i; + nsfw_mem_stat_t *mem_stat_item = NULL; + for (i = 0; i < NSFW_MEM_STAT_NUM; i++) + { + if (FALSE == g_mem_stat[i].alloc_flag) + { + g_mem_stat[i].alloc_flag = TRUE; + mem_stat_item = &g_mem_stat[i]; + break; + } + } + + if (NULL == mem_stat_item) + { + NSFW_LOGERR ("mem stat full]module=%s,type=%u,name=%s,size=%u", + module, mem_type, mem_name, mem_size); + return; + } + + mem_stat_item->mem_type = mem_type; + mem_stat_item->mem_size = mem_size; + + if (EOK != STRCPY_S (mem_stat_item->module, NSFW_MEM_MODULE_LEN, module)) + { + NSFW_LOGERR ("STRNCPY_S failed"); + return; + } + if (EOK != STRCPY_S (mem_stat_item->mem_name, NSFW_MEM_NAME_LEN, mem_name)) + { + NSFW_LOGERR ("STRNCPY_S failed"); + return; + } + + return; +} + +/***************************************************************************** +* Prototype : nsfw_mem_stat_print +* Description : print all memory info +* Input : None +* Output : None +* Return Value : void +* Calls : +* Called By : +* +*****************************************************************************/ +void +nsfw_mem_stat_print () +{ + int i; + for (i = 0; i < NSFW_MEM_STAT_NUM; i++) + { + if (TRUE == g_mem_stat[i].alloc_flag) + { + NSFW_LOGINF ("mem_module=%s,name=%s,type=%u,size=%u", + g_mem_stat[i].module, g_mem_stat[i].mem_name, + g_mem_stat[i].mem_type, g_mem_stat[i].mem_size); + } + } + +} + +#ifdef SYS_MEM_RES_STAT +void +clear_mem_stat_item () +{ + if (EOK != MEMSET_S ((char *) &g_max_mem_list, sizeof (mem_stat_mgr), + 0, sizeof (mem_stat_mgr))) + { + NSFW_LOGERR ("MEMSET_S failed"); + } +} + +void +insert_mem_stat_item (char *name, u64 len) +{ + int j, temp; + + if (g_max_mem_list.item_num == 0) + { + if (EOK != + STRCPY_S (g_max_mem_list.item[0].name, + sizeof (g_max_mem_list.item[0].name), name)) + { + NSFW_LOGERR ("STRCPY_S failed"); + } + g_max_mem_list.item[0].size = len; + g_max_mem_list.item_num++; + return; + } + else if (g_max_mem_list.item_num < MAX_STAT_ITEM_NUM) + { + if (len <= g_max_mem_list.item[g_max_mem_list.item_num - 1].size) + { + if (EOK != + STRCPY_S (g_max_mem_list.item[g_max_mem_list.item_num].name, + sizeof (g_max_mem_list.item + [g_max_mem_list.item_num].name), name)) + { + NSFW_LOGERR ("STRCPY_S failed"); + } + g_max_mem_list.item[g_max_mem_list.item_num].size = len; + g_max_mem_list.item_num++; + return; + } + j = 0; + temp = g_max_mem_list.item_num; + while (j < temp) + { + if (len >= g_max_mem_list.item[j].size) + { + goto insert_it; + } + j++; + } + if (j == temp) + { + if (EOK != + STRCPY_S (g_max_mem_list.item[j].name, + sizeof (g_max_mem_list.item[j].name), name)) + { + NSFW_LOGERR ("STRCPY_S failed"); + } + g_max_mem_list.item[j].size = len; + g_max_mem_list.item_num++; + return; + } + } + else + { + j = 0; + temp = MAX_STAT_ITEM_NUM - 1; + while (j < MAX_STAT_ITEM_NUM) + { + if (len >= g_max_mem_list.item[j].size) + { + goto insert_it; + } + j++; + } + } + + return; + +insert_it: + while (temp - 1 >= j) + { + if (EOK != + STRCPY_S (g_max_mem_list.item[temp].name, + sizeof (g_max_mem_list.item[temp].name), + g_max_mem_list.item[temp - 1].name)) + { + NSFW_LOGERR ("STRCPY_S failed"); + } + g_max_mem_list.item[temp].size = g_max_mem_list.item[temp - 1].size; + temp--; + } + if (EOK != + STRCPY_S (g_max_mem_list.item[j].name, + sizeof (g_max_mem_list.item[j].name), name)) + { + NSFW_LOGERR ("STRCPY_S failed"); + } + g_max_mem_list.item[j].size = len; + g_max_mem_list.item_num++; + return; +} + +int +get_mem_stat_item (int idx, char **name, u64 * len) +{ + if (idx < 0 || idx >= MAX_STAT_ITEM_NUM) + { + return -1; + } + + *name = g_max_mem_list.item[idx].name; + *len = g_max_mem_list.item[idx].size; + + return 0; +} +#endif + +static int nsfw_mem_stat_init (void *param); +static int +nsfw_mem_stat_init (void *param) +{ + MEM_STAT (NSFW_MEM_STAT_MODULE, "g_mem_stat", NSFW_NSHMEM, + sizeof (g_mem_stat)); + nsfw_mem_stat_print (); +#ifdef SYS_MEM_RES_STAT + clear_mem_stat_item (); +#endif + return 0; +} + +/* *INDENT-OFF* */ +NSFW_MODULE_NAME (NSFW_MEM_STAT_MODULE) +NSFW_MODULE_PRIORITY (99) +NSFW_MODULE_INIT (nsfw_mem_stat_init) +/* *INDENT-ON* */ + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif /* __cplusplus */ |