seqreader.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/time.h>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8. #define BUFSIZE 65536
  9. int main(int argc, char *argv[]) {
  10. int err;
  11. size_t SKIP = 1;
  12. DIR *dir;
  13. struct dirent *ent;
  14. struct timeval start, tv;
  15. size_t us;
  16. size_t files = 0;
  17. size_t total_size = 0;
  18. size_t skip;
  19. size_t run;
  20. char buffer[65536];
  21. if (argc < 2) {
  22. printf("Usage: %s <directory> [skip]\n", argv[0]);
  23. exit(0);
  24. }
  25. chdir(argv[1]);
  26. if (argc > 2) {
  27. SKIP = atoi(argv[2]);
  28. printf("Skip %i\n", SKIP);
  29. }
  30. gettimeofday(&start, NULL);
  31. for (run = 0; run < SKIP; run++) {
  32. skip = 0;
  33. dir = opendir(".");
  34. while (ent = readdir(dir)) {
  35. struct stat st;
  36. if (((skip++)%SKIP) != run) continue;
  37. if (stat(ent->d_name, &st)) continue;
  38. if (!S_ISREG(st.st_mode)) continue;
  39. FILE *f = fopen(ent->d_name, "r");
  40. if (!f) continue;
  41. int size = st.st_blksize;
  42. if (size > BUFSIZE) {
  43. printf("Buffer too small\n");
  44. exit(1);
  45. }
  46. while (!feof(f)) {
  47. err = fread(buffer, 1, size, f);
  48. if (err < 0) {
  49. printf("Read failed\n");
  50. exit(1);
  51. }
  52. }
  53. fclose(f);
  54. total_size += st.st_size;
  55. files++;
  56. gettimeofday(&tv, NULL);
  57. us = (tv.tv_sec - start.tv_sec) * 1000000 + (tv.tv_usec - start.tv_usec);
  58. 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);
  59. }
  60. closedir(dir);
  61. }
  62. }