libclamav/perflogging.h
e0ac80ab
 /*
  *  Gather statistics from performance sensitive code.
  *
e1cbc270
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2008-2013 Sourcefire, Inc.
e0ac80ab
  *
  *  Authors: Török Edvin
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
  *
  *  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, write to the Free Software
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
  */
 #ifndef PERFLOGGING_H
 #define PERFLOGGING_H
 
 /* this is a compile-time selectable, default off module to log certain
  * statistics, such as which tries are used, efficiency of filtering and so on.
  * it must have as little overhead as possible */
 
15839138
 /* #define CLI_PERF_LOGGING */
e0ac80ab
 #ifdef CLI_PERF_LOGGING
 
 #ifndef __GNUC__
 #error "Performance logging requires GNU C compatible compiler"
 #else
 /*TODO: maybe we need a GCC version check too here */
 #include <pthread.h>
380ae304
 #include <assert.h>
e030ba4f
 #include "clamav-types.h"
e0ac80ab
 
 enum perf_log_sumable {
288057e9
     RAW_BYTES_SCANNED,
     FILTER_BYTES_SCANNED,
     AC_SCANNED,
     BM_SCANNED,
     __LAST_SUMABLE
e0ac80ab
 };
 
 enum perf_log_countable {
288057e9
     TRIE_SCANNED,
     FILTER_LOAD,
     FILTER_END_LOAD,
     TRIE_ORIG_LEN,
     __LAST_COUNTABLE
e0ac80ab
 };
 
 extern __thread int last_flushed;
 extern __thread int cli_perf_registered;
 extern __thread uint64_t cli_perf_sum_tls[__LAST_SUMABLE];
 extern __thread uint64_t cli_perf_count_tls[__LAST_COUNTABLE][256];
 extern __thread int last_flushed;
 
 extern uint64_t cli_perf_sum[__LAST_SUMABLE];
 extern uint64_t cli_perf_count[__LAST_COUNTABLE][256];
 
 void cli_perf_register(void);
 void cli_perf_flush(void);
 
 static inline void cli_perf_enter(void)
 {
288057e9
     if (!cli_perf_registered) cli_perf_register();
     if (cli_perf_sum_tls[RAW_BYTES_SCANNED] - last_flushed > 100 * 1024 * 1024) {
         cli_perf_flush();
         last_flushed = cli_perf_sum_tls[RAW_BYTES_SCANNED];
     }
e0ac80ab
 }
 
 static inline void cli_perf_log_add(enum perf_log_sumable kind, uint64_t add)
 {
288057e9
     cli_perf_enter();
     assert(kind < __LAST_SUMABLE);
     cli_perf_sum_tls[kind] += add;
e0ac80ab
 }
 
 static inline void cli_perf_log_count2(enum perf_log_countable kind, uint8_t event, uint64_t cnt)
 {
288057e9
     cli_perf_enter();
     assert(kind < __LAST_COUNTABLE);
     cli_perf_count_tls[kind][event] += cnt;
e0ac80ab
 }
 
 static inline void cli_perf_log_count(enum perf_log_countable kind, uint8_t event)
 {
288057e9
     cli_perf_log_count2(kind, event, 1);
e0ac80ab
 }
 
 #endif
 
 #else
288057e9
 #define cli_perf_log_count(a, b) \
     do {                         \
     } while (0)
e0ac80ab
 #endif
 
 #endif