1 /*!\file stats.hxx
2 
3    \brief Statistics (fs, av, cache, etc.) routines (header file)
4 
5 *//*
6 
7    ClamFS - An user-space anti-virus protected file system
8    Copyright (C) 2008-2019 Krzysztof Burghardt
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24 
25 #ifndef CLAMFS_STATS_HXX
26 #define CLAMFS_STATS_HXX
27 
28 #include "config.h"
29 
30 #include <cstring>
31 #include <stdlib.h>
32 #ifdef HAVE_MALLOC_H
33    #include <malloc.h>
34 #endif
35 
36 #ifdef DMALLOC
37    #include <dmalloc.h>
38 #endif
39 
40 #include "rlog.hxx"
41 
42 namespace clamfs {
43 
44 using namespace std;
45 
46 extern RLogChannel *Info;
47 
48 /*!\class Stats
49    \brief Statistics module for ClamFS fs, av, cache and more
50 
51    Statistic data collection class with easy to use interface,
52    simple analisis and ability to dump statistics to rLog.
53 */
54 class Stats {
55     public:
56         /*!\brief Constructor for Stats
57            \param dumpEvery time in seconds between stats dump
58         */
59         Stats(time_t dumpEvery = 0);
60         /*!\brief Destructor for Stats */
61         ~Stats();
62 
63         /*!\brief Enable memory statistics */
enableMemoryStats()64         void enableMemoryStats() { memoryStats = true; }
65 
66         /*!\brief Dump filesystem statistics to log */
67         void dumpFilesystemStatsToLog();
68 
69         /*!\brief Dump memory statistics to log */
70         void dumpMemoryStatsToLog();
71 
72          /*!\brief Periodically dump statistics to log */
73         void periodicDumpToLog();
74 
75     private:
76         /*!\brief Forbid usage of copy constructor */
77         Stats(const Stats& aStats);
78         /*!\brief Forbid usage of assignment operator */
79         Stats& operator = (const Stats& aStats);
80 
81         /*!\brief Timestamp of last stats dump */
82         time_t lastdump;
83 
84         /*!\brief Dump stats every seconds */
85         time_t every;
86 
87     public:
88         /*!\brief early cache hit counter */
89         unsigned long long earlyCacheHit;
90         /*!\brief early cache miss counter */
91         unsigned long long earlyCacheMiss;
92         /*!\brief late cache hit counter */
93         unsigned long long lateCacheHit;
94         /*!\brief late cache miss counter */
95         unsigned long long lateCacheMiss;
96 
97         /*!\brief whitelist hit counter */
98         unsigned long long whitelistHit;
99         /*!\brief blacklist hit counter */
100         unsigned long long blacklistHit;
101 
102         /*!\brief files bigger than maximal-size hit counter */
103         unsigned long long tooBigFile;
104 
105         /*!\brief open() function call counter */
106         unsigned long long openCalled;
107         /*!\brief open() call allowed by AV counter */
108         unsigned long long openAllowed;
109         /*!\brief open() call denied by AV counter */
110         unsigned long long openDenied;
111 
112         /*!\brief a/v scan failed (clamd unavailable, permission problem, etc.) */
113         unsigned long long scanFailed;
114 
115         /*!\brief indicates that memory statistics should be included */
116         bool memoryStats;
117 };
118 
119 /*!\brief extern to access stats pointer from clamfs.cxx */
120 extern Stats* stats;
121 
122 /*!\def INC_STAT_COUNTER
123    \brief Increment statistic module counter
124    \param counter name of counter to increment
125 
126    This macro is intended to easier update ClamFS stats module
127    counters. This macro check if stats module was initialized
128    and if so updates statistic counters.
129 */
130 #define INC_STAT_COUNTER(counter) do {\
131     if (stats) {\
132         ++(stats->counter);\
133     }\
134 } while(0)
135 
136 
137 } /* namespace clamfs */
138 
139 #endif /* CLAMFS_STATS_HXX */
140 
141 /* EoF */
142