summaryrefslogtreecommitdiff
path: root/ddekit_test/main.c
blob: bb9ccf83cfa5592bf85b9576b6c3e8bef927b280 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <mach.h>

#include "dde.h"
#include "ddekit/condvar.h"
#include "ddekit/semaphore.h"
#include "ddekit/thread.h"
#include "ddekit/printf.h"
#include "ddekit/assert.h"

#define VERBOSE 1

boolean_t using_std = 1;

/****************
 ** Timer test **
 ****************/

#include "ddekit/timer.h"

static int twenty_timer = -1;

static void __attribute__((unused)) dummy(void *arg) { }

static void timer_func(void *arg)
{
	ddekit_printf("Timed func called with arg %d\n", (int)arg);

	if ((int)arg == 15) {
		int del = ddekit_del_timer(twenty_timer);
		ddekit_printf("\tdropped 20s timer: %d\n", del);
	}

	if ((int)arg == 30)
		ddekit_printf("\033[32;1mEND TIMER TEST\033[0m\n");
}


/* One tick each second. */
static void tick(void *arg)
{
	static int cnt=0;
	int r=0;

	ddekit_printf("tick %d\n", cnt++);
	
	r = ddekit_add_timer(tick, 0, jiffies + HZ);
}


static void timer_test(void)
{
	int i;

	ddekit_printf("\033[32;1mBEGIN TIMER TEST\033[0m\n");

	ddekit_printf("HZ: %d\n", HZ);
	ddekit_printf("JIFFIES: %lld\n", jiffies);
	
	ddekit_init_timers();
	ddekit_thread_msleep(1000);

	i = ddekit_add_timer(tick, 0, jiffies+HZ);
	ddekit_printf("added timer: %d\n", i);

	i = ddekit_add_timer(timer_func, (void *)5, jiffies + 5*HZ);
	ddekit_printf("added timer: %d\n", i);
	i = ddekit_add_timer(timer_func, (void *)10, jiffies + 10*HZ);
	ddekit_printf("added timer: %d\n", i);
	i = ddekit_add_timer(timer_func, (void *)15, jiffies + 15*HZ);
	ddekit_printf("added timer: %d\n", i);
	twenty_timer = ddekit_add_timer(timer_func, (void *)20, jiffies + 20*HZ);
	ddekit_printf("added timer: %d\n", twenty_timer);
	i = ddekit_add_timer(timer_func, (void *)30, jiffies + 30*HZ);
	ddekit_printf("added timer: %d\n", i);

	ddekit_thread_msleep (10 * 1000);
}

/********************************
 ** Memory and page-table test **
 ********************************/

#include "ddekit/memory.h"
#include "ddekit/pgtab.h"

static void memory_test(void)
{
	{
		/* simple malloc */
		ddekit_printf("\033[32;1mBEGIN SIMPLE MEMORY TEST\033[0m\n");

		int i, j;
		unsigned size = 508;
		unsigned *p[10];

		for (i = 0; i < 10; ++i) {
			p[i] = ddekit_simple_malloc(size);
			for (j = 0; j < size/sizeof(unsigned); ++j)
				(p[i])[j] = i;
			ddekit_printf("malloc(%d) => %p\n", size, p[i]);
		}

		i = 4;
		ddekit_simple_free(p[i]);
		ddekit_printf("free(%p)\n", p[i]);

		p[i] = ddekit_simple_malloc(size);
		for (j = 0; j < size/sizeof(unsigned); ++j)
			(p[i])[j] = 0xeeeeeeee;
		ddekit_printf("malloc(%d) => %p\n", size, p[i]);

		for (i = 9; i >= 0; --i) {
			ddekit_simple_free(p[i]);
			ddekit_printf("free(%p)\n", p[i]);
		}

		i = 0;
		p[i] = ddekit_simple_malloc(size);
		for (j = 0; j < size/sizeof(unsigned); ++j)
			(p[i])[j] = 0xaaaaaaaa;
		ddekit_printf("malloc(%d) => %p\n", size, p[i]);

		ddekit_printf("\033[32;1mEND SIMPLE MEMORY TEST\033[0m\n");
	}
	{
		/* large malloc */
		ddekit_printf("\033[32;1mBEGIN LARGE MEMORY TEST\033[0m\n");

		const size_t malloc_size = 1024 * 4096;

		char *p = ddekit_large_malloc(malloc_size);

		ddekit_printf("virt->phys mappings:\n");
		char *tmp = p;
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));
		tmp = p + malloc_size / 2;
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));
		tmp = p + malloc_size;
		ddekit_printf("Expected error follows...\n");
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));

		ddekit_large_free(p);
		tmp = p + malloc_size;
		ddekit_printf("Expected error follows...\n");
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));

		p = ddekit_large_malloc(malloc_size);

		ddekit_printf("virt->phys mappings:\n");
		tmp = p;
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));
		tmp = p + malloc_size / 2;
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));
		tmp = p + malloc_size;
		ddekit_printf("Expected error follows...\n");
		ddekit_printf("  %p -> %p\n", tmp, ddekit_pgtab_get_physaddr(tmp));

		ddekit_large_free(p);

		ddekit_printf("\033[32;1mEND LARGE MEMORY TEST\033[0m\n");
	}
	{
		int x = 0;
		/* slabs */
		ddekit_printf("\033[32;1mBEGIN SLAB MEMORY TEST\033[0m\n");

		/* Test slabs for contiguous and non-contiguous case. */
		for ( ; x < 2; ++x) {
			ddekit_printf("Contiguous mode set to %d\n", x);

			struct ddekit_slab *slab = ddekit_slab_init(256, x);
			if (!slab) ddekit_debug("slab cache init failed");

			unsigned const magic = 0xdeadbeef;
			ddekit_slab_set_data(slab, (void *)magic);
			if (ddekit_slab_get_data(slab) != (void *)magic)
				ddekit_debug("user pointer differs");

			unsigned i;
			unsigned *p[64];

			for (i = 0; i < sizeof(p)/sizeof(*p); ++i) {
				p[i]  = ddekit_slab_alloc(slab);
				*p[i] = i | 0xaffe0000;
				if (i % 8 == 0) {
					if (x)
						ddekit_printf("slab_alloc[%d] => %p (%p phys)\n",
						              i, p[i], ddekit_pgtab_get_physaddr(p[i]));
					else
						ddekit_printf("slab_alloc[%d] => %p (no phys)\n", i, p[i]);
				}
			}

			for (i = 0; i < sizeof(p)/sizeof(*p); ++i) {
				ddekit_slab_free(slab, p[i]);
			}
		}

		ddekit_printf("\033[32;1mEND SLAB MEMORY TEST\033[0m\n");
	}

	{
		ddekit_printf("\033[32;1mBEGIN PGTAB TEST\033[0m\n");
		ddekit_addr_t phys = 0x12345000;
		void *virt = (void *)0xABC00000;
		void *virt2 = (void *)0;
		void *virt3 = (void *)0;

		ddekit_printf("4 invalid resolutions...\n");
		ddekit_printf("virt_to_phys(0) = %p\n", ddekit_pgtab_get_physaddr(0));
		ddekit_printf("phys_to_virt(0) = %p\n", ddekit_pgtab_get_virtaddr(0));
		ddekit_printf("virt_to_phys(%p) = %p\n", virt, ddekit_pgtab_get_physaddr(virt));
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		virt = ddekit_large_malloc(16387);
		virt2 = ddekit_large_malloc(32769);
		virt3 = ddekit_large_malloc(8193);

		ddekit_printf("6 correct lookups.\n");

		phys = ddekit_pgtab_get_physaddr(virt);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		phys = ddekit_pgtab_get_physaddr(virt2);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt2, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		phys = ddekit_pgtab_get_physaddr(virt3);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt3, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		ddekit_printf("freeing 2nd area.\n");
		ddekit_large_free(virt2);


		ddekit_printf("2 errors.\n");

		phys = ddekit_pgtab_get_physaddr(virt2);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt2, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		ddekit_printf("4 correct lookups.\n");

		phys = ddekit_pgtab_get_physaddr(virt);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));

		phys = ddekit_pgtab_get_physaddr(virt3);
		ddekit_printf("virt_to_phys(%p) = %p\n", virt3, phys);
		ddekit_printf("phys_to_virt(%p) = %p\n", phys, ddekit_pgtab_get_virtaddr(phys));


		ddekit_printf("\033[32;1mEND PGTAB TEST\033[0m\n");
	}
}

