Skip to content

Commit

Permalink
Fixed API issues related to new performance counters framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 26, 2015
1 parent 0443e5c commit 051ef9d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/com/frostwire/jlibtorrent/DHT.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public void waitNodes(int nodes) {
// ignore
}

ready = s.getStatus().getDHTNodes() > nodes;
ready = s.getStats().getDHTNodes() > nodes;
}
}

public int nodes() {
return s.getStatus().getDHTNodes();
public long nodes() {
return s.getStats().getDHTNodes();
}

public void get(String sha1) {
Expand Down
12 changes: 12 additions & 0 deletions src/com/frostwire/jlibtorrent/LibTorrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ public static StatsMetric[] sessionStatsMetrics() {

return arr;
}

/**
* given a name of a metric, this function returns the counter index of it,
* or -1 if it could not be found. The counter index is the index into the
* values array returned by session_stats_alert.
*
* @param name
* @return
*/
public static int findMetricIdx(String name) {
return libtorrent.find_metric_idx(name);
}
}
33 changes: 24 additions & 9 deletions src/com/frostwire/jlibtorrent/Session.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.DhtImmutableItemAlert;
import com.frostwire.jlibtorrent.alerts.GenericAlert;
import com.frostwire.jlibtorrent.alerts.*;
import com.frostwire.jlibtorrent.swig.*;
import com.frostwire.jlibtorrent.swig.session.options_t;

Expand Down Expand Up @@ -32,14 +30,16 @@ public final class Session {

private static final Logger LOG = Logger.getLogger(Session.class);

private static final long REQUEST_STATUS_RESOLUTION_MILLIS = 500;
private static final long REQUEST_STATS_RESOLUTION_MILLIS = 1000;
private static final long ALERTS_LOOP_WAIT_MILLIS = 500;

private static final Map<Integer, CastAlertFunction> CAST_TABLE = buildCastAlertTable();
private static final StatsMetric[] statsMetrics = LibTorrent.sessionStatsMetrics();

private final session s;

private long lastStatsRequestTime;
private long[] lastStatsValues;

private final SparseArray<ArrayList<AlertListener>> listeners;
private final SparseArray<AlertListener[]> listenerSnapshots;
private boolean running;
Expand Down Expand Up @@ -457,12 +457,10 @@ public void postTorrentUpdates() {
}

/**
* This function will post a session_stats_alert object, containing a
* This function will post a {@link com.frostwire.jlibtorrent.alerts.SessionStatsAlert} object, containing a
* snapshot of the performance counters from the internals of libtorrent.
* To interpret these counters, query the session via
* session_stats_metrics().
* <p/>
* For more information, see the session-statistics_ section.
*/
public void postSessionStats() {
s.post_session_stats();
Expand Down Expand Up @@ -841,8 +839,15 @@ public void run() {

Alert<?> alert = null;

if (listeners.indexOfKey(type) >= 0) {
if (type == AlertType.SESSION_STATS_ALERT.getSwig()) {
alert = castAlert(swigAlert);
lastStatsValues = ((SessionStatsAlert) alert).getValues();
}

if (listeners.indexOfKey(type) >= 0) {
if (alert == null) {
alert = castAlert(swigAlert);
}
fireAlert(alert, type);
}

Expand All @@ -855,6 +860,12 @@ public void run() {
}
deque.clear();
}

long now = System.currentTimeMillis();
if ((now - lastStatsRequestTime) >= REQUEST_STATS_RESOLUTION_MILLIS) {
lastStatsRequestTime = now;
postSessionStats();
}
}
}
};
Expand Down Expand Up @@ -1019,6 +1030,10 @@ private Alert<?> castAlert(alert a) {
return r;
}

public SessionStats getStats() {
return new SessionStats(lastStatsValues);
}

/**
* Flags to be passed in to remove_torrent().
*/
Expand Down
20 changes: 20 additions & 0 deletions src/com/frostwire/jlibtorrent/SessionStats.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.frostwire.jlibtorrent;

/**
* @author gubatron
* @author aldenml
*/
public final class SessionStats {

private static final int dhtNodesIdx = LibTorrent.findMetricIdx("dht.dht_nodes");

private final long[] statsValues;

SessionStats(long[] statsValues) {
this.statsValues = statsValues;
}

public long getDHTNodes() {
return statsValues != null ? statsValues[dhtNodesIdx] : 0;
}
}
5 changes: 5 additions & 0 deletions src/com/frostwire/jlibtorrent/StatsMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public int getValueIndex() {
public int getType() {
return sm.getType();
}

@Override
public String toString() {
return sm.getName() + ":" + sm.getValue_index() + ":" + (sm.getType() == TYPE_COUNTER ? "counter" : "gauge");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* The session_stats_alert is posted when the user requests session statistics by
* calling post_session_stats() on the session object. Its category is
* calling {@link com.frostwire.jlibtorrent.Session#postSessionStats()} on the session object. Its category is
* ``status_notification``, but it is not subject to filtering, since it's only
* manually posted anyway.
*
Expand Down
2 changes: 1 addition & 1 deletion src/com/frostwire/jlibtorrent/demo/DhtPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void alert(Alert<?> alert) {
signal.await();

System.out.println("DHT with peers");
System.out.println("Peers: " + s.getStatus().getDHTNodes());
System.out.println("Peers: " + s.getStats().getDHTNodes());

byte[] seed = new byte[Ed25519.SEED_SIZE];
int r = Ed25519.createSeed(seed);
Expand Down
1 change: 0 additions & 1 deletion src/com/frostwire/jlibtorrent/demo/DhtRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void alert(Alert<?> alert) {
System.out.println("Waiting for nodes in DHT");
dht.waitNodes(1);
System.out.println("Nodes in DHT: " + dht.nodes());
System.out.println("Global DHT nodes: " + s.getStatus(true).getDHTGlobalNodes());
}

private static List<Pair<String, Integer>> defaultRouters() {
Expand Down
21 changes: 21 additions & 0 deletions src/com/frostwire/jlibtorrent/demo/MetricsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.frostwire.jlibtorrent.demo;

import com.frostwire.jlibtorrent.LibTorrent;
import com.frostwire.jlibtorrent.StatsMetric;

/**
* @author gubatron
* @author aldenml
*/
public final class MetricsTest {

public static void main(String[] args) throws Throwable {
StatsMetric[] statsMetrics = LibTorrent.sessionStatsMetrics();

for (StatsMetric sm : statsMetrics) {
System.out.println(sm);
}

System.out.println(LibTorrent.findMetricIdx("dht.dht_nodes"));
}
}

0 comments on commit 051ef9d

Please sign in to comment.