"Fossies" - the Fresh Open Source Software Archive

Member "snort-2.9.17/src/memory_stats.c" (16 Oct 2020, 6327 Bytes) of package /linux/misc/snort-2.9.17.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "memory_stats.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.9.16.1_vs_2.9.17.

    1 /*
    2  **
    3  **  memory_stats.c
    4  **
    5  **  Copyright (C) 2014-2020 Cisco and/or its affiliates. All rights reserved.
    6  **  Author(s):   Puneeth Kumar C V <puneetku@cisco.com>
    7  **
    8  **  This program is free software; you can redistribute it and/or modify
    9  **  it under the terms of the GNU General Public License Version 2 as
   10  **  published by the Free Software Foundation.  You may not use, modify or
   11  **  distribute this program under any other version of the GNU General
   12  **  Public License.
   13  **
   14  **  This program is distributed in the hope that it will be useful,
   15  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
   16  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17  **  GNU General Public License for more details.
   18  **
   19  **  You should have received a copy of the GNU General Public License
   20  **  along with this program; if not, write to the Free Software
   21  **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   22  **
   23  */
   24 
   25 #ifdef HAVE_CONFIG_H
   26 #include "config.h"
   27 #endif
   28 #include <string.h>
   29 #include <stdio.h>
   30 #include <ctype.h>
   31 #include <sys/stat.h>
   32 
   33 #include "snort.h"
   34 #include "preprocids.h"
   35 #include "memory_stats.h"
   36 
   37 static MemoryStatsDisplayFunc MemStatsDisplayCallback[PP_MAX] = {0};
   38 static PreprocMemInfo *preproc_mem_info[PP_MAX] = {0};
   39 
   40 static int PopulateMemStatsBuff(char *buffer, uint32_t preproc_id)
   41 {
   42     return snprintf(buffer, CS_STATS_BUF_SIZE, "\n   Heap Memory:\n"
   43                 "                   Session: %14zu bytes\n"
   44                 "             Configuration: %14zu bytes\n"
   45                 "             --------------         ------------\n"
   46                 "              Total Memory: %14zu bytes\n"
   47                 "              No of allocs: %14d times\n"
   48                 "               IP sessions: %14d times\n"
   49                 "----------------------------------------------------\n"
   50                 , preproc_mem_info[preproc_id]->session_memory.used_memory
   51                 , preproc_mem_info[preproc_id]->cfg_memory.used_memory
   52                 , preproc_mem_info[preproc_id]->session_memory.used_memory +
   53                                            preproc_mem_info[preproc_id]->cfg_memory.used_memory
   54                 , preproc_mem_info[preproc_id]->session_memory.num_of_alloc +
   55                                            preproc_mem_info[preproc_id]->cfg_memory.num_of_alloc
   56                 , preproc_mem_info[preproc_id]->session_memory.num_of_free +
   57                                            preproc_mem_info[preproc_id]->cfg_memory.num_of_free);
   58 }
   59 
   60 static inline void PreprocDisplaystats(char *buffer, uint32_t preproc_id)
   61 {
   62     int len = 0;
   63     if (!(preproc_id != PP_ALL && preproc_id >= PP_MAX))
   64     {
   65          if ( (preproc_id != PP_ALL) && (MemStatsDisplayCallback[preproc_id] != 0 && preproc_mem_info[preproc_id] != NULL) )
   66          {
   67              len += MemStatsDisplayCallback[preproc_id](buffer);
   68              len += PopulateMemStatsBuff(buffer + len, preproc_id);
   69          }
   70          else
   71              for (preproc_id = PP_BO; preproc_id < PP_MAX; preproc_id++)
   72                 if(MemStatsDisplayCallback[preproc_id] != 0 && preproc_mem_info[preproc_id] != NULL)
   73                 {
   74                    len += MemStatsDisplayCallback[preproc_id](buffer + len);
   75                    len += PopulateMemStatsBuff(buffer + len, preproc_id);
   76                 }
   77     }
   78     else
   79     {
   80         snprintf(buffer, CS_STATS_BUF_SIZE, "\nInvalid preprocessor.\n");   
   81     }
   82 }
   83 
   84 void MemoryPostFunction(uint16_t type, void *old_context, struct _THREAD_ELEMENT *te, ControlDataSendFunc f)
   85 {
   86     char *buffer = (char*) calloc(MEM_STATS_BUF_SIZE + 1, 1);
   87     uint32_t preproc_id;
   88     
   89     if(old_context)
   90     {
   91         preproc_id = *((uint32_t *) old_context);
   92         PreprocDisplaystats(buffer, preproc_id); 
   93 
   94         if (-1 == f(te, (const uint8_t *)buffer, strlen(buffer)))
   95             LogMessage("Unable to send data to the frontend\n");
   96     }
   97     free(buffer);  
   98 }
   99 
  100 int MemoryControlFunction(uint16_t type, void *new_context, void **old_context)
  101 {
  102     if(new_context)
  103         *old_context = new_context;
  104     else    
  105         LogMessage("\nnew_context is NULL\n");
  106     return 0;
  107 }
  108 
  109 int MemoryPreFunction(uint16_t type, const uint8_t *data, uint32_t length,
  110                         void **new_context, char *statusBuf, int statusBuf_len)
  111 {
  112     if(data)
  113         *new_context = (void*) data;
  114     else
  115          LogMessage("\ndata is NULL\n"); 
  116     return 0; 
  117 }
  118 
  119 int RegisterMemoryStatsFunction(uint preproc, char* preproc_name, MemoryStatsDisplayFunc cb)
  120 {
  121     if (preproc >= PP_MAX)
  122     {
  123         return -1;
  124     }
  125 
  126     MemStatsDisplayCallback[preproc] = cb;
  127     
  128     if (preproc_mem_info[preproc] == NULL)
  129     {
  130         preproc_mem_info[preproc] = (PreprocMemInfo *)SnortAlloc(sizeof(PreprocMemInfo));    
  131         preproc_mem_info[preproc]->preproc_name = preproc_name;
  132     }
  133  
  134     return 0;
  135 }
  136 
  137 void* SnortPreprocAlloc (int num, unsigned long size, uint32_t preproc, bool cfg)
  138 {
  139     void *pv = calloc(num, size);
  140 
  141     if ( pv )
  142     {
  143         if (preproc_mem_info[preproc] == 0)
  144         {
  145             return pv;
  146         }
  147 
  148         if (cfg)
  149         {
  150             preproc_mem_info[preproc]->cfg_memory.used_memory += size;
  151             preproc_mem_info[preproc]->cfg_memory.num_of_alloc++;
  152         }
  153         else
  154         {
  155             preproc_mem_info[preproc]->session_memory.used_memory += size;
  156             preproc_mem_info[preproc]->session_memory.num_of_alloc++;
  157         }
  158     }
  159    
  160     else
  161     { 
  162         FatalError("Unable to allocate memory!  (%lu requested)\n", size);
  163     }
  164 
  165     return pv;
  166 }
  167 
  168 void SnortPreprocFree (void *ptr, uint32_t size, uint32_t preproc, bool cfg)
  169 {
  170     if( ptr )
  171     {
  172         free(ptr);
  173         ptr = NULL;
  174     }
  175 
  176     if (preproc_mem_info[preproc] == NULL)
  177     {
  178         return;
  179     }
  180     if (cfg)
  181     {
  182         preproc_mem_info[preproc]->cfg_memory.used_memory -= size;
  183         preproc_mem_info[preproc]->cfg_memory.num_of_free++;
  184     }
  185     else
  186     {
  187         preproc_mem_info[preproc]->session_memory.used_memory -= size;
  188         preproc_mem_info[preproc]->session_memory.num_of_free++;
  189     } 
  190 }
  191 
  192 void MemoryStatsFree ()
  193 {
  194     int i;
  195     for (i = 0;i < PP_MAX;i++)
  196     {
  197        if (preproc_mem_info[i])
  198        {
  199           free( preproc_mem_info[i]);
  200           preproc_mem_info[i] = NULL;
  201        }
  202     }
  203 }