2 ** Copyright 2003-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
9 #include <sys/syscalls.h>
10 #include <sys/types.h>
11 #include <sys/resource.h>
12 #include <newos/errors.h>
13 #include <newos/drivers.h>
20 printf("(testing constructor)\n");
23 printf("(testing destructor)\n");
36 static test_option opts
[] = {
37 { "0", "sleep test", &sleep_test
, 0 },
38 { "1", "spawn threads, run forever", &thread_spawn_test
, 0 },
39 { "2", "spawn threads, kill them", &thread_spawn_test
, 1 },
40 { "3", "spawn threads, self terminating", &thread_spawn_test
, 2 },
41 { "4", "syscall benchmark", &syscall_bench
, 0 },
42 { "5", "test signals", &sig_test
, 0 },
43 { "6", "fpu safety test", &fpu_test
, 0 },
47 static int get_line(char *buf
, int buflen
)
54 if(c
== '\n' || c
== '\r') {
59 buf
[i
++] = tolower(c
);
68 int main(int argc
, char **argv
)
72 printf("welcome to the newos testapp!\n");
74 for(int cnt
= 0; cnt
< argc
; cnt
++) {
75 printf("arg %d = %s \n",cnt
,argv
[cnt
]);
79 printf("Select from tests below, or 'x' to exit:\n");
81 for(test_option
*opt
= opts
; opt
->command
; opt
++) {
82 printf("%s\t%s\n", opt
->command
, opt
->name
);
87 get_line(command
, sizeof(command
));
89 if(strcmp(command
, "x") == 0)
91 if(strlen(command
) == 0)
94 for(test_option
*opt
= opts
; opt
->command
; opt
++) {
95 if(strcmp(command
, opt
->command
) == 0) {
97 int err
= opt
->func(opt
->arg
);
98 printf("test returns %d\n", err
);
106 int foo(int argc
, char **argv
)
117 fd
= open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE
);
121 static char buf
[] = {
122 0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0x0F, 0x00, 0x0F, 0x00, 0x0F,
123 0x00, 0x08, 0x00, 0x45, 0x00, 0x00, 0x28, 0xF9, 0x55, 0x40, 0x00,
124 0x40, 0x06, 0xC0, 0x02, 0xC0, 0xA8, 0x00, 0x01, 0xC0, 0xA8, 0x00,
125 0x26, 0x00, 0x50, 0x0B, 0x5C, 0x81, 0xD6, 0xFA, 0x48, 0xBB, 0x17,
126 0x03, 0xC9, 0x50, 0x10, 0x7B, 0xB0, 0x6C, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
130 pwrite(fd
, buf
, &len
, 0);
135 fd
= open("/dev/net/rtl8139/0", "", STREAM_TYPE_DEVICE
);
143 pread(fd
, buf
, &len
, 0);
144 printf("%d read %d bytes\n", foo
++, len
);
149 // XXX dangerous! This overwrites the beginning of your hard drive
155 printf("opening /dev/bus/ide/0/0/raw\n");
156 fd
= open("/dev/bus/ide/0/0/raw", "", STREAM_TYPE_DEVICE
);
157 printf("fd = %d\n", fd
);
160 rc
= pread(fd
, buf
, &bytes_read
, 0);
161 printf("rc = %d, bytes_read = %d\n", rc
, bytes_read
);
170 rc
= pwrite(fd
, buf
, &bytes_read
, 1024);
171 printf("rc = %d, bytes_read = %d\n", rc
, bytes_read
);
181 for(i
=0; i
<10; i
++) {
182 tids
[i
] = _kern_thread_create_thread("foo", &test_thread
, (void *)i
);
183 _kern_thread_resume_thread(tids
[i
]);
187 _kern_proc_kill_proc(_kern_get_current_proc_id());
190 for(i=0; i<10; i++) {
191 _kern_thread_kill_thread(tids[i]);
193 printf("thread_is dead\n");
201 _kern_proc_create_proc("/boot/bin/true", "true", 32);
206 void *buf
= (void *)0x60000000;
211 fd
= open("/boot/testapp", "", STREAM_TYPE_FILE
);
213 rc
= pread(fd
, buf
, &len
, 0);
214 printf("rc from read = 0x%x\n", rc
);
223 fd
= open("/dev/audio/pcbeep/1", STREAM_TYPE_DEVICE
, 0);
225 printf("writing to the speaker\n");
227 pwrite(fd
, &data
, 1, 0);
230 pwrite(fd
, &data
, 1, 0);
246 fd
= open("/dev/ps2mouse", STREAM_TYPE_DEVICE
, 0);
248 printf("failed to open device\n");
252 bytes_read
= read(fd
, buf
, 3);
254 printf("failed to read device\n");
258 printf("Status: %X\nDelta X: %d\nDelta Y: %d\n", buf
[0], buf
[1], buf
[2]);
267 fd
= open("/dev/disk/netblock/0/raw", STREAM_TYPE_DEVICE
, 0);
269 printf("could not open netblock\n");
273 ioctl(fd
, 90001, NULL
, 0);
275 for(i
=0; i
<1*1024*1024; i
+= 512) {
276 for(j
=0; j
<512/4; j
++)
278 write(fd
, buf
, sizeof(buf
));
281 // pread(fd, buf, sizeof(buf), 0);
282 // pwrite(fd, buf, sizeof(buf), 512);
292 rc
= getrlimit(RLIMIT_NOFILE
, &rl
);
294 printf("error in getrlimit\n");
298 printf("RLIMIT_NOFILE = %lu\n", rl
.rlim_cur
);
300 for(i
= 0; i
< NUM_FDS
; i
++) {
301 fds
[i
] = open("/boot/bin/testapp", 0);
307 printf("opened %d files\n", i
);
311 printf("Setting RLIMIT_NOFILE to %lu\n", rl
.rlim_cur
);
312 rc
= setrlimit(RLIMIT_NOFILE
, &rl
);
314 printf("error in setrlimit\n");
319 rc
= getrlimit(RLIMIT_NOFILE
, &rl
);
321 printf("error in getrlimit\n");
325 printf("RLIMIT_NOFILE = %lu\n", rl
.rlim_cur
);
327 for(;i
< NUM_FDS
; i
++) {
328 fds
[i
] = open("/boot/bin/testapp", 0);
334 printf("opened a total of %d files\n", i
);
342 rid
= _kern_vm_map_file("netblock", &ptr
, REGION_ADDR_ANY_ADDRESS
, 16*1024, LOCK_RW
,
343 REGION_NO_PRIVATE_MAP
, "/dev/disk/netblock/0/raw", 0);
345 printf("error mmaping device\n");
349 // play around with it
350 printf("mmaped device at %p\n", ptr
);
351 printf("%d\n", *(int *)ptr
);
352 printf("%d\n", *((int *)ptr
+ 1));
353 printf("%d\n", *((int *)ptr
+ 2));
354 printf("%d\n", *((int *)ptr
+ 3));
361 // printf("spawning %d copies of true\n", 10000);
369 t
= _kern_system_time();
371 id
= _kern_proc_create_proc("/boot/bin/true", "true", NULL
, 0, 20);
373 printf("new proc returned 0x%x!\n", id
);
376 _kern_proc_wait_on_proc(id
, NULL
);
378 printf("done (%Ld usecs)\n", _kern_system_time() - t
);
386 printf("spawning two cpu eaters\n");
388 // _kern_thread_resume_thread(_kern_thread_create_thread("cpu eater 1", &cpu_eater_thread, 0));
389 // _kern_thread_resume_thread(_kern_thread_create_thread("cpu eater 2", &cpu_eater_thread, 0));
391 printf("spawning %d threads\n", 10000);
393 for(i
=0; i
<10000; i
++) {
399 t
= _kern_system_time();
401 id
= _kern_thread_create_thread("testthread", &dummy_thread
, 0);
402 _kern_thread_resume_thread(id
);
403 _kern_thread_wait_on_thread(id
, NULL
);
405 printf("done (%Ld usecs)\n", _kern_system_time() - t
);
412 static double f
[5] = { 2.43, 5.23, 342.34, 234123.2, 1.4 };
414 printf("spawning a few floating point crunchers\n");
416 id
= _kern_thread_create_thread("fpu thread0", &fpu_cruncher_thread
, &f
[0]);
417 _kern_thread_resume_thread(id
);
419 id
= _kern_thread_create_thread("fpu thread1", &fpu_cruncher_thread
, &f
[1]);
420 _kern_thread_resume_thread(id
);
422 id
= _kern_thread_create_thread("fpu thread2", &fpu_cruncher_thread
, &f
[2]);
423 _kern_thread_resume_thread(id
);
425 id
= _kern_thread_create_thread("fpu thread3", &fpu_cruncher_thread
, &f
[3]);
426 _kern_thread_resume_thread(id
);
428 id
= _kern_thread_create_thread("fpu thread4", &fpu_cruncher_thread
, &f
[4]);
429 _kern_thread_resume_thread(id
);
432 printf("passed the test\n");
442 devfs_framebuffer_info fb
;
446 fd
= open("/dev/graphics/fb/0", 0);
448 printf("error opening framebuffer device\n");
452 err
= ioctl(fd
, IOCTL_DEVFS_GET_FRAMEBUFFER_INFO
, &fb
, sizeof(fb
));
454 printf("error getting framebuffer info\n");
458 err
= ioctl(fd
, IOCTL_DEVFS_MAP_FRAMEBUFFER
, &framebuffer
, sizeof(framebuffer
));
460 printf("error mapping framebuffer\n");
465 for(i
=0; i
<fb
.height
; i
++) {
466 uint16 row
[fb
.width
];
467 for(j
=0; j
<fb
.width
; j
++) {
468 uint16 color
= ((j
+i
+k
) & 0x1f) << 11;
471 memcpy(framebuffer
+ i
*fb
.width
*2, row
, sizeof(row
));
480 printf("mounting nfs filesystem\n");
483 err
= _kern_mount("/nfs", "192.168.0.4:/disk", "nfs", NULL
);
484 printf("mount returns %d\n", err
);
491 fd
= _kern_opendir("/nfs");
493 for(i
=0; i
<16; i
++) {
494 memset(buf
, 0, sizeof(buf
));
495 err
= _kern_readdir(fd
, buf
, sizeof(buf
));
496 printf("read returns %d '%s'\n", err
, buf
);
501 printf("exiting w/return code %d\n", rc
);