Guest User

Untitled

a guest
Jan 7th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <assert.h>
  7. #include <time.h>
  8.  
  9. #define BLOCK 1024*1024
  10. #define SIZE 512
  11.  
  12. #define STEP 64
  13. #define COUNT 1000
  14.  
  15. int compare (const void *a, const void *b)
  16. {
  17. return (int)(*(long *)a - *(long*)b);
  18. }
  19.  
  20. long n_sec (struct timespec *start, struct timespec *stop)
  21. {
  22. long diff_sec = stop->tv_sec - start->tv_sec;
  23. long diff_nsec = stop->tv_nsec - start->tv_nsec;
  24. long wall_nsec = (diff_sec * 1000000000) + diff_nsec;
  25. return wall_nsec;
  26. }
  27.  
  28. int main (int argc, char *argv[])
  29. {
  30. if (argc < 2)
  31. {
  32. printf("usage: generate <file name>\n");
  33. return -1;
  34. }
  35.  
  36. char *name = argv[1];
  37.  
  38. int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  39. int flag = O_RDWR | O_CREAT;
  40. int fd = open(name, flag, mode);
  41. assert(fd != -1);
  42.  
  43.  
  44. int entries = SIZE / STEP;
  45.  
  46. long **table = malloc(sizeof(long*) * entries);
  47.  
  48. for (int i = 0; i < entries; ++i)
  49. {
  50. table[i] = malloc(sizeof(long) * COUNT);
  51. }
  52.  
  53. for (int c = 0; c < COUNT; c++)
  54. {
  55. int b = rand() % BLOCK;
  56. lseek (fd, b*SIZE, SEEK_SET);
  57.  
  58. for (int e = 0; e < entries; e++)
  59. {
  60. struct timespec t_start, t_stop;
  61. int buffer;
  62. clock_gettime(CLOCK_MONOTONIC, &t_start);
  63. read(fd, &buffer, sizeof(int));
  64. clock_gettime(CLOCK_MONOTONIC, &t_stop);
  65. lseek (fd, STEP - sizeof(int), SEEK_CUR);
  66. table[e][c]= n_sec(&t_start, &t_stop);
  67. }
  68. }
  69.  
  70. printf ("#N\tMin\tQ1\tMed\tQ3\tD9\tMax\n");
  71. for (int e = 0; e < entries; ++e)
  72. {
  73. qsort(table[e], COUNT, sizeof(long), compare);
  74. long min = table[e][0];
  75. long q1 = table[e][COUNT/4];
  76. long med = table[e][COUNT/2];
  77. long q3 = table[e][3 * (COUNT/4)];
  78. long d9 = table[e][9 * (COUNT/10)];
  79. long max = table[e][COUNT - 1];
  80. printf("%d\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\n",
  81. e*STEP, min, q1, med, q3, d9, max);
  82.  
  83. }
  84.  
  85.  
  86. close (fd);
  87. printf("done\n");
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment