~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/perflogging.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Gather statistics from performance sensitive code.
 
3
 *
 
4
 *  Copyright (C) 2008 Sourcefire, Inc.
 
5
 *
 
6
 *  Authors: Török Edvin
 
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.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
20
 *  MA 02110-1301, USA.
 
21
 */
 
22
#ifdef HAVE_CONFIG_H
 
23
#include "clamav-config.h"
 
24
#endif
 
25
 
 
26
#include "perflogging.h"
 
27
#include <stdio.h>
 
28
#ifdef CLI_PERF_LOGGING
 
29
 
 
30
__thread last_flushed = 0;
 
31
__thread cli_perf_registered = 0;
 
32
__thread uint64_t cli_perf_sum_tls[__LAST_SUMABLE];
 
33
__thread uint64_t cli_perf_count_tls[__LAST_COUNTABLE][256];
 
34
 
 
35
uint64_t cli_perf_sum[__LAST_SUMABLE];
 
36
uint64_t cli_perf_count[__LAST_COUNTABLE][256];
 
37
 
 
38
static pthread_key_t thread_exit_key;
 
39
int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); 
 
40
 
 
41
static void cli_perf_thread_exit(void* arg)
 
42
{
 
43
        /* save counters into global */
 
44
        cli_perf_flush();
 
45
}
 
46
 
 
47
void __attribute__((constructor)) __cli_perf_init(void)
 
48
{
 
49
        pthread_key_create(&thread_exit_key, cli_perf_thread_exit);
 
50
}
 
51
 
 
52
void __attribute__((destructor)) __cli_perf_exit(void)
 
53
{
 
54
        cli_perf_thread_exit(NULL);
 
55
}
 
56
 
 
57
static int dummy;
 
58
void cli_perf_register(void)
 
59
{
 
60
        /* set a fake key, so that destructor gets called */
 
61
        pthread_setspecific(thread_exit_key, &dummy);
 
62
        cli_perf_registered = 1;
 
63
}
 
64
 
 
65
static const char *perf_log_names_sum[__LAST_SUMABLE] = {
 
66
        "raw scanned",
 
67
        "filter scanned",
 
68
        "AC scanned",
 
69
        "BM scanned"
 
70
};
 
71
 
 
72
static const char *perf_log_names_cnt[__LAST_COUNTABLE] = {
 
73
        "trie bytes scanned",
 
74
        "filter position load",
 
75
        "filter end load",
 
76
        "trie pattern original length"
 
77
};
 
78
 
 
79
#define NONE __LAST_SUMABLE
 
80
static enum perf_log_sumable perf_log_percent[__LAST_SUMABLE] = {
 
81
        NONE,
 
82
        RAW_BYTES_SCANNED,
 
83
        RAW_BYTES_SCANNED,
 
84
        RAW_BYTES_SCANNED,
 
85
};
 
86
 
 
87
static enum perf_log_countable perf_log_percent_cnt[__LAST_COUNTABLE] = {
 
88
        RAW_BYTES_SCANNED,
 
89
        NONE,
 
90
        NONE,
 
91
        NONE,
 
92
};
 
93
 
 
94
static void cli_perf_print(void)
 
95
{
 
96
        enum perf_log_sumable i;
 
97
        enum perf_log_countable j;
 
98
        unsigned k;
 
99
 
 
100
        uint64_t raw_scanned = cli_perf_sum[RAW_BYTES_SCANNED];
 
101
        const double MEGA = 1024*1024.0;
 
102
 
 
103
        /* in multiscan mode multiple threads can output, so output a unique id
 
104
         * here*/
 
105
        printf("PERF: %p\n", &cli_perf_registered);
 
106
        for(i=0;i<__LAST_SUMABLE;i++) {
 
107
                printf("PERF: %s: %g MB", perf_log_names_sum[i], cli_perf_sum[i] / MEGA);
 
108
                if (perf_log_percent[i] != NONE)
 
109
                        printf("(%6.3f%%)", 100.0*cli_perf_sum[i] / cli_perf_sum[perf_log_percent[i]]);
 
110
                printf("\n");
 
111
        }
 
112
        printf("\n");
 
113
        for(j=0;j<__LAST_COUNTABLE;j++) {
 
114
                printf("PERF: %s: ", perf_log_names_cnt[j]);
 
115
                for (k=0;k<256;k++)
 
116
                        if (cli_perf_count[j][k]) {
 
117
                                printf(" %u -> %ju", k, cli_perf_count[j][k]);
 
118
                                if (perf_log_percent_cnt[j] != NONE)
 
119
                                        printf("(%6.3f%%)", 100.0*cli_perf_count[j][k] / cli_perf_sum[perf_log_percent_cnt[j]]);
 
120
                        }
 
121
                printf("\n");
 
122
        }
 
123
        printf("\n");
 
124
}
 
125
 
 
126
static pthread_mutex_t cli_perf_log_mutex = PTHREAD_MUTEX_INITIALIZER;
 
127
void cli_perf_flush(void)
 
128
{
 
129
        unsigned i, j;
 
130
 
 
131
        pthread_mutex_lock(&cli_perf_log_mutex);
 
132
 
 
133
        for (i = 0; i < __LAST_SUMABLE; i++) {
 
134
                cli_perf_sum[i] += cli_perf_sum_tls[i];
 
135
                cli_perf_sum_tls[i] = 0;
 
136
        }
 
137
 
 
138
        for (i = 0; i < __LAST_COUNTABLE; i++) {
 
139
                for (j = 0; j < 256; j++) {
 
140
                        cli_perf_count[i][j] += cli_perf_count_tls[i][j];
 
141
                        cli_perf_count_tls[i][j] = 0;
 
142
                }
 
143
        }
 
144
 
 
145
        cli_perf_print();
 
146
        pthread_mutex_unlock(&cli_perf_log_mutex);
 
147
}
 
148
#endif