Skip to content

Commit 0cce381

Browse files
committed
Throttle working.
git-svn-id: svn://rakshasa.no/libtorrent/trunk/libtorrent@9 e378c898-3ddf-0310-93e7-cc216c733640
1 parent 296e0b4 commit 0cce381

16 files changed

+222
-93
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
If you want news, watch CNN or something.

TODO

+2
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ fix socketBase setAsync
4141
We're counting bytes we haven't downloaded, fixme.
4242

4343
Recursively update the up/down rates in throttle.
44+
45+
Implement min size of chunks sent in peer_connection

client/README

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ If your system doesn't have "execinfo.h", uncomment "#define
1010
USE_EXECINFO" in rtorrent.cc. This will be handled by an autoconf
1111
script later.
1212

13+
All:
14+
15+
Ctrl-C - Quit
16+
17+
A - Increase Throttle by 1 KiB
18+
Z - Decrease Throttle by 1 KiB
19+
20+
S - Increase Throttle by 5 KiB
21+
X - Decrease Throttle by 5 KiB
22+
23+
D - Increase Throttle by 50 KiB
24+
C - Decrease Throttle by 50 KiB
25+
26+
1327
Main:
1428

1529
Up/Down - Select a download

client/display.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ void Display::drawDownloads(torrent::DList::const_iterator mark) {
7575
torrent::get(first, torrent::TRACKER_MSG).c_str());
7676
}
7777

78-
mvprintw(maxY - 1, 0, "Port: %i Handshakes: %i Loops: %i",
78+
mvprintw(maxY - 1, 0, "Port: %i Handshakes: %i Throttle: %i KiB Loops: %i",
7979
(int)torrent::get(torrent::LISTEN_PORT),
8080
(int)torrent::get(torrent::HANDSHAKES_TOTAL),
81+
(int)torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) / 1000,
8182
loops);
8283

8384
refresh();

client/download.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@ void Download::draw() {
5757
break;
5858
}
5959

60-
mvprintw(maxY - 3, 0, "Torrent: %.1f / %.1f MiB Rate:%5.1f /%5.1f KiB Uploaded: %.1f MiB",
60+
mvprintw(maxY - 3, 0, "Torrent: %.1f / %.1f MiB Rate: %5.1f/%5.1f KiB Uploaded: %.1f MiB",
6161
(double)torrent::get(m_dItr, torrent::BYTES_DONE) / 1000000.0,
6262
(double)torrent::get(m_dItr, torrent::BYTES_TOTAL) / 1000000.0,
63-
(double)torrent::get(m_dItr, torrent::RATE_DOWN) / 1000.0,
6463
(double)torrent::get(m_dItr, torrent::RATE_UP) / 1000.0,
64+
(double)torrent::get(m_dItr, torrent::RATE_DOWN) / 1000.0,
6565
(double)torrent::get(m_dItr, torrent::BYTES_UPLOADED) / 1000000.0);
6666

6767

68-
mvprintw(maxY - 2, 0, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i",
68+
mvprintw(maxY - 2, 0, "Peers: %i(%i) Min/Max: %i/%i Uploads: %i Throttle: %i KiB",
6969
(int)torrent::get(m_dItr, torrent::PEERS_CONNECTED),
7070
(int)torrent::get(m_dItr, torrent::PEERS_NOT_CONNECTED),
7171
(int)torrent::get(m_dItr, torrent::PEERS_MIN),
7272
(int)torrent::get(m_dItr, torrent::PEERS_MAX),
73-
(int)torrent::get(m_dItr, torrent::UPLOADS_MAX));
73+
(int)torrent::get(m_dItr, torrent::UPLOADS_MAX),
74+
(int)torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) / 1000);
7475

