-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdMpxStats.cc
301 lines (254 loc) · 10.2 KB
/
XrdMpxStats.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/******************************************************************************/
/* */
/* X r d M p x S t a t s . c c */
/* */
/* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD 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 Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "XrdApps/XrdMpxXml.hh"
#include "XrdNet/XrdNetAddr.hh"
#include "XrdNet/XrdNetOpts.hh"
#include "XrdNet/XrdNetSocket.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysLogger.hh"
#include "XrdSys/XrdSysHeaders.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdSys/XrdSysPthread.hh"
/******************************************************************************/
/* G l o b a l V a r i a b l e s */
/******************************************************************************/
namespace XrdMpx
{
XrdSysLogger Logger;
XrdSysError Say(&Logger, "mpxstats");
static const int addSender = 0x0001;
int Opts;
};
using namespace XrdMpx;
/******************************************************************************/
/* X r d M p x O u t */
/******************************************************************************/
class XrdMpxOut
{
public:
struct statsBuff
{statsBuff *Next;
XrdNetSockAddr From;
int Dlen;
char Data[8190];
char Pad[2];
};
void Add(statsBuff *sbP);
statsBuff *getBuff();
void *Run(XrdMpxXml *xP);
XrdMpxOut() : Ready(0), inQ(0), Free(0) {}
~XrdMpxOut() {}
private:
XrdSysMutex myMutex;
XrdSysSemaphore Ready;
statsBuff *inQ;
statsBuff *Free;
};
/******************************************************************************/
/* X r d M p x O u t : : A d d */
/******************************************************************************/
void XrdMpxOut::Add(statsBuff *sbP)
{
// Add this to the queue and signal the processing thread
//
myMutex.Lock();
sbP->Next = inQ;
inQ = sbP;
Ready.Post();
myMutex.UnLock();
}
/******************************************************************************/
/* X r d M p x O u t : : g e t B u f f */
/******************************************************************************/
XrdMpxOut::statsBuff *XrdMpxOut::getBuff()
{
statsBuff *sbP;
// Use an available buffer or allocate one
//
myMutex.Lock();
if ((sbP = Free)) Free = sbP->Next;
else sbP = new statsBuff;
myMutex.UnLock();
return sbP;
}
/******************************************************************************/
/* X r d M p x O u t : : R u n */
/******************************************************************************/
void *XrdMpxOut::Run(XrdMpxXml *xP)
{
XrdNetAddr theAddr;
const char *Host = 0;
char *bP, obuff[sizeof(statsBuff)*2];
statsBuff *sbP;
int wLen, rc;
// Simply loop formating and outputing the buffers
//
while(1)
{Ready.Wait();
myMutex.Lock();
if ((sbP = inQ)) inQ = sbP->Next;
myMutex.UnLock();
if (!sbP) continue;
if (xP)
{if (!(Opts & addSender)) Host = 0;
else if (theAddr.Set(&(sbP->From.Addr))) Host = 0;
else Host = theAddr.Name();
wLen = xP->Format(Host, sbP->Data, obuff);
bP = obuff;
} else {
bP = sbP->Data;
*(bP + sbP->Dlen) = '\n';
wLen = sbP->Dlen+1;
}
while(wLen > 0)
{do {rc = write(STDOUT_FILENO, bP, wLen);}
while(rc < 0 && errno == EINTR);
wLen -= rc; bP += rc;
}
myMutex.Lock(); sbP->Next = Free; Free = sbP; myMutex.UnLock();
}
// Should never get here
//
return (void *)0;
}
/******************************************************************************/
/* G l o b a l O b j e c t s */
/******************************************************************************/
namespace XrdMpx
{
XrdMpxOut statsQ;
};
/******************************************************************************/
/* T h r e a d I n t e r f a c e s */
/******************************************************************************/
void *mainOutput(void *parg)
{
XrdMpxXml *xP = static_cast<XrdMpxXml *>(parg);
return statsQ.Run(xP);
}
/******************************************************************************/
/* U s a g e */
/******************************************************************************/
void Usage(int rc)
{
std::cerr <<"\nUsage: mpxstats [-f {cgi|flat|xml}] -p <port> [-s]" <<std::endl;
exit(rc);
}
/******************************************************************************/
/* m a i n */
/******************************************************************************/
int main(int argc, char *argv[])
{
extern char *optarg;
extern int opterr, optopt;
sigset_t myset;
pthread_t tid;
XrdMpxXml::fmtType fType = XrdMpxXml::fmtXML;
XrdMpxOut::statsBuff *sbP = 0;
XrdNetSocket mySocket(&Say);
XrdMpxXml *xP = 0;
SOCKLEN_t fromLen;
int Port = 0, retc, udpFD;
char buff[64], c;
bool Debug;
// Process the options
//
opterr = 0; Debug = false; Opts = 0;
if (argc > 1 && '-' == *argv[1])
while ((c = getopt(argc,argv,"df:p:s")) && ((unsigned char)c != 0xff))
{ switch(c)
{
case 'd': Debug = true;
break;
case 'f': if (!strcmp(optarg, "cgi" )) fType = XrdMpxXml::fmtCGI;
else if (!strcmp(optarg, "flat")) fType = XrdMpxXml::fmtFlat;
else if (!strcmp(optarg, "xml" )) fType = XrdMpxXml::fmtXML;
else {Say.Emsg(":", "Invalid format - ", optarg); Usage(1);}
break;
case 'h': Usage(0);
break;
case 'p': if (!(Port = atoi(optarg)))
{Say.Emsg(":", "Invalid port number - ", optarg); Usage(1);}
break;
case 's': Opts |= addSender;
break;
default: sprintf(buff,"'%c'", optopt);
if (c == ':') Say.Emsg(":", buff, "value not specified.");
else Say.Emsg(0, buff, "option is invalid");
Usage(1);
break;
}
}
// Make sure port has been specified
//
if (!Port) {Say.Emsg(":", "Port has not been specified."); Usage(1);}
// Turn off sigpipe and host a variety of others before we start any threads
//
signal(SIGPIPE, SIG_IGN); // Solaris optimization
sigemptyset(&myset);
sigaddset(&myset, SIGPIPE);
sigaddset(&myset, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &myset, NULL);
// Set the default stack size here
//
if (sizeof(long) > 4) XrdSysThread::setStackSize((size_t)1048576);
else XrdSysThread::setStackSize((size_t)786432);
// Create a UDP socket and bind it to a port
//
if (mySocket.Open(0, Port, XRDNET_SERVER|XRDNET_UDPSOCKET, 0) < 0)
{Say.Emsg(":", -mySocket.LastError(), "create udp socket"); exit(4);}
udpFD = mySocket.Detach();
// Establish format
//
if (fType != XrdMpxXml::fmtXML) xP = new XrdMpxXml(fType, Debug);
// Now run a thread to output whatever we get
//
if ((retc = XrdSysThread::Run(&tid, mainOutput, (void *)xP,
XRDSYSTHREAD_BIND, "Output")))
{Say.Emsg(":", retc, "create output thread"); exit(4);}
// Now simply wait for the messages
//
fromLen = sizeof(sbP->From);
while(1)
{sbP = statsQ.getBuff();
retc = recvfrom(udpFD, sbP->Data, sizeof(sbP->Data), 0,
&sbP->From.Addr, &fromLen);
if (retc < 0) {Say.Emsg(":", retc, "recv udp message"); exit(8);}
sbP->Dlen = retc;
statsQ.Add(sbP);
}
// Should never get here
//
return 0;
}