2014-07-09 00:37:13 +02:00
|
|
|
#include "session_view.hpp"
|
|
|
|
#include "print.hpp"
|
|
|
|
|
2015-04-26 15:38:56 +02:00
|
|
|
#include <algorithm> // for std::max
|
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
session_view::session_view()
|
2015-05-05 04:32:14 +02:00
|
|
|
: m_position(0)
|
|
|
|
, m_print_utp_stats(false)
|
2014-07-09 00:37:13 +02:00
|
|
|
{
|
2014-08-01 08:07:48 +02:00
|
|
|
using libtorrent::find_metric_idx;
|
|
|
|
|
2014-07-10 10:32:32 +02:00
|
|
|
m_width = 128;
|
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
std::vector<lt::stats_metric> metrics = lt::session_stats_metrics();
|
|
|
|
m_cnt[0].resize(metrics.size(), 0);
|
|
|
|
m_cnt[1].resize(metrics.size(), 0);
|
|
|
|
|
2014-08-01 08:07:48 +02:00
|
|
|
m_queued_bytes_idx = find_metric_idx("disk.queued_write_bytes");
|
|
|
|
m_wasted_bytes_idx = find_metric_idx("net.recv_redundant_bytes");
|
|
|
|
m_failed_bytes_idx = find_metric_idx("net.recv_failed_bytes");
|
|
|
|
m_num_peers_idx = find_metric_idx("peer.num_peers_connected");
|
|
|
|
m_recv_payload_idx = find_metric_idx("net.recv_payload_bytes");
|
|
|
|
m_sent_payload_idx = find_metric_idx("net.sent_payload_bytes");
|
|
|
|
m_unchoked_idx = find_metric_idx("peer.num_peers_up_unchoked");
|
|
|
|
m_unchoke_slots_idx = find_metric_idx("ses.num_unchoke_slots");
|
|
|
|
m_limiter_up_queue_idx = find_metric_idx("net.limiter_up_queue");
|
|
|
|
m_limiter_down_queue_idx = find_metric_idx("net.limiter_down_queue");
|
|
|
|
m_queued_writes_idx = find_metric_idx("disk.num_write_jobs");
|
|
|
|
m_queued_reads_idx = find_metric_idx("disk.num_read_jobs");
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2014-08-01 08:07:48 +02:00
|
|
|
m_writes_cache_idx = find_metric_idx("disk.write_cache_blocks");
|
|
|
|
m_reads_cache_idx = find_metric_idx("disk.read_cache_blocks");
|
|
|
|
m_pinned_idx = find_metric_idx("disk.pinned_blocks");
|
|
|
|
m_num_blocks_read_idx = find_metric_idx("disk.num_blocks_read");
|
|
|
|
m_cache_hit_idx = find_metric_idx("disk.num_blocks_cache_hits");
|
|
|
|
m_blocks_in_use_idx = find_metric_idx("disk.disk_blocks_in_use");
|
|
|
|
m_blocks_written_idx = find_metric_idx("disk.num_blocks_written");
|
|
|
|
m_write_ops_idx = find_metric_idx("disk.num_write_ops");
|
2014-07-10 10:32:32 +02:00
|
|
|
|
2014-08-01 08:07:48 +02:00
|
|
|
m_mfu_size_idx = find_metric_idx("disk.arc_mfu_size");
|
|
|
|
m_mfu_ghost_idx = find_metric_idx("disk.arc_mfu_ghost_size");
|
|
|
|
m_mru_size_idx = find_metric_idx("disk.arc_mru_size");
|
|
|
|
m_mru_ghost_idx = find_metric_idx("disk.arc_mru_ghost_size");
|
2015-01-04 22:31:02 +01:00
|
|
|
|
|
|
|
m_utp_idle = find_metric_idx("utp.num_utp_idle");
|
|
|
|
m_utp_syn_sent = find_metric_idx("utp.num_utp_syn_sent");
|
|
|
|
m_utp_connected = find_metric_idx("utp.num_utp_connected");
|
|
|
|
m_utp_fin_sent = find_metric_idx("utp.num_utp_fin_sent");
|
|
|
|
m_utp_close_wait = find_metric_idx("utp.num_utp_close_wait");
|
2014-07-09 00:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_view::set_pos(int pos)
|
|
|
|
{
|
|
|
|
m_position = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_view::pos() const { return m_position; }
|
|
|
|
|
|
|
|
int session_view::height() const
|
|
|
|
{
|
2015-01-04 22:31:02 +01:00
|
|
|
return 3 + m_print_utp_stats;
|
2014-07-09 00:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void session_view::render()
|
|
|
|
{
|
|
|
|
char str[1024];
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
int y = m_position;
|
|
|
|
|
|
|
|
float seconds = (m_timestamp[0] - m_timestamp[1]) / 1000000.f;
|
|
|
|
|
2016-04-20 06:45:32 +02:00
|
|
|
int download_rate = int((m_cnt[0][m_recv_payload_idx] - m_cnt[1][m_recv_payload_idx])
|
|
|
|
/ seconds);
|
|
|
|
int upload_rate = int((m_cnt[0][m_sent_payload_idx] - m_cnt[1][m_sent_payload_idx])
|
|
|
|
/ seconds);
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
pos += std::snprintf(str, sizeof(str), "%s%s fail: %s down: %s (%s) "
|
2014-07-10 10:32:32 +02:00
|
|
|
" bw queue: %s | %s conns: %3d unchoked: %2d / %2d "
|
|
|
|
" %s\x1b[K"
|
|
|
|
, esc("48;5;238")
|
|
|
|
, esc("1")
|
|
|
|
, add_suffix(m_cnt[0][m_failed_bytes_idx]).c_str()
|
2014-07-09 00:37:13 +02:00
|
|
|
, color(add_suffix(download_rate, "/s"), col_green).c_str()
|
|
|
|
, color(add_suffix(m_cnt[0][m_recv_payload_idx]), col_green).c_str()
|
2016-04-20 06:45:32 +02:00
|
|
|
, color(to_string(int(m_cnt[0][m_limiter_up_queue_idx]), 3), col_red).c_str()
|
|
|
|
, color(to_string(int(m_cnt[0][m_limiter_down_queue_idx]), 3), col_green).c_str()
|
2014-07-10 10:32:32 +02:00
|
|
|
, int(m_cnt[0][m_num_peers_idx])
|
|
|
|
, int(m_cnt[0][m_unchoked_idx])
|
|
|
|
, int(m_cnt[0][m_unchoke_slots_idx])
|
|
|
|
, esc("0"));
|
2014-07-09 00:37:13 +02:00
|
|
|
|
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "%s%swaste: %s up: %s (%s) "
|
2014-07-10 10:32:32 +02:00
|
|
|
"disk queue: %s | %s cache w: %3d%% r: %3d%% "
|
|
|
|
"size: w: %s r: %s total: %s %s\x1b[K"
|
2016-02-27 23:41:18 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
, esc("40")
|
|
|
|
#else
|
2014-07-10 10:32:32 +02:00
|
|
|
, esc("48;5;238")
|
2016-02-27 23:41:18 +01:00
|
|
|
#endif
|
2014-07-10 10:32:32 +02:00
|
|
|
, esc("1")
|
2014-07-09 00:37:13 +02:00
|
|
|
, add_suffix(m_cnt[0][m_wasted_bytes_idx]).c_str()
|
2014-07-10 10:32:32 +02:00
|
|
|
, color(add_suffix(upload_rate, "/s"), col_red).c_str()
|
|
|
|
, color(add_suffix(m_cnt[0][m_sent_payload_idx]), col_red).c_str()
|
2016-04-20 06:45:32 +02:00
|
|
|
, color(to_string(int(m_cnt[0][m_queued_reads_idx]), 3), col_red).c_str()
|
|
|
|
, color(to_string(int(m_cnt[0][m_queued_writes_idx]), 3), col_green).c_str()
|
2014-07-09 00:37:13 +02:00
|
|
|
, int((m_cnt[0][m_blocks_written_idx] - m_cnt[0][m_write_ops_idx]) * 100
|
2016-10-16 09:12:41 +02:00
|
|
|
/ (std::max)(std::int64_t(1), m_cnt[0][m_blocks_written_idx]))
|
2014-07-09 00:37:13 +02:00
|
|
|
, int(m_cnt[0][m_cache_hit_idx] * 100
|
2016-10-16 09:12:41 +02:00
|
|
|
/ (std::max)(std::int64_t(1), m_cnt[0][m_num_blocks_read_idx]))
|
2014-07-09 00:37:13 +02:00
|
|
|
, add_suffix(m_cnt[0][m_writes_cache_idx] * 16 * 1024).c_str()
|
|
|
|
, add_suffix(m_cnt[0][m_reads_cache_idx] * 16 * 1024).c_str()
|
|
|
|
, add_suffix(m_cnt[0][m_blocks_in_use_idx] * 16 * 1024).c_str()
|
2014-07-10 10:32:32 +02:00
|
|
|
, esc("0"));
|
2014-07-09 00:37:13 +02:00
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
|
|
|
|
|
|
|
/*
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "| timing - "
|
2014-07-09 00:37:13 +02:00
|
|
|
" read: %6d ms | write: %6d ms | hash: %6d"
|
|
|
|
, cs.average_read_time / 1000, cs.average_write_time / 1000
|
|
|
|
, cs.average_hash_time / 1000);
|
|
|
|
|
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "| jobs - queued: %4d (%4d) pending: %4d blocked: %4d "
|
2014-07-09 00:37:13 +02:00
|
|
|
"queued-bytes: %5" PRId64 " kB"
|
|
|
|
, cs.queued_jobs, cs.peak_queued, cs.pending_jobs, cs.blocked_jobs
|
|
|
|
, m_cnt[0][m_queued_bytes_idx] / 1000);
|
|
|
|
|
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "| cache - total: %4d read: %4d write: %4d pinned: %4d write-queue: %4d"
|
2014-07-09 00:37:13 +02:00
|
|
|
, cs.read_cache_size + cs.write_cache_size, cs.read_cache_size
|
|
|
|
, cs.write_cache_size, cs.pinned_blocks
|
|
|
|
, int(m_cnt[0][m_queued_bytes_idx] / 0x4000));
|
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
2014-07-10 10:32:32 +02:00
|
|
|
*/
|
|
|
|
int mru_size = int(m_cnt[0][m_mru_size_idx] + m_cnt[0][m_mru_ghost_idx]);
|
|
|
|
int mfu_size = int(m_cnt[0][m_mfu_size_idx] + m_cnt[0][m_mfu_ghost_idx]);
|
2014-07-09 00:37:13 +02:00
|
|
|
int arc_size = mru_size + mfu_size;
|
|
|
|
|
2014-07-10 10:32:32 +02:00
|
|
|
char mru_caption[100];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(mru_caption, sizeof(mru_caption), "MRU: %d (%d)"
|
2014-07-10 10:32:32 +02:00
|
|
|
, int(m_cnt[0][m_mru_size_idx]), int(m_cnt[0][m_mru_ghost_idx]));
|
|
|
|
char mfu_caption[100];
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(mfu_caption, sizeof(mfu_caption), "MFU: %d (%d)"
|
2014-07-10 10:32:32 +02:00
|
|
|
, int(m_cnt[0][m_mfu_size_idx]), int(m_cnt[0][m_mfu_ghost_idx]));
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
pos = std::snprintf(str, sizeof(str), "cache: ");
|
2014-07-09 00:37:13 +02:00
|
|
|
if (arc_size > 0)
|
|
|
|
{
|
|
|
|
if (mru_size > 0)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
pos += std::snprintf(str + pos, sizeof(str) - pos, "%s"
|
2016-04-20 06:45:32 +02:00
|
|
|
, progress_bar(int(m_cnt[0][m_mru_ghost_idx] * 1000 / mru_size)
|
2014-07-10 10:32:32 +02:00
|
|
|
, mru_size * (m_width-8) / arc_size, col_yellow, '-', '#'
|
|
|
|
, mru_caption, progress_invert).c_str());
|
2014-07-09 00:37:13 +02:00
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
pos += std::snprintf(str + pos, sizeof(str) - pos, "|");
|
2014-07-09 00:37:13 +02:00
|
|
|
if (mfu_size)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
pos += std::snprintf(str + pos, sizeof(str) - pos, "%s"
|
2016-04-20 06:45:32 +02:00
|
|
|
, progress_bar(int(m_cnt[0][m_mfu_size_idx] * 1000 / mfu_size)
|
2014-07-10 10:32:32 +02:00
|
|
|
, mfu_size * (m_width-8) / arc_size, col_green, '#', '-'
|
|
|
|
, mfu_caption).c_str());
|
2014-07-09 00:37:13 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-17 15:24:06 +02:00
|
|
|
pos += std::snprintf(str + pos, sizeof(str) - pos, "\x1b[K");
|
2014-07-09 00:37:13 +02:00
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
2015-01-04 22:31:02 +01:00
|
|
|
|
|
|
|
if (m_print_utp_stats)
|
|
|
|
{
|
2016-05-17 15:24:06 +02:00
|
|
|
std::snprintf(str, sizeof(str), "uTP idle: %d syn: %d est: %d fin: %d wait: %d\x1b[K"
|
2015-01-04 22:31:02 +01:00
|
|
|
, int(m_cnt[0][m_utp_idle])
|
|
|
|
, int(m_cnt[0][m_utp_syn_sent])
|
|
|
|
, int(m_cnt[0][m_utp_connected])
|
|
|
|
, int(m_cnt[0][m_utp_fin_sent])
|
|
|
|
, int(m_cnt[0][m_utp_close_wait]));
|
|
|
|
set_cursor_pos(0, y++);
|
|
|
|
print(str);
|
|
|
|
}
|
2014-07-09 00:37:13 +02:00
|
|
|
}
|
|
|
|
|
2016-10-16 09:12:41 +02:00
|
|
|
void session_view::update_counters(std::int64_t* stats_counters
|
2016-06-18 20:01:38 +02:00
|
|
|
, int num_cnt, std::uint64_t t)
|
2014-07-09 00:37:13 +02:00
|
|
|
{
|
|
|
|
// only update the previous counters if there's been enough
|
|
|
|
// time since it was last updated
|
|
|
|
if (t - m_timestamp[1] > 2000000)
|
|
|
|
{
|
|
|
|
m_cnt[1].swap(m_cnt[0]);
|
|
|
|
m_timestamp[1] = m_timestamp[0];
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
m_cnt[0].assign(stats_counters, stats_counters + num_cnt);
|
2014-07-09 00:37:13 +02:00
|
|
|
m_timestamp[0] = t;
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|