7576
mvprintw(maxY - 1, 0, "Tracker: [%c:%i] %s",
7677
torrent::get(m_dItr, torrent::TRACKER_CONNECTING) ? 'C' : ' ',

client/rtorrent.cc

+66-34
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void signal_handler(int signum) {
3939
switch (signum) {
4040
case SIGINT:
4141
if (shutdown) {
42-
torrent::cleanup();
42+
//torrent::cleanup();
4343
exit(0);
4444
}
4545

@@ -173,53 +173,85 @@ int main(int argc, char** argv) {
173173
if (!FD_ISSET(0, &rset))
174174
continue;
175175

176-
switch (displayState) {
177-
case DISPLAY_MAIN:
178-
switch (getch()) {
179-
case KEY_DOWN:
180-
++curDownload;
176+
lastDraw -= (1 << 30);
177+
178+
int c = getch();
179+
int64_t constRate = torrent::get(torrent::THROTTLE_ROOT_CONST_RATE);
180+
181+
switch (c) {
182+
case 'a':
183+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate + 1000);
184+
break;
185+
186+
case 'z':
187+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate - 1000);
188+
break;
189+
190+
case 's':
191+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate + 5000);
192+
break;
193+
194+
case 'x':
195+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate - 5000);
196+
break;
197+
198+
case 'd':
199+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate + 50000);
200+
break;
201+
202+
case 'c':
203+
torrent::set(torrent::THROTTLE_ROOT_CONST_RATE, constRate - 50000);
204+
break;
205+
206+
default:
207+
switch (displayState) {
208+
case DISPLAY_MAIN:
209+
switch (c) {
210+
case KEY_DOWN:
211+
++curDownload;
181212

182-
break;
213+
break;
183214

184-
case KEY_UP:
185-
--curDownload;
215+
case KEY_UP:
216+
--curDownload;
186217

187-
break;
218+
break;
188219

189-
case KEY_RIGHT:
190-
if (curDownload != torrent::downloads().end()) {
191-
download = Download(curDownload);
192-
displayState = DISPLAY_DOWNLOAD;
220+
case KEY_RIGHT:
221+
if (curDownload != torrent::downloads().end()) {
222+
download = Download(curDownload);
223+
displayState = DISPLAY_DOWNLOAD;
224+
}
225+
226+
break;
227+
228+
case 'l':
229+
displayState = DISPLAY_LOG;
230+
break;
231+
232+
default:
233+
break;
193234
}
194235

195236
break;
196237

197-
case 'l':
198-
displayState = DISPLAY_LOG;
238+
case DISPLAY_DOWNLOAD:
239+
displayState = download.key(c) ? DISPLAY_DOWNLOAD : DISPLAY_MAIN;
199240
break;
200241

201-
default:
202-
break;
242+
case DISPLAY_LOG:
243+
switch (getch()) {
244+
case '\n':
245+
case ' ':
246+
displayState = DISPLAY_MAIN;
247+
break;
248+
default:
249+
break;
250+
}
203251
}
204252

205253
break;
206-
207-
case DISPLAY_DOWNLOAD:
208-
displayState = download.key(getch()) ? DISPLAY_DOWNLOAD : DISPLAY_MAIN;
209-
break;
210-
211-
case DISPLAY_LOG:
212-
switch (getch()) {
213-
case '\n':
214-
case ' ':
215-
displayState = DISPLAY_MAIN;
216-
break;
217-
default:
218-
break;
219-
}
220254
}
221-
222-
lastDraw -= (1 << 30);
223255
}
224256

225257
delete display;

torrent/Makefile.am

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ libtorrent_la_SOURCES = \
3232
peer_connection_extra.h \
3333
rate.cc \
3434
rate.h \
35-
service.cc \
36-
service.h \
3735
settings.cc \
3836
settings.h \
3937
socket_base.cc \
@@ -49,7 +47,9 @@ libtorrent_la_SOURCES = \
4947
tracker_query.cc \
5048
tracker_query.h \
5149
torrent.cc \
52-
torrent.h
50+
torrent.h \
51+
service.cc \
52+
service.h
5353

5454
libtorrentincludedir = $(includedir)/torrent
5555
libtorrentinclude_HEADERS = torrent.h exceptions.h

torrent/peer_connection.cc

+11-10
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,6 @@ void PeerConnection::parseReadBuf() {
525525
m_up.list.push_back(Piece(index, offset, length));
526526
insertWrite();
527527

528-
m_throttle.activate();
529-
530528
} else if (rItr != m_up.list.end()) {
531529

532530
if (m_up.state != WRITE_MSG || rItr != m_up.list.begin())
@@ -567,14 +565,6 @@ void PeerConnection::fillWriteBuf() {
567565
if (m_sendChoked) {
568566
m_sendChoked = false;
569567

570-
if (m_up.choked) {
571-
// Clear the request queue and mmaped chunk.
572-
m_up.list.clear();
573-
m_up.data = Chunk();
574-
575-
m_throttle.idle();
576-
}
577-
578568
if ((Timer::cache() - m_lastChoked).seconds() < 10) {
579569
// Wait with the choke message.
580570
insertService(m_lastChoked + 10 * 1000000,
@@ -585,6 +575,17 @@ void PeerConnection::fillWriteBuf() {
585575
bufCmd(m_up.choked ? CHOKE : UNCHOKE, 1);
586576

587577
m_lastChoked = Timer::cache();
578+
579+
if (m_up.choked) {
580+
// Clear the request queue and mmaped chunk.
581+
m_up.list.clear();
582+
m_up.data = Chunk();
583+
584+
m_throttle.idle();
585+
586+
} else {
587+
m_throttle.activate();
588+
}
588589
}
589590
}
590591

torrent/rate.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ class Rate {
2424

2525
// We don't need to clean up old entries here since bytes() is called
2626
// every 30 seconds by Download::CHOKE_CYCLE.
27-
m_entries.push_front(std::make_pair(Timer::cache(), bytes));
2827
m_bytes += bytes;
28+
29+
// Todo: Only create a new entry each seconds or something (right-shift the
30+
// time or something)
31+
m_entries.push_front(std::make_pair(Timer::cache(), bytes));
2932
}
3033

3134
bool operator < (Rate& r) {

torrent/settings.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ DownloadSettings::DownloadSettings() :
2727
chokeGracePeriod(55 * 1000000)
2828
{}
2929

30-
int ThrottleSettings::minPeriod = 500000;
30+
int ThrottleSettings::minPeriod = 1000000;
3131
int ThrottleSettings::wakeupPoint = 2048;
32+
int ThrottleSettings::starvePoint = 2048;
33+
int ThrottleSettings::starveBuffer = 1024;
34+
3235
int ThrottleSettings::minChunk = 512;
3336

3437
ThrottleSettings::ThrottleSettings() :

torrent/settings.h

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class ThrottleSettings {
4545

4646
static int minPeriod;
4747
static int wakeupPoint;
48+
49+
// If greater than 'starvePoint' bytes was left then limit the amount
50+
// allocated.
51+
static int starvePoint;
52+
53+
// Each turn give spent + starveBuffer bytes if not starved.
54+
static int starveBuffer;
55+
56+
// Min size of chunks to send out.
4857
static int minChunk;
4958
};
5059

0 commit comments

Comments
 (0)