File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / example / test.c
Revision 1.16: download - view: text, annotated - select for diffs - revision graph
Tue May 19 15:53:56 2026 UTC (3 weeks, 3 days ago) by misho
Branches: MAIN
CVS tags: sched8_7, sched8_6, SCHED8_6, SCHED8_5, HEAD
Version 8.5

Changelog:
 - Implements internal profiling for tasks

    1: #include <stdio.h>
    2: #include <stdlib.h>
    3: #include <string.h>
    4: #include <unistd.h>
    5: #include <fcntl.h>
    6: #include <signal.h>
    7: #include <sys/types.h>
    8: #include <sys/stat.h>
    9: #include <sys/socket.h>
   10: #include <netinet/in.h>
   11: #include "../inc/config.h"
   12: #include <aitsched.h>
   13: 
   14: intptr_t Kill[1];
   15: #ifdef AIO_SUPPORT
   16: struct iovec iv[3], wiv[3], riv[3];
   17: #endif
   18: volatile uint64_t total;
   19: 
   20: void *event(sched_task_t *arg)
   21: {
   22: 	printf("Event::\n");
   23: 	taskExit(arg, NULL);
   24: }
   25: 
   26: void *regular(sched_task_t *arg)
   27: {
   28: 	printf("Task::\n");
   29: 	taskExit(arg, NULL);
   30: }
   31: 
   32: void *timer(sched_task_t *arg)
   33: {
   34: 	printf("Timer %p sec::\n", TASK_ARG(arg));
   35: 	taskExit(arg, NULL);
   36: }
   37: 
   38: void *r(sched_task_t *arg)
   39: {
   40: 	printf("read:: %ld bytes wait\n", (long) TASK_RET(arg));
   41: 	Kill[0]++;
   42: 	taskExit(arg, NULL);
   43: }
   44: 
   45: void *w(sched_task_t *arg)
   46: {
   47: 	printf("write:: %ld bytes wait\n", (long) TASK_RET(arg));
   48: 	taskExit(arg, NULL);
   49: }
   50: 
   51: void *once(sched_task_t *arg)
   52: {
   53: 	printf("once::\n");
   54: 	taskExit(arg, NULL);
   55: }
   56: 
   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);
   69: 	taskExit(arg, NULL);
   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));
   87: 	taskExit(arg, NULL);
   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: 
  101: 	taskExit(arg, NULL);
  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: 
  115: 	taskExit(arg, NULL);
  116: }
  117: #endif
  118: 
  119: void *thr(sched_task_t *arg)
  120: {
  121: 	printf("tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  122: 	taskExit(arg, 42);
  123: }
  124: 
  125: void *thr4kill(sched_task_t *arg)
  126: {
  127: 	char blah[BUFSIZ];
  128: 
  129: 	printf("THREAD!!! tid (%lx):: %s\n", TASK_VAL(arg), __func__);
  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: 
  136: void sig(int s)
  137: {
  138: 	switch (s) {
  139: 		case SIGTERM:
  140: 		case SIGINT:
  141: 			printf("I'm in switch case %d\n", s);
  142: 			Kill[0]++;
  143: 			break;
  144: 		case SIGHUP:
  145: 			printf("Test SIGHUP\n");
  146: 			break;
  147: 	}
  148: }
  149: 
  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: 
  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: 
  183: int
  184: main(int argc, char **argv)
  185: {
  186: 	sched_root_task_t *root;
  187: 	int f, fd;
  188: 	struct sockaddr_in sin;
  189: 	struct timespec ts = { 20, 0 };
  190: 	struct timespec p = { 0, 10000000 };
  191: //	struct sigaction sa;
  192: 	sched_task_t *t;
  193: #ifdef AIO_SUPPORT
  194: 	char *ole = malloc(BUFSIZ);
  195: 	register int i;
  196: #endif
  197: 
  198: 	/*
  199: 	sa.sa_handler = sig;
  200: 	sigemptyset(&sa.sa_mask);
  201: 	sigaction(SIGTERM, &sa, NULL);
  202: 	*/
  203: 
  204: 	f = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  205: 	if (f == -1)
  206: 		return 1;
  207: #ifndef __linux__
  208: 	sin.sin_len = sizeof sin;
  209: #endif
  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: 
  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: 
  221: 	root = schedBegin();
  222: 	if (!root) {
  223: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  224: 		return 1;
  225: 	}
  226: 
  227: 	if (argc > 1)
  228: 		ROOT_PROFILING(root, prof);
  229: 
  230: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  231: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  232: 		return 4;
  233: 	} else
  234: 		ts.tv_sec = 15;
  235: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  236: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  237: 		return 4;
  238: 	} else
  239: 		ts.tv_sec = 10;
  240: 
  241: 	if (!schedEvent(root, event, "piuk", 1234, NULL, 0)) {
  242: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  243: 		return 2;
  244: 	}
  245: 
  246: 	if (!schedTask(root, regular, "piuk", 1111, NULL, 0)) {
  247: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  248: 		return 3;
  249: 	}
  250: 
  251: 	if (!schedTimer(root, timer, (void*) (intptr_t) ts.tv_sec, ts, NULL, 0)) {
  252: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  253: 		return 4;
  254: 	}
  255: 
  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: 	}
  260: 	if (!schedRead(root, r, "rrr", f, NULL, 0)) {
  261: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  262: 		return 5;
  263: 	}
  264: 	schedCancel(t);
  265: 
  266: 	if (!schedWrite(root, w, "www", f, NULL, 0)) {
  267: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  268: 		return 6;
  269: 	}
  270: 
  271: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  272: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  273: 		return 7;
  274: 	}
  275: 	if (!schedThread(root, thr, "mdaaaa this is thread task", 8192, NULL, 0)) {
  276: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  277: 		printf("stack is too small\n");
  278: 	}
  279: 	if (!schedThread(root, thr, "mdaaaa this is thread task -detached", 131072, NULL, 0)) {
  280: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  281: 		return 7;
  282: 	}
  283: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j", 0, NULL, 0)) {
  284: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  285: 		return 7;
  286: 	}
  287: 	printf("~~~try to cancel tid = %lx\n", TASK_VAL(t));
  288: 	schedCancel(t);
  289: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j2", 0, NULL, 0)) {
  290: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  291: 		return 7;
  292: 	}
  293: 	if (!(t = schedThread(root, thr4kill, "0aaaa", 0, NULL, 0))) {
  294: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  295: 		return 7;
  296: 	}
  297: 	if (!schedThread(root, thr, "mdaaaa this is thread task -j3", 4096, NULL, 0)) {
  298: 		printf("Error:: #%d - %s\n", sched_GetErrno(), sched_GetError());
  299: 		printf("stack is too small\n");
  300: 	}
  301: 	sleep(1);
  302: 	schedCancel(t);
  303: 
  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: 
  340: 	schedCallOnce(root, once, "000000", 42, NULL, 0);
  341: 
  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);
  349: 	schedRun(root, Kill);
  350: 	schedSignalDispatch(root, 0);
  351: 	schedEnd(&root);
  352: 	sleep(1);
  353: 
  354: #ifdef AIO_SUPPORT
  355: 	for (i = 0; i < 3; i++)
  356: 		free(iv[i].iov_base);
  357: #endif
  358: 
  359: 	close(fd);
  360: 	close(f);
  361: 
  362: 	if (argc > 1)
  363: 		printf("Total spend of time for all tasks = %lu\n", total);
  364: 	return 0;
  365: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>