Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024, 2025 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * (author) M Kersten, S Mullender
15 : * Dataflow processing only works on a code
16 : * sequence that does not include additional (implicit) flow of control
17 : * statements and, ideally, consist of expensive BAT operations.
18 : * The dataflow portion is identified as a guarded block,
19 : * whose entry is controlled by the function language.dataflow();
20 : *
21 : * The dataflow worker tries to follow the sequence of actions
22 : * as laid out in the plan, but abandon this track when it hits
23 : * a blocking operator, or an instruction for which not all arguments
24 : * are available or resources become scarce.
25 : *
26 : * The flow graphs is organized such that parallel threads can
27 : * access it mostly without expensive locking and dependent
28 : * variables are easy to find..
29 : */
30 : #include "monetdb_config.h"
31 : #include "mal_dataflow.h"
32 : #include "mal_exception.h"
33 : #include "mal_private.h"
34 : #include "mal_internal.h"
35 : #include "mal_runtime.h"
36 : #include "mal_resource.h"
37 : #include "mal_function.h"
38 :
39 : #define DFLOWpending 0 /* runnable */
40 : #define DFLOWrunning 1 /* currently in progress */
41 : #define DFLOWwrapup 2 /* done! */
42 : #define DFLOWretry 3 /* reschedule */
43 : #define DFLOWskipped 4 /* due to errors */
44 :
45 : /* The per instruction status of execution */
46 : typedef struct FLOWEVENT {
47 : struct DATAFLOW *flow; /* execution context */
48 : int pc; /* pc in underlying malblock */
49 : int blocks; /* awaiting for variables */
50 : sht state; /* of execution */
51 : lng clk;
52 : sht cost;
53 : lng hotclaim; /* memory foot print of result variables */
54 : lng argclaim; /* memory foot print of arguments */
55 : lng maxclaim; /* memory foot print of largest argument, could be used to indicate result size */
56 : struct FLOWEVENT *next; /* linked list for queues */
57 : } *FlowEvent, FlowEventRec;
58 :
59 : typedef struct queue {
60 : int exitcount; /* how many threads should exit */
61 : FlowEvent first, last; /* first and last element of the queue */
62 : MT_Lock l; /* it's a shared resource, ie we need locks */
63 : MT_Sema s; /* threads wait on empty queues */
64 : } Queue;
65 :
66 : /*
67 : * The dataflow dependency is administered in a graph list structure.
68 : * For each instruction we keep the list of instructions that
69 : * should be checked for eligibility once we are finished with it.
70 : */
71 : typedef struct DATAFLOW {
72 : Client cntxt; /* for debugging and client resolution */
73 : MalBlkPtr mb; /* carry the context */
74 : MalStkPtr stk;
75 : int start, stop; /* guarded block under consideration */
76 : FlowEvent status; /* status of each instruction */
77 : ATOMIC_PTR_TYPE error; /* error encountered */
78 : int *nodes; /* dependency graph nodes */
79 : int *edges; /* dependency graph */
80 : MT_Lock flowlock; /* lock to protect the above */
81 : Queue *done; /* instructions handled */
82 : bool set_qry_ctx;
83 : } *DataFlow, DataFlowRec;
84 :
85 : struct worker {
86 : MT_Id id;
87 : enum { WAITING, RUNNING, FREE, EXITED, FINISHING } flag;
88 : ATOMIC_PTR_TYPE cntxt; /* client we do work for (NULL -> any) */
89 : MT_Sema s;
90 : struct worker *next;
91 : char errbuf[GDKMAXERRLEN]; /* GDKerrbuf so that we can allocate before fork */
92 : };
93 : /* heads of three mutually exclusive linked lists, all using the .next
94 : * field in the worker struct */
95 : static struct worker *workers; /* "working" workers */
96 : static struct worker *exited_workers; /* to be joined threads (.flag==EXITED) */
97 : static struct worker *free_workers; /* free workers (.flag==FREE) */
98 : static int free_count = 0; /* number of free threads */
99 : static int free_max = 0; /* max number of spare free threads */
100 :
101 : static Queue *todo = 0; /* pending instructions */
102 :
103 : static ATOMIC_TYPE exiting = ATOMIC_VAR_INIT(0);
104 : static MT_Lock dataflowLock = MT_LOCK_INITIALIZER(dataflowLock);
105 :
106 : /*
107 : * Calculate the size of the dataflow dependency graph.
108 : */
109 : static int
110 : DFLOWgraphSize(MalBlkPtr mb, int start, int stop)
111 : {
112 : int cnt = 0;
113 :
114 14456983 : for (int i = start; i < stop; i++)
115 14296325 : cnt += getInstrPtr(mb, i)->argc;
116 160658 : return cnt;
117 : }
118 :
119 : /*
120 : * The dataflow execution is confined to a barrier block.
121 : * Within the block there are multiple flows, which, in principle,
122 : * can be executed in parallel.
123 : */
124 :
125 : static Queue *
126 161015 : q_create(const char *name)
127 : {
128 161015 : Queue *q = GDKzalloc(sizeof(Queue));
129 :
130 161015 : if (q == NULL)
131 : return NULL;
132 161015 : MT_lock_init(&q->l, name);
133 161015 : MT_sema_init(&q->s, 0, name);
134 161015 : return q;
135 : }
136 :
137 : static void
138 160658 : q_destroy(Queue *q)
139 : {
140 160658 : assert(q);
141 160658 : MT_lock_destroy(&q->l);
142 160657 : MT_sema_destroy(&q->s);
143 160650 : GDKfree(q);
144 160656 : }
145 :
146 : /* keep a simple LIFO queue. It won't be a large one, so shuffles of requeue is possible */
147 : /* we might actually sort it for better scheduling behavior */
148 : static void
149 22316495 : q_enqueue(Queue *q, FlowEvent d)
150 : {
151 22316495 : assert(q);
152 22316495 : assert(d);
153 22316495 : MT_lock_set(&q->l);
154 22342033 : if (q->first == NULL) {
155 5396497 : assert(q->last == NULL);
156 5396497 : q->first = q->last = d;
157 : } else {
158 16945536 : assert(q->last != NULL);
159 16945536 : q->last->next = d;
160 16945536 : q->last = d;
161 : }
162 22342033 : d->next = NULL;
163 22342033 : MT_lock_unset(&q->l);
164 22367881 : MT_sema_up(&q->s);
165 22326640 : }
166 :
167 : /*
168 : * A priority queue over the hot claims of memory may
169 : * be more effective. It prioritizes those instructions
170 : * that want to use a big recent result
171 : */
172 :
173 : static void
174 0 : q_requeue(Queue *q, FlowEvent d)
175 : {
176 0 : assert(q);
177 0 : assert(d);
178 0 : MT_lock_set(&q->l);
179 0 : if (q->first == NULL) {
180 0 : assert(q->last == NULL);
181 0 : q->first = q->last = d;
182 0 : d->next = NULL;
183 : } else {
184 0 : assert(q->last != NULL);
185 0 : d->next = q->first;
186 0 : q->first = d;
187 : }
188 0 : MT_lock_unset(&q->l);
189 0 : MT_sema_up(&q->s);
190 0 : }
191 :
192 : static FlowEvent
193 22609632 : q_dequeue(Queue *q, Client cntxt)
194 : {
195 22609632 : assert(q);
196 22609632 : MT_sema_down(&q->s);
197 22621924 : if (ATOMIC_GET(&exiting))
198 : return NULL;
199 22619373 : MT_lock_set(&q->l);
200 22592480 : if (cntxt == NULL && q->exitcount > 0) {
201 160658 : q->exitcount--;
202 160658 : MT_lock_unset(&q->l);
203 160658 : return NULL;
204 : }
205 :
206 22431822 : FlowEvent *dp = &q->first;
207 22431822 : FlowEvent pd = NULL;
208 : /* if cntxt == NULL, return the first event, if cntxt != NULL, find
209 : * the first event in the queue with matching cntxt value and return
210 : * that */
211 22431822 : if (cntxt != NULL) {
212 21948049 : while (*dp && (*dp)->flow->cntxt != cntxt) {
213 20399697 : pd = *dp;
214 20399697 : dp = &pd->next;
215 : }
216 : }
217 22431822 : FlowEvent d = *dp;
218 22431822 : if (d) {
219 22295283 : *dp = d->next;
220 22295283 : d->next = NULL;
221 22295283 : if (*dp == NULL)
222 5415115 : q->last = pd;
223 : }
224 22431822 : MT_lock_unset(&q->l);
225 22402422 : return d;
226 : }
227 :
228 : /*
229 : * We simply move an instruction into the front of the queue.
230 : * Beware, we assume that variables are assigned a value once, otherwise
231 : * the order may really create errors.
232 : * The order of the instructions should be retained as long as possible.
233 : * Delay processing when we run out of memory. Push the instruction back
234 : * on the end of queue, waiting for another attempt. Problem might become
235 : * that all threads but one are cycling through the queue, each time
236 : * finding an eligible instruction, but without enough space.
237 : * Therefore, we wait for a few milliseconds as an initial punishment.
238 : *
239 : * The process could be refined by checking for cheap operations,
240 : * i.e. those that would require no memory at all (aggr.count)
241 : * This, however, would lead to a dependency to the upper layers,
242 : * because in the kernel we don't know what routines are available
243 : * with this property. Nor do we maintain such properties.
244 : */
245 :
246 : static void
247 4734 : DFLOWworker(void *T)
248 : {
249 4734 : struct worker *t = (struct worker *) T;
250 4734 : bool locked = false;
251 : #ifdef _MSC_VER
252 : srand((unsigned int) GDKusec());
253 : #endif
254 4734 : GDKsetbuf(t->errbuf); /* where to leave errors */
255 4732 : snprintf(t->s.name, sizeof(t->s.name), "DFLOWsema%04zu", MT_getpid());
256 :
257 163111 : for (;;) {
258 163111 : DataFlow flow;
259 163111 : FlowEvent fe = 0, fnxt = 0;
260 163111 : str error = 0;
261 163111 : int i;
262 163111 : lng claim;
263 163111 : Client cntxt;
264 163111 : InstrPtr p;
265 :
266 163111 : GDKclrerr();
267 :
268 163064 : if (t->flag == WAITING) {
269 : /* wait until we are allowed to start working */
270 160643 : MT_sema_down(&t->s);
271 160649 : t->flag = RUNNING;
272 160649 : if (ATOMIC_GET(&exiting)) {
273 : break;
274 : }
275 : }
276 163070 : assert(t->flag == RUNNING);
277 163070 : cntxt = ATOMIC_PTR_GET(&t->cntxt);
278 14391066 : while (1) {
279 14391066 : MT_thread_set_qry_ctx(NULL);
280 14363746 : if (fnxt == 0) {
281 8523777 : MT_thread_setworking("waiting for work");
282 8532563 : cntxt = ATOMIC_PTR_GET(&t->cntxt);
283 8532563 : fe = q_dequeue(todo, cntxt);
284 8543064 : if (fe == NULL) {
285 299461 : if (cntxt) {
286 : /* we're not done yet with work for the current
287 : * client (as far as we know), so give up the CPU
288 : * and let the scheduler enter some more work, but
289 : * first compensate for the down we did in
290 : * dequeue */
291 136925 : MT_sema_up(&todo->s);
292 136923 : MT_sleep_ms(1);
293 14527860 : continue;
294 : }
295 : /* no more work to be done: exit */
296 162536 : break;
297 : }
298 8243603 : if (fe->flow->cntxt && fe->flow->cntxt->mythread)
299 8242769 : MT_thread_setworking(fe->flow->cntxt->mythread);
300 : } else
301 : fe = fnxt;
302 14085223 : if (ATOMIC_GET(&exiting)) {
303 : break;
304 : }
305 14085223 : fnxt = 0;
306 14085223 : assert(fe);
307 14085223 : flow = fe->flow;
308 14085223 : assert(flow);
309 14085223 : MT_thread_set_qry_ctx(flow->set_qry_ctx ? &flow->cntxt->
310 : qryctx : NULL);
311 :
312 : /* whenever we have a (concurrent) error, skip it */
313 14102472 : if (ATOMIC_PTR_GET(&flow->error)) {
314 4295 : q_enqueue(flow->done, fe);
315 4289 : continue;
316 : }
317 :
318 14098177 : p = getInstrPtr(flow->mb, fe->pc);
319 14098177 : claim = fe->argclaim;
320 28223787 : if (p->fcn != (MALfcn) deblockdataflow && /* never block on deblockdataflow() */
321 14098177 : !MALadmission_claim(flow->cntxt, flow->mb, flow->stk, p, claim)) {
322 0 : fe->hotclaim = 0; /* don't assume priority anymore */
323 0 : fe->maxclaim = 0;
324 0 : MT_lock_set(&todo->l);
325 0 : FlowEvent last = todo->last;
326 0 : MT_lock_unset(&todo->l);
327 0 : if (last == NULL)
328 0 : MT_sleep_ms(DELAYUNIT);
329 0 : q_requeue(todo, fe);
330 0 : continue;
331 : }
332 14125610 : ATOMIC_BASE_TYPE wrks = ATOMIC_INC(&flow->cntxt->workers);
333 14125610 : ATOMIC_BASE_TYPE mwrks = ATOMIC_GET(&flow->mb->workers);
334 14126023 : while (wrks > mwrks) {
335 39406 : if (ATOMIC_CAS(&flow->mb->workers, &mwrks, wrks))
336 : break;
337 : }
338 14125638 : error = runMALsequence(flow->cntxt, flow->mb, fe->pc, fe->pc + 1,
339 : flow->stk, 0, 0);
340 13973316 : ATOMIC_DEC(&flow->cntxt->workers);
341 : /* release the memory claim */
342 13973316 : MALadmission_release(flow->cntxt, flow->mb, flow->stk, p, claim);
343 :
344 14045463 : MT_lock_set(&flow->flowlock);
345 14128169 : fe->state = DFLOWwrapup;
346 14128169 : MT_lock_unset(&flow->flowlock);
347 14109721 : if (error) {
348 529 : void *null = NULL;
349 : /* only collect one error (from one thread, needed for stable testing) */
350 529 : if (!ATOMIC_PTR_CAS(&flow->error, &null, error))
351 47 : freeException(error);
352 : /* after an error we skip the rest of the block */
353 529 : q_enqueue(flow->done, fe);
354 529 : continue;
355 : }
356 :
357 : /* see if you can find an eligible instruction that uses the
358 : * result just produced. Then we can continue with it right away.
359 : * We are just looking forward for the last block, which means we
360 : * are safe from concurrent actions. No other thread can steal it,
361 : * because we hold the logical lock.
362 : * All eligible instructions are queued
363 : */
364 14109192 : p = getInstrPtr(flow->mb, fe->pc);
365 14109192 : assert(p);
366 14109192 : fe->hotclaim = 0;
367 14109192 : fe->maxclaim = 0;
368 :
369 29383961 : for (i = 0; i < p->retc; i++) {
370 15281577 : lng footprint;
371 15281577 : footprint = getMemoryClaim(flow->mb, flow->stk, p, i, FALSE);
372 15274769 : fe->hotclaim += footprint;
373 15274769 : if (footprint > fe->maxclaim)
374 5504428 : fe->maxclaim = footprint;
375 : }
376 :
377 : /* Try to get rid of the hot potato or locate an alternative to proceed.
378 : */
379 : #define HOTPOTATO
380 : #ifdef HOTPOTATO
381 : /* HOT potato choice */
382 14102384 : int last = 0, nxt = -1;
383 14102384 : lng nxtclaim = -1;
384 :
385 14102384 : MT_lock_set(&flow->flowlock);
386 14129328 : for (last = fe->pc - flow->start;
387 56591277 : last >= 0 && (i = flow->nodes[last]) > 0;
388 42461949 : last = flow->edges[last]) {
389 42461949 : if (flow->status[i].state == DFLOWpending
390 42457122 : && flow->status[i].blocks == 1) {
391 : /* find the one with the largest footprint */
392 11519995 : if (nxt == -1 || flow->status[i].argclaim > nxtclaim) {
393 6368373 : nxt = i;
394 6368373 : nxtclaim = flow->status[i].argclaim;
395 : }
396 : }
397 : }
398 : /* hot potato can not be removed, use alternative to proceed */
399 14129328 : if (nxt >= 0) {
400 5890221 : flow->status[nxt].state = DFLOWrunning;
401 5890221 : flow->status[nxt].blocks = 0;
402 5890221 : flow->status[nxt].hotclaim = fe->hotclaim;
403 5890221 : flow->status[nxt].argclaim += fe->hotclaim;
404 5890221 : if (flow->status[nxt].maxclaim < fe->maxclaim)
405 2284914 : flow->status[nxt].maxclaim = fe->maxclaim;
406 : fnxt = flow->status + nxt;
407 : }
408 14129328 : MT_lock_unset(&flow->flowlock);
409 : #endif
410 :
411 14111753 : q_enqueue(flow->done, fe);
412 14086384 : if (fnxt == 0 && profilerStatus) {
413 0 : profilerHeartbeatEvent("wait");
414 : }
415 : }
416 162536 : MT_lock_set(&dataflowLock);
417 163144 : if (GDKexiting() || ATOMIC_GET(&exiting) || free_count >= free_max) {
418 : locked = true;
419 : break;
420 : }
421 159045 : free_count++;
422 159045 : struct worker **tp = &workers;
423 935912 : while (*tp && *tp != t)
424 776867 : tp = &(*tp)->next;
425 159045 : assert(*tp && *tp == t);
426 159045 : *tp = t->next;
427 159045 : t->flag = FREE;
428 159045 : t->next = free_workers;
429 159045 : free_workers = t;
430 159045 : MT_lock_unset(&dataflowLock);
431 159045 : MT_thread_setworking("idle, waiting for new client");
432 159045 : MT_sema_down(&t->s);
433 159041 : if (GDKexiting() || ATOMIC_GET(&exiting))
434 : break;
435 158388 : assert(t->flag == WAITING);
436 : }
437 637 : if (!locked)
438 637 : MT_lock_set(&dataflowLock);
439 4736 : if (t->flag != FINISHING) {
440 4058 : struct worker **tp = t->flag == FREE ? &free_workers : &workers;
441 17524 : while (*tp && *tp != t)
442 13466 : tp = &(*tp)->next;
443 4058 : assert(*tp && *tp == t);
444 4058 : *tp = t->next;
445 4058 : t->flag = EXITED;
446 4058 : t->next = exited_workers;
447 4058 : exited_workers = t;
448 : }
449 4736 : MT_lock_unset(&dataflowLock);
450 4730 : GDKsetbuf(NULL);
451 4722 : }
452 :
453 : /*
454 : * Create an interpreter pool.
455 : * One worker will adaptively be available for each client.
456 : * The remainder are taken from the GDKnr_threads argument and
457 : * typically is equal to the number of cores
458 : * The workers are assembled in a local table to enable debugging.
459 : */
460 : static int
461 357 : DFLOWinitialize(void)
462 : {
463 357 : int limit;
464 357 : int created = 0;
465 :
466 357 : MT_lock_set(&mal_contextLock);
467 357 : MT_lock_set(&dataflowLock);
468 357 : if (todo) {
469 : /* somebody else beat us to it */
470 0 : MT_lock_unset(&dataflowLock);
471 0 : MT_lock_unset(&mal_contextLock);
472 0 : return 0;
473 : }
474 357 : free_max = GDKgetenv_int("dataflow_max_free",
475 : GDKnr_threads < 4 ? 4 : GDKnr_threads);
476 357 : todo = q_create("todo");
477 357 : if (todo == NULL) {
478 0 : MT_lock_unset(&dataflowLock);
479 0 : MT_lock_unset(&mal_contextLock);
480 0 : return -1;
481 : }
482 357 : limit = GDKnr_threads ? GDKnr_threads - 1 : 0;
483 2850 : while (limit > 0) {
484 2493 : limit--;
485 2493 : struct worker *t = GDKmalloc(sizeof(*t));
486 2493 : if (t == NULL) {
487 0 : TRC_CRITICAL(MAL_SERVER, "cannot allocate structure for worker");
488 0 : continue;
489 : }
490 2493 : *t = (struct worker) {
491 : .flag = RUNNING,
492 : .cntxt = ATOMIC_PTR_VAR_INIT(NULL),
493 : };
494 2493 : MT_sema_init(&t->s, 0, "DFLOWsema"); /* placeholder name */
495 2493 : if (MT_create_thread(&t->id, DFLOWworker, t,
496 : MT_THR_JOINABLE, "DFLOWworkerXXXX") < 0) {
497 0 : MT_sema_destroy(&t->s);
498 0 : GDKfree(t);
499 : } else {
500 2493 : t->next = workers;
501 2493 : workers = t;
502 2493 : created++;
503 : }
504 : }
505 357 : if (created == 0) {
506 : /* no threads created */
507 0 : q_destroy(todo);
508 0 : todo = NULL;
509 0 : MT_lock_unset(&dataflowLock);
510 0 : MT_lock_unset(&mal_contextLock);
511 0 : return -1;
512 : }
513 357 : MT_lock_unset(&dataflowLock);
514 357 : MT_lock_unset(&mal_contextLock);
515 357 : return 0;
516 : }
517 :
518 : /*
519 : * The dataflow administration is based on administration of
520 : * how many variables are still missing before it can be executed.
521 : * For each instruction we keep a list of instructions whose
522 : * blocking counter should be decremented upon finishing it.
523 : */
524 : static str
525 160658 : DFLOWinitBlk(DataFlow flow, MalBlkPtr mb, int size)
526 : {
527 160658 : int pc, i, j, k, l, n, etop = 0;
528 160658 : int *assign;
529 160658 : InstrPtr p;
530 :
531 160658 : if (flow == NULL)
532 0 : throw(MAL, "dataflow", "DFLOWinitBlk(): Called with flow == NULL");
533 160658 : if (mb == NULL)
534 0 : throw(MAL, "dataflow", "DFLOWinitBlk(): Called with mb == NULL");
535 160658 : assign = (int *) GDKzalloc(mb->vtop * sizeof(int));
536 160658 : if (assign == NULL)
537 0 : throw(MAL, "dataflow", SQLSTATE(HY013) MAL_MALLOC_FAIL);
538 160658 : etop = flow->stop - flow->start;
539 14295627 : for (n = 0, pc = flow->start; pc < flow->stop; pc++, n++) {
540 14134969 : p = getInstrPtr(mb, pc);
541 14134969 : if (p == NULL) {
542 0 : GDKfree(assign);
543 0 : throw(MAL, "dataflow",
544 : "DFLOWinitBlk(): getInstrPtr() returned NULL");
545 : }
546 :
547 : /* initial state, ie everything can run */
548 14134969 : flow->status[n].flow = flow;
549 14134969 : flow->status[n].pc = pc;
550 14134969 : flow->status[n].state = DFLOWpending;
551 14134969 : flow->status[n].cost = -1;
552 14134969 : ATOMIC_PTR_SET(&flow->status[n].flow->error, NULL);
553 :
554 : /* administer flow dependencies */
555 67001962 : for (j = p->retc; j < p->argc; j++) {
556 : /* list of instructions that wake n-th instruction up */
557 52866993 : if (!isVarConstant(mb, getArg(p, j)) && (k = assign[getArg(p, j)])) {
558 26151801 : assert(k < pc); /* only dependencies on earlier instructions */
559 : /* add edge to the target instruction for wakeup call */
560 26151801 : k -= flow->start;
561 26151801 : if (flow->nodes[k]) {
562 : /* add wakeup to tail of list */
563 229741857 : for (i = k; flow->edges[i] > 0; i = flow->edges[i])
564 : ;
565 23506110 : flow->nodes[etop] = n;
566 23506110 : flow->edges[etop] = -1;
567 23506110 : flow->edges[i] = etop;
568 23506110 : etop++;
569 23506110 : (void) size;
570 23506110 : if (etop == size) {
571 212 : int *tmp;
572 : /* in case of realloc failure, the original
573 : * pointers will be freed by the caller */
574 212 : tmp = (int *) GDKrealloc(flow->nodes,
575 : sizeof(int) * 2 * size);
576 212 : if (tmp == NULL) {
577 0 : GDKfree(assign);
578 0 : throw(MAL, "dataflow",
579 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
580 : }
581 212 : flow->nodes = tmp;
582 212 : tmp = (int *) GDKrealloc(flow->edges,
583 : sizeof(int) * 2 * size);
584 212 : if (tmp == NULL) {
585 0 : GDKfree(assign);
586 0 : throw(MAL, "dataflow",
587 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
588 : }
589 212 : flow->edges = tmp;
590 212 : size *= 2;
591 : }
592 : } else {
593 2645691 : flow->nodes[k] = n;
594 2645691 : flow->edges[k] = -1;
595 : }
596 :
597 26151801 : flow->status[n].blocks++;
598 : }
599 :
600 : /* list of instructions to be woken up explicitly */
601 52866993 : if (!isVarConstant(mb, getArg(p, j))) {
602 : /* be careful, watch out for garbage collection interference */
603 : /* those should be scheduled after all its other uses */
604 28046928 : l = getEndScope(mb, getArg(p, j));
605 28046928 : if (l != pc && l < flow->stop && l > flow->start) {
606 : /* add edge to the target instruction for wakeup call */
607 16331820 : assert(pc < l); /* only dependencies on earlier instructions */
608 16331820 : l -= flow->start;
609 16331820 : if (flow->nodes[n]) {
610 : /* add wakeup to tail of list */
611 78373501 : for (i = n; flow->edges[i] > 0; i = flow->edges[i])
612 : ;
613 8024700 : flow->nodes[etop] = l;
614 8024700 : flow->edges[etop] = -1;
615 8024700 : flow->edges[i] = etop;
616 8024700 : etop++;
617 8024700 : if (etop == size) {
618 209 : int *tmp;
619 : /* in case of realloc failure, the original
620 : * pointers will be freed by the caller */
621 209 : tmp = (int *) GDKrealloc(flow->nodes,
622 : sizeof(int) * 2 * size);
623 209 : if (tmp == NULL) {
624 0 : GDKfree(assign);
625 0 : throw(MAL, "dataflow",
626 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
627 : }
628 209 : flow->nodes = tmp;
629 209 : tmp = (int *) GDKrealloc(flow->edges,
630 : sizeof(int) * 2 * size);
631 209 : if (tmp == NULL) {
632 0 : GDKfree(assign);
633 0 : throw(MAL, "dataflow",
634 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
635 : }
636 209 : flow->edges = tmp;
637 209 : size *= 2;
638 : }
639 : } else {
640 8307120 : flow->nodes[n] = l;
641 8307120 : flow->edges[n] = -1;
642 : }
643 16331820 : flow->status[l].blocks++;
644 : }
645 : }
646 : }
647 :
648 29448137 : for (j = 0; j < p->retc; j++)
649 15313168 : assign[getArg(p, j)] = pc; /* ensure recognition of dependency on first instruction and constant */
650 : }
651 160658 : GDKfree(assign);
652 :
653 160658 : return MAL_SUCCEED;
654 : }
655 :
656 : /*
657 : * Parallel processing is mostly driven by dataflow, but within this context
658 : * there may be different schemes to take instructions into execution.
659 : * The admission scheme (and wrapup) are the necessary scheduler hooks.
660 : * A scheduler registers the functions needed and should release them
661 : * at the end of the parallel block.
662 : * They take effect after we have ensured that the basic properties for
663 : * execution hold.
664 : */
665 : static str
666 160658 : DFLOWscheduler(DataFlow flow, struct worker *w)
667 : {
668 160658 : int last;
669 160658 : int i;
670 160658 : int j;
671 160658 : InstrPtr p;
672 160658 : int tasks = 0, actions = 0;
673 160658 : str ret = MAL_SUCCEED;
674 160658 : FlowEvent fe, f = 0;
675 :
676 160658 : if (flow == NULL)
677 0 : throw(MAL, "dataflow", "DFLOWscheduler(): Called with flow == NULL");
678 160658 : actions = flow->stop - flow->start;
679 160658 : if (actions == 0)
680 0 : throw(MAL, "dataflow", "Empty dataflow block");
681 : /* initialize the eligible statements */
682 160658 : fe = flow->status;
683 :
684 160658 : ATOMIC_DEC(&flow->cntxt->workers);
685 160658 : MT_lock_set(&flow->flowlock);
686 14456864 : for (i = 0; i < actions; i++)
687 14135548 : if (fe[i].blocks == 0) {
688 1056356 : p = getInstrPtr(flow->mb, fe[i].pc);
689 1056356 : if (p == NULL) {
690 0 : MT_lock_unset(&flow->flowlock);
691 0 : ATOMIC_INC(&flow->cntxt->workers);
692 0 : throw(MAL, "dataflow",
693 : "DFLOWscheduler(): getInstrPtr(flow->mb,fe[i].pc) returned NULL");
694 : }
695 1056356 : fe[i].argclaim = 0;
696 6348447 : for (j = p->retc; j < p->argc; j++)
697 5292091 : fe[i].argclaim += getMemoryClaim(fe[0].flow->mb,
698 5292100 : fe[0].flow->stk, p, j, FALSE);
699 1056347 : flow->status[i].state = DFLOWrunning;
700 1056347 : q_enqueue(todo, flow->status + i);
701 : }
702 160658 : MT_lock_unset(&flow->flowlock);
703 160657 : MT_sema_up(&w->s);
704 :
705 160657 : while (actions != tasks) {
706 14134594 : f = q_dequeue(flow->done, NULL);
707 14134935 : if (ATOMIC_GET(&exiting))
708 : break;
709 14134935 : if (f == NULL) {
710 0 : ATOMIC_INC(&flow->cntxt->workers);
711 0 : throw(MAL, "dataflow",
712 : "DFLOWscheduler(): q_dequeue(flow->done) returned NULL");
713 : }
714 :
715 : /*
716 : * When an instruction is finished we have to reduce the blocked
717 : * counter for all dependent instructions. for those where it
718 : * drops to zero we can scheduler it we do it here instead of the scheduler
719 : */
720 :
721 14134935 : MT_lock_set(&flow->flowlock);
722 14134573 : tasks++;
723 14134573 : for (last = f->pc - flow->start;
724 56609846 : last >= 0 && (i = flow->nodes[last]) > 0; last = flow->edges[last])
725 42475658 : if (flow->status[i].state == DFLOWpending) {
726 36591131 : flow->status[i].argclaim += f->hotclaim;
727 36591131 : if (flow->status[i].blocks == 1) {
728 7188396 : flow->status[i].blocks--;
729 7188396 : flow->status[i].state = DFLOWrunning;
730 7188396 : q_enqueue(todo, flow->status + i);
731 : } else {
732 29402735 : flow->status[i].blocks--;
733 : }
734 : }
735 14294844 : MT_lock_unset(&flow->flowlock);
736 : }
737 : /* release the worker from its specific task (turn it into a
738 : * generic worker) */
739 160657 : ATOMIC_PTR_SET(&w->cntxt, NULL);
740 160657 : ATOMIC_INC(&flow->cntxt->workers);
741 : /* wrap up errors */
742 160657 : assert(flow->done->last == 0);
743 160657 : if ((ret = ATOMIC_PTR_XCG(&flow->error, NULL)) != NULL) {
744 482 : TRC_DEBUG(MAL_SERVER, "Errors encountered: %s\n", ret);
745 : }
746 : return ret;
747 : }
748 :
749 : /* called and returns with dataflowLock locked, temporarily unlocks
750 : * join the thread associated with the worker and destroy the structure */
751 : static inline void
752 4736 : finish_worker(struct worker *t)
753 : {
754 4736 : t->flag = FINISHING;
755 4736 : MT_lock_unset(&dataflowLock);
756 4736 : MT_join_thread(t->id);
757 4736 : MT_sema_destroy(&t->s);
758 4736 : GDKfree(t);
759 4736 : MT_lock_set(&dataflowLock);
760 4736 : }
761 :
762 : /* We create a pool of GDKnr_threads-1 generic workers, that is,
763 : * workers that will take on jobs from any clients. In addition, we
764 : * create a single specific worker per client (i.e. each time we enter
765 : * here). This specific worker will only do work for the client for
766 : * which it was started. In this way we can guarantee that there will
767 : * always be progress for the client, even if all other workers are
768 : * doing something big.
769 : *
770 : * When all jobs for a client have been done (there are no more
771 : * entries for the client in the queue), the specific worker turns
772 : * itself into a generic worker. At the same time, we signal that one
773 : * generic worker should exit and this function returns. In this way
774 : * we make sure that there are once again GDKnr_threads-1 generic
775 : * workers. */
776 : str
777 160645 : runMALdataflow(Client cntxt, MalBlkPtr mb, int startpc, int stoppc,
778 : MalStkPtr stk)
779 : {
780 160645 : DataFlow flow = NULL;
781 160645 : str msg = MAL_SUCCEED;
782 160645 : int size;
783 160645 : bit *ret;
784 160645 : struct worker *t;
785 :
786 160645 : if (stk == NULL)
787 0 : throw(MAL, "dataflow", "runMALdataflow(): Called with stk == NULL");
788 160645 : ret = getArgReference_bit(stk, getInstrPtr(mb, startpc), 0);
789 160645 : *ret = FALSE;
790 :
791 160645 : assert(stoppc > startpc);
792 :
793 : /* check existence of workers */
794 160645 : if (todo == NULL) {
795 : /* create thread pool */
796 357 : if (GDKnr_threads <= 1 || DFLOWinitialize() < 0) {
797 : /* no threads created, run serially */
798 0 : *ret = TRUE;
799 0 : return MAL_SUCCEED;
800 : }
801 : }
802 160645 : assert(todo);
803 : /* in addition, create one more worker that will only execute
804 : * tasks for the current client to compensate for our waiting
805 : * until all work is done */
806 160645 : MT_lock_set(&dataflowLock);
807 : /* join with already exited threads */
808 162271 : while (exited_workers != NULL) {
809 1613 : assert(exited_workers->flag == EXITED);
810 1613 : struct worker *t = exited_workers;
811 1613 : exited_workers = exited_workers->next;
812 1613 : finish_worker(t);
813 : }
814 160658 : assert(cntxt != NULL);
815 160658 : if (free_workers != NULL) {
816 158407 : t = free_workers;
817 158407 : assert(t->flag == FREE);
818 158407 : assert(free_count > 0);
819 158407 : free_count--;
820 158407 : free_workers = t->next;
821 158407 : t->next = workers;
822 158407 : workers = t;
823 158407 : t->flag = WAITING;
824 158407 : ATOMIC_PTR_SET(&t->cntxt, cntxt);
825 158407 : MT_sema_up(&t->s);
826 : } else {
827 2251 : t = GDKmalloc(sizeof(*t));
828 2251 : if (t != NULL) {
829 2251 : *t = (struct worker) {
830 : .flag = WAITING,
831 : .cntxt = ATOMIC_PTR_VAR_INIT(cntxt),
832 : };
833 2251 : MT_sema_init(&t->s, 0, "DFLOWsema"); /* placeholder name */
834 2251 : if (MT_create_thread(&t->id, DFLOWworker, t,
835 : MT_THR_JOINABLE, "DFLOWworkerXXXX") < 0) {
836 0 : MT_sema_destroy(&t->s);
837 0 : GDKfree(t);
838 0 : t = NULL;
839 : } else {
840 2251 : t->next = workers;
841 2251 : workers = t;
842 : }
843 : }
844 2251 : if (t == NULL) {
845 : /* cannot start new thread, run serially */
846 0 : *ret = TRUE;
847 0 : MT_lock_unset(&dataflowLock);
848 0 : return MAL_SUCCEED;
849 : }
850 : }
851 160658 : MT_lock_unset(&dataflowLock);
852 :
853 160658 : flow = (DataFlow) GDKzalloc(sizeof(DataFlowRec));
854 160658 : if (flow == NULL)
855 0 : throw(MAL, "dataflow", SQLSTATE(HY013) MAL_MALLOC_FAIL);
856 :
857 160658 : size = DFLOWgraphSize(mb, startpc, stoppc);
858 160658 : size += stoppc - startpc;
859 :
860 321316 : *flow = (DataFlowRec) {
861 : .cntxt = cntxt,
862 : .mb = mb,
863 : .stk = stk,
864 160658 : .set_qry_ctx = MT_thread_get_qry_ctx() != NULL,
865 : /* keep real block count, exclude brackets */
866 160658 : .start = startpc + 1,
867 : .stop = stoppc,
868 160658 : .done = q_create("flow->done"),
869 160658 : .status = (FlowEvent) GDKzalloc((stoppc - startpc + 1) *
870 : sizeof(FlowEventRec)),
871 : .error = ATOMIC_PTR_VAR_INIT(NULL),
872 160658 : .nodes = (int *) GDKzalloc(sizeof(int) * size),
873 160658 : .edges = (int *) GDKzalloc(sizeof(int) * size),
874 : };
875 :
876 160658 : if (flow->done == NULL) {
877 0 : GDKfree(flow->status);
878 0 : GDKfree(flow->nodes);
879 0 : GDKfree(flow->edges);
880 0 : GDKfree(flow);
881 0 : throw(MAL, "dataflow",
882 : "runMALdataflow(): Failed to create flow->done queue");
883 : }
884 :
885 160658 : if (flow->status == NULL || flow->nodes == NULL || flow->edges == NULL) {
886 0 : q_destroy(flow->done);
887 0 : GDKfree(flow->status);
888 0 : GDKfree(flow->nodes);
889 0 : GDKfree(flow->edges);
890 0 : GDKfree(flow);
891 0 : throw(MAL, "dataflow", SQLSTATE(HY013) MAL_MALLOC_FAIL);
892 : }
893 :
894 160658 : MT_lock_init(&flow->flowlock, "flow->flowlock");
895 160658 : msg = DFLOWinitBlk(flow, mb, size);
896 :
897 160658 : if (msg == MAL_SUCCEED)
898 160658 : msg = DFLOWscheduler(flow, t);
899 :
900 160658 : GDKfree(flow->status);
901 160657 : GDKfree(flow->edges);
902 160658 : GDKfree(flow->nodes);
903 160658 : q_destroy(flow->done);
904 160656 : MT_lock_destroy(&flow->flowlock);
905 160656 : GDKfree(flow);
906 :
907 : /* we created one worker, now tell one worker to exit again */
908 160657 : MT_lock_set(&todo->l);
909 160658 : todo->exitcount++;
910 160658 : MT_lock_unset(&todo->l);
911 160658 : MT_sema_up(&todo->s);
912 :
913 160658 : return msg;
914 : }
915 :
916 : str
917 0 : deblockdataflow(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
918 : {
919 0 : int *ret = getArgReference_int(stk, pci, 0);
920 0 : int *val = getArgReference_int(stk, pci, 1);
921 0 : (void) cntxt;
922 0 : (void) mb;
923 0 : *ret = *val;
924 0 : return MAL_SUCCEED;
925 : }
926 :
927 : static void
928 356 : stopMALdataflow(void)
929 : {
930 356 : ATOMIC_SET(&exiting, 1);
931 356 : if (todo) {
932 356 : MT_lock_set(&dataflowLock);
933 : /* first wake up all running threads */
934 356 : int n = 0;
935 993 : for (struct worker *t = free_workers; t; t = t->next)
936 637 : n++;
937 2842 : for (struct worker *t = workers; t; t = t->next)
938 2486 : n++;
939 3479 : while (n-- > 0) {
940 : /* one UP for each thread we know about */
941 3479 : MT_sema_up(&todo->s);
942 : }
943 993 : while (free_workers) {
944 637 : struct worker *t = free_workers;
945 637 : assert(free_count > 0);
946 637 : free_count--;
947 637 : free_workers = free_workers->next;
948 637 : MT_sema_up(&t->s);
949 637 : finish_worker(t);
950 : }
951 397 : while (workers) {
952 41 : struct worker *t = workers;
953 41 : workers = workers->next;
954 41 : finish_worker(t);
955 : }
956 2801 : while (exited_workers) {
957 2445 : struct worker *t = exited_workers;
958 2445 : exited_workers = exited_workers->next;
959 2445 : finish_worker(t);
960 : }
961 356 : MT_lock_unset(&dataflowLock);
962 : }
963 356 : }
964 :
965 : void
966 356 : mal_dataflow_reset(void)
967 : {
968 356 : stopMALdataflow();
969 356 : workers = exited_workers = NULL;
970 356 : if (todo) {
971 356 : MT_lock_destroy(&todo->l);
972 356 : MT_sema_destroy(&todo->s);
973 356 : GDKfree(todo);
974 : }
975 356 : todo = 0; /* pending instructions */
976 356 : ATOMIC_SET(&exiting, 0);
977 356 : }
|