Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <unistd.h>
- #define L2_cache 3145728 // 3 mb - L2 cache size
- #define string_size 16 // 64b - cache string size
- using namespace std;
- const int N = 3145728;
- union ticks {
- unsigned long long t64;
- struct s32 { long th, tl; } t32;
- } start, end;
- void getT (ostream& F, int* array, int offset){ // function that counts time taken for element
- double minCycles = 0;
- if (array[0] == 0){ // first cache miss
- if (array[string_size * 2] == 0){ // second cache miss
- usleep(100);
- asm("rdtsc\n":"=a"(start.t32.th),"=d"(start.t32.tl));
- if (array[string_size * 4 + offset] == 0){ // acess to the element
- asm("rdtsc\n":"=a"(end.t32.th),"=d"(end.t32.tl));
- }
- }
- }
- minCycles = (end.t64 - start.t64); // counting time
- F << minCycles << endl; // printing time to the file
- }
- int main(){
- int *array = new int[N];
- int *clearCacheArray = new int[L2_cache];
- int k = 0;
- ofstream F ("measure.log", ofstream::out);
- memset(array, 0, N * sizeof(int));
- memset(clearCacheArray, 0, L2_cache * sizeof(int));
- for (volatile int i = 1; i < 1025; ++i){
- getT(F, array, i);
- for (volatile int j = 0; j < L2_cache; ++j)
- if (clearCacheArray[j] == 0)
- k++;
- }
- cout << "Job done" << endl;
- F.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement