git: 9front

Download patch

ref: 115ed86a2d469cf03661866c239a7c0784e97264
parent: cb5787d3b38186a2c95aeba88531339a9c61679e
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Sat Apr 20 18:47:39 EDT 2013

ip/torrent: use NPROC processes in parallel for calculating initial piece hashes

--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -49,6 +49,7 @@
 };
 
 int debug;
+int nproc = 1;
 int killgroup = -1;
 int port = 6881;
 char *deftrack = "http://exodus.desync.com/announce";
@@ -1197,6 +1198,12 @@
 		usage();
 	} ARGEND;
 
+	if((s = getenv("NPROC")) != 0){
+		if((nproc = atoi(s)) <= 0)
+			nproc = 1;
+		free(s);
+	}
+
 	fd = 0;
 	if(*argv)
 		if((fd = open(*argv, OREAD)) < 0)
@@ -1301,8 +1308,18 @@
 	if(len)
 		sysfatal("pieces do not match file length");
 
-	for(i = 0; i<npieces; i++)
-		havepiece(i);
+	for(i=0; i<nproc; i++){
+		switch(rfork(RFPROC|RFMEM)){
+		case -1:
+			sysfatal("fork: %r");
+		case 0:
+			for(; i<npieces; i+=nproc)
+				havepiece(i);
+			exits(0);
+		}
+	}
+	while(waitpid() >= 0)
+		;
 
 	srand(time(0));
 	atnotify(catch, 1);
--