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 9151643 : for (int i = start; i < stop; i++)
115 8993912 : cnt += getInstrPtr(mb, i)->argc;
116 157731 : 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 158087 : q_create(const char *name)
127 : {
128 158087 : Queue *q = GDKzalloc(sizeof(Queue));
129 :
130 158087 : if (q == NULL)
131 : return NULL;
132 158087 : MT_lock_init(&q->l, name);
133 158087 : MT_sema_init(&q->s, 0, name);
134 158087 : return q;
135 : }
136 :
137 : static void
138 157731 : q_destroy(Queue *q)
139 : {
140 157731 : assert(q);
141 157731 : MT_lock_destroy(&q->l);
142 157730 : MT_sema_destroy(&q->s);
143 157731 : GDKfree(q);
144 157731 : }
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 14285639 : q_enqueue(Queue *q, FlowEvent d)
150 : {
151 14285639 : assert(q);
152 14285639 : assert(d);
153 14285639 : MT_lock_set(&q->l);
154 14282028 : if (q->first == NULL) {
155 4728232 : assert(q->last == NULL);
156 4728232 : q->first = q->last = d;
157 : } else {
158 9553796 : assert(q->last != NULL);
159 9553796 : q->last->next = d;
160 9553796 : q->last = d;
161 : }
162 14282028 : d->next = NULL;
163 14282028 : MT_lock_unset(&q->l);
164 14284332 : MT_sema_up(&q->s);
165 14281988 : }
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 2092602 : q_requeue(Queue *q, FlowEvent d)
175 : {
176 2092602 : assert(q);
177 2092602 : assert(d);
178 2092602 : MT_lock_set(&q->l);
179 2092602 : if (q->first == NULL) {
180 26 : assert(q->last == NULL);
181 26 : q->first = q->last = d;
182 26 : d->next = NULL;
183 : } else {
184 2092576 : assert(q->last != NULL);
185 2092576 : d->next = q->first;
186 2092576 : q->first = d;
187 : }
188 2092602 : MT_lock_unset(&q->l);
189 2092602 : MT_sema_up(&q->s);
190 2092602 : }
191 :
192 : static FlowEvent
193 16757012 : q_dequeue(Queue *q, Client cntxt)
194 : {
195 16757012 : assert(q);
196 16757012 : MT_sema_down(&q->s);
197 16751030 : if (ATOMIC_GET(&exiting))
198 : return NULL;
199 16749699 : MT_lock_set(&q->l);
200 16728309 : if (cntxt == NULL && q->exitcount > 0) {
201 157731 : q->exitcount--;
202 157731 : MT_lock_unset(&q->l);
203 157731 : return NULL;
204 : }
205 :
206 16570578 : FlowEvent *dp = &q->first;
207 16570578 : 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 16570578 : if (cntxt != NULL) {
212 11565679 : while (*dp && (*dp)->flow->cntxt != cntxt) {
213 8807123 : pd = *dp;
214 8807123 : dp = &pd->next;
215 : }
216 : }
217 16570578 : FlowEvent d = *dp;
218 16570578 : if (d) {
219 16341536 : *dp = d->next;
220 16341536 : d->next = NULL;
221 16341536 : if (*dp == NULL)
222 4745423 : q->last = pd;
223 : }
224 16570578 : MT_lock_unset(&q->l);
225 16560106 : 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 7815 : DFLOWworker(void *T)
248 : {
249 7815 : struct worker *t = (struct worker *) T;
250 7815 : bool locked = false;
251 : #ifdef _MSC_VER
252 : srand((unsigned int) GDKusec());
253 : #endif
254 7815 : GDKsetbuf(t->errbuf); /* where to leave errors */
255 7815 : snprintf(t->s.name, sizeof(t->s.name), "DFLOWsema%04zu", MT_getpid());
256 :
257 158796 : for (;;) {
258 158796 : DataFlow flow;
259 158796 : FlowEvent fe = 0, fnxt = 0;
260 158796 : str error = 0;
261 158796 : int i;
262 158796 : lng claim;
263 158796 : Client cntxt;
264 158796 : InstrPtr p;
265 :
266 158796 : GDKclrerr();
267 :
268 158795 : if (t->flag == WAITING) {
269 : /* wait until we are allowed to start working */
270 157730 : MT_sema_down(&t->s);
271 157731 : t->flag = RUNNING;
272 157731 : if (ATOMIC_GET(&exiting)) {
273 : break;
274 : }
275 : }
276 158796 : assert(t->flag == RUNNING);
277 158796 : cntxt = ATOMIC_PTR_GET(&t->cntxt);
278 11314577 : while (1) {
279 11314577 : MT_thread_set_qry_ctx(NULL);
280 11305652 : if (fnxt == 0) {
281 7927402 : MT_thread_setworking("waiting for work");
282 7930656 : cntxt = ATOMIC_PTR_GET(&t->cntxt);
283 7930656 : fe = q_dequeue(todo, cntxt);
284 7931021 : if (fe == NULL) {
285 388104 : 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 229310 : MT_sema_up(&todo->s);
292 229310 : MT_sleep_ms(1);
293 11543883 : continue;
294 : }
295 : /* no more work to be done: exit */
296 158794 : break;
297 : }
298 7542917 : if (fe->flow->cntxt && fe->flow->cntxt->mythread)
299 7542066 : MT_thread_setworking(fe->flow->cntxt->mythread);
300 : } else
301 : fe = fnxt;
302 10920770 : if (ATOMIC_GET(&exiting)) {
303 : break;
304 : }
305 10920770 : fnxt = 0;
306 10920770 : assert(fe);
307 10920770 : flow = fe->flow;
308 10920770 : assert(flow);
309 10920770 : 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 10923668 : if (ATOMIC_PTR_GET(&flow->error)) {
314 4177 : q_enqueue(flow->done, fe);
315 4181 : continue;
316 : }
317 :
318 10919491 : p = getInstrPtr(flow->mb, fe->pc);
319 10919491 : claim = fe->argclaim;
320 21842142 : if (p->fcn != (MALfcn) deblockdataflow && /* never block on deblockdataflow() */
321 10919491 : !MALadmission_claim(flow->cntxt, flow->mb, flow->stk, p, claim)) {
322 2092602 : fe->hotclaim = 0; /* don't assume priority anymore */
323 2092602 : fe->maxclaim = 0;
324 2092602 : MT_lock_set(&todo->l);
325 2092602 : FlowEvent last = todo->last;
326 2092602 : MT_lock_unset(&todo->l);
327 2092602 : if (last == NULL)
328 26 : MT_sleep_ms(DELAYUNIT);
329 2092602 : q_requeue(todo, fe);
330 2092602 : continue;
331 : }
332 8830049 : ATOMIC_BASE_TYPE wrks = ATOMIC_INC(&flow->cntxt->workers);
333 8830049 : ATOMIC_BASE_TYPE mwrks = ATOMIC_GET(&flow->mb->workers);
334 8830062 : while (wrks > mwrks) {
335 17320 : if (ATOMIC_CAS(&flow->mb->workers, &mwrks, wrks))
336 : break;
337 : }
338 8830055 : error = runMALsequence(flow->cntxt, flow->mb, fe->pc, fe->pc + 1,
339 : flow->stk, 0, 0);
340 8829006 : ATOMIC_DEC(&flow->cntxt->workers);
341 : /* release the memory claim */
342 8829006 : MALadmission_release(flow->cntxt, flow->mb, flow->stk, p, claim);
343 :
344 8829954 : MT_lock_set(&flow->flowlock);
345 8831355 : fe->state = DFLOWwrapup;
346 8831355 : MT_lock_unset(&flow->flowlock);
347 8831036 : if (error) {
348 507 : void *null = NULL;
349 : /* only collect one error (from one thread, needed for stable testing) */
350 507 : if (!ATOMIC_PTR_CAS(&flow->error, &null, error))
351 25 : freeException(error);
352 : /* after an error we skip the rest of the block */
353 507 : q_enqueue(flow->done, fe);
354 507 : 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 8830529 : p = getInstrPtr(flow->mb, fe->pc);
365 8830529 : assert(p);
366 8830529 : fe->hotclaim = 0;
367 8830529 : fe->maxclaim = 0;
368 :
369 18355985 : for (i = 0; i < p->retc; i++) {
370 9526213 : lng footprint;
371 9526213 : footprint = getMemoryClaim(flow->mb, flow->stk, p, i, FALSE);
372 9525456 : fe->hotclaim += footprint;
373 9525456 : if (footprint > fe->maxclaim)
374 3861366 : 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 8829772 : int last = 0, nxt = -1;
383 8829772 : lng nxtclaim = -1;
384 :
385 8829772 : MT_lock_set(&flow->flowlock);
386 8831267 : for (last = fe->pc - flow->start;
387 33454376 : last >= 0 && (i = flow->nodes[last]) > 0;
388 24623109 : last = flow->edges[last]) {
389 24623109 : if (flow->status[i].state == DFLOWpending
390 24622163 : && flow->status[i].blocks == 1) {
391 : /* find the one with the largest footprint */
392 6733503 : if (nxt == -1 || flow->status[i].argclaim > nxtclaim) {
393 3697621 : nxt = i;
394 3697621 : nxtclaim = flow->status[i].argclaim;
395 : }
396 : }
397 : }
398 : /* hot potato can not be removed, use alternative to proceed */
399 8831267 : if (nxt >= 0) {
400 3385834 : flow->status[nxt].state = DFLOWrunning;
401 3385834 : flow->status[nxt].blocks = 0;
402 3385834 : flow->status[nxt].hotclaim = fe->hotclaim;
403 3385834 : flow->status[nxt].argclaim += fe->hotclaim;
404 3385834 : if (flow->status[nxt].maxclaim < fe->maxclaim)
405 1576275 : flow->status[nxt].maxclaim = fe->maxclaim;
406 : fnxt = flow->status + nxt;
407 : }
408 8831267 : MT_lock_unset(&flow->flowlock);
409 : #endif
410 :
411 8830829 : q_enqueue(flow->done, fe);
412 8829185 : if (fnxt == 0 && profilerStatus) {
413 0 : profilerHeartbeatEvent("wait");
414 : }
415 : }
416 158794 : MT_lock_set(&dataflowLock);
417 158794 : if (GDKexiting() || ATOMIC_GET(&exiting) || free_count >= free_max) {
418 : locked = true;
419 : break;
420 : }
421 151625 : free_count++;
422 151625 : struct worker **tp = &workers;
423 563577 : while (*tp && *tp != t)
424 411952 : tp = &(*tp)->next;
425 151625 : assert(*tp && *tp == t);
426 151625 : *tp = t->next;
427 151625 : t->flag = FREE;
428 151625 : t->next = free_workers;
429 151625 : free_workers = t;
430 151625 : MT_lock_unset(&dataflowLock);
431 151625 : MT_thread_setworking("idle, waiting for new client");
432 151625 : MT_sema_down(&t->s);
433 151624 : if (GDKexiting() || ATOMIC_GET(&exiting))
434 : break;
435 150982 : assert(t->flag == WAITING);
436 : }
437 642 : if (!locked)
438 642 : MT_lock_set(&dataflowLock);
439 7811 : if (t->flag != FINISHING) {
440 7138 : struct worker **tp = t->flag == FREE ? &free_workers : &workers;
441 34450 : while (*tp && *tp != t)
442 27312 : tp = &(*tp)->next;
443 7138 : assert(*tp && *tp == t);
444 7138 : *tp = t->next;
445 7138 : t->flag = EXITED;
446 7138 : t->next = exited_workers;
447 7138 : exited_workers = t;
448 : }
449 7811 : MT_lock_unset(&dataflowLock);
450 7811 : GDKsetbuf(NULL);
451 7811 : }
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 356 : DFLOWinitialize(void)
462 : {
463 356 : int limit;
464 356 : int created = 0;
465 :
466 356 : MT_lock_set(&mal_contextLock);
467 356 : MT_lock_set(&dataflowLock);
468 356 : 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 356 : free_max = GDKgetenv_int("dataflow_max_free",
475 : GDKnr_threads < 4 ? 4 : GDKnr_threads);
476 356 : todo = q_create("todo");
477 356 : if (todo == NULL) {
478 0 : MT_lock_unset(&dataflowLock);
479 0 : MT_lock_unset(&mal_contextLock);
480 0 : return -1;
481 : }
482 356 : limit = GDKnr_threads ? GDKnr_threads - 1 : 0;
483 1422 : while (limit > 0) {
484 1066 : limit--;
485 1066 : struct worker *t = GDKmalloc(sizeof(*t));
486 1066 : if (t == NULL) {
487 0 : TRC_CRITICAL(MAL_SERVER, "cannot allocate structure for worker");
488 0 : continue;
489 : }
490 1066 : *t = (struct worker) {
491 : .flag = RUNNING,
492 : .cntxt = ATOMIC_PTR_VAR_INIT(NULL),
493 : };
494 1066 : MT_sema_init(&t->s, 0, "DFLOWsema"); /* placeholder name */
495 1066 : 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 1066 : t->next = workers;
501 1066 : workers = t;
502 1066 : created++;
503 : }
504 : }
505 356 : 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 356 : MT_lock_unset(&dataflowLock);
514 356 : MT_lock_unset(&mal_contextLock);
515 356 : 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 157731 : DFLOWinitBlk(DataFlow flow, MalBlkPtr mb, int size)
526 : {
527 157731 : int pc, i, j, k, l, n, etop = 0;
528 157731 : int *assign;
529 157731 : InstrPtr p;
530 :
531 157731 : if (flow == NULL)
532 0 : throw(MAL, "dataflow", "DFLOWinitBlk(): Called with flow == NULL");
533 157731 : if (mb == NULL)
534 0 : throw(MAL, "dataflow", "DFLOWinitBlk(): Called with mb == NULL");
535 157731 : assign = (int *) GDKzalloc(mb->vtop * sizeof(int));
536 157731 : if (assign == NULL)
537 0 : throw(MAL, "dataflow", SQLSTATE(HY013) MAL_MALLOC_FAIL);
538 157731 : etop = flow->stop - flow->start;
539 8994020 : for (n = 0, pc = flow->start; pc < flow->stop; pc++, n++) {
540 8836289 : p = getInstrPtr(mb, pc);
541 8836289 : 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 8836289 : flow->status[n].flow = flow;
549 8836289 : flow->status[n].pc = pc;
550 8836289 : flow->status[n].state = DFLOWpending;
551 8836289 : flow->status[n].cost = -1;
552 8836289 : ATOMIC_PTR_SET(&flow->status[n].flow->error, NULL);
553 :
554 : /* administer flow dependencies */
555 41481413 : for (j = p->retc; j < p->argc; j++) {
556 : /* list of instructions that wake n-th instruction up */
557 32645124 : if (!isVarConstant(mb, getArg(p, j)) && (k = assign[getArg(p, j)])) {
558 14961925 : assert(k < pc); /* only dependencies on earlier instructions */
559 : /* add edge to the target instruction for wakeup call */
560 14961925 : k -= flow->start;
561 14961925 : if (flow->nodes[k]) {
562 : /* add wakeup to tail of list */
563 106343879 : for (i = k; flow->edges[i] > 0; i = flow->edges[i])
564 : ;
565 13278725 : flow->nodes[etop] = n;
566 13278725 : flow->edges[etop] = -1;
567 13278725 : flow->edges[i] = etop;
568 13278725 : etop++;
569 13278725 : (void) size;
570 13278725 : if (etop == size) {
571 220 : int *tmp;
572 : /* in case of realloc failure, the original
573 : * pointers will be freed by the caller */
574 220 : tmp = (int *) GDKrealloc(flow->nodes,
575 : sizeof(int) * 2 * size);
576 220 : if (tmp == NULL) {
577 0 : GDKfree(assign);
578 0 : throw(MAL, "dataflow",
579 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
580 : }
581 220 : flow->nodes = tmp;
582 220 : tmp = (int *) GDKrealloc(flow->edges,
583 : sizeof(int) * 2 * size);
584 220 : if (tmp == NULL) {
585 0 : GDKfree(assign);
586 0 : throw(MAL, "dataflow",
587 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
588 : }
589 220 : flow->edges = tmp;
590 220 : size *= 2;
591 : }
592 : } else {
593 1683200 : flow->nodes[k] = n;
594 1683200 : flow->edges[k] = -1;
595 : }
596 :
597 14961925 : flow->status[n].blocks++;
598 : }
599 :
600 : /* list of instructions to be woken up explicitly */
601 32645124 : 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 16718835 : l = getEndScope(mb, getArg(p, j));
605 16718835 : if (l != pc && l < flow->stop && l > flow->start) {
606 : /* add edge to the target instruction for wakeup call */
607 9674421 : assert(pc < l); /* only dependencies on earlier instructions */
608 9674421 : l -= flow->start;
609 9674421 : if (flow->nodes[n]) {
610 : /* add wakeup to tail of list */
611 40064206 : for (i = n; flow->edges[i] > 0; i = flow->edges[i])
612 : ;
613 4640970 : flow->nodes[etop] = l;
614 4640970 : flow->edges[etop] = -1;
615 4640970 : flow->edges[i] = etop;
616 4640970 : etop++;
617 4640970 : if (etop == size) {
618 117 : int *tmp;
619 : /* in case of realloc failure, the original
620 : * pointers will be freed by the caller */
621 117 : tmp = (int *) GDKrealloc(flow->nodes,
622 : sizeof(int) * 2 * size);
623 117 : if (tmp == NULL) {
624 0 : GDKfree(assign);
625 0 : throw(MAL, "dataflow",
626 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
627 : }
628 117 : flow->nodes = tmp;
629 117 : tmp = (int *) GDKrealloc(flow->edges,
630 : sizeof(int) * 2 * size);
631 117 : if (tmp == NULL) {
632 0 : GDKfree(assign);
633 0 : throw(MAL, "dataflow",
634 : SQLSTATE(HY013) MAL_MALLOC_FAIL);
635 : }
636 117 : flow->edges = tmp;
637 117 : size *= 2;
638 : }
639 : } else {
640 5033451 : flow->nodes[n] = l;
641 5033451 : flow->edges[n] = -1;
642 : }
643 9674421 : flow->status[l].blocks++;
644 : }
645 : }
646 : }
647 :
648 18368625 : for (j = 0; j < p->retc; j++)
649 9532336 : assign[getArg(p, j)] = pc; /* ensure recognition of dependency on first instruction and constant */
650 : }
651 157731 : GDKfree(assign);
652 :
653 157731 : 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 157731 : DFLOWscheduler(DataFlow flow, struct worker *w)
667 : {
668 157731 : int last;
669 157731 : int i;
670 157731 : int j;
671 157731 : InstrPtr p;
672 157731 : int tasks = 0, actions = 0;
673 157731 : str ret = MAL_SUCCEED;
674 157731 : FlowEvent fe, f = 0;
675 :
676 157731 : if (flow == NULL)
677 0 : throw(MAL, "dataflow", "DFLOWscheduler(): Called with flow == NULL");
678 157731 : actions = flow->stop - flow->start;
679 157731 : if (actions == 0)
680 0 : throw(MAL, "dataflow", "Empty dataflow block");
681 : /* initialize the eligible statements */
682 157731 : fe = flow->status;
683 :
684 157731 : ATOMIC_DEC(&flow->cntxt->workers);
685 157731 : MT_lock_set(&flow->flowlock);
686 9151751 : for (i = 0; i < actions; i++)
687 8836289 : if (fe[i].blocks == 0) {
688 987492 : p = getInstrPtr(flow->mb, fe[i].pc);
689 987492 : 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 987492 : fe[i].argclaim = 0;
696 5923221 : for (j = p->retc; j < p->argc; j++)
697 4935729 : fe[i].argclaim += getMemoryClaim(fe[0].flow->mb,
698 4935736 : fe[0].flow->stk, p, j, FALSE);
699 987485 : flow->status[i].state = DFLOWrunning;
700 987485 : q_enqueue(todo, flow->status + i);
701 : }
702 157731 : MT_lock_unset(&flow->flowlock);
703 157731 : MT_sema_up(&w->s);
704 :
705 157731 : while (actions != tasks) {
706 8835948 : f = q_dequeue(flow->done, NULL);
707 8835947 : if (ATOMIC_GET(&exiting))
708 : break;
709 8835947 : 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 8835947 : MT_lock_set(&flow->flowlock);
722 8835525 : tasks++;
723 8835525 : for (last = f->pc - flow->start;
724 33469750 : last >= 0 && (i = flow->nodes[last]) > 0; last = flow->edges[last])
725 24633983 : if (flow->status[i].state == DFLOWpending) {
726 21249185 : flow->status[i].argclaim += f->hotclaim;
727 21249185 : if (flow->status[i].blocks == 1) {
728 4462882 : flow->status[i].blocks--;
729 4462882 : flow->status[i].state = DFLOWrunning;
730 4462882 : q_enqueue(todo, flow->status + i);
731 : } else {
732 16786303 : flow->status[i].blocks--;
733 : }
734 : }
735 8993498 : MT_lock_unset(&flow->flowlock);
736 : }
737 : /* release the worker from its specific task (turn it into a
738 : * generic worker) */
739 157730 : ATOMIC_PTR_SET(&w->cntxt, NULL);
740 157730 : ATOMIC_INC(&flow->cntxt->workers);
741 : /* wrap up errors */
742 157730 : assert(flow->done->last == 0);
743 157730 : 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 7811 : finish_worker(struct worker *t)
753 : {
754 7811 : t->flag = FINISHING;
755 7811 : MT_lock_unset(&dataflowLock);
756 7811 : MT_join_thread(t->id);
757 7811 : MT_sema_destroy(&t->s);
758 7811 : GDKfree(t);
759 7811 : MT_lock_set(&dataflowLock);
760 7811 : }
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 157731 : runMALdataflow(Client cntxt, MalBlkPtr mb, int startpc, int stoppc,
778 : MalStkPtr stk)
779 : {
780 157731 : DataFlow flow = NULL;
781 157731 : str msg = MAL_SUCCEED;
782 157731 : int size;
783 157731 : bit *ret;
784 157731 : struct worker *t;
785 :
786 157731 : if (stk == NULL)
787 0 : throw(MAL, "dataflow", "runMALdataflow(): Called with stk == NULL");
788 157731 : ret = getArgReference_bit(stk, getInstrPtr(mb, startpc), 0);
789 157731 : *ret = FALSE;
790 :
791 157731 : assert(stoppc > startpc);
792 :
793 : /* check existence of workers */
794 157731 : if (todo == NULL) {
795 : /* create thread pool */
796 356 : if (GDKnr_threads <= 1 || DFLOWinitialize() < 0) {
797 : /* no threads created, run serially */
798 0 : *ret = TRUE;
799 0 : return MAL_SUCCEED;
800 : }
801 : }
802 157731 : 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 157731 : MT_lock_set(&dataflowLock);
807 : /* join with already exited threads */
808 163831 : while (exited_workers != NULL) {
809 6100 : assert(exited_workers->flag == EXITED);
810 6100 : struct worker *t = exited_workers;
811 6100 : exited_workers = exited_workers->next;
812 6100 : finish_worker(t);
813 : }
814 157731 : assert(cntxt != NULL);
815 157731 : if (free_workers != NULL) {
816 150982 : t = free_workers;
817 150982 : assert(t->flag == FREE);
818 150982 : assert(free_count > 0);
819 150982 : free_count--;
820 150982 : free_workers = t->next;
821 150982 : t->next = workers;
822 150982 : workers = t;
823 150982 : t->flag = WAITING;
824 150982 : ATOMIC_PTR_SET(&t->cntxt, cntxt);
825 150982 : MT_sema_up(&t->s);
826 : } else {
827 6749 : t = GDKmalloc(sizeof(*t));
828 6749 : if (t != NULL) {
829 6749 : *t = (struct worker) {
830 : .flag = WAITING,
831 : .cntxt = ATOMIC_PTR_VAR_INIT(cntxt),
832 : };
833 6749 : MT_sema_init(&t->s, 0, "DFLOWsema"); /* placeholder name */
834 6749 : 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 6749 : t->next = workers;
841 6749 : workers = t;
842 : }
843 : }
844 6749 : 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 157731 : MT_lock_unset(&dataflowLock);
852 :
853 157731 : flow = (DataFlow) GDKzalloc(sizeof(DataFlowRec));
854 157731 : if (flow == NULL)
855 0 : throw(MAL, "dataflow", SQLSTATE(HY013) MAL_MALLOC_FAIL);
856 :
857 157731 : size = DFLOWgraphSize(mb, startpc, stoppc);
858 157731 : size += stoppc - startpc;
859 :
860 315462 : *flow = (DataFlowRec) {
861 : .cntxt = cntxt,
862 : .mb = mb,
863 : .stk = stk,
864 157731 : .set_qry_ctx = MT_thread_get_qry_ctx() != NULL,
865 : /* keep real block count, exclude brackets */
866 157731 : .start = startpc + 1,
867 : .stop = stoppc,
868 157731 : .done = q_create("flow->done"),
869 157731 : .status = (FlowEvent) GDKzalloc((stoppc - startpc + 1) *
870 : sizeof(FlowEventRec)),
871 : .error = ATOMIC_PTR_VAR_INIT(NULL),
872 157731 : .nodes = (int *) GDKzalloc(sizeof(int) * size),
873 157731 : .edges = (int *) GDKzalloc(sizeof(int) * size),
874 : };
875 :
876 157731 : 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 157731 : 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 157731 : MT_lock_init(&flow->flowlock, "flow->flowlock");
895 157730 : msg = DFLOWinitBlk(flow, mb, size);
896 :
897 157731 : if (msg == MAL_SUCCEED)
898 157731 : msg = DFLOWscheduler(flow, t);
899 :
900 157731 : GDKfree(flow->status);
901 157731 : GDKfree(flow->edges);
902 157731 : GDKfree(flow->nodes);
903 157731 : q_destroy(flow->done);
904 157731 : MT_lock_destroy(&flow->flowlock);
905 157731 : GDKfree(flow);
906 :
907 : /* we created one worker, now tell one worker to exit again */
908 157731 : MT_lock_set(&todo->l);
909 157731 : todo->exitcount++;
910 157731 : MT_lock_unset(&todo->l);
911 157731 : MT_sema_up(&todo->s);
912 :
913 157731 : 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 355 : stopMALdataflow(void)
929 : {
930 355 : ATOMIC_SET(&exiting, 1);
931 355 : if (todo) {
932 355 : MT_lock_set(&dataflowLock);
933 : /* first wake up all running threads */
934 355 : int n = 0;
935 997 : for (struct worker *t = free_workers; t; t = t->next)
936 642 : n++;
937 1418 : for (struct worker *t = workers; t; t = t->next)
938 1063 : n++;
939 2060 : while (n-- > 0) {
940 : /* one UP for each thread we know about */
941 2060 : MT_sema_up(&todo->s);
942 : }
943 997 : while (free_workers) {
944 642 : struct worker *t = free_workers;
945 642 : assert(free_count > 0);
946 642 : free_count--;
947 642 : free_workers = free_workers->next;
948 642 : MT_sema_up(&t->s);
949 642 : finish_worker(t);
950 : }
951 386 : while (workers) {
952 31 : struct worker *t = workers;
953 31 : workers = workers->next;
954 31 : finish_worker(t);
955 : }
956 1393 : while (exited_workers) {
957 1038 : struct worker *t = exited_workers;
958 1038 : exited_workers = exited_workers->next;
959 1038 : finish_worker(t);
960 : }
961 355 : MT_lock_unset(&dataflowLock);
962 : }
963 355 : }
964 :
965 : void
966 355 : mal_dataflow_reset(void)
967 : {
968 355 : stopMALdataflow();
969 355 : workers = exited_workers = NULL;
970 355 : if (todo) {
971 355 : MT_lock_destroy(&todo->l);
972 355 : MT_sema_destroy(&todo->s);
973 355 : GDKfree(todo);
974 : }
975 355 : todo = 0; /* pending instructions */
976 355 : ATOMIC_SET(&exiting, 0);
977 355 : }
|