Annotation of libaitsched/example/test.c, revision 1.16
1.2 misho 1: #include <stdio.h>
1.7 misho 2: #include <stdlib.h>
3: #include <string.h>
1.2 misho 4: #include <unistd.h>
5: #include <fcntl.h>
1.5 misho 6: #include <signal.h>
1.2 misho 7: #include <sys/types.h>
8: #include <sys/stat.h>
9: #include <sys/socket.h>
10: #include <netinet/in.h>
1.7 misho 11: #include "../inc/config.h"
1.2 misho 12: #include <aitsched.h>
13:
1.14 misho 14: intptr_t Kill[1];
1.7 misho 15: #ifdef AIO_SUPPORT
16: struct iovec iv[3], wiv[3], riv[3];
17: #endif
1.16 ! misho 18: volatile uint64_t total;
1.2 misho 19:
20: void *event(sched_task_t *arg)
21: {
22: printf("Event::\n");
1.10 misho 23: taskExit(arg, NULL);
1.2 misho 24: }
25:
1.8 misho 26: void *regular(sched_task_t *arg)
1.2 misho 27: {
1.8 misho 28: printf("Task::\n");
1.10 misho 29: taskExit(arg, NULL);
1.2 misho 30: }
31:
32: void *timer(sched_task_t *arg)
33: {
1.6 misho 34: printf("Timer %p sec::\n", TASK_ARG(arg));
1.10 misho 35: taskExit(arg, NULL);
1.2 misho 36: }
37:
38: void *r(sched_task_t *arg)
39: {
1.12 misho 40: printf("read:: %ld bytes wait\n", (long) TASK_RET(arg));
1.14 misho 41: Kill[0]++;
1.10 misho 42: taskExit(arg, NULL);
1.2 misho 43: }
44:
45: void *w(sched_task_t *arg)
46: {
1.12 misho 47: printf("write:: %ld bytes wait\n", (long) TASK_RET(arg));
1.10 misho 48: taskExit(arg, NULL);
1.2 misho 49: }
50:
51: void *once(sched_task_t *arg)
52: {
53: printf("once::\n");
1.10 misho 54: taskExit(arg, NULL);
1.2 misho 55: }
56:
1.7 misho 57: #ifdef AIO_SUPPORT
58: void *aioread(sched_task_t *arg);
59: void *aiowrite(sched_task_t *arg)
60: {
61: char *ole = malloc(BUFSIZ);
62:
63: printf("AIO write[%d]:: %d bytes\n%p\n", TASK_FD(arg), (int) TASK_DATLEN(arg),
64: TASK_DATA(arg));
65: free(TASK_DATA(arg));
66:
67: memset(ole, 0, BUFSIZ);
68: schedAIORead(TASK_ROOT(arg), aioread, NULL, TASK_FD(arg), ole, BUFSIZ - 1, -1);
1.10 misho 69: taskExit(arg, NULL);
1.7 misho 70: }
71:
72: void *aioread(sched_task_t *arg)
73: {
74: char *ole = malloc(BUFSIZ);
75: int len;
76:
77: printf("AIO read[%d]:: %d bytes\n%s\n-------\n", TASK_FD(arg), (int) TASK_DATLEN(arg),
78: (char*) TASK_DATA(arg));
79:
80: if (TASK_ARG(arg)) {
81: len = strlcpy(ole, "++++++BAHURA OR CULTURE .... A CULTURE OR BAHURA :-)\n", BUFSIZ);
82: printf("sched Write len=%d %p\n", len, ole);
83: schedAIOWrite(TASK_ROOT(arg), aiowrite, TASK_ARG(arg), TASK_FD(arg), ole, len, -1);
84:
85: }
86: free(TASK_DATA(arg));
1.10 misho 87: taskExit(arg, NULL);
1.7 misho 88: }
89:
90: void *aiobulkread(sched_task_t *arg)
91: {
92: struct iovec *iv = TASK_DATA(arg);
93: register int i;
94:
95: printf("aioBULKread::\n");
96: for (i = 0; i < 3; i++) {
97: printf("%d) rlen[%d]=%s\n---\n", i, iv[i].iov_len, (char*) iv[i].iov_base);
98: free(iv[i].iov_base);
99: }
100:
1.10 misho 101: taskExit(arg, NULL);
1.7 misho 102: }
103:
104: void *aiobulkwrite(sched_task_t *arg)
105: {
106: struct iovec *iv = TASK_DATA(arg);
107: register int i;
108:
109: printf("aioBULKwrite::\n");
110: for (i = 0; i < 3; i++) {
111: printf("%d) wlen=%d\n", i, iv[i].iov_len);
112: free(iv[i].iov_base);
113: }
114:
1.10 misho 115: taskExit(arg, NULL);
1.7 misho 116: }
117: #endif
118:
1.9 misho 119: void *thr(sched_task_t *arg)
120: {
1.10 misho 121: printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
1.9 misho 122: taskExit(arg, 42);
123: }
124:
1.10 misho 125: void *thr4kill(sched_task_t *arg)
126: {
127: char blah[BUFSIZ];
128:
1.14 misho 129: printf("THREAD!!! tid (%lx):: %s\n", TASK_VAL(arg), __func__);
1.10 misho 130:
131: read(0, blah, sizeof blah);
132: printf("never see!!! (%lx):: %s (%d == %d)\n", TASK_VAL(arg), (char*) TASK_ARG(arg), TASK_TYPE(arg), taskTHREAD);
133: taskExit(arg, 0);
134: }
135:
1.5 misho 136: void sig(int s)
137: {
138: switch (s) {
139: case SIGTERM:
1.15 misho 140: case SIGINT:
141: printf("I'm in switch case %d\n", s);
1.14 misho 142: Kill[0]++;
1.5 misho 143: break;
1.15 misho 144: case SIGHUP:
145: printf("Test SIGHUP\n");
146: break;
1.5 misho 147: }
148: }
149:
1.15 misho 150: void *sigt(sched_task_t *arg)
151: {
152: int s = TASK_VAL(arg);
153:
154: printf("Received signal #%d\n", s);
155:
156: sig(s);
157:
158: schedSignalSelf(arg);
159: taskExit(arg, NULL);
160: }
161:
1.16 ! misho 162: static void*
! 163: prof(void *task, void *stage)
! 164: {
! 165: sched_task_t *t = task;
! 166: struct timespec ts;
! 167: static volatile uint64_t ns;
! 168:
! 169: clock_gettime(CLOCK_MONOTONIC, &ts);
! 170: if (!stage)
! 171: ns = (uint64_t) ts.tv_sec * 1000000000LL + ts.tv_nsec;
! 172: else {
! 173: ns = (uint64_t) ts.tv_sec * 1000000000LL + ts.tv_nsec - ns;
! 174: printf("Task ran for %lu ns: id=%p type=%d call=%p -> #%ld\n", ns,
! 175: TASK_ID(t), TASK_TYPE(t), TASK_FUNC(t), TASK_RET(t));
! 176:
! 177: total += ns;
! 178: }
! 179:
! 180: return NULL;
! 181: }
! 182:
1.2 misho 183: int
184: main(int argc, char **argv)
185: {
186: sched_root_task_t *root;
1.7 misho 187: int f, fd;
1.2 misho 188: struct sockaddr_in sin;
1.5 misho 189: struct timespec ts = { 20, 0 };
1.15 misho 190: struct timespec p = { 0, 10000000 };
191: // struct sigaction sa;
1.10 misho 192: sched_task_t *t;
1.7 misho 193: #ifdef AIO_SUPPORT
194: char *ole = malloc(BUFSIZ);
195: register int i;
196: #endif
1.5 misho 197:
1.15 misho 198: /*
1.5 misho 199: sa.sa_handler = sig;
200: sigemptyset(&sa.sa_mask);
201: sigaction(SIGTERM, &sa, NULL);
1.15 misho 202: */
1.2 misho 203:
204: f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
205: if (f == -1)
206: return 1;
1.14 misho 207: #ifndef __linux__
1.2 misho 208: sin.sin_len = sizeof sin;
1.14 misho 209: #endif
1.2 misho 210: sin.sin_family = AF_INET;
211: sin.sin_port = htons(2345);
212: sin.sin_addr.s_addr = INADDR_ANY;
213: if (bind(f, (struct sockaddr*) &sin, sizeof sin) == -1)
214: return 1;
215:
1.7 misho 216: fd = open("test_aio.dat", O_CREAT | O_RDWR, 0644);
217: if (fd == -1)
218: return 1;
219: printf("fd=%d\n", fd);
220:
1.2 misho 221: root = schedBegin();
222: if (!root) {
223: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
224: return 1;
225: }
226:
1.16 ! misho 227: if (argc > 1)
! 228: ROOT_PROFILING(root, prof);
! 229:
1.6 misho 230: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5 misho 231: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
232: return 4;
233: } else
234: ts.tv_sec = 15;
1.6 misho 235: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.5 misho 236: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
237: return 4;
238: } else
239: ts.tv_sec = 10;
240:
1.4 misho 241: if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
1.2 misho 242: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
243: return 2;
244: }
245:
1.8 misho 246: if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
1.2 misho 247: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
248: return 3;
249: }
250:
1.6 misho 251: if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
1.2 misho 252: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
253: return 4;
254: }
255:
1.14 misho 256: if (!(t = schedRead(root, r, "rrr_test", f, NULL, 0))) {
257: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
258: return 5;
259: }
1.4 misho 260: if (!schedRead(root, r, "rrr", f, NULL, 0)) {
1.2 misho 261: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
262: return 5;
263: }
1.14 misho 264: schedCancel(t);
1.2 misho 265:
1.4 misho 266: if (!schedWrite(root, w, "www", f, NULL, 0)) {
1.2 misho 267: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
268: return 6;
269: }
270:
1.13 misho 271: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
1.10 misho 272: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
273: return 7;
274: }
1.13 misho 275: if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
1.9 misho 276: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
1.14 misho 277: printf("stack is too small\n");
1.9 misho 278: }
1.13 misho 279: if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
1.9 misho 280: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
281: return 7;
282: }
1.13 misho 283: if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
1.9 misho 284: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
285: return 7;
286: }
1.14 misho 287: printf("~~~try to cancel tid = %lx\n", TASK_VAL(t));
1.10 misho 288: schedCancel(t);
1.13 misho 289: if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
1.10 misho 290: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
291: return 7;
292: }
1.13 misho 293: if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
1.10 misho 294: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
295: return 7;
296: }
1.13 misho 297: if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
1.10 misho 298: printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
1.14 misho 299: printf("stack is too small\n");
1.10 misho 300: }
301: sleep(1);
302: schedCancel(t);
1.9 misho 303:
1.7 misho 304: #ifdef AIO_SUPPORT
305: memset(ole, 0, BUFSIZ);
306: if (!schedAIORead(root, aioread, (void*) f, fd, ole, BUFSIZ - 1, 0))
307: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
308:
309:
310: iv[0].iov_len = 5;
311: iv[1].iov_len = 2;
312: iv[2].iov_len = 50;
313: for (i = 0; i < 3; i++)
314: iv[i].iov_base = malloc(iv[i].iov_len);
315: if (!schedLIORead(root, aiobulkread, NULL, fd, iv, 3, 0))
316: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
317: fsync(fd);
318: for (i = 0; i < 3; i++) {
319: wiv[i].iov_len = 100;
320: wiv[i].iov_base = malloc(wiv[i].iov_len);
321: }
322: strlcpy(wiv[0].iov_base, "12345678900000000000000000\n", wiv[0].iov_len);
323: wiv[0].iov_len = strlen(wiv[0].iov_base) + 1;
324: strlcpy(wiv[1].iov_base, "222222222222222222222222\n", wiv[1].iov_len);
325: wiv[1].iov_len = strlen(wiv[1].iov_base) + 1;
326: strlcpy(wiv[2].iov_base, "333\n", wiv[2].iov_len);
327: wiv[2].iov_len = strlen(wiv[2].iov_base) + 1;
328: if (!schedLIOWrite(root, aiobulkwrite, NULL, fd, wiv, 3, 0))
329: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
330:
331: for (i = 0; i < 3; i++) {
332: riv[i].iov_len = 5;
333: riv[i].iov_base = malloc(riv[i].iov_len + 1);
334: memset(riv[i].iov_base, 0, riv[i].iov_len + 1);
335: }
336: if (!schedLIORead(root, aiobulkread, NULL, fd, riv, 3, 0))
337: printf("Warning:: #%d - %s\n", sched_GetErrno(), sched_GetError());
338: #endif
339:
1.4 misho 340: schedCallOnce(root, once, "000000", 42, NULL, 0);
1.2 misho 341:
1.15 misho 342: printf("add signals\n");
343: schedSignal(root, sigt, NULL, SIGHUP, NULL, 0);
344: schedSignal(root, sigt, NULL, SIGTERM, NULL, 0);
345: schedSignal(root, sigt, NULL, SIGINT, NULL, 0);
346:
347: schedPolling(root, &p, NULL);
348: schedSignalDispatch(root, 42);
1.14 misho 349: schedRun(root, Kill);
1.15 misho 350: schedSignalDispatch(root, 0);
1.2 misho 351: schedEnd(&root);
1.14 misho 352: sleep(1);
1.2 misho 353:
1.7 misho 354: #ifdef AIO_SUPPORT
355: for (i = 0; i < 3; i++)
356: free(iv[i].iov_base);
357: #endif
358:
359: close(fd);
1.2 misho 360: close(f);
1.16 ! misho 361:
! 362: if (argc > 1)
! 363: printf("Total spend of time for all tasks = %lu\n", total);
1.2 misho 364: return 0;
365: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>