Search in sources :

Example 1 with PEPeerManagerStats

use of com.biglybt.core.peer.PEPeerManagerStats in project BiglyBT by BiglySoftware.

the class DownloadManagerRateController method update.

static void update() {
    tick_count++;
    if ((!enable_limit_handling) || pm_map.size() == 0 || NetworkManager.isSeedingOnlyUploadRate() || NetworkManager.getMaxUploadRateBPSNormal() != 0 || core == null || speed_manager == null || speed_manager.getSpeedTester() == null) {
        rate_limit = 0;
        return;
    }
    int num_complete = 0;
    int num_incomplete = 0;
    int num_interesting = 0;
    int i_up_total = 0;
    int c_up_total = 0;
    long mono_now = SystemTime.getMonotonousTime();
    for (Map.Entry<PEPeerManager, PMState> entry : pm_map.entrySet()) {
        PEPeerManager pm = entry.getKey();
        PMState state = entry.getValue();
        boolean is_complete = !pm.hasDownloadablePiece();
        PEPeerManagerStats pm_stats = pm.getStats();
        long up_bytes = pm_stats.getTotalDataBytesSentNoLan() + pm_stats.getTotalProtocolBytesSentNoLan();
        long diff = state.setBytesUp(up_bytes);
        if (is_complete) {
            num_complete++;
            c_up_total += diff;
        } else {
            num_incomplete++;
            i_up_total += diff;
            if (state.isInteresting(mono_now)) {
                num_interesting++;
            }
        }
        if (state.isComplete() != is_complete) {
            if (is_complete) {
                pm.addRateLimiter(limiter, true);
            } else {
                pm.removeRateLimiter(limiter, true);
            }
            state.setComplete(is_complete);
        }
    }
    if (num_incomplete == 0 || num_complete == 0 || num_interesting == 0) {
        rate_limit = 0;
        return;
    }
    boolean skipped_tick = false;
    if (last_tick_processed != tick_count - 1) {
        pm_last_bad_limit = 0;
        latest_choke = 0;
        wait_until_tick = 0;
        ticks_to_sample_start = 0;
        sample_num = 0;
        incomplete_samples = 0;
        complete_samples = 0;
        skipped_tick = true;
    }
    last_tick_processed = tick_count;
    if (skipped_tick || tick_count < wait_until_tick) {
        return;
    }
    try {
        long real_now = SystemTime.getCurrentTime();
        SpeedManagerPingMapper mapper = speed_manager.getActiveMapper();
        if (rate_limit == 0) {
            rate_limit = speed_manager.getEstimatedUploadCapacityBytesPerSec().getBytesPerSec();
            if (rate_limit == 0) {
                rate_limit = DEFAULT_UP_LIMIT;
            }
        }
        SpeedManagerLimitEstimate last_bad = mapper.getLastBadUploadLimit();
        if (last_bad != null) {
            int last_bad_limit = last_bad.getBytesPerSec();
            if (last_bad_limit != pm_last_bad_limit) {
                pm_last_bad_limit = last_bad_limit;
                SpeedManagerLimitEstimate[] bad_ups = mapper.getBadUploadHistory();
                int total = last_bad.getBytesPerSec();
                int count = 1;
                for (SpeedManagerLimitEstimate bad : bad_ups) {
                    long t = bad.getWhen();
                    if (real_now - t <= 30 * 1000 && bad.getBytesPerSec() != last_bad_limit) {
                        total += bad.getBytesPerSec();
                        count++;
                    }
                }
                latest_choke = total / count;
                int new_rate_limit;
                if (rate_limit == 0) {
                    new_rate_limit = latest_choke / 2;
                } else {
                    new_rate_limit = rate_limit / 2;
                }
                if (new_rate_limit < slack_bytes_per_sec) {
                    new_rate_limit = slack_bytes_per_sec;
                }
                rate_limit = new_rate_limit;
                wait_until_tick = tick_count + WAIT_AFTER_CHOKE_TICKS;
                ticks_to_sample_start = 0;
                sample_num = 0;
                complete_samples = 0;
                incomplete_samples = 0;
                last_rate_limit = 0;
                return;
            }
        }
        if (ticks_to_sample_start > 0) {
            ticks_to_sample_start--;
        } else if (sample_num < SAMPLE_COUNT) {
            complete_samples += c_up_total;
            incomplete_samples += i_up_total;
            sample_num++;
        } else {
            double incomplete_average = incomplete_samples / SAMPLE_COUNT;
            double complete_average = complete_samples / SAMPLE_COUNT;
            double overall_average = (complete_samples + incomplete_samples) / SAMPLE_COUNT;
            int action = -1;
            try {
                if (last_rate_limit == 0) {
                    action = 1;
                } else {
                    double overall_change = overall_average - last_overall_average;
                    if (overall_change < 0) {
                        if (rate_limit < last_rate_limit) {
                            // System.out.println( "average decreased" );
                            action = 1;
                        } else {
                            action = 0;
                        }
                    } else {
                        double last_ratio = last_incomplete_average / last_complete_average;
                        double ratio = incomplete_average / complete_average;
                        if (rate_limit < last_rate_limit && ratio >= last_ratio) {
                            action = -1;
                        } else if (rate_limit > last_rate_limit && ratio <= last_ratio) {
                            double i_up_change = incomplete_average - last_incomplete_average;
                            if (i_up_change >= 1024) {
                                action = -1;
                            } else {
                                action = 1;
                            }
                        } else {
                            action = 1;
                        }
                    }
                }
            } finally {
                int new_rate_limit;
                if (action > 0) {
                    int ceiling = latest_choke == 0 ? DEFAULT_UP_LIMIT : latest_choke;
                    int diff = (ceiling - rate_limit) / 4;
                    if (diff > MAX_UP_DIFF) {
                        diff = MAX_UP_DIFF;
                    } else if (diff < MIN_DIFF) {
                        diff = MIN_DIFF;
                    }
                    new_rate_limit = rate_limit + diff;
                    if (new_rate_limit > 100 * 1024 * 1024) {
                        new_rate_limit = 100 * 1024 * 1024;
                    }
                } else if (action < 0) {
                    int diff = rate_limit / 5;
                    if (diff > MAX_DOWN_DIFF) {
                        diff = MAX_DOWN_DIFF;
                    } else if (diff < MIN_DIFF) {
                        diff = MIN_DIFF;
                    }
                    new_rate_limit = rate_limit - diff;
                    if (new_rate_limit < slack_bytes_per_sec) {
                        new_rate_limit = slack_bytes_per_sec;
                    }
                } else {
                    new_rate_limit = rate_limit;
                }
                last_rate_limit = rate_limit;
                last_overall_average = overall_average;
                last_complete_average = complete_average;
                last_incomplete_average = incomplete_average;
                rate_limit = new_rate_limit;
                sample_num = 0;
                complete_samples = 0;
                incomplete_samples = 0;
            }
        }
    } finally {
    // System.out.println( "rate=" + DisplayFormatters.formatByteCountToKiBEtcPerSec( rate_limit ) + ", last_choke=" + latest_choke );
    }
}
Also used : SpeedManagerLimitEstimate(com.biglybt.core.speedmanager.SpeedManagerLimitEstimate) PEPeerManagerStats(com.biglybt.core.peer.PEPeerManagerStats) SpeedManagerPingMapper(com.biglybt.core.speedmanager.SpeedManagerPingMapper) PEPeerManager(com.biglybt.core.peer.PEPeerManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with PEPeerManagerStats

use of com.biglybt.core.peer.PEPeerManagerStats in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method timerTick.

protected void timerTick(int tick_count) {
    if (tick_count % 15 == 0) {
        checkShareRatioProgress();
    }
    if (!history_retention_required) {
        return;
    }
    PEPeerManager pm = download_manager.getPeerManager();
    if (pm == null) {
        return;
    }
    PEPeerManagerStats stats = pm.getStats();
    long send_rate = stats.getDataSendRate() + stats.getProtocolSendRate();
    long receive_rate = stats.getDataReceiveRate() + stats.getProtocolReceiveRate();
    long peer_swarm_average = getTotalAveragePerPeer();
    long entry = ((((send_rate - 1 + HISTORY_DIV / 2) / HISTORY_DIV) << 42) & 0x7ffffc0000000000L) | ((((receive_rate - 1 + HISTORY_DIV / 2) / HISTORY_DIV) << 21) & 0x000003ffffe00000L) | ((((peer_swarm_average - 1 + HISTORY_DIV / 2) / HISTORY_DIV)) & 0x00000000001fffffL);
    synchronized (this) {
        if (history != null) {
            history[history_pos++] = entry;
            if (history_pos == HISTORY_MAX_SECS) {
                history_pos = 0;
                history_wrapped = true;
            }
        }
    }
}
Also used : PEPeerManagerStats(com.biglybt.core.peer.PEPeerManagerStats) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 3 with PEPeerManagerStats

use of com.biglybt.core.peer.PEPeerManagerStats in project BiglyBT by BiglySoftware.

the class Show method execute.

@Override
public void execute(String commandName, ConsoleInput ci, List args) {
    if (args.isEmpty()) {
        printHelp(ci.out, args);
        return;
    }
    String subCommand = (String) args.remove(0);
    if (subCommand.equalsIgnoreCase("options") || subCommand.equalsIgnoreCase("o")) {
        ci.invokeCommand("set", null);
    } else if (subCommand.equalsIgnoreCase("files") || subCommand.equalsIgnoreCase("f")) {
        ci.invokeCommand("add", Arrays.asList(new String[] { "--list" }));
    } else if (subCommand.equalsIgnoreCase("torrents") || subCommand.equalsIgnoreCase("t")) {
        ci.out.println("> -----");
        ci.torrents.clear();
        ci.torrents.addAll(ci.getGlobalManager().getDownloadManagers());
        Collections.sort(ci.torrents, new TorrentComparator());
        if (ci.torrents.isEmpty()) {
            ci.out.println("No Torrents");
            ci.out.println("> -----");
            return;
        }
        long totalReceived = 0;
        long totalSent = 0;
        long totalDiscarded = 0;
        int connectedSeeds = 0;
        int connectedPeers = 0;
        PEPeerManagerStats ps;
        boolean bShowOnlyActive = false;
        boolean bShowOnlyComplete = false;
        boolean bShowOnlyIncomplete = false;
        boolean bShowOnlyTransferring = false;
        int bShowDeadForDays = 0;
        for (ListIterator<String> iter = args.listIterator(); iter.hasNext(); ) {
            String arg = (String) iter.next();
            if ("active".equalsIgnoreCase(arg) || "a".equalsIgnoreCase(arg)) {
                bShowOnlyActive = true;
                iter.remove();
            } else if ("complete".equalsIgnoreCase(arg) || "c".equalsIgnoreCase(arg)) {
                bShowOnlyComplete = true;
                iter.remove();
            } else if ("incomplete".equalsIgnoreCase(arg) || "i".equalsIgnoreCase(arg)) {
                bShowOnlyIncomplete = true;
                iter.remove();
            } else if ("transferring".equalsIgnoreCase(arg) || "x".equalsIgnoreCase(arg)) {
                bShowOnlyTransferring = true;
                bShowOnlyActive = true;
                iter.remove();
            } else if ("dead".equalsIgnoreCase(arg) || "d".equalsIgnoreCase(arg)) {
                iter.remove();
                // default 1 week
                bShowDeadForDays = 7;
                if (iter.hasNext()) {
                    String days = iter.next();
                    try {
                        bShowDeadForDays = Integer.parseInt(days);
                        iter.remove();
                    } catch (Throwable e) {
                        iter.previous();
                    }
                }
            }
        }
        Iterator torrent;
        if (args.size() > 0) {
            List matchedTorrents = new TorrentFilter().getTorrents(ci.torrents, args);
            torrent = matchedTorrents.iterator();
        } else
            torrent = ci.torrents.iterator();
        List shown_torrents = new ArrayList();
        while (torrent.hasNext()) {
            DownloadManager dm = (DownloadManager) torrent.next();
            DownloadManagerStats stats = dm.getStats();
            boolean bDownloadCompleted = stats.getDownloadCompleted(false) == 1000;
            boolean bCanShow = ((bShowOnlyComplete == bShowOnlyIncomplete) || (bDownloadCompleted && bShowOnlyComplete) || (!bDownloadCompleted && bShowOnlyIncomplete));
            if (bCanShow && bShowOnlyActive) {
                int dmstate = dm.getState();
                bCanShow = (dmstate == DownloadManager.STATE_SEEDING) || (dmstate == DownloadManager.STATE_DOWNLOADING) || (dmstate == DownloadManager.STATE_CHECKING) || (dmstate == DownloadManager.STATE_INITIALIZING) || (dmstate == DownloadManager.STATE_ALLOCATING);
            }
            if (bCanShow && bShowOnlyTransferring) {
                try {
                    ps = dm.getPeerManager().getStats();
                    bCanShow = ps.getDataSendRate() > 0 || ps.getDataReceiveRate() > 0;
                } catch (Exception e) {
                }
            }
            if (bCanShow && bShowDeadForDays > 0) {
                int dmstate = dm.getState();
                bCanShow = false;
                if (dmstate == DownloadManager.STATE_SEEDING || (bDownloadCompleted && (dmstate == DownloadManager.STATE_QUEUED || dmstate == DownloadManager.STATE_STOPPED))) {
                    long seeding_secs = stats.getSecondsOnlySeeding();
                    long seeding_days = seeding_secs / (24 * 60 * 60);
                    if (seeding_days >= bShowDeadForDays) {
                        int secs_since_last_up = stats.getTimeSinceLastDataSentInSeconds();
                        if (secs_since_last_up == -1) {
                            // never uploaded
                            bCanShow = true;
                        } else {
                            int days_since_last_up = secs_since_last_up / (24 * 60 * 60);
                            if (days_since_last_up >= bShowDeadForDays) {
                                bCanShow = true;
                            }
                        }
                    }
                }
            }
            if (bCanShow) {
                shown_torrents.add(dm);
                try {
                    PEPeerManager pm = dm.getPeerManager();
                    ps = pm == null ? null : pm.getStats();
                } catch (Exception e) {
                    ps = null;
                }
                if (ps != null) {
                    totalReceived += dm.getStats().getTotalDataBytesReceived();
                    totalSent += dm.getStats().getTotalDataBytesSent();
                    totalDiscarded += ps.getTotalDiscarded();
                    connectedSeeds += dm.getNbSeeds();
                    connectedPeers += dm.getNbPeers();
                }
                ci.out.print(((shown_torrents.size() < 10) ? " " : "") + shown_torrents.size() + " ");
                ci.out.println(getTorrentSummary(dm));
                ci.out.println();
            }
        }
        ci.torrents.clear();
        ci.torrents.addAll(shown_torrents);
        GlobalManager gm = ci.getGlobalManager();
        ci.out.println("Total Speed (down/up): " + DisplayFormatters.formatByteCountToKiBEtcPerSec(gm.getStats().getDataReceiveRate() + gm.getStats().getProtocolReceiveRate()) + " / " + DisplayFormatters.formatByteCountToKiBEtcPerSec(gm.getStats().getDataSendRate() + gm.getStats().getProtocolSendRate()));
        ci.out.println("Transferred Volume (down/up/discarded): " + DisplayFormatters.formatByteCountToKiBEtc(totalReceived) + " / " + DisplayFormatters.formatByteCountToKiBEtc(totalSent) + " / " + DisplayFormatters.formatByteCountToKiBEtc(totalDiscarded));
        ci.out.println("Total Connected Peers (seeds/peers): " + Integer.toString(connectedSeeds) + " / " + Integer.toString(connectedPeers));
        ci.out.println("> -----");
    } else if (subCommand.equalsIgnoreCase("dht") || subCommand.equalsIgnoreCase("d")) {
        showDHTStats(ci);
    } else if (subCommand.equalsIgnoreCase("nat") || subCommand.equalsIgnoreCase("n")) {
        IndentWriter iw = new IndentWriter(new PrintWriter(ci.out));
        iw.setForce(true);
        NetworkAdmin.getSingleton().logNATStatus(iw);
    } else if (subCommand.equalsIgnoreCase("stats") || subCommand.equalsIgnoreCase("s")) {
        String pattern = CoreStats.ST_ALL;
        if (args.size() > 0) {
            pattern = (String) args.get(0);
            if (pattern.equals("*")) {
                pattern = ".*";
            }
        }
        if (args.size() > 1) {
            CoreStats.setEnableAverages(((String) args.get(1)).equalsIgnoreCase("on"));
        }
        java.util.Set types = new HashSet();
        types.add(pattern);
        Map reply = CoreStats.getStats(types);
        Iterator it = reply.entrySet().iterator();
        List lines = new ArrayList();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            lines.add(entry.getKey() + " -> " + entry.getValue());
        }
        Collections.sort(lines);
        for (int i = 0; i < lines.size(); i++) {
            ci.out.println(lines.get(i));
        }
    } else if (subCommand.equalsIgnoreCase("diag") || subCommand.equalsIgnoreCase("z")) {
        try {
            ci.out.println("Writing diagnostics to file 'az.diag'");
            FileWriter fw = new FileWriter("az.diag");
            PrintWriter pw = new PrintWriter(fw);
            AEDiagnostics.generateEvidence(pw);
            pw.flush();
            fw.close();
        } catch (Throwable e) {
            ci.out.println(e);
        }
    } else {
        if ((ci.torrents == null) || (ci.torrents != null) && ci.torrents.isEmpty()) {
            ci.out.println("> Command 'show': No torrents in list (try 'show torrents' first).");
            return;
        }
        try {
            int number = Integer.parseInt(subCommand);
            if ((number == 0) || (number > ci.torrents.size())) {
                ci.out.println("> Command 'show': Torrent #" + number + " unknown.");
                return;
            }
            DownloadManager dm = (DownloadManager) ci.torrents.get(number - 1);
            printTorrentDetails(ci.out, dm, number, args);
        } catch (Exception e) {
            ci.out.println("> Command 'show': Subcommand '" + subCommand + "' unknown.");
            return;
        }
    }
}
Also used : IndentWriter(com.biglybt.core.util.IndentWriter) HashSet(java.util.HashSet) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) DownloadManager(com.biglybt.core.download.DownloadManager) GlobalManager(com.biglybt.core.global.GlobalManager) ListIterator(java.util.ListIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) DownloadManagerStats(com.biglybt.core.download.DownloadManagerStats) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) PEPeerManagerStats(com.biglybt.core.peer.PEPeerManagerStats) PEPeerManager(com.biglybt.core.peer.PEPeerManager) Map(java.util.Map)

Aggregations

PEPeerManager (com.biglybt.core.peer.PEPeerManager)3 PEPeerManagerStats (com.biglybt.core.peer.PEPeerManagerStats)3 Map (java.util.Map)2 DownloadManager (com.biglybt.core.download.DownloadManager)1 DownloadManagerStats (com.biglybt.core.download.DownloadManagerStats)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 SpeedManagerLimitEstimate (com.biglybt.core.speedmanager.SpeedManagerLimitEstimate)1 SpeedManagerPingMapper (com.biglybt.core.speedmanager.SpeedManagerPingMapper)1 IndentWriter (com.biglybt.core.util.IndentWriter)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1