File: perflogging.c

package info (click to toggle)
clamav 0.98.7+dfsg-0+deb6u2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 60,204 kB
  • ctags: 49,129
  • sloc: cpp: 267,090; ansic: 152,211; sh: 35,196; python: 2,630; makefile: 2,220; perl: 1,690; pascal: 1,218; lisp: 184; csh: 117; xml: 38; asm: 32; exp: 4
file content (148 lines) | stat: -rw-r--r-- 3,833 bytes parent folder | download | duplicates (3)
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
/*
 *  Gather statistics from performance sensitive code.
 *
 *  Copyright (C) 2008 Sourcefire, Inc.
 *
 *  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.
 */
#ifdef HAVE_CONFIG_H
#include "clamav-config.h"
#endif

#include "perflogging.h"
#include <stdio.h>
#ifdef CLI_PERF_LOGGING

__thread last_flushed = 0;
__thread cli_perf_registered = 0;
__thread uint64_t cli_perf_sum_tls[__LAST_SUMABLE];
__thread uint64_t cli_perf_count_tls[__LAST_COUNTABLE][256];

uint64_t cli_perf_sum[__LAST_SUMABLE];
uint64_t cli_perf_count[__LAST_COUNTABLE][256];

static pthread_key_t thread_exit_key;
int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); 

static void cli_perf_thread_exit(void* arg)
{
	/* save counters into global */
	cli_perf_flush();
}

void __attribute__((constructor)) __cli_perf_init(void)
{
	pthread_key_create(&thread_exit_key, cli_perf_thread_exit);
}

void __attribute__((destructor)) __cli_perf_exit(void)
{
	cli_perf_thread_exit(NULL);
}

static int dummy;
void cli_perf_register(void)
{
	/* set a fake key, so that destructor gets called */
	pthread_setspecific(thread_exit_key, &dummy);
	cli_perf_registered = 1;
}

static const char *perf_log_names_sum[__LAST_SUMABLE] = {
	"raw scanned",
	"filter scanned",
	"AC scanned",
	"BM scanned"
};

static const char *perf_log_names_cnt[__LAST_COUNTABLE] = {
	"trie bytes scanned",
	"filter position load",
	"filter end load",
	"trie pattern original length"
};

#define NONE __LAST_SUMABLE
static enum perf_log_sumable perf_log_percent[__LAST_SUMABLE] = {
	NONE,
	RAW_BYTES_SCANNED,
	RAW_BYTES_SCANNED,
	RAW_BYTES_SCANNED,
};

static enum perf_log_countable perf_log_percent_cnt[__LAST_COUNTABLE] = {
	RAW_BYTES_SCANNED,
	NONE,
	NONE,
	NONE,
};

static void cli_perf_print(void)
{
	enum perf_log_sumable i;
	enum perf_log_countable j;
	unsigned k;

	uint64_t raw_scanned = cli_perf_sum[RAW_BYTES_SCANNED];
	const double MEGA = 1024*1024.0;

	/* in multiscan mode multiple threads can output, so output a unique id
	 * here*/
	printf("PERF: %p\n", &cli_perf_registered);
	for(i=0;i<__LAST_SUMABLE;i++) {
		printf("PERF: %s: %g MB", perf_log_names_sum[i], cli_perf_sum[i] / MEGA);
		if (perf_log_percent[i] != NONE)
			printf("(%6.3f%%)", 100.0*cli_perf_sum[i] / cli_perf_sum[perf_log_percent[i]]);
		printf("\n");
	}
	printf("\n");
	for(j=0;j<__LAST_COUNTABLE;j++) {
		printf("PERF: %s: ", perf_log_names_cnt[j]);
		for (k=0;k<256;k++)
			if (cli_perf_count[j][k]) {
				printf(" %u -> %ju", k, cli_perf_count[j][k]);
				if (perf_log_percent_cnt[j] != NONE)
					printf("(%6.3f%%)", 100.0*cli_perf_count[j][k] / cli_perf_sum[perf_log_percent_cnt[j]]);
			}
		printf("\n");
	}
	printf("\n");
}

static pthread_mutex_t cli_perf_log_mutex = PTHREAD_MUTEX_INITIALIZER;
void cli_perf_flush(void)
{
	unsigned i, j;

	pthread_mutex_lock(&cli_perf_log_mutex);

	for (i = 0; i < __LAST_SUMABLE; i++) {
		cli_perf_sum[i] += cli_perf_sum_tls[i];
		cli_perf_sum_tls[i] = 0;
	}

	for (i = 0; i < __LAST_COUNTABLE; i++) {
		for (j = 0; j < 256; j++) {
			cli_perf_count[i][j] += cli_perf_count_tls[i][j];
			cli_perf_count_tls[i][j] = 0;
		}
	}

	cli_perf_print();
	pthread_mutex_unlock(&cli_perf_log_mutex);
}
#endif