-
Notifications
You must be signed in to change notification settings - Fork 0
/
caSnooper.cc
233 lines (215 loc) · 6.32 KB
/
caSnooper.cc
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
// CaSnooper: Server that logs broadcasts
#define DEBUG_TIME 0
#if 0
// The following are used for the modified base
# define ALLOCATE_STORAGE
# include "snoopCA.h"
#endif
#include "fdManager.h"
#include "snoopServer.h"
#include "ut.h"
// Interval for rate statistics in seconds
#define RATE_STATS_INTERVAL 1u
#define DELAY .01 // 10 ms
// Function prototypes
extern int main(int argc, const char **argv);
static void usage();
//
// main()
// (example single threaded ca server tool main loop)
//
extern int main(int argc, const char **argv)
{
snoopRateStatsTimer *statTimer = NULL;
double delay=DELAY;
int doStats = 0;
snoopServer *pCAS;
int debugLevel = 0u;
double executionTime;
double waitTime = 0.0;
int forever = 1;
char *prefix = NULL;
char *individualName = NULL;
int nCheck = NCHECK_DEFAULT;
int nPrint = NPRINT_DEFAULT;
int nSigma = NSIGMA_DEFAULT;
double nLimit = NLIMIT_DEFAULT;
int i;
for (i=1; i<argc; i++) {
if(sscanf(argv[i],"-c %d", &nCheck)==1) {
continue;
}
if(sscanf(argv[i], "-d %d", &debugLevel)==1) {
continue;
}
if(!strcmp(argv[i], "-h")) {
usage();
return(0);
}
if(!strncmp(argv[i],"-i",2)) {
individualName=(char *)malloc(NAMESIZE*sizeof(char));
if(individualName) {
strncpy(individualName,&argv[i][2],NAMESIZE);
individualName[NAMESIZE-1]='\0';
}
continue;
}
if(sscanf(argv[i],"-l %lf", &nLimit)==1) {
continue;
}
if(sscanf(argv[i],"-p %d", &nPrint)==1) {
continue;
}
if(!strncmp(argv[i],"-n",2)) {
prefix=(char *)malloc(PREFIX_SIZE*sizeof(char));
if(prefix) {
strncpy(prefix,&argv[i][2],PREFIX_SIZE);
prefix[PREFIX_SIZE-1]='\0';
doStats = 1;
}
continue;
}
if(sscanf(argv[i],"-s %d", &nSigma)==1) {
continue;
}
if(sscanf(argv[i],"-t %lf", &executionTime)==1) {
forever = aitFalse;
continue;
}
if(sscanf(argv[i],"-w %lf", &waitTime)==1) {
continue;
}
print("Unknown option: \"%s\"\n", argv[i]);
usage();
return(1);
}
// Start
print("Starting %s at %s\n",CASNOOPER_VERSION_STRING,timeStamp());
print("%s\n",EPICS_VERSION_STRING);
// Create the server
pCAS = new snoopServer(prefix,individualName,nCheck,nPrint,nSigma,nLimit);
if(!pCAS) {
print("Could not create server\n");
return(-1);
}
pCAS->setDebugLevel(debugLevel);
pCAS->disable();
print("Individual Name is %s\n",
pCAS->getIndividualName()?pCAS->getIndividualName():"NULL");
if(doStats) {
print("PV name prefix is %s\n",
pCAS->getPrefix()?pCAS->getPrefix():"NULL");
} else {
print("Internal PV names are not being published\n");
}
// Main loop
osiTime begin(osiTime::getCurrent());
// Loop here until the specified wait time
double delay0=(double)(osiTime::getCurrent() - begin);
double wait=waitTime;
#if DEBUG_TIME
print("delay0=%g wait=%g\n",delay0,wait);
#endif
while(delay0 < wait) {
#if EPICS_REVISION > 13
fileDescriptorManager.process(delay0);
#else
osiTime osiDelay(delay0);
fileDescriptorManager.process(osiDelay);
#endif
delay0=(double)(osiTime::getCurrent() - begin);
#if DEBUG_TIME
print("delay0=%g wait=%g\n",delay0,wait);
#endif
}
// Initialize stat counters
if(doStats) {
#if EPICS_REVISION > 13
// Start a default timer queue
epicsTimerQueueActive &queue =
epicsTimerQueueActive::allocate(true);
statTimer = new snoopRateStatsTimer(queue,RATE_STATS_INTERVAL,pCAS);
if(statTimer) {
// Call the expire routine to initialize it
statTimer->expire(epicsTime::getCurrent());
// Then start the timer
statTimer->start();
} else {
print("Could not start statistics timer\n");
}
#else
osiTime rateStatsDelay(RATE_STATS_INTERVAL,0u);
statTimer = new snoopRateStatsTimer(rateStatsDelay,pCAS);
// Call the expire routine to initialize it
if(statTimer) {
statTimer->expire();
} else {
print("Could not start statistics timer\n");
}
#endif
}
// Start the processing
double processedTime = 0.0;
pCAS->enable();
osiTime start(osiTime::getCurrent());
while (aitTrue) {
#if EPICS_REVISION > 13
fileDescriptorManager.process(delay);
#else
osiTime osiDelay(delay);
fileDescriptorManager.process(osiDelay);
#endif
processedTime=(double)(osiTime::getCurrent() - start);
pCAS->setProcessTime(processedTime);
if(forever) {
} else {
if(processedTime > executionTime) break;
}
if(pCAS->doReport()) pCAS->report();
if(pCAS->doReset()) pCAS->reset();
if(pCAS->doQuit()) break;
}
// Print timing
double elapsedTime=processedTime+(double)(start-begin);
print("\nCaSnooper terminating after %.2f seconds [%.2f minutes]\n",
elapsedTime,elapsedTime/60.);
print(" Data collected for %.2f seconds [%.2f minutes]\n",
processedTime,processedTime/60.);
#if 0
print("\nServer Statistics:\n");
pCAS->show(2u);
#endif
// Do final report
if(!forever) pCAS->report();
delete pCAS;
return(0);
}
void usage()
{
print(
"%s %s\n"
"Usage: caSnooper [options]\n"
" Options:\n"
" -c<integer> Check validity of top n requests (0 means all)\n"
" -d<integer> Set debug level to n\n"
" -h Help (This message)\n"
" -i<string> Specify a PV name to watch individually\n"
" -l<decimal> Print all requests over n Hz\n"
" -p<integer> Print top n requests (0 means all)\n"
" -n[<string>] Make internal PV names available\n"
" Use string as prefix for internal PV names\n"
" (%d chars max length) Default string is: %s\n"
" -s<integer> Print all requests over n sigma\n"
" -t<decimal> Run n seconds, then print report\n"
" -w<decimal> Wait n sec before collecting data\n"
"\n",
CASNOOPER_VERSION_STRING,EPICS_VERSION_STRING,PREFIX_SIZE-1,DEFAULT_PREFIX);
}