Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
jit_policy.h
1/*
2** JIT compilation policy for the extensible SQL parser.
3**
4** Decides when to JIT compile a snapshot based on runtime metrics.
5** The policy tracks per-snapshot usage statistics and triggers
6** background JIT compilation when the expected benefit (faster action
7** table lookups) exceeds the compilation cost.
8**
9** Thread safety: JITMetrics use atomic counters so multiple parser
10** threads can update them concurrently. The background compilation
11** thread is managed internally and publishes the JIT context to the
12** snapshot via an atomic pointer store.
13*/
14#ifndef JIT_POLICY_H
15#define JIT_POLICY_H
16
17#include <stdint.h>
18#include <stdbool.h>
19#include <stdatomic.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25/* Forward declaration */
26typedef struct ParserSnapshot ParserSnapshot;
27
28/* ------------------------------------------------------------------ */
29/* JIT metrics */
30/* ------------------------------------------------------------------ */
31
38typedef struct JITMetrics {
39 atomic_uint_fast64_t parse_count;
40 atomic_uint_fast64_t total_parse_time_ns;
41 atomic_uint_fast64_t action_lookup_count;
42 atomic_uint_fast64_t total_tokens_parsed;
43 atomic_int is_jitted;
44 atomic_int jit_in_progress;
46
47/* ------------------------------------------------------------------ */
48/* Policy configuration */
49/* ------------------------------------------------------------------ */
50
93
94/* ------------------------------------------------------------------ */
95/* Policy API */
96/* ------------------------------------------------------------------ */
97
98/*
99** Return the default policy configuration.
100** Defaults:
101** min_parse_count = 200
102** min_total_parse_time_ns = 10,000,000 (10 ms cumulative)
103** min_avg_lookups_per_parse = 100
104** background_compile = true
105** min_grammar_states = 500
106** min_avg_tokens_per_parse = 200
107** enabled = true
108** tokenizer_jit_enabled = true
109*/
110JITPolicyConfig jit_policy_default_config(void);
111
112/*
113** Initialize a JITMetrics struct to zero. Must be called before
114** the metrics are used.
115*/
116void jit_metrics_init(JITMetrics *m);
117
118/*
119** Record the completion of a parse session.
120** Updates parse_count, total_parse_time_ns, and action_lookup_count.
121** Thread-safe (uses atomic operations).
122*/
123void jit_metrics_record_parse(JITMetrics *m,
124 uint64_t parse_time_ns,
125 uint64_t action_lookups);
126
127/*
128** Record the number of tokens processed in a parse session.
129** Thread-safe (uses atomic operations).
130*/
131void jit_metrics_record_tokens(JITMetrics *m, uint64_t token_count);
132
133/*
134** Evaluate whether a snapshot should be JIT compiled based on its
135** current metrics and the given policy configuration.
136**
137** Returns true if the metrics exceed all thresholds and the snapshot
138** is not already JIT compiled or in the process of being compiled.
139*/
140bool jit_should_compile(const JITMetrics *m, const JITPolicyConfig *config);
141
142/*
143** Possibly trigger JIT compilation for a snapshot.
144**
145** Checks the metrics against the policy; if compilation is warranted:
146** - If config->background_compile is true, spawns a detached thread
147** that compiles and atomically publishes the JIT context.
148** - If config->background_compile is false, compiles synchronously.
149**
150** Returns:
151** 0 -- compilation triggered (or already done)
152** 1 -- metrics do not yet warrant compilation
153** -1 -- error (JIT unavailable, snapshot NULL, etc.)
154**
155** Thread-safe: uses the jit_in_progress flag to prevent concurrent
156** compilation attempts on the same snapshot.
157*/
158int jit_maybe_compile(ParserSnapshot *snap,
159 JITMetrics *m,
160 const JITPolicyConfig *config);
161
162/*
163** Shut down the JIT policy subsystem.
164** Waits for any in-flight background compilations to finish.
165** Call once at application shutdown.
166*/
167void jit_policy_shutdown(void);
168
169#ifdef __cplusplus
170}
171#endif
172
173#endif /* JIT_POLICY_H */
Per-snapshot runtime metrics used by the JIT policy.
Definition jit_policy.h:38
atomic_uint_fast64_t parse_count
Number of parse sessions.
Definition jit_policy.h:39
atomic_uint_fast64_t action_lookup_count
Total action table lookups.
Definition jit_policy.h:41
atomic_int jit_in_progress
1 if background compile active
Definition jit_policy.h:44
atomic_int is_jitted
1 if JIT code is attached
Definition jit_policy.h:43
atomic_uint_fast64_t total_tokens_parsed
Cumulative tokens across all parses.
Definition jit_policy.h:42
atomic_uint_fast64_t total_parse_time_ns
Cumulative parse wall-clock (ns)
Definition jit_policy.h:40
Tunable thresholds for the JIT compilation policy.
Definition jit_policy.h:57
bool enabled
Master switch for JIT compilation.
Definition jit_policy.h:85
bool tokenizer_jit_enabled
Enable JIT compilation of the keyword tokenizer.
Definition jit_policy.h:91
uint32_t min_avg_tokens_per_parse
Minimum average tokens per parse session before JIT.
Definition jit_policy.h:81
uint32_t min_grammar_states
Minimum number of parser states before considering JIT.
Definition jit_policy.h:77
uint64_t min_avg_lookups_per_parse
Minimum average action lookups per parse session.
Definition jit_policy.h:69
uint64_t min_parse_count
Minimum number of parse sessions before considering JIT.
Definition jit_policy.h:60
uint64_t min_total_parse_time_ns
Minimum cumulative parse time (nanoseconds) before JIT.
Definition jit_policy.h:65
bool background_compile
If true, JIT compilation happens on a background thread.
Definition jit_policy.h:73
Opaque snapshot handle.
Definition snapshot.h:117