Metrics! Added support for a "state of the server" report, in which

managers can participate, reporting lies, damned lies and useful
statistics. Initial participants include the client manager, the
connection manager and the distributed object manager.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1894 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-11-05 02:17:56 +00:00
parent 65bc9f7eff
commit fa206b76ed
6 changed files with 252 additions and 18 deletions
@@ -1,5 +1,5 @@
//
// $Id: Connection.java,v 1.10 2002/10/29 23:51:26 mdb Exp $
// $Id: Connection.java,v 1.11 2002/11/05 02:17:56 mdb Exp $
package com.threerings.presents.server.net;
@@ -213,11 +213,12 @@ public abstract class Connection implements NetEventHandler
/**
* Called when our client socket has data available for reading.
*/
public void handleEvent (long when, Selectable source, short events)
public int handleEvent (long when, Selectable source, short events)
{
// make a note that we received an event as of this time
_lastEvent = when;
int bytesIn = 0;
try {
// we're lazy about creating our input streams because we may
// be inheriting them from our authing connection and we don't
@@ -229,6 +230,10 @@ public abstract class Connection implements NetEventHandler
// read the available data and see if we have a whole frame
if (_fin.readFrame(_in)) {
// make a note of how many bytes are in this frame
// (including the frame length bytes which aren't reported
// in available())
bytesIn = _fin.available() + 4;
// parse the message and pass it on
UpstreamMessage msg = (UpstreamMessage)_oin.readObject();
// Log.info("Read message " + msg + ".");
@@ -252,6 +257,8 @@ public abstract class Connection implements NetEventHandler
// deal with the failure
handleFailure(ioe);
}
return bytesIn;
}
// documentation inherited from interface
@@ -1,5 +1,5 @@
//
// $Id: ConnectionManager.java,v 1.23 2002/10/29 23:51:26 mdb Exp $
// $Id: ConnectionManager.java,v 1.24 2002/11/05 02:17:56 mdb Exp $
package com.threerings.presents.server.net;
@@ -31,6 +31,7 @@ import com.threerings.presents.server.PresentsServer;
* mechanism rather than via threads.
*/
public class ConnectionManager extends LoopingThread
implements PresentsServer.Reporter
{
/**
* Constructs and initialized a connection manager (binding the socket
@@ -42,6 +43,9 @@ public class ConnectionManager extends LoopingThread
_port = port;
_selset = new SelectSet();
// register as a "state of server" reporter
PresentsServer.registerReporter(this);
try {
// create our listening socket and add it to the select set
_listener = new NonblockingServerSocket(_port);
@@ -54,9 +58,11 @@ public class ConnectionManager extends LoopingThread
_litem = new SelectItem(_listener, Selectable.ACCEPT_READY);
// when an ACCEPT_READY event happens, we do this:
_litem.obj = new NetEventHandler() {
public void handleEvent (
long when, Selectable item, short events) {
public int handleEvent (long when, Selectable item, short events) {
acceptConnection();
// there's no easy way to measure bytes read when
// accepting a connection, so we claim nothing
return 0;
}
public void checkIdle (long now) {
// we're never idle
@@ -131,6 +137,32 @@ public class ConnectionManager extends LoopingThread
_authq.append(conn);
}
// documentation inherited from interface PresentsServer.Reporter
public void appendReport (StringBuffer report, long now, long sinceLast)
{
long bytesIn, bytesOut, msgsIn, msgsOut;
synchronized (this) {
bytesIn = _bytesIn; _bytesIn = 0L;
bytesOut = _bytesOut; _bytesOut = 0L;
msgsIn = _msgsIn; _msgsIn = 0;
msgsOut = _msgsOut; _msgsOut = 0;
}
report.append("* presents.net.ConnectionManager:\n");
report.append("- Network input: ");
report.append(bytesIn).append(" bytes, ");
report.append(msgsIn).append(" msgs, ");
long avgIn = (msgsIn == 0) ? 0 : (bytesIn/msgsIn);
report.append(avgIn).append(" avg size, ");
report.append(bytesIn*1000/sinceLast).append(" bps\n");
report.append("- Network output: ");
report.append(bytesOut).append(" bytes, ");
report.append(msgsOut).append(" msgs, ");
long avgOut = (msgsOut == 0) ? 0 : (bytesOut/msgsOut);
report.append(avgOut).append(" avg size, ");
report.append(bytesOut*1000/sinceLast).append(" bps\n");
}
/**
* Notifies the connection observers of a connection event. Used
* internally.
@@ -217,7 +249,11 @@ public class ConnectionManager extends LoopingThread
oout.flush();
// then write framed message to real output stream
_framer.writeFrameAndReset(conn.getOutputStream());
int out = _framer.writeFrameAndReset(conn.getOutputStream());
synchronized (this) {
_bytesOut += out;
_msgsOut++;
}
} catch (IOException ioe) {
// instruct the connection to deal with its failure
@@ -266,8 +302,17 @@ public class ConnectionManager extends LoopingThread
try {
SelectItem item = active[i];
NetEventHandler handler = (NetEventHandler)item.obj;
handler.handleEvent(
int got = handler.handleEvent(
iterStamp, item.getSelectable(), item.revents);
if (got != 0) {
synchronized (this) {
_bytesIn += got;
// we know that the handlers only report having
// read bytes when they have a whole message, so
// we can count thusly
_msgsIn++;
}
}
} catch (Exception e) {
Log.warning("Error processing network data.");
@@ -377,14 +422,19 @@ public class ConnectionManager extends LoopingThread
protected SelectItem _litem;
protected Queue _deathq = new Queue();
protected Queue _authq = new Queue();
protected Queue _outq = new Queue();
protected FramingOutputStream _framer;
protected Queue _authq = new Queue();
protected ArrayList _observers = new ArrayList();
/** Bytes in and out in the last reporting period. */
protected long _bytesIn, _bytesOut;
/** Messages read and written in the last reporting period. */
protected int _msgsIn, _msgsOut;
/**
* How long we wait for network events before checking our running
* flag to see if we should still be running.
@@ -1,5 +1,5 @@
//
// $Id: NetEventHandler.java,v 1.4 2002/10/29 23:51:26 mdb Exp $
// $Id: NetEventHandler.java,v 1.5 2002/11/05 02:17:56 mdb Exp $
package com.threerings.presents.server.net;
@@ -22,8 +22,11 @@ public interface NetEventHandler
* Called when a network event has occurred on the supplied source.
* The <code>events</code> parameter indicates which event or events
* have occurred.
*
* @return the number of bytes read from the network as a result of
* handling this event.
*/
public void handleEvent (long when, Selectable source, short events);
public int handleEvent (long when, Selectable source, short events);
/**
* Called to ensure that this selectable has not been idle for longer