-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.cpp
88 lines (69 loc) · 1.75 KB
/
generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "generator.h"
#include <math.h>
#ifdef _TEST_GENERATOR
#include <iostream>
#endif
using namespace gen;
uint64_t generator::read_rdtscp()
{
static uint64_t timestamp = 0;
return timestamp++;
}
generator::generator(int pages) : g_num_pages(pages), g_is_initialized(false), g_init_timestamp(0)
{
}
int generator::request_page()
{
return _rand_uniform(g_num_pages);
}
req_type generator::request_type()
{
return (_rand_uniform(g_num_pages) >= (g_num_pages / 2)) ?
req_type::R_READ : req_type::R_WRITE;
}
int generator::request_number_of_pages()
{
/* I am using lambda = 1 */
return (1 + floor(_rand_exponential(1)));
}
int generator::request_timestamp()
{
init:
if(g_is_initialized)
return (read_rdtscp() - g_init_timestamp);
else {
request_init();
goto init;
}
}
void generator::request_init()
{
if(!g_is_initialized) {
g_is_initialized = true;
g_init_timestamp = read_rdtscp();
}
}
int generator::_rand_uniform(int max)
{
static std::default_random_engine generator(read_rdtscp());
std::uniform_int_distribution<int> distribution(0,max);
return distribution(generator);
}
int generator::_rand_exponential(double lambda)
{
static std::default_random_engine generator(read_rdtscp());
std::exponential_distribution<double> distribution(lambda);
return distribution(generator);
}
#ifdef _TEST_GENERATOR
using namespace std;
int main() {
gen::generator g(1000);
for(int i = 0; i < 10; i++) {
cout << "page = " << g.request_page() << endl;
cout << "type = " << g.request_type() << endl;
cout << "# pg = " << g.request_number_of_pages() << endl;
cout << " tsc = " << g.request_timestamp() << endl;
}
}
#endif