282008/19: Completeness of the files of a torrent
28.1Problem
Recently popular way of file-sharing via the Internet is the use of torrents. One torrent allows many files (defined in the torrent) simultaneous (or interleaved) download, and for this reason most files are only partially (unusably) available.
It is reasonable to assume, that the downloading program which manages the torrent downloads the parts of files practically randomly. Consider a torrent that has many files of the same length. Based on the above assumption, give a theoretical prediction on what is the distribution of the length of the files as a function of a given average “global” readiness fraction (global fraction of total data to be downloaded).
Extend this theoretical relation, for the case when the torrent has many files with different length! Test the theory with comparing the prediction with actual measurements made on real torrent download, available at the following web address:
http://pkomar.web.elte.hu/Ortvay2008.html
(translated by Dezső Varga)
28.2Explanation
A torrent describes a set of files that a client program downloads from other computers. The data are transferred in pieces of equal size, and the client completes them in an order that we may treat as random. At a given moment a fraction \(g\) of all pieces is complete; we call \(g\) the global completion. Each file then has its own completeness \(x\), the fraction of its pieces that are complete. We have to find the distribution of \(x\) over the files, that is how many files are complete to a given degree, as a function of \(g\): first when all files have the same length, then when the lengths differ. The English translation asks for “the distribution of the length of the files”; the Hungarian original asks for the distribution of the files by completeness, and we answer that question.
The problem also asks us to compare the prediction with measurements. The web page given in the problem is no longer served, but the Internet Archive has a copy of it from 9 May 2024. The page listed eleven data files, recorded at global completions of about 10, 20, 30, 40, 50, 60, 70, 80, 85, 90 and 95%, and a summary spreadsheet. The data files were never archived, and by 2024 the spreadsheet link already returned a “not found” page. We could not find the measured data anywhere else, so we cannot carry out the comparison with them. Instead, we state precisely what a comparison has to check, and carry it out on simulated downloads of two invented torrents.
28.3Theory
Pieces. In the BitTorrent protocol the contents of all files of a torrent are joined into one long sequence of bytes, which is cut into pieces of equal size, typically a few hundred kilobytes. The piece is the unit that the client requests from other computers, checks against a checksum stored in the torrent, and stores. We first assume that every piece belongs to a single file, so that a file of \(n\) pieces is complete to the degree \(x = k/n\) when \(k\) of its pieces are complete. At the end of the solution we drop this assumption. In the whole torrent there are \(N\) pieces, and when \(K\) of them are complete the global completion is \(g = K/N\), which is also the fraction of the data that is complete.
Random download. Our model of the download is that every set of \(K\) pieces is equally likely to be the set of complete pieces. This is the same as drawing \(K\) pieces at random, one after another, without putting any back. The standard BitTorrent client chooses the next piece by how few of the other computers have it (rarest first) [67]. The rule does not refer to the files, and the client chooses at random among equally rare pieces, so it is reasonable to expect the download to look random from the point of view of the files; this is the assumption of the problem. Clients that download files one after another, or let the user give some files priority, violate the model.
The hypergeometric distribution. Take one file of \(n\) pieces. The number of ways to choose the \(K\) complete pieces so that exactly \(k\) of them belong to the file is the number of ways to choose \(k\) of its \(n\) pieces times the number of ways to choose the other \(K-k\) among the \(N-n\) pieces outside it. Dividing by the number \(\binom NK\) of all choices gives the probability
\[ P_n(k) = \frac{\binom nk \binom{N-n}{K-k}}{\binom NK} , \qquad k = 0, 1, \dots, n , \tag{190}\]called the hypergeometric distribution. Its mean and variance follow from indicator variables. Let \(b_j = 1\) if the \(j\)-th piece of the file is complete and \(b_j = 0\) otherwise, so \(k = b_1 + \dots + b_n\). Each piece is complete with the probability \(K/N = g\), so the mean of \(k\) is \(ng\). Two different pieces are both complete with the probability \(K(K-1)/[N(N-1)]\), which is slightly less than \(g^2\): the covariance of \(b_j\) and \(b_l\) is \(K(K-1)/[N(N-1)] - g^2 = -g(1-g)/(N-1)\). The variance of a sum is the sum of the \(n\) variances \(g(1-g)\) plus the \(n(n-1)\) covariances, so
\[ \langle x \rangle = g , \qquad \sigma_x^2 = \frac{g(1-g)}{n}\,\frac{N-n}{N-1} , \tag{191}\]where \(x = k/n\) and \(\sigma_x\) is its standard deviation.
Binomial and normal approximations. If the file is a small part of the torrent, \(n \ll N\), then removing a few of its pieces hardly changes the chance that another piece is complete, and the pieces behave as if each were complete independently with the probability \(g\). Then \(P_n(k)\) becomes the binomial distribution \(\binom nk g^k (1-g)^{n-k}\), and the factor \((N-n)/(N-1)\) in (191) becomes 1. A sum of many independent random terms has approximately a normal (Gaussian) distribution, by the central limit theorem. When both \(ng\) and \(n(1-g)\) are large, \(x\) is therefore approximately normally distributed with the mean \(g\) and the standard deviation \(\sqrt{g(1-g)/n}\).
Mixtures. If a population consists of several groups, the histogram of the whole population is the sum of the histograms of the groups. In probability language, the distribution of a randomly chosen member is the average of the group distributions weighted by the group sizes, which is called a mixture.
28.4Solution
Files of equal length. Let the torrent consist of \(M\) files of \(n\) pieces each, so \(N = Mn\). Each file has the distribution (190), so the expected number of files that have exactly \(k\) complete pieces, that is the completeness \(x = k/n\), is
\[ M P_n(k) \approx M \binom nk g^k (1-g)^{n-k} \approx \frac{M}{n}\,\frac{1}{\sqrt{2\pi\sigma_x^2}}\,\exp\!\left[-\frac{(x-g)^2}{2\sigma_x^2}\right] , \qquad \sigma_x^2 = \frac{g(1-g)}{n} . \tag{192}\]The predicted distribution of the completeness is therefore a peak at \(x = g\), which moves from 0 to 1 as the download proceeds. Its width \(\sigma_x\) is proportional to \(1/\sqrt n\), so the longer the files, the closer they all stay to the global completion. The width is largest at \(g = 1/2\) and goes to zero at \(g = 0\) and \(g = 1\). The prediction is symmetric under exchanging \(g\) with \(1-g\) and \(x\) with \(1-x\), because complete and missing pieces play the same role in the model.
As an example take torrent A of 500 files of 40 pieces each, \(N = 20\,000\). The standard deviation of \(x\) is \(0.047\) at \(g = 0.1\) and \(0.9\), \(0.072\) at \(g = 0.3\) and \(0.7\), and \(0.079\) at \(g = 0.5\). The factor \((N-n)/(N-1) = 0.998\) changes these values by less than \(0.0002\), and at \(g = 0.5\) the hypergeometric and binomial probabilities differ by at most \(1.3\times10^{-4}\), while the largest of them is \(0.1255\). Figure 54 shows the predicted distributions together with a simulated download, described below.
A file is usable only when it is complete. The probability that all \(n\) pieces of a file are complete is \(\binom Kn/\binom Nn \approx g^n\), and the probability that none is complete is approximately \((1-g)^n\). For \(n = 40\), only \(1.5\%\) of the files are complete at \(g = 0.9\) and \(12.8\%\) at \(g = 0.95\). Half of the files are complete only at \(g = 0.5^{1/40} = 0.983\), and nine tenths at \(g = 0.997\). This quantifies the remark of the problem that most files remain unusable for a long time.
Files of different lengths. Now let file \(i\) consist of \(n_i\) pieces, \(i = 1, \dots, M\), with \(N = n_1 + \dots + n_M\). The completeness \(x_i\) of each file has the distribution (190) with its own \(n_i\). The expected histogram of the completeness over the files is the sum of these distributions, a mixture: the expected number of files with the completeness \(x\) is
\[ H_g(x) = \sum_{i=1}^{M} \;\sum_{k:\,k/n_i = x} P_{n_i}(k) . \tag{193}\]If \(w(n)\) is the fraction of files that have \(n\) pieces, this is \(M \sum_n w(n) P_n(nx)\), the equal-length prediction averaged over the length distribution. All files have the mean completeness \(g\), but their spreads differ. The long files form a narrow peak at \(x = g\). The short files spread over the whole interval from 0 to 1, and a file of one piece has either \(x = 0\), with the probability \(1-g\), or \(x = 1\), with the probability \(g\). The predicted histogram is therefore a narrow peak on a broad base, with separate spikes at \(x = 0\) and \(x = 1\). The mean square deviation of the completeness from \(g\), averaged over the files, is the average of the variances (191), \(g(1-g)\langle 1/n\rangle\) for \(n_i \ll N\), where \(\langle\cdot\rangle\) is the average over the files. The expected fraction of complete files is \(\langle g^n\rangle\), and that of untouched files is \(\langle (1-g)^n\rangle\).
Torrent B, our example, has 300 files whose lengths we drew at random with a uniform distribution of \(\ln n\) between \(\ln 1\) and \(\ln 2000\), rounded to whole pieces. This gives many short files and few long ones. It has \(N = 72\,224\) pieces in total; the median file has 32 pieces, 14 files have a single piece, 104 files have at most 10 pieces, and 102 files have at least 100. For this list \(\langle 1/n\rangle = 0.147\), so the root mean square deviation of the completeness from \(g\) is \(0.192\) at \(g = 0.5\), more than twice the value \(0.079\) for torrent A. Figure 55 shows the predicted histograms at three stages of the download.
The completeness averaged with the file lengths as weights, \(\sum_i n_i x_i / N = K/N\), equals \(g\) exactly at every moment. The plain average over the files equals \(g\) only on average, and in a single download it fluctuates: in our simulation of torrent B it was \(0.511\) at \(g = 0.5\). The complete files are mostly the short ones. In torrent B the expected fraction of complete files is \(1.3\%\) at \(g = 0.2\), \(5.5\%\) at \(g = 0.5\), \(16.4\%\) at \(g = 0.8\) and \(35.2\%\) at \(g = 0.95\), but at \(g = 0.95\) these files hold only \(1.1\%\) of the data (Figure 56).
Pieces shared by two files. In a real torrent the file boundaries do not coincide with piece boundaries. A piece at a boundary holds the end of one file and the beginning of the next, and a file shorter than a piece may lie inside a single piece. Let \(c_{ij}\) be the fraction of file \(i\) that lies in piece \(j\), so \(\sum_j c_{ij} = 1\), and let \(b_j = 1\) if piece \(j\) is complete. The completeness of file \(i\), measured in bytes, is \(x_i = \sum_j c_{ij} b_j\). Its mean is still \(g\), and with the same covariances as in the Theory part its variance is
\[ \sigma_i^2 = \frac{g(1-g)}{N-1}\left(N S_i - 1\right) , \qquad S_i = \sum_j c_{ij}^2 . \tag{194}\]For a file that occupies \(n\) whole pieces, \(S_i = 1/n\), and (194) reduces to (191). So \(1/S_i\) acts as the effective number of pieces of the file. A file is complete only when all pieces that it touches are complete, so its probability of being complete is approximately \(g^{m_i}\), where \(m_i\) is the number of pieces it touches. Two neighboring files that share a piece are no longer independent, which changes the fluctuations of the histogram but not its expected shape.
How to test the prediction with measurements. A measurement at one moment gives, for each file, its length and its completeness. From these we compute \(g\) as the length-weighted mean completeness, and the predicted histogram (193) from the list of lengths, with no free parameter. The comparison has three parts. First, the histogram of the measured completeness is compared with the prediction bin by bin, for example with a chi-square test. Second, for each file we compute the normalized deviation \(z_i = (x_i - g)/\sigma_i\) with (194). If the model holds, the average of \(z_i^2\) over the files has the expected value 1 at every \(g\), whatever the file lengths. It fluctuates about 1 by roughly \(\sqrt{2/M}\), which we check below. Third, the measured fraction of complete files is compared with \(\langle g^{m}\rangle\). A client that completes files one after another, or a user who gives some files priority, produces too many files at \(x = 0\) and \(x = 1\) and an average of \(z_i^2\) much larger than 1.
Simulated download. In place of the lost measurements we simulated downloads. A download is a random order of all \(N\) pieces, generated by a random permutation; at the global completion \(g\) the complete pieces are the first \(K = gN\) of that order, rounded to the nearest integer. We recorded the completeness of every file at the eleven values of \(g\) of the original measurements.
For torrent A the dots in Figure 54 come from a single download. The mean completeness is exactly \(g\), as it must be for equal lengths, and the standard deviations over the files are \(0.052\), \(0.078\), \(0.080\), \(0.075\) and \(0.050\) at \(g = 0.1\), \(0.3\), \(0.5\), \(0.7\) and \(0.9\), compared with the predicted \(0.047\), \(0.072\), \(0.079\), \(0.072\) and \(0.047\). A chi-square test of the five histograms, with the bins at the ends merged until each expects at least 5 files, gives the probabilities \(0.015\), \(0.56\), \(0.59\), \(0.91\) and \(0.046\) of a deviation at least as large. The two low values belong to the same download, whose snapshots are not independent: this download happened to have a few more files far from the peak at early and late stages. To decide whether this is chance, we repeated the download 300 times. The average of \(z_i^2\) over the files, averaged over the 300 downloads, lies between \(0.997\) and \(1.002\) at the five values of \(g\), and its scatter from download to download is \(0.060\) to \(0.068\), in agreement with \(\sqrt{2/500} = 0.063\).
For torrent B, the dots in Figure 55 are again a single download. At \(g = 0.2\), 49 files were untouched and 4 complete, against the expected \(49.1\) and \(4.0\); at \(g = 0.5\) the numbers were 17 and 20 against \(16.5\) and \(16.5\); at \(g = 0.8\), 4 and 50 against \(4.0\) and \(49.1\). Over 300 downloads the average of \(z_i^2\) lies between \(0.992\) and \(1.004\) at all eleven values of \(g\). The average histograms over the 300 downloads at \(g = 0.2\), \(0.5\) and \(0.8\) agree with (193) in all 66 bins within \(2.6\) standard errors, as expected for this many bins. The scatter of the average of \(z_i^2\) from download to download, \(0.075\) to \(0.111\), is somewhat larger than \(\sqrt{2/300} = 0.082\) at the extreme values of \(g\), because the \(z_i\) of the short files are far from normally distributed.
Finally we tested the shared pieces. We gave 300 files lengths drawn with a uniform distribution of the logarithm between \(0.2\) and 2000 pieces, without rounding, joined them and cut the result into \(70\,346\) pieces. Here 49 files are shorter than one piece, and 28 lie entirely inside one piece. Over 300 downloads the average of \(z_i^2\), computed with (194), is \(1.008\), \(1.003\) and \(1.002\) at \(g = 0.2\), \(0.5\) and \(0.8\).
These simulations check the formulas, not the assumption behind them: a simulated download is random by construction. Whether a real client downloads the pieces in an order that is random with respect to the files can only be decided with measured data, by the test described above. In summary, under random download the completeness of a file of \(n\) pieces has the hypergeometric distribution (190), close to a normal peak at \(x = g\) of width \(\sqrt{g(1-g)/n}\), and for files of different lengths the histogram is the mixture (193): a narrow peak at \(x = g\) from the long files on a broad base from the short ones, with spikes at \(x = 0\) and \(x = 1\).
References
- [67] B. Cohen. Incentives build robustness in BitTorrent. Workshop on Economics of Peer-to-Peer Systems, Berkeley, 2003.