ddekit_sem_t *sem;
ddekit_condvar_t *cond;
ddekit_lock_t cond_lock;
int status;

static void thread_func (void *arg)
{
  ddekit_lock_t lock = arg;
  int ret;

  ddekit_printf ("%s thread starts at %d\n",
		 ddekit_thread_get_name (ddekit_thread_myself ()), time (NULL));
  ddekit_lock_lock (&lock);
  ddekit_thread_sleep (&lock);
  ddekit_lock_unlock (&lock);
  ddekit_printf ("%s thread wakes up at %d\n",
		 ddekit_thread_get_name (ddekit_thread_myself ()), time (NULL));

  ret = ddekit_sem_down_timed (sem, 1000);
  ddekit_printf ("%s thread enter a semaphore at %d, timeout: %d\n",
		 ddekit_thread_get_name (ddekit_thread_myself ()),
		 time (NULL), ret != 0);

  ddekit_printf ("%s thread waits for signal at %d\n",
		 ddekit_thread_get_name (ddekit_thread_myself ()), time (NULL));
  ddekit_lock_lock (&cond_lock);
  while (!status)
    ddekit_condvar_wait (cond, &cond_lock);
  ddekit_lock_unlock (&cond_lock);
  ddekit_printf ("%s thread wakes up at %d\n",
		 ddekit_thread_get_name (ddekit_thread_myself ()), time (NULL));
}

static void thread_test ()
{
  ddekit_thread_t *thread1 = NULL;
  ddekit_thread_t *thread2 = NULL;
  ddekit_lock_t lock1;
  ddekit_lock_t lock2;
  
  sem = ddekit_sem_init (0);
  cond = ddekit_condvar_init ();
  status = 0;
  ddekit_lock_init (&cond_lock);
  ddekit_lock_init (&lock1);
  ddekit_lock_init (&lock2);

  thread1 = ddekit_thread_create (thread_func, lock1, "test1");
  thread2 = ddekit_thread_create (thread_func, lock2, "test2");

  sleep (3);
  ddekit_lock_lock (&lock1);
  ddekit_thread_wakeup (thread1);
  ddekit_lock_unlock (&lock1);

  ddekit_lock_lock (&lock2);
  ddekit_thread_wakeup (thread2);
  ddekit_lock_unlock (&lock2);
  sleep (2);

  ddekit_sem_up (sem);
  ddekit_sem_up (sem);
  sleep (1);

  ddekit_printf ("main thread wakes up the other two at %d\n", time (NULL));
  ddekit_lock_lock (&cond_lock);
  status = 1;
  ddekit_condvar_broadcast (cond);
  ddekit_lock_unlock (&cond_lock);
  sleep (1);
}


int main(int argc, char **argv)
{
	ddekit_log(1, "Hey there, %s running...", argv[0]);

	ddekit_init();

	if (0) memory_test();
	if (0) timer_test();
	if (1) thread_test();

	return 0;
}