1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! Functions called from within the Weld runtime.
//!
//! These are functions that are accessed from generated code.

use fnv;
use libc;

pub mod ffi;

use self::ffi::*;

use libc::c_char;
use std::alloc::System as Allocator;

use fnv::FnvHashMap;

use std::ffi::CStr;
use std::fmt;
use std::ptr;
use std::sync::Once;

use std::alloc::{GlobalAlloc, Layout};

pub type Ptr = *mut u8;

/// Initialize the Weld runtime only once.
static ONCE: Once = Once::new();

/// Alignment for allocations.
const DEFAULT_ALIGN: usize = 8;

/// An errno set by the runtime but also used by the Weld API.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd)]
#[repr(u64)]
pub enum WeldRuntimeErrno {
    /// Indicates success.
    ///
    /// This will always be 0.  
    Success = 0,
    /// Invalid configuration.
    ConfigurationError,
    /// Dynamic library load error.
    LoadLibraryError,
    /// Weld compilation error.
    CompileError,
    /// Array out-of-bounds error.
    ArrayOutOfBounds,
    /// A Weld iterator was invalid.
    BadIteratorLength,
    /// Mismatched Zip error.
    ///
    /// This error is thrown if the vectors in a Zip have different lengths.
    MismatchedZipSize,
    /// Out of memory error.
    ///
    /// This error is thrown if the amount of memory allocated by the runtime exceeds the limit set
    /// by the configuration.
    OutOfMemory,
    RunNotFound,
    /// An unknown error.
    Unknown,
    /// A deserialization error.
    ///
    /// This error occurs if a buffer being deserialized has an invalid length.
    DeserializationError,
    /// A key was not found in a dictionary.
    KeyNotFoundError,
    /// An assertion evaluated to `false`.
    AssertionError,
    /// Maximum errno value.
    ///
    /// All errors will have a value less than this value and greater than 0.
    ErrnoMax,
}

impl fmt::Display for WeldRuntimeErrno {
    /// Just return the errno name.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// Maintains information about a single Weld run.
#[derive(Debug, PartialEq)]
pub struct WeldRuntimeContext {
    /// Maps pointers to allocation size in bytes.
    allocations: FnvHashMap<Ptr, Layout>,
    /// An error code set for the context.
    errno: WeldRuntimeErrno,
    /// A result pointer set by the runtime.
    result: Ptr,
    /// The number of worker threads.
    nworkers: i32,
    /// A memory limit.
    memlimit: usize,
    /// Number of allocated bytes so far.
    ///
    /// This will always be equal to `allocations.values().sum()`.
    allocated: usize,
}

/// Private API used by the FFI.
impl WeldRuntimeContext {
    unsafe fn malloc(&mut self, size: i64) -> Ptr {
        if size == 0 {
            trace!("Alloc'd 0-size pointer (null)");
            return ptr::null_mut();
        }

        let size = size as usize;

        if self.allocated + size > self.memlimit {
            self.errno = WeldRuntimeErrno::OutOfMemory;
            panic!(
                "Weld run ran out of memory (limit={}, attempted to allocate {}",
                self.memlimit,
                self.allocated + size
            );
        }
        let layout = Layout::from_size_align_unchecked(size as usize, DEFAULT_ALIGN);
        let mem = Allocator.alloc(layout);

        self.allocated += layout.size();
        trace!("Alloc'd pointer {:?} ({} bytes)", mem, layout.size());

        self.allocations.insert(mem, layout);
        mem
    }

    unsafe fn realloc(&mut self, pointer: Ptr, size: i64) -> Ptr {
        if pointer.is_null() {
            return self.malloc(size);
        }

        let size = size as usize;
        let old_layout = self.allocations.remove(&pointer).unwrap();
        if self.allocated - old_layout.size() + size > self.memlimit {
            self.errno = WeldRuntimeErrno::OutOfMemory;
            panic!(
                "Weld run ran out of memory (limit={}, attempted to allocate {}",
                self.memlimit,
                self.allocated - old_layout.size() + size
            );
        }

        // Must pass *old* layout to realloc!
        let mem = Allocator.realloc(pointer, old_layout, size);
        let new_layout = Layout::from_size_align_unchecked(size, DEFAULT_ALIGN);

        self.allocated -= old_layout.size();
        self.allocated += new_layout.size();

        self.allocations.insert(mem, new_layout);
        mem
    }

    fn set_errno(&mut self, errno: WeldRuntimeErrno) {
        self.errno = errno;
        panic!("Weld runtime threw error: {}", self.errno)
    }

    fn set_result(&mut self, result: Ptr) {
        self.result = result;
    }

    fn result(&self) -> Ptr {
        self.result
    }
}

// Public API.
impl WeldRuntimeContext {
    /// Construct a new `WeldRuntimeContext`.
    pub fn new(nworkers: i32, memlimit: i64) -> WeldRuntimeContext {
        WeldRuntimeContext {
            allocations: FnvHashMap::default(),
            errno: WeldRuntimeErrno::Success,
            result: ptr::null_mut(),
            nworkers,
            memlimit: memlimit as usize,
            allocated: 0,
        }
    }

    /// Free an allocated data value.
    ///
    /// Panics if the passed value was not allocated by the Weld runtime.
    pub unsafe fn free(&mut self, pointer: Ptr) {
        if pointer.is_null() {
            trace!("Freed null pointer (no-op) in runst_free()");
            return;
        }

        let layout = self.allocations.remove(&pointer).unwrap();

        trace!(
            "Freeing pointer {:?} ({} bytes) in runst_free()",
            pointer,
            layout.size()
        );

        Allocator.dealloc(pointer, layout);
        self.allocated -= layout.size();
    }

    /// Returns the number of bytes allocated by this Weld run.
    pub fn memory_usage(&self) -> i64 {
        self.allocated as i64
    }

    /// Returns a 64-bit ID identifying this run.
    pub fn run_id(&self) -> i64 {
        0
    }

    /// Returns the error code of this run.
    fn errno(&self) -> WeldRuntimeErrno {
        self.errno
    }

    /// Returns the number of worker threads set for this run.
    pub fn threads(&self) -> i32 {
        self.nworkers
    }

    /// Returns the memory limit of this run.
    pub fn memory_limit(&self) -> i64 {
        self.memlimit as i64
    }
}

impl Drop for WeldRuntimeContext {
    fn drop(&mut self) {
        // Free memory allocated by the run.
        trace!("Allocations: {}", self.allocations.len());
        unsafe {
            for (pointer, layout) in self.allocations.iter() {
                trace!(
                    "Freeing pointer {:?} ({} bytes) in drop()",
                    *pointer,
                    layout.size()
                );
                Allocator.dealloc(*pointer, layout.clone());
            }
        }
    }
}

unsafe fn initialize() {
    ONCE.call_once(|| {
        // Hack to prevent symbols from being compiled out in a Rust binary.
        let mut x = weld_runst_init as usize;
        x += weld_runst_set_result as usize;
        x += weld_runst_get_result as usize;
        x += weld_runst_malloc as usize;
        x += weld_runst_realloc as usize;
        x += weld_runst_free as usize;
        x += weld_runst_get_errno as usize;
        x += weld_runst_set_errno as usize;
        x += weld_runst_assert as usize;
        x += weld_runst_print as usize;

        trace!("Runtime initialized with hashed values {}", x);
    });
}