sanity checkin of work-in-progress on an nfs implementation.
[newos.git] / apps / testapp / main.c
blobe17168d42b4a352d79b4d5b4a72cd6d3e4c49d2f
1 /*
2 ** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <string.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <sys/syscalls.h>
9 #include <sys/types.h>
10 #include <sys/resource.h>
11 #include <newos/errors.h>
12 #include <newos/drivers.h>
14 static int pipe_test(void);
16 static int pipe_test(void);
18 static void port_test(void);
19 static int port_test_thread_func(void* arg);
21 static int test_thread(void *args)
23 int i = (int)args;
25 sys_snooze(1000000);
26 for(;;) {
27 printf("%c", 'a' + i);
29 return 0;
32 static int dummy_thread(void *args)
34 return 1;
37 static int cpu_eater_thread(void *args)
39 for(;;)
43 static int fpu_cruncher_thread(void *args)
45 double y = *(double *)args;
46 double z;
48 for(;;) {
49 z = y * 1.47;
50 y = z / 1.47;
51 if(y != *(double *)args)
52 printf("error: y %f\n", y);
56 int main(int argc, char **argv)
58 int fd;
59 size_t len;
60 char c;
61 int rc = 0;
62 int cnt;
63 printf("testapp\n");
65 for(cnt = 0; cnt< argc; cnt++) {
66 printf("arg %d = %s \n",cnt,argv[cnt]);
69 printf("my thread id is %d\n", sys_get_current_thread_id());
70 #if 0
71 printf("enter something: ");
73 for(;;) {
74 c = getchar();
75 printf("%c", c);
77 #endif
78 #if 0
79 for(;;) {
80 sys_snooze(100000);
81 printf("booyah!");
84 for(;;);
85 #endif
86 #if 0
87 printf("waiting 5 seconds\n");
88 sys_snooze(5000000);
89 #endif
90 #if 0
91 fd = sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
92 if(fd >= 0) {
93 for(;;) {
94 size_t len;
95 static char buf[] = {
96 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x0F, 0x00, 0x0F, 0x00, 0x0F,
97 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0xF9, 0x55, 0x40, 0x00,
98 0x40, 0x06, 0xC0, 0x02, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00,
99 0x26, 0x00, 0x50, 0x0B, 0x5C, 0x81, 0xD6, 0xFA, 0x48, 0xBB, 0x17,
100 0x03, 0xC9, 0x50, 0x10, 0x7B, 0xB0, 0x6C, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
103 len = sizeof(buf);
104 sys_write(fd, buf, 0, &len);
107 #endif
108 #if 0
109 fd = sys_open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE);
110 if(fd >= 0) {
111 int foo = 0;
112 for(;;) {
113 size_t len;
114 char buf[1500];
116 len = sizeof(buf);
117 sys_read(fd, buf, 0, &len);
118 printf("%d read %d bytes\n", foo++, len);
121 #endif
123 // XXX dangerous! This overwrites the beginning of your hard drive
124 #if 0
126 char buf[512];
127 size_t bytes_read;
129 printf("opening /dev/bus/ide/0/0/raw\n");
130 fd = sys_open("/dev/bus/ide/0/0/raw", "", STREAM_TYPE_DEVICE);
131 printf("fd = %d\n", fd);
133 bytes_read = 512;
134 rc = sys_read(fd, buf, 0, &bytes_read);
135 printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
137 buf[0] = 'f';
138 buf[1] = 'o';
139 buf[2] = 'o';
140 buf[3] = '2';
141 buf[4] = 0;
143 bytes_read = 512;
144 rc = sys_write(fd, buf, 1024, &bytes_read);
145 printf("rc = %d, bytes_read = %d\n", rc, bytes_read);
147 sys_close(fd);
149 #endif
150 #if 0
152 thread_id tids[10];
153 int i;
155 for(i=0; i<10; i++) {
156 tids[i] = sys_thread_create_thread("foo", &test_thread, (void *)i);
157 sys_thread_resume_thread(tids[i]);
160 sys_snooze(5000000);
161 sys_proc_kill_proc(sys_get_current_proc_id());
163 sys_snooze(3000000);
164 for(i=0; i<10; i++) {
165 sys_thread_kill_thread(tids[i]);
167 printf("thread_is dead\n");
168 sys_snooze(5000000);
171 #endif
172 #if 0
174 for(;;)
175 sys_proc_create_proc("/boot/bin/true", "true", 32);
177 #endif
178 #if 0
180 void *buf = (void *)0x60000000;
181 int fd;
182 int rc;
183 int len = 512;
185 fd = sys_open("/boot/testapp", "", STREAM_TYPE_FILE);
187 rc = sys_read(fd, buf, 0, &len);
188 printf("rc from read = 0x%x\n", rc);
189 sys_close(fd);
191 #endif
192 #if 0
194 char data;
195 int fd;
197 fd = sys_open("/dev/audio/pcbeep/1", STREAM_TYPE_DEVICE, 0);
198 if(fd >= 0) {
199 printf("writing to the speaker\n");
200 data = 3;
201 sys_write(fd, &data, 0, 1);
202 sys_snooze(1000000);
203 data = 0;
204 sys_write(fd, &data, 0, 1);
205 sys_close(fd);
208 #endif
210 #if 0
212 port_test();
214 #endif
215 #if 0
217 int fd, bytes_read;
218 char buf[3];
220 fd = sys_open("/dev/ps2mouse", STREAM_TYPE_DEVICE, 0);
221 if(fd < 0) {
222 printf("failed to open device\n");
223 return -1;
226 bytes_read = sys_read(fd, buf, -1, 3);
227 if(bytes_read < 3) {
228 printf("failed to read device\n");
229 return -1;
232 printf("Status: %X\nDelta X: %d\nDelta Y: %d\n", buf[0], buf[1], buf[2]);
234 #endif
235 #if 0
237 int fd;
238 int buf[512/4];
239 int i, j;
241 fd = sys_open("/dev/disk/netblock/0/raw", STREAM_TYPE_DEVICE, 0);
242 if(fd < 0) {
243 printf("could not open netblock\n");
244 return -1;
247 sys_ioctl(fd, 90001, NULL, 0);
249 for(i=0; i<1*1024*1024; i += 512) {
250 for(j=0; j<512/4; j++)
251 buf[j] = i + j*4;
252 sys_write(fd, buf, -1, sizeof(buf));
255 // sys_read(fd, buf, 0, sizeof(buf));
256 // sys_write(fd, buf, 512, sizeof(buf));
258 #endif
259 #if 0
261 #define NUM_FDS 150
263 int fds[NUM_FDS], i;
264 struct rlimit rl;
266 rc = getrlimit(RLIMIT_NOFILE, &rl);
267 if (rc < 0) {
268 printf("error in getrlimit\n");
269 return -1;
272 printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
274 for(i = 0; i < NUM_FDS; i++) {
275 fds[i] = open("/boot/bin/testapp", 0);
276 if (fds[i] < 0) {
277 break;
281 printf("opened %d files\n", i);
283 rl.rlim_cur += 15;
285 printf("Setting RLIMIT_NOFILE to %lu\n", rl.rlim_cur);
286 rc = setrlimit(RLIMIT_NOFILE, &rl);
287 if (rc < 0) {
288 printf("error in setrlimit\n");
289 return -1;
292 rl.rlim_cur = 0;
293 rc = getrlimit(RLIMIT_NOFILE, &rl);
294 if (rc < 0) {
295 printf("error in getrlimit\n");
296 return -1;
299 printf("RLIMIT_NOFILE = %lu\n", rl.rlim_cur);
301 for(;i < NUM_FDS; i++) {
302 fds[i] = open("/boot/bin/testapp", 0);
303 if (fds[i] < 0) {
304 break;
308 printf("opened a total of %d files\n", i);
310 #endif
311 #if 0
313 region_id rid;
314 void *ptr;
316 rid = sys_vm_map_file("netblock", &ptr, REGION_ADDR_ANY_ADDRESS, 16*1024, LOCK_RW,
317 REGION_NO_PRIVATE_MAP, "/dev/disk/netblock/0/raw", 0);
318 if(rid < 0) {
319 printf("error mmaping device\n");
320 return -1;
323 // play around with it
324 printf("mmaped device at %p\n", ptr);
325 printf("%d\n", *(int *)ptr);
326 printf("%d\n", *((int *)ptr + 1));
327 printf("%d\n", *((int *)ptr + 2));
328 printf("%d\n", *((int *)ptr + 3));
330 #endif
331 #if 0
333 int i;
335 // printf("spawning %d copies of true\n", 10000);
337 for(i=0; ; i++) {
338 proc_id id;
339 bigtime_t t;
341 printf("%d...", i);
343 t = sys_system_time();
345 id = sys_proc_create_proc("/boot/bin/true", "true", NULL, 0, 20);
346 if(id <= 0x2) {
347 printf("new proc returned 0x%x!\n", id);
348 return -1;
350 sys_proc_wait_on_proc(id, NULL);
352 printf("done (%Ld usecs)\n", sys_system_time() - t);
355 #endif
356 #if 0
358 int i;
360 printf("spawning two cpu eaters\n");
362 // sys_thread_resume_thread(sys_thread_create_thread("cpu eater 1", &cpu_eater_thread, 0));
363 // sys_thread_resume_thread(sys_thread_create_thread("cpu eater 2", &cpu_eater_thread, 0));
365 printf("spawning %d threads\n", 10000);
367 for(i=0; i<10000; i++) {
368 thread_id id;
369 bigtime_t t;
371 printf("%d...", i);
373 t = sys_system_time();
375 id = sys_thread_create_thread("testthread", &dummy_thread, 0);
376 sys_thread_resume_thread(id);
377 sys_thread_wait_on_thread(id, NULL);
379 printf("done (%Ld usecs)\n", sys_system_time() - t);
382 #endif
383 #if 0
385 thread_id id;
386 static double f[5] = { 2.43, 5.23, 342.34, 234123.2, 1.4 };
388 printf("spawning a few floating point crunchers\n");
390 id = sys_thread_create_thread("fpu thread0", &fpu_cruncher_thread, &f[0]);
391 sys_thread_resume_thread(id);
393 id = sys_thread_create_thread("fpu thread1", &fpu_cruncher_thread, &f[1]);
394 sys_thread_resume_thread(id);
396 id = sys_thread_create_thread("fpu thread2", &fpu_cruncher_thread, &f[2]);
397 sys_thread_resume_thread(id);
399 id = sys_thread_create_thread("fpu thread3", &fpu_cruncher_thread, &f[3]);
400 sys_thread_resume_thread(id);
402 id = sys_thread_create_thread("fpu thread4", &fpu_cruncher_thread, &f[4]);
403 sys_thread_resume_thread(id);
405 getchar();
406 printf("passed the test\n");
408 #endif
409 #if 0
410 rc = pipe_test();
411 #endif
412 #if 0
414 int i,j,k;
415 int fd;
416 devfs_framebuffer_info fb;
417 int err;
418 void *framebuffer;
420 fd = sys_open("/dev/graphics/fb/0", STREAM_TYPE_DEVICE, 0);
421 if(fd < 0) {
422 printf("error opening framebuffer device\n");
423 return -1;
426 err = sys_ioctl(fd, IOCTL_DEVFS_GET_FRAMEBUFFER_INFO, &fb, sizeof(fb));
427 if(err < 0) {
428 printf("error getting framebuffer info\n");
429 return -1;
432 err = sys_ioctl(fd, IOCTL_DEVFS_MAP_FRAMEBUFFER, &framebuffer, sizeof(framebuffer));
433 if(err < 0) {
434 printf("error mapping framebuffer\n");
435 return -1;
438 for(k=0;; k++) {
439 for(i=0; i<fb.height; i++) {
440 uint16 row[fb.width];
441 for(j=0; j<fb.width; j++) {
442 uint16 color = ((j+i+k) & 0x1f) << 11;
443 row[j] = color;
445 memcpy(framebuffer + i*fb.width*2, row, sizeof(row));
449 #endif
450 #if 1
452 int err;
454 printf("mounting nfs filesystem\n");
456 sys_create("/nfs", STREAM_TYPE_DIR);
457 err = sys_mount("/nfs", "192.168.0.4:/disk", "nfs", NULL);
458 printf("mount returns %d\n", err);
461 int i;
462 int fd;
463 char buf[1024];
465 fd = sys_open("/nfs", STREAM_TYPE_DIR, 0);
467 for(i=0; i<16; i++) {
468 memset(buf, 0, sizeof(buf));
469 err = sys_read(fd, buf, -1, sizeof(buf));
470 printf("read returns %d '%s'\n", err, buf);
474 #endif
475 printf("exiting w/return code %d\n", rc);
476 return rc;
479 static int pipe_read_thread(void *args)
481 int fd = *(int *)args;
482 int err;
483 char buf[1024];
484 int i;
486 for(;;) {
487 err = read(fd, buf, sizeof(buf));
488 printf("pipe_read_thread: read returns %d\n", err);
489 if(err < 0)
490 break;
492 printf("'");
493 for(i=0; i<err; i++)
494 printf("%c", buf[i]);
495 printf("'\n");
498 return err;
501 static int pipe_test(void)
503 int fds[2];
504 int err;
505 char buf[1024];
506 thread_id id;
508 err = pipe(fds);
509 printf("pipe returns %d\n", err);
510 printf("%d %d\n", fds[0], fds[1]);
512 #if 1
513 id = sys_thread_create_thread("pipe read thread", &pipe_read_thread, &fds[1]);
514 sys_thread_resume_thread(id);
516 sys_snooze(2000000);
518 err = write(fds[0], "this is a test", sizeof("this is a test"));
519 printf("write returns %d\n", err);
520 if(err < 0)
521 return err;
523 sys_snooze(2000000);
525 close(fds[0]);
526 close(fds[1]);
527 #endif
528 #if 0
529 // close the reader end and write to it
530 close(fds[1]);
531 err = write(fds[0], "this is a test", sizeof("this is a test"));
532 printf("write returns %d\n", err);
534 sys_snooze(2000000);
535 #endif
536 return 0;
540 * testcode ports
543 port_id test_p1, test_p2, test_p3, test_p4;
545 static void port_test(void)
547 char testdata[5];
548 thread_id t;
549 int res;
550 int32 dummy;
551 int32 dummy2;
553 strcpy(testdata, "abcd");
555 printf("porttest: port_create()\n");
556 test_p1 = sys_port_create(1, "test port #1");
557 test_p2 = sys_port_create(10, "test port #2");
558 test_p3 = sys_port_create(1024, "test port #3");
559 test_p4 = sys_port_create(1024, "test port #4");
561 printf("porttest: port_find()\n");
562 printf("'test port #1' has id %d (should be %d)\n", sys_port_find("test port #1"), test_p1);
564 printf("porttest: port_write() on 1, 2 and 3\n");
565 sys_port_write(test_p1, 1, &testdata, sizeof(testdata));
566 sys_port_write(test_p2, 666, &testdata, sizeof(testdata));
567 sys_port_write(test_p3, 999, &testdata, sizeof(testdata));
568 printf("porttest: port_count(test_p1) = %d\n", sys_port_count(test_p1));
570 printf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n");
571 sys_port_write_etc(test_p1, 1, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000);
572 printf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n");
573 res = sys_port_write_etc(test_p2, 777, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000);
574 printf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD");
576 printf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n");
577 res = sys_port_read_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), PORT_FLAG_TIMEOUT, 1000000);
578 printf("porttest: res=%d, %s\n", res, res == ERR_PORT_TIMED_OUT ? "ok" : "BAD");
580 printf("porttest: spawning thread for port 1\n");
581 t = sys_thread_create_thread("port_test", port_test_thread_func, NULL);
582 // resume thread
583 sys_thread_resume_thread(t);
585 printf("porttest: write\n");
586 sys_port_write(test_p1, 1, &testdata, sizeof(testdata));
588 // now we can write more (no blocking)
589 printf("porttest: write #2\n");
590 sys_port_write(test_p1, 2, &testdata, sizeof(testdata));
591 printf("porttest: write #3\n");
592 sys_port_write(test_p1, 3, &testdata, sizeof(testdata));
594 printf("porttest: waiting on spawned thread\n");
595 sys_thread_wait_on_thread(t, NULL);
597 printf("porttest: close p1\n");
598 sys_port_close(test_p2);
599 printf("porttest: attempt write p1 after close\n");
600 res = sys_port_write(test_p2, 4, &testdata, sizeof(testdata));
601 printf("porttest: port_write ret %d\n", res);
603 printf("porttest: testing delete p2\n");
604 sys_port_delete(test_p2);
606 printf("porttest: end test main thread\n");
609 static int port_test_thread_func(void* arg)
611 int msg_code;
612 int n;
613 char buf[5];
615 printf("porttest: port_test_thread_func()\n");
617 n = sys_port_read(test_p1, &msg_code, &buf, 3);
618 printf("port_read #1 code %d len %d buf %3s\n", msg_code, n, buf);
619 n = sys_port_read(test_p1, &msg_code, &buf, 4);
620 printf("port_read #1 code %d len %d buf %4s\n", msg_code, n, buf);
621 buf[4] = 'X';
622 n = sys_port_read(test_p1, &msg_code, &buf, 5);
623 printf("port_read #1 code %d len %d buf %5s\n", msg_code, n, buf);
625 printf("porttest: testing delete p1 from other thread\n");
626 sys_port_delete(test_p1);
627 printf("porttest: end port_test_thread_func()\n");
629 return 0;