NVMLib  very early alpha
A library to optimally use a Hybrid RAM setup.
uv_loop_demo_example.c
Go to the documentation of this file.
1 
12 // Compile: `$ gcc uv_loop_demo_example.c -luv -lpthread`
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <pthread.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include <uv.h>
21 
22 #define DELETE_LOOP_SLEEP_TIME 5000 // 5 milli secs
23 #define MOVE_LOOP_SLEEP_TIME 5000 // 5 milli secs
24 #define MAIN_LOOP_SLEEP_TIME 10000 // 5 milli secs
25 
26 #define ARRAY_SIZE 20
27 
30 
31 uv_mutex_t nvram_mutex;
32 uv_mutex_t dram_mutex;
33 
35  for (int i=0; i<ARRAY_SIZE; i++) {
36  HASHMAP_DRAM[i] = rand() % 100;
37  HASHMAP_NVRAM[i] = 0;
38  }
39 }
40 
41 void print_array(int arr[], int size, char* msg) {
42  printf("%s\n", msg);
43  for(int i=0; i<size-1; i++) {
44  printf("%d, ", arr[i]);
45  }
46  printf("%d\n", arr[size-1]);
47 }
48 
49 void move_from_dram(uv_work_t *req) {
50  int idx = *((int*)req->data);
51 
52  uv_mutex_lock(&nvram_mutex);
53  uv_mutex_lock(&dram_mutex);
54 
55  printf("Move from DRAM to NVRAM: %d at [%ld ms]\n", idx, (uv_hrtime() / 1000000) % 100000);
56  HASHMAP_NVRAM[idx] = HASHMAP_DRAM[idx];
57  HASHMAP_DRAM[idx] = 0;
58  // HASHMAP_DRAM[idx] = rand() % 100;
59 
60  sleep(5);
61  uv_mutex_unlock(&dram_mutex);
62  uv_mutex_unlock(&nvram_mutex);
63 }
64 
65 void move_from_nvram(uv_work_t *req) {
66  int idx = *((int*)req->data);
67 
68  uv_mutex_lock(&dram_mutex);
69  uv_mutex_lock(&nvram_mutex);
70 
71  printf("Move from NVRAM to DRAM: %d at [%ld ms]\n", idx, (uv_hrtime() / 1000000) % 100000);
72  HASHMAP_DRAM[idx] = HASHMAP_NVRAM[idx];
73  HASHMAP_NVRAM[idx] = 0;
74  // HASHMAP_NVRAM[idx] = rand() % 100;
75 
76  sleep(2);
77  uv_mutex_unlock(&nvram_mutex);
78  uv_mutex_unlock(&dram_mutex);
79 }
80 
81 void delete_from_dram(uv_work_t *req) {
82  int idx = *((int*)req->data);
83 
84  uv_mutex_lock(&dram_mutex);
85 
86  printf("Delete from DRAM: %d at [%ld ms]\n", idx, (uv_hrtime() / 1000000) % 100000);
87  HASHMAP_DRAM[idx] = 0;
88 
89  uv_mutex_unlock(&dram_mutex);
90 }
91 
92 void delete_from_nvram(uv_work_t *req) {
93  int idx = *((int*)req->data);
94 
95  uv_mutex_lock(&nvram_mutex);
96 
97  printf("Delete from NVRAM: %d at [%ld ms]\n", idx, (uv_hrtime() / 1000000) % 100000);
98  HASHMAP_NVRAM[idx] = 0;
99 
100  uv_mutex_unlock(&nvram_mutex);
101 }
102 
103 void on_after_work(uv_work_t* req, int status) {
104  free(req);
105 }
106 
107 
108 void on_logistics_timer(uv_timer_t *timer, int status) {
109  uint64_t timestamp = uv_hrtime();
110  printf("on_logistics_timer [%ld ms]\n", (timestamp / 1000000) % 100000);
111  uv_work_t* work_req = (uv_work_t*)malloc(sizeof(*work_req));
112  if (rand() % 2) {
113  work_req->data = malloc(sizeof(int));
114  *((int *)(work_req->data)) = rand() % ARRAY_SIZE;
115  // *((int *)(work_req->data)) = 11;
116  uv_queue_work(timer->loop, work_req, move_from_nvram, on_after_work);
117  } else {
118  work_req->data = malloc(sizeof(int));
119  *((int *)(work_req->data)) = rand() % ARRAY_SIZE;
120  // *((int *)(work_req->data)) = 11;
121  uv_queue_work(timer->loop, work_req, move_from_dram, on_after_work);
122  }
123 }
124 
125 void on_deletion_timer(uv_timer_t *timer, int status) {
126  uint64_t timestamp = uv_hrtime();
127  printf("on_deletion_timer [%ld ms]\n", (timestamp / 1000000) % 100000);
128  uv_work_t* work_req = (uv_work_t*)malloc(sizeof(*work_req));
129  if (rand() % 2) {
130  work_req->data = malloc(sizeof(int));
131  *((int *)(work_req->data)) = rand() % ARRAY_SIZE;
132  // *((int *)(work_req->data)) = 11;
133 
134  uv_queue_work(timer->loop, work_req, delete_from_nvram, on_after_work);
135  } else {
136  work_req->data = malloc(sizeof(int));
137  *((int *)(work_req->data)) = rand() % ARRAY_SIZE;
138  // *((int *)(work_req->data)) = 11;
139 
140  uv_queue_work(timer->loop, work_req, delete_from_dram, on_after_work);
141  }
142 }
143 
144 void *logistics_thread_function(void *data){
145  uv_loop_t *thread_loop = (uv_loop_t *) data;
146  fprintf(stderr, "Logistics thread will start event loop\n");
147 
148  uv_timer_t timer_logistics;
149  uv_timer_init(thread_loop, &timer_logistics);
150  uv_timer_start(&timer_logistics, on_logistics_timer, 0, MOVE_LOOP_SLEEP_TIME);
151 
152  //Start this loop
153  uv_run(thread_loop, UV_RUN_DEFAULT);
154  pthread_exit(NULL);
155 }
156 
157 void *deletion_thread_function(void *data){
158  uv_loop_t *thread_loop = (uv_loop_t *) data;
159  fprintf(stderr, "Delete thread will start event loop\n");
160 
161  uv_timer_t timer_deletion;
162  uv_timer_init(thread_loop, &timer_deletion);
163  uv_timer_start(&timer_deletion, on_deletion_timer, 0, DELETE_LOOP_SLEEP_TIME);
164 
165  //Start this loop
166  uv_run(thread_loop, UV_RUN_DEFAULT);
167  pthread_exit(NULL);
168 }
169 
170 // void *thread_maintainer_thread(void *data){
171 // uv_loop_t *thread_loop = (uv_loop_t *) data;
172 // fprintf(stderr, "Thread-maintainer thread will start event loop\n");
173 
174 // // uv_timer_t timer_logistics;
175 // // uv_timer_init(thread_loop, &timer_logistics);
176 
177 // // uv_timer_t timer_deletion;
178 // // uv_timer_init(thread_loop, &timer_deletion);
179 
180 // // uv_timer_start(&timer_logistics, on_logistics_timer, 0, MOVE_LOOP_SLEEP_TIME);
181 // // uv_timer_start(&timer_deletion, on_deletion_timer, 0, DELETE_LOOP_SLEEP_TIME);
182 
183 // //Start this loop
184 // uv_run(thread_loop);
185 // pthread_exit(NULL);
186 // }
187 
188 int main() {
189  srand(time(NULL));
190 
192 
193  uv_mutex_init(&nvram_mutex);
194  uv_mutex_init(&dram_mutex);
195 
196  pthread_t logistics_thread, deletion_thread;
197 
198  uv_loop_t *logistics_loop = uv_loop_new();
199  pthread_create(&logistics_thread, NULL, logistics_thread_function, logistics_loop);
200 
201  uv_loop_t *deletion_loop = uv_loop_new();
202  pthread_create(&deletion_thread, NULL, deletion_thread_function, deletion_loop);
203 
204  while(1) {
205  uv_mutex_lock(&nvram_mutex);
206  uv_mutex_lock(&dram_mutex);
207 
208  printf("==========================================\n");
210  printf("------------------------------------------\n");
212  printf("==========================================\n");
213 
214 
215  uv_mutex_unlock(&dram_mutex);
216  uv_mutex_unlock(&nvram_mutex);
217 
218  // sleep(MAIN_LOOP_SLEEP_TIME);
219 
220  // sleep in milliseconds
221  struct timespec ts;
222  ts.tv_sec = MAIN_LOOP_SLEEP_TIME / 1000;
223  ts.tv_nsec = (MAIN_LOOP_SLEEP_TIME % 1000) * 1000000;
224  nanosleep(&ts, NULL);
225  }
226 
227  return 0;
228 }
DELETE_LOOP_SLEEP_TIME
#define DELETE_LOOP_SLEEP_TIME
This file is a demo for how object_maintainence.c would work.
Definition: uv_loop_demo_example.c:22
MAIN_LOOP_SLEEP_TIME
#define MAIN_LOOP_SLEEP_TIME
Definition: uv_loop_demo_example.c:24
on_logistics_timer
void on_logistics_timer(uv_timer_t *timer, int status)
Definition: uv_loop_demo_example.c:108
array_initialise
void array_initialise()
Definition: uv_loop_demo_example.c:34
on_after_work
void on_after_work(uv_work_t *req, int status)
Definition: uv_loop_demo_example.c:103
nvram_mutex
uv_mutex_t nvram_mutex
Definition: uv_loop_demo_example.c:31
delete_from_dram
void delete_from_dram(uv_work_t *req)
Definition: uv_loop_demo_example.c:81
HASHMAP_NVRAM
int HASHMAP_NVRAM[ARRAY_SIZE]
Definition: uv_loop_demo_example.c:29
delete_from_nvram
void delete_from_nvram(uv_work_t *req)
Definition: uv_loop_demo_example.c:92
uint64_t
__uint64_t uint64_t
Definition: globals.h:51
MOVE_LOOP_SLEEP_TIME
#define MOVE_LOOP_SLEEP_TIME
Definition: uv_loop_demo_example.c:23
NULL
#define NULL
Definition: list.h:13
ARRAY_SIZE
#define ARRAY_SIZE
Definition: uv_loop_demo_example.c:26
on_deletion_timer
void on_deletion_timer(uv_timer_t *timer, int status)
Definition: uv_loop_demo_example.c:125
dram_mutex
uv_mutex_t dram_mutex
Definition: uv_loop_demo_example.c:32
HASHMAP_DRAM
int HASHMAP_DRAM[ARRAY_SIZE]
Definition: uv_loop_demo_example.c:28
move_from_dram
void move_from_dram(uv_work_t *req)
Definition: uv_loop_demo_example.c:49
print_array
void print_array(int arr[], int size, char *msg)
Definition: uv_loop_demo_example.c:41
main
int main()
Definition: uv_loop_demo_example.c:188
move_from_nvram
void move_from_nvram(uv_work_t *req)
Definition: uv_loop_demo_example.c:65
logistics_thread_function
void * logistics_thread_function(void *data)
Definition: uv_loop_demo_example.c:144
deletion_thread_function
void * deletion_thread_function(void *data)
Definition: uv_loop_demo_example.c:157