Skip to content

Commit

Permalink
More work with new libtorrent statistical framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 26, 2015
1 parent 0ce7222 commit d9d7569
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/com/frostwire/jlibtorrent/LibTorrent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.swig.libtorrent;
import com.frostwire.jlibtorrent.swig.stats_metric_vector;

/**
* @author gubatron
Expand All @@ -25,4 +26,26 @@ public static String version() {
public static String toHex(byte[] data) {
return libtorrent.to_hex(Vectors.bytes2char_vector(data));
}

/**
* This free function returns the list of available metrics exposed by
* libtorrent's statistics API. Each metric has a name and a *value index*.
* The value index is the index into the array in session_stats_alert where
* this metric's value can be found when the session stats is sampled (by
* calling post_session_stats()).
*
* @return
*/
public static StatsMetric[] sessionStatsMetrics() {
stats_metric_vector v = libtorrent.session_stats_metrics();

int size = (int) v.size();
StatsMetric[] arr = new StatsMetric[size];

for (int i = 0; i < size; i++) {
arr[i] = new StatsMetric(v.get(i));
}

return arr;
}
}
1 change: 1 addition & 0 deletions src/com/frostwire/jlibtorrent/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public final class Session {
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;

Expand Down
37 changes: 37 additions & 0 deletions src/com/frostwire/jlibtorrent/StatsMetric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.swig.stats_metric;

/**
* Describes one statistics metric from the session.
*
* @author gubatron
* @author aldenml
*/
public final class StatsMetric {

public static final int TYPE_COUNTER = stats_metric.type_counter;
public static final int TYPE_GAUGE = stats_metric.type_gauge;

private final stats_metric sm;

public StatsMetric(stats_metric sm) {
this.sm = sm;
}

public stats_metric getSwig() {
return sm;
}

public String getName() {
return sm.getName();
}

public int getValueIndex() {
return sm.getValue_index();
}

public int getType() {
return sm.getType();
}
}
3 changes: 3 additions & 0 deletions src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void readPiece(ReadPieceAlert alert) {
public void stateChanged(StateChangedAlert alert) {
}

public void sessionStats(SessionStatsAlert alert) {
}

public void dhtReply(DhtReplyAlert alert) {
}

Expand Down
11 changes: 11 additions & 0 deletions src/com/frostwire/jlibtorrent/Vectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ public static long[] int64_vector2longs(int64_vector v) {
return arr;
}

public static long[] uint64_vector2longs(uint64_vector v) {
int size = (int) v.size();
long[] arr = new long[size];

for (int i = 0; i < size; i++) {
arr[i] = v.get(i).longValue();
}

return arr;
}

public static boolean[] bool_vector2booleans(bool_vector v) {
int size = (int) v.size();
boolean[] arr = new boolean[size];
Expand Down
1 change: 1 addition & 0 deletions src/com/frostwire/jlibtorrent/alerts/AlertType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum AlertType {
EXTERNAL_IP(external_ip_alert.alert_type),
LISTEN_SUCCEEDED(listen_succeeded_alert.alert_type),
STATE_UPDATE(state_update_alert.alert_type),
SESSION_STATS_ALERT(session_stats_alert.alert_type),
SCRAPE_REPLY(scrape_reply_alert.alert_type),
SCRAPE_FAILED(scrape_failed_alert.alert_type),
LSD_PEER(lsd_peer_alert.alert_type),
Expand Down
48 changes: 48 additions & 0 deletions src/com/frostwire/jlibtorrent/alerts/SessionStatsAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.frostwire.jlibtorrent.alerts;

import com.frostwire.jlibtorrent.Vectors;
import com.frostwire.jlibtorrent.swig.session_stats_alert;

/**
* The session_stats_alert is posted when the user requests session statistics by
* calling post_session_stats() on the session object. Its category is
* ``status_notification``, but it is not subject to filtering, since it's only
* manually posted anyway.
*
* @author gubatron
* @author aldenml
*/
public final class SessionStatsAlert extends AbstractAlert<session_stats_alert> {

public SessionStatsAlert(session_stats_alert alert) {
super(alert);
}

/**
* the number of microseconds since the session was
* started. It represent the time when the snapshot of values was taken. When
* the network thread is under heavy load, the latency between calling
* post_session_stats() and receiving this alert may be significant, and
* the timestamp may help provide higher accuracy in measurements.
*
* @return
*/
public long getStatsTimestamp() {
return alert.getTimestamp().longValue();
}

/**
* An array are a mix of *counters* and *gauges*, which
* meanings can be queries via the session_stats_metrics() function on the session.
* The mapping from a specific metric to an index into this array is constant for a
* specific version of libtorrent, but may differ for other versions. The intended
* usage is to request the mapping, i.e. call session_stats_metrics(), once
* on startup, and then use that mapping to interpret these values throughout
* the process' runtime.
*
* @return
*/
public long[] getValues() {
return Vectors.uint64_vector2longs(alert.getValues());
}
}
1 change: 1 addition & 0 deletions swig/libtorrent.i
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ namespace libtorrent {
CAST_ALERT_METHOD(incoming_connection_alert)
CAST_ALERT_METHOD(add_torrent_alert)
CAST_ALERT_METHOD(state_update_alert)
CAST_ALERT_METHOD(session_stats_alert)
CAST_ALERT_METHOD(torrent_update_alert)
CAST_ALERT_METHOD(rss_item_alert)
CAST_ALERT_METHOD(dht_error_alert)
Expand Down

0 comments on commit d9d7569

Please sign in to comment.