1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9/*
10 * (c) Martin Kersten
11 * MonetDB Basic Definitions
12 * Definitions that need to included in every file of the Monet system, as well as in user defined module implementations.
13 */
14#ifndef _MAL_H
15#define _MAL_H
16
17#include "gdk.h"
18
19#ifdef WIN32
20#if !defined(LIBMAL) && !defined(LIBATOMS) && !defined(LIBKERNEL) && !defined(LIBMAL) && !defined(LIBOPTIMIZER) && !defined(LIBSCHEDULER) && !defined(LIBMONETDB5)
21#define mal_export extern __declspec(dllimport)
22#else
23#define mal_export extern __declspec(dllexport)
24#endif
25#else
26#define mal_export extern
27#endif
28
29#ifdef HAVE_SYS_TIMES_H
30# include <sys/times.h>
31#endif
32
33/*
34 * MonetDB Calling Options
35 * The number of invocation arguments is kept to a minimum.
36 * See `man mserver5` or tools/mserver/mserver5.1
37 * for additional system variable settings.
38 */
39#define MAXSCRIPT 64
40
41/* The compile time debugging flags are turned into bit masks, akin to GDK */
42mal_export lng OPTdebug;
43
44#define OPTaliases (1 )
45#define OPTcandidates ((lng)1 << 1)
46#define OPTcoercion ((lng)1 << 2)
47#define OPTcommonterms ((lng)1 << 3)
48#define OPTconstants ((lng)1 << 4)
49#define OPTcostmodel ((lng)1 << 5)
50#define OPTdataflow ((lng)1 << 6)
51#define OPTdeadcode ((lng)1 << 7)
52#define OPTemptybind ((lng)1 << 8)
53#define OPTevaluate ((lng)1 << 9)
54#define OPTgarbagecollector ((lng)1 << 10)
55#define OPTgenerator ((lng)1 << 11)
56#define OPTinline ((lng)1 << 12)
57#define OPTjit ((lng)1 << 13)
58#define OPTjson ((lng)1 << 14)
59#define OPTmacros ((lng)1 << 15)
60#define OPTmatpack ((lng)1 << 15)
61#define OPTmergetable ((lng)1 << 16)
62#define OPTmitosis ((lng)1 << 17)
63#define OPTmultiplex ((lng)1 << 18)
64#define OPToltp ((lng)1 << 19)
65#define OPTpipes ((lng)1 << 20)
66#define OPTpostfix ((lng)1 << 21)
67#define OPTprelude ((lng)1 << 22)
68#define OPTprofiler ((lng)1 << 23)
69#define OPTprojectionpath ((lng)1 << 24)
70#define OPTpushselect ((lng)1 << 25)
71#define OPTquerylog ((lng)1 << 26)
72#define OPTreduce ((lng)1 << 27)
73#define OPTremap ((lng)1 << 28)
74#define OPTremotequeries ((lng)1 << 29)
75#define OPTreorder ((lng)1 << 30)
76#define OPTsupport ((lng)1 << 31)
77#define OPTvolcano ((lng)1 << 32)
78#define OPTwlc ((lng)1 << 33)
79
80mal_export lng MALdebug;
81
82/*
83 * MonetDB assumes it can use most of the machines memory,
84 * leaving a small portion for other programs.
85 */
86#define GB (((lng)1024)*1024*1024)
87#define MEMORY_THRESHOLD (0.2 * GDK_mem_maxsize > 8 * GB? GDK_mem_maxsize - 8 * GB: 0.8 * GDK_mem_maxsize)
88
89mal_export char monet_cwd[FILENAME_MAX];
90mal_export char monet_characteristics[4096];
91mal_export stream *maleventstream;
92
93#ifdef HAVE_HGE
94mal_export int have_hge;
95#endif
96
97/*
98 See gdk/gdk.h for the definition of all debug masks.
99 See `man mserver5` or tools/mserver/mserver5.1
100 for a documentation of all debug options.
101*/
102#define GRPthreads (THRDMASK | PARMASK)
103#define GRPmemory (MEMMASK | ALLOCMASK )
104#define GRPproperties (CHECKMASK | PROPMASK | BATMASK )
105#define GRPio (IOMASK | PERFMASK )
106#define GRPheaps (HEAPMASK)
107#define GRPtransactions (TMMASK | DELTAMASK | TEMMASK)
108#define GRPmodules (LOADMASK)
109#define GRPalgorithms (ALGOMASK | ESTIMASK)
110#define GRPperformance (DEADBEEFMASK)
111#define GRPoptimizers (OPTMASK)
112#define GRPforcemito (FORCEMITOMASK | NOSYNCMASK)
113
114mal_export MT_Lock mal_contextLock;
115mal_export MT_Lock mal_remoteLock;
116mal_export MT_Lock mal_profileLock ;
117mal_export MT_Lock mal_copyLock ;
118mal_export MT_Lock mal_delayLock ;
119mal_export MT_Lock mal_oltpLock ;
120
121mal_export int mal_init(void);
122mal_export _Noreturn void mal_exit(int status);
123mal_export void mserver_reset(void);
124
125/* This should be here, but cannot, as "Client" isn't known, yet ... |-(
126 * For now, we move the prototype declaration to src/mal/mal_client.c,
127 * the only place where it is currently used. Maybe, we should concider
128 * also moving the implementation there...
129 */
130
131/* Listing modes are globally known */
132#define LIST_INPUT 1 /* echo original input */
133#define LIST_MAL_NAME 2 /* show variable name */
134#define LIST_MAL_TYPE 4 /* show type resolutoin */
135#define LIST_MAL_VALUE 8 /* list bat tuple count */
136#define LIST_MAL_PROPS 16 /* show variable properties */
137#define LIST_MAL_MAPI 32 /* output Mapi compatible output */
138#define LIST_MAL_REMOTE 64 /* output MAL for remote execution */
139#define LIST_MAL_CALL (LIST_MAL_NAME | LIST_MAL_VALUE )
140#define LIST_MAL_DEBUG (LIST_MAL_NAME | LIST_MAL_VALUE | LIST_MAL_TYPE | LIST_MAL_PROPS)
141#define LIST_MAL_ALL (LIST_MAL_NAME | LIST_MAL_VALUE | LIST_MAL_TYPE | LIST_MAL_PROPS | LIST_MAL_MAPI)
142
143/* type check status is kept around to improve type checking efficiency */
144#define TYPE_ERROR -1
145#define TYPE_UNKNOWN 0
146#define TYPE_RESOLVED 2
147#define GARBAGECONTROL 3
148
149#define VARARGS 1 /* deal with variable arguments */
150#define VARRETS 2
151
152typedef int malType;
153typedef str (*MALfcn) ();
154
155typedef struct SYMDEF {
156 struct SYMDEF *peer; /* where to look next */
157 struct SYMDEF *skip; /* skip to next different symbol */
158 str name;
159 int kind; /* what kind of symbol */
160 struct MALBLK *def; /* the details of the MAL fcn */
161} *Symbol, SymRecord;
162
163typedef struct VARRECORD {
164 char id[IDLENGTH]; /* use the space for the full name */
165 malType type; /* internal type signature */
166 bool constant:1,
167 typevar:1,
168 fixedtype:1,
169 udftype:1,
170 cleanup:1,
171 initialized:1,
172 used:1,
173 disabled:1;
174 short depth; /* scope block depth, set to -1 if not used */
175 short worker; /* thread id of last worker producing it */
176 ValRecord value;
177 int declared; /* pc index when it was first assigned */
178 int updated; /* pc index when it was first updated */
179 int eolife; /* pc index when it should be garbage collected */
180 int stc; /* pc index for rendering schema.table.column */
181 BUN rowcnt; /* estimated row count*/
182} *VarPtr, VarRecord;
183
184/* For performance analysis we keep track of the number of calls and
185 * the total time spent while executing the instruction. (See
186 * mal_profiler.c)
187 */
188
189typedef struct {
190 bte token; /* instruction type */
191 bit barrier; /* flow of control modifier takes:
192 BARRIER, LEAVE, REDO, EXIT, CATCH, RAISE */
193 bit typechk; /* type check status */
194 bte gc; /* garbage control flags */
195 bit polymorphic; /* complex type analysis */
196 bit varargs; /* variable number of arguments */
197 int jump; /* controlflow program counter */
198 int pc; /* location in MAL plan for profiler*/
199 MALfcn fcn; /* resolved function address */
200 struct MALBLK *blk; /* resolved MAL function address */
201 int mitosis; /* old mtProp value */
202 /* inline statistics */
203 lng clock; /* when the last call was started */
204 lng ticks; /* total micro seconds spent in last call */
205 int calls; /* number of calls made to this instruction */
206 lng totticks; /* total time spent on this instruction. */
207 lng wbytes; /* number of bytes produced in last instruction */
208 /* the core admin */
209 str modname; /* module context, reference into namespace */
210 str fcnname; /* function name, reference into namespace */
211 int argc, retc, maxarg; /* total and result argument count */
212 int argv[FLEXIBLE_ARRAY_MEMBER]; /* at least a few entries */
213} *InstrPtr, InstrRecord;
214
215typedef struct MALBLK {
216 char binding[IDLENGTH]; /* related C-function */
217 str help; /* supportive commentary */
218 oid tag; /* unique block tag */
219 struct MALBLK *alternative;
220 int vtop; /* next free slot */
221 int vsize; /* size of variable arena */
222 int vid; /* generate local variable counter */
223 VarRecord *var; /* Variable table */
224 int stop; /* next free slot */
225 int ssize; /* byte size of arena */
226 InstrPtr *stmt; /* Instruction location */
227
228 bool inlineProp:1, /* inline property */
229 unsafeProp:1, /* unsafe property */
230 sealedProp:1; /* sealed property (opertions for sealed object should be on the full object once) */
231
232 str errors; /* left over errors */
233 struct MALBLK *history; /* of optimizer actions */
234 short keephistory; /* do we need the history at all */
235 int maxarg; /* keep track on the maximal arguments used */
236 ptr replica; /* for the replicator tests */
237 lng starttime; /* track when the query started, for resource management */
238 lng runtime; /* average execution time of block in ticks */
239 int calls; /* number of calls */
240 lng optimize; /* total optimizer time */
241 int activeClients; /* load during mitosis optimization */
242} *MalBlkPtr, MalBlkRecord;
243
244#define STACKINCR 128
245#define MAXGLOBALS (4 * STACKINCR)
246
247typedef int (*DFhook) (void *, void *, void *, void *);
248
249typedef struct MALSTK {
250 int stksize;
251 int stktop;
252 int stkbot; /* the first variable to be initialized */
253 int stkdepth; /* to protect against runtime stack overflow */
254 int calldepth; /* to protect against runtime stack overflow */
255 short keepAlive; /* do not garbage collect when set */
256 /*
257 * Parallel processing is mostly driven by dataflow, but within this context
258 * there may be different schemes to take instructions into execution.
259 * The admission scheme (and wrapup) are the necessary scheduler hooks.
260 */
261 DFhook admit;
262 DFhook wrapup;
263
264/*
265 * It is handy to administer the timing in the stack frame
266 * for use in profiling instructions.
267 */
268 struct timeval clock; /* time this stack was created */
269 char cmd; /* debugger and runtime communication */
270 char status; /* srunning 'R' suspended 'S', quiting 'Q' */
271 int pcup; /* saved pc upon a recursive all */
272 oid tag; /* unique invocation call tag */
273 int workers; /* Actual number of concurrent workers */
274 lng memory; /* Actual memory claim highwater mark */
275
276 struct MALSTK *up; /* stack trace list */
277 struct MALBLK *blk; /* associated definition */
278 ValRecord stk[FLEXIBLE_ARRAY_MEMBER];
279} MalStack, *MalStkPtr;
280
281#define MAXOLTPLOCKS 1024
282typedef unsigned char OLTPlocks[MAXOLTPLOCKS];
283
284#define OLTPclear(X) memset((char*)X, 0, sizeof(X))
285
286#endif /* _MAL_H*/
287