aboutsummaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/src/mem_mgr/nsfw_mem_stat.c
blob: 92d0eda3c22020b7788d84d9175f09480a6ccdb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
*
* 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

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 (20)
NSFW_MODULE_INIT (nsfw_mem_stat_init)
/* *INDENT-ON* */

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif /* __cplusplus */