这是一个本人学习 csapp 的 learning 库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.6 KiB

  1. /*
  2. * tracegen.c - Running the binary tracegen with valgrind produces
  3. * a memory trace of all of the registered transpose functions.
  4. *
  5. * The beginning and end of each registered transpose function's trace
  6. * is indicated by reading from "marker" addresses. These two marker
  7. * addresses are recorded in file for later use.
  8. */
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <assert.h>
  12. #include <unistd.h>
  13. #include <getopt.h>
  14. #include "cachelab.h"
  15. #include <string.h>
  16. /* External variables declared in cachelab.c */
  17. extern trans_func_t func_list[MAX_TRANS_FUNCS];
  18. extern int func_counter;
  19. /* External function from trans.c */
  20. extern void registerFunctions();
  21. /* Markers used to bound trace regions of interest */
  22. volatile char MARKER_START, MARKER_END;
  23. static int A[256][256];
  24. static int B[256][256];
  25. static int M;
  26. static int N;
  27. int validate(int fn,int M, int N, int A[N][M], int B[M][N]) {
  28. int C[M][N];
  29. memset(C,0,sizeof(C));
  30. correctTrans(M,N,A,C);
  31. for(int i=0;i<M;i++) {
  32. for(int j=0;j<N;j++) {
  33. if(B[i][j]!=C[i][j]) {
  34. printf("Validation failed on function %d! Expected %d but got %d at B[%d][%d]\n",fn,C[i][j],B[i][j],i,j);
  35. return 0;
  36. }
  37. }
  38. }
  39. return 1;
  40. }
  41. int main(int argc, char* argv[]){
  42. int i;
  43. char c;
  44. int selectedFunc=-1;
  45. while( (c=getopt(argc,argv,"M:N:F:")) != -1){
  46. switch(c){
  47. case 'M':
  48. M = atoi(optarg);
  49. break;
  50. case 'N':
  51. N = atoi(optarg);
  52. break;
  53. case 'F':
  54. selectedFunc = atoi(optarg);
  55. break;
  56. case '?':
  57. default:
  58. printf("./tracegen failed to parse its options.\n");
  59. exit(1);
  60. }
  61. }
  62. /* Register transpose functions */
  63. registerFunctions();
  64. /* Fill A with data */
  65. initMatrix(M,N, A, B);
  66. /* Record marker addresses */
  67. FILE* marker_fp = fopen(".marker","w");
  68. assert(marker_fp);
  69. fprintf(marker_fp, "%llx %llx",
  70. (unsigned long long int) &MARKER_START,
  71. (unsigned long long int) &MARKER_END );
  72. fclose(marker_fp);
  73. if (-1==selectedFunc) {
  74. /* Invoke registered transpose functions */
  75. for (i=0; i < func_counter; i++) {
  76. MARKER_START = 33;
  77. (*func_list[i].func_ptr)(M, N, A, B);
  78. MARKER_END = 34;
  79. if (!validate(i,M,N,A,B))
  80. return i+1;
  81. }
  82. } else {
  83. MARKER_START = 33;
  84. (*func_list[selectedFunc].func_ptr)(M, N, A, B);
  85. MARKER_END = 34;
  86. if (!validate(selectedFunc,M,N,A,B))
  87. return selectedFunc+1;
  88. }
  89. return 0;
  90. }