From 4d74b51b252dc23d4cb9a886823f356c8d4d78d9 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 21 Jan 2015 17:01:10 +0100 Subject: [PATCH 1/5] cmd: sum up & display storage pieces memory usage --- bin/cmd.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index 05c0d896..4bbfe129 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -438,11 +438,14 @@ function drawTorrent (torrent) { '{green:blocked:} {bold:' + torrent.numBlockedPeers + '}' ) clivas.line('{80:}') - linesremaining -= 8 + linesremaining -= 9 var pieces = torrent.storage.pieces + var storageMem = 0 for (var i = 0; i < pieces.length; i++) { var piece = pieces[i] + if (piece.buffer) + storageMem += piece.buffer.length if (piece.verified || piece.blocksWritten === 0) { continue; } @@ -453,6 +456,9 @@ function drawTorrent (torrent) { clivas.line('{4+cyan:' + i + '} ' + bar); linesremaining -= 1 } + clivas.line( + '{red:storage mem:} {bold:' + Math.ceil(storageMem / 1024) + ' KB} ' + ) clivas.line('{80:}') linesremaining -= 1 From 5a70596d842ba47f0bcdaf7c720c0f671e135862 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 21 Jan 2015 17:26:00 +0100 Subject: [PATCH 2/5] fs-storage: rm thunky OS IO cache is way smarter than this stupid indefinite caching. --- lib/fs-storage.js | 10 +++++----- package.json | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/fs-storage.js b/lib/fs-storage.js index cd32d4e5..c3d5569c 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -9,7 +9,6 @@ var path = require('path') var raf = require('random-access-file') var rimraf = require('rimraf') var Storage = require('./storage') -var thunky = require('thunky') var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir() function noop () {} @@ -47,7 +46,7 @@ function FSStorage (parsedTorrent, opts) { var pieceLength = file.pieceLength var filePath = path.join(self.path, file.path) - var openWrite = thunky(function (cb) { + var openWrite = function (cb) { var fileDir = path.dirname(filePath) mkdirp(fileDir, function (err) { @@ -58,16 +57,16 @@ function FSStorage (parsedTorrent, opts) { file.fd = fd cb(null, fd) }) - }) + } - var openRead = thunky(function (cb) { + var openRead = function (cb) { // TODO: no need for fs.exists call, just try opening and handle error. // fs.exists then open creates opportunity for race condition. fs.exists(filePath, function (exists) { if (exists) return openWrite(cb) cb(self.nonExistentError) }) - }) + } file.pieces.forEach(function (piece) { var index = piece.index @@ -160,6 +159,7 @@ FSStorage.prototype._onPieceDone = function (piece) { var writeToNextFile = function (err) { if (err) return self.emit('error', err) if (i >= end) { + delete piece.buffer return cb() } diff --git a/package.json b/package.json index e5ff75e1..e91132ee 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "simple-get": "^1.0.0", "simple-sha1": "^2.0.0", "speedometer": "^0.1.2", - "thunky": "^0.1.0", "torrent-discovery": "^2.0.1", "ut_metadata": "^2.1.0", "ut_pex": "^1.0.1", From ce16c23c4ae99fe24de58b002760cf724ff9313c Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 21 Jan 2015 17:45:54 +0100 Subject: [PATCH 3/5] cmd: paint reserved blocks in blue --- bin/cmd.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index 4bbfe129..f1e695c8 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -451,7 +451,19 @@ function drawTorrent (torrent) { } var bar = '' for (var j = 0; j < piece.blocks.length; j++) { - bar += piece.blocks[j] ? '{green:█}' : '{red:█}'; + switch(piece.blocks[j]) { + case 0: + bar += '{red:█}'; + break; + case 1: + bar += '{blue:█}'; + break; + case 2: + bar += '{green:█}'; + break; + default: + throw 'Invalid block state: ' + piece.blocks[j] + } } clivas.line('{4+cyan:' + i + '} ' + bar); linesremaining -= 1 From 62f786c840b75d06b2f9928c893c7f66bf751eeb Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 22 Jan 2015 00:08:30 +0100 Subject: [PATCH 4/5] Revert "fs-storage: rm thunky" This reverts commit 5a70596d842ba47f0bcdaf7c720c0f671e135862. --- lib/fs-storage.js | 10 +++++----- package.json | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/fs-storage.js b/lib/fs-storage.js index c3d5569c..cd32d4e5 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -9,6 +9,7 @@ var path = require('path') var raf = require('random-access-file') var rimraf = require('rimraf') var Storage = require('./storage') +var thunky = require('thunky') var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir() function noop () {} @@ -46,7 +47,7 @@ function FSStorage (parsedTorrent, opts) { var pieceLength = file.pieceLength var filePath = path.join(self.path, file.path) - var openWrite = function (cb) { + var openWrite = thunky(function (cb) { var fileDir = path.dirname(filePath) mkdirp(fileDir, function (err) { @@ -57,16 +58,16 @@ function FSStorage (parsedTorrent, opts) { file.fd = fd cb(null, fd) }) - } + }) - var openRead = function (cb) { + var openRead = thunky(function (cb) { // TODO: no need for fs.exists call, just try opening and handle error. // fs.exists then open creates opportunity for race condition. fs.exists(filePath, function (exists) { if (exists) return openWrite(cb) cb(self.nonExistentError) }) - } + }) file.pieces.forEach(function (piece) { var index = piece.index @@ -159,7 +160,6 @@ FSStorage.prototype._onPieceDone = function (piece) { var writeToNextFile = function (err) { if (err) return self.emit('error', err) if (i >= end) { - delete piece.buffer return cb() } diff --git a/package.json b/package.json index e91132ee..e5ff75e1 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "simple-get": "^1.0.0", "simple-sha1": "^2.0.0", "speedometer": "^0.1.2", + "thunky": "^0.1.0", "torrent-discovery": "^2.0.1", "ut_metadata": "^2.1.0", "ut_pex": "^1.0.1", From 21f3092f36c2b70b221baea79d28889904f42eec Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 22 Jan 2015 00:08:57 +0100 Subject: [PATCH 5/5] FSStorage: free written piece buffers --- lib/fs-storage.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/fs-storage.js b/lib/fs-storage.js index cd32d4e5..6942e40e 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -160,6 +160,7 @@ FSStorage.prototype._onPieceDone = function (piece) { var writeToNextFile = function (err) { if (err) return self.emit('error', err) if (i >= end) { + delete piece.buffer return cb() }