Browse Source

Report speed in seqreader using proper MB/s

Suren A. Chilingaryan 12 years ago
parent
commit
8464f1a53d
1 changed files with 19 additions and 9 deletions
  1. 19 9
      seqreader.c

+ 19 - 9
seqreader.c

@@ -11,8 +11,8 @@
 #include <string.h>
 #include <errno.h>
 
-#define BUFSIZE 1048576//65536
-#define BLOCK_SIZE 16384//16384
+#define BUFSIZE 65536
+#define BLOCK_SIZE 16384
 #define WRITE_INTERVAL 1
 
 
@@ -28,7 +28,9 @@ int main(int argc, char *argv[]) {
     size_t last_write = 0;
     size_t skip;
     size_t run;
+    ssize_t res;
     char buffer[BUFSIZE];
+    long double mcoef = 1000000. / (1024 * 1024);
     
     if (argc < 2) {
 	printf("Usage: %s <directory|device> [skip]\n", argv[0]);
@@ -42,25 +44,33 @@ int main(int argc, char *argv[]) {
 	    exit(1);
 	}
 	
-	int size = BLOCK_SIZE;
+	size_t size = BLOCK_SIZE;
 	
 	gettimeofday(&start, NULL);
         
-        err = read(fd, buffer, size);
-	while (err > 0) {
-	    total_size += err;
+        res = read(fd, buffer, size);
+	while (res > 0) {
+	    if (res != size) {
+		printf("Incomplete read: %zu bytes read instead of %zu\n", res, size);
+		exit(-1);
+	    }
+	    total_size += res;
 
 	    gettimeofday(&tv, NULL);
 	    us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
 	    if ((us - last_write) > WRITE_INTERVAL * 1000000) {
 		last_write = us;
-		printf("Reading: %s (%lu GB),  Measured speed: %lu mB/s\n", argv[0], total_size / 1024 / 1024 / 1024, total_size / us);
+		printf("Reading: %s (%lu GB),  Measured speed: %zu MB/s\n", argv[0], total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
 	    }
-	    err = read(fd, buffer, size);
+	    res = read(fd, buffer, size);
 	}
 	
 	close(fd);
 
+	if (res < 0) {
+	    printf("Read failed with errno %i\n", errno);
+	    exit(-1);
+	}
 
 	return 0;
     }
@@ -110,7 +120,7 @@ int main(int argc, char *argv[]) {
 	
 	gettimeofday(&tv, NULL);
 	us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
-	printf("Reading: %s (%lu MB), Read: %lu files (%lu GB),  Measured speed: %lu mB/s\n", ent->d_name, st.st_size/1024/1024, files, total_size / 1024 / 1024 / 1024, total_size / us);
+	printf("Reading: %s (%lu MB), Read: %lu files (%lu GB),  Measured speed: %zu MB/s\n", ent->d_name, st.st_size/1024/1024, files, total_size / 1024 / 1024 / 1024, (size_t)(mcoef * total_size / us));
       }
       closedir(dir);
     }