2018-08-11 10:57:58 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
mjit_worker.c - Worker for MRI method JIT compiler
|
|
|
|
|
|
|
|
Copyright (C) 2017 Vladimir Makarov <vmakarov@redhat.com>.
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// NOTE: All functions in this file are executed on MJIT worker. So don't
|
|
|
|
// call Ruby methods (C functions that may call rb_funcall) or trigger
|
|
|
|
// GC (using ZALLOC, xmalloc, xfree, etc.) in this file.
|
2018-08-11 10:57:58 +03:00
|
|
|
|
|
|
|
/* We utilize widely used C compilers (GCC and LLVM Clang) to
|
|
|
|
implement MJIT. We feed them a C code generated from ISEQ. The
|
|
|
|
industrial C compilers are slower than regular JIT engines.
|
|
|
|
Generated code performance of the used C compilers has a higher
|
|
|
|
priority over the compilation speed.
|
|
|
|
|
|
|
|
So our major goal is to minimize the ISEQ compilation time when we
|
|
|
|
use widely optimization level (-O2). It is achieved by
|
|
|
|
|
|
|
|
o Using a precompiled version of the header
|
|
|
|
o Keeping all files in `/tmp`. On modern Linux `/tmp` is a file
|
|
|
|
system in memory. So it is pretty fast
|
|
|
|
o Implementing MJIT as a multi-threaded code because we want to
|
|
|
|
compile ISEQs in parallel with iseq execution to speed up Ruby
|
|
|
|
code execution. MJIT has one thread (*worker*) to do
|
|
|
|
parallel compilations:
|
|
|
|
o It prepares a precompiled code of the minimized header.
|
|
|
|
It starts at the MRI execution start
|
|
|
|
o It generates PIC object files of ISEQs
|
|
|
|
o It takes one JIT unit from a priority queue unless it is empty.
|
|
|
|
o It translates the JIT unit ISEQ into C-code using the precompiled
|
|
|
|
header, calls CC and load PIC code when it is ready
|
|
|
|
o Currently MJIT put ISEQ in the queue when ISEQ is called
|
|
|
|
o MJIT can reorder ISEQs in the queue if some ISEQ has been called
|
|
|
|
many times and its compilation did not start yet
|
|
|
|
o MRI reuses the machine code if it already exists for ISEQ
|
|
|
|
o The machine code we generate can stop and switch to the ISEQ
|
|
|
|
interpretation if some condition is not satisfied as the machine
|
|
|
|
code can be speculative or some exception raises
|
|
|
|
o Speculative machine code can be canceled.
|
|
|
|
|
|
|
|
Here is a diagram showing the MJIT organization:
|
|
|
|
|
|
|
|
_______
|
|
|
|
|header |
|
|
|
|
|_______|
|
|
|
|
| MRI building
|
|
|
|
--------------|----------------------------------------
|
|
|
|
| MRI execution
|
|
|
|
|
|
|
|
|
_____________|_____
|
|
|
|
| | |
|
|
|
|
| ___V__ | CC ____________________
|
|
|
|
| | |----------->| precompiled header |
|
|
|
|
| | | | |____________________|
|
|
|
|
| | | | |
|
|
|
|
| | MJIT | | |
|
|
|
|
| | | | |
|
|
|
|
| | | | ____V___ CC __________
|
|
|
|
| |______|----------->| C code |--->| .so file |
|
|
|
|
| | |________| |__________|
|
|
|
|
| | |
|
|
|
|
| | |
|
|
|
|
| MRI machine code |<-----------------------------
|
|
|
|
|___________________| loading
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __sun
|
|
|
|
#define __EXTENSIONS__ 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "vm_core.h"
|
|
|
|
#include "mjit.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "ruby_assert.h"
|
2018-10-21 16:21:28 +03:00
|
|
|
#include "ruby/debug.h"
|
2018-08-11 10:57:58 +03:00
|
|
|
#include "ruby/thread.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
# include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#include "dln.h"
|
2018-08-11 12:37:14 +03:00
|
|
|
|
2018-08-11 17:18:55 +03:00
|
|
|
#include "ruby/util.h"
|
2019-04-14 08:26:46 +03:00
|
|
|
#undef strdup // ruby_strdup may trigger GC
|
2018-08-11 17:18:55 +03:00
|
|
|
|
2018-08-11 12:37:14 +03:00
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
# define MAXPATHLEN 1024
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define dlopen(name,flag) ((void*)LoadLibrary(name))
|
|
|
|
#define dlerror() strerror(rb_w32_map_errno(GetLastError()))
|
|
|
|
#define dlsym(handle,name) ((void*)GetProcAddress((handle),(name)))
|
2018-10-07 15:28:46 +03:00
|
|
|
#define dlclose(handle) (!FreeLibrary(handle))
|
2018-08-11 12:37:14 +03:00
|
|
|
#define RTLD_NOW -1
|
2018-08-11 12:58:20 +03:00
|
|
|
|
2018-10-12 18:14:51 +03:00
|
|
|
#define waitpid(pid,stat_loc,options) (WaitForSingleObject((HANDLE)(pid), INFINITE), GetExitCodeProcess((HANDLE)(pid), (LPDWORD)(stat_loc)), CloseHandle((HANDLE)pid), (pid))
|
2018-08-11 12:58:20 +03:00
|
|
|
#define WIFEXITED(S) ((S) != STILL_ACTIVE)
|
|
|
|
#define WEXITSTATUS(S) (S)
|
|
|
|
#define WIFSIGNALED(S) (0)
|
|
|
|
typedef intptr_t pid_t;
|
2018-08-11 12:37:14 +03:00
|
|
|
#endif
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Atomically set function pointer if possible.
|
2018-08-11 12:58:20 +03:00
|
|
|
#define MJIT_ATOMIC_SET(var, val) (void)ATOMIC_PTR_EXCHANGE(var, val)
|
|
|
|
|
2018-08-11 12:37:14 +03:00
|
|
|
#define MJIT_TMP_PREFIX "_ruby_mjit_"
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// The unit structure that holds metadata of ISeq for MJIT.
|
2018-08-11 12:37:14 +03:00
|
|
|
struct rb_mjit_unit {
|
2019-04-14 08:26:46 +03:00
|
|
|
// Unique order number of unit.
|
2018-08-11 12:37:14 +03:00
|
|
|
int id;
|
2019-04-14 08:26:46 +03:00
|
|
|
// Dlopen handle of the loaded object file.
|
2018-08-11 12:37:14 +03:00
|
|
|
void *handle;
|
2019-04-20 06:00:08 +03:00
|
|
|
rb_iseq_t *iseq;
|
2018-08-11 12:37:14 +03:00
|
|
|
#ifndef _MSC_VER
|
2019-04-14 08:26:46 +03:00
|
|
|
// This value is always set for `compact_all_jit_code`. Also used for lazy deletion.
|
2018-08-11 12:37:14 +03:00
|
|
|
char *o_file;
|
2019-04-14 08:26:46 +03:00
|
|
|
// true if it's inherited from parent Ruby process and lazy deletion should be skipped.
|
|
|
|
// `o_file = NULL` can't be used to skip lazy deletion because `o_file` could be used
|
|
|
|
// by child for `compact_all_jit_code`.
|
2019-01-10 17:31:18 +03:00
|
|
|
bool o_file_inherited_p;
|
2018-08-11 12:37:14 +03:00
|
|
|
#endif
|
2018-08-18 04:10:03 +03:00
|
|
|
#if defined(_WIN32)
|
2019-04-14 08:26:46 +03:00
|
|
|
// DLL cannot be removed while loaded on Windows. If this is set, it'll be lazily deleted.
|
2018-08-11 12:37:14 +03:00
|
|
|
char *so_file;
|
|
|
|
#endif
|
2019-04-14 08:26:46 +03:00
|
|
|
// Only used by unload_units. Flag to check this unit is currently on stack or not.
|
2018-08-11 12:37:14 +03:00
|
|
|
char used_code_p;
|
2018-11-01 02:55:22 +03:00
|
|
|
struct list_node unode;
|
2019-04-14 07:52:02 +03:00
|
|
|
// mjit_compile's optimization switches
|
|
|
|
struct rb_mjit_compile_info compile_info;
|
2018-08-11 12:37:14 +03:00
|
|
|
};
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Linked list of struct rb_mjit_unit.
|
2018-08-11 12:37:14 +03:00
|
|
|
struct rb_mjit_unit_list {
|
2018-11-01 02:55:22 +03:00
|
|
|
struct list_head head;
|
2019-04-14 08:26:46 +03:00
|
|
|
int length; // the list length
|
2018-08-11 12:37:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern void rb_native_mutex_lock(rb_nativethread_lock_t *lock);
|
|
|
|
extern void rb_native_mutex_unlock(rb_nativethread_lock_t *lock);
|
|
|
|
extern void rb_native_mutex_initialize(rb_nativethread_lock_t *lock);
|
|
|
|
extern void rb_native_mutex_destroy(rb_nativethread_lock_t *lock);
|
|
|
|
|
|
|
|
extern void rb_native_cond_initialize(rb_nativethread_cond_t *cond);
|
|
|
|
extern void rb_native_cond_destroy(rb_nativethread_cond_t *cond);
|
|
|
|
extern void rb_native_cond_signal(rb_nativethread_cond_t *cond);
|
|
|
|
extern void rb_native_cond_broadcast(rb_nativethread_cond_t *cond);
|
|
|
|
extern void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex);
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// process.c
|
2018-08-11 12:58:20 +03:00
|
|
|
extern rb_pid_t ruby_waitpid_locked(rb_vm_t *, rb_pid_t, int *status, int options, rb_nativethread_cond_t *cond);
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// A copy of MJIT portion of MRI options since MJIT initialization. We
|
|
|
|
// need them as MJIT threads still can work when the most MRI data were
|
|
|
|
// freed.
|
2018-08-11 12:58:20 +03:00
|
|
|
struct mjit_options mjit_opts;
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
// true if MJIT is enabled.
|
|
|
|
bool mjit_enabled = false;
|
|
|
|
// true if JIT-ed code should be called. When `ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS`
|
|
|
|
// and `mjit_call_p == false`, any JIT-ed code execution is cancelled as soon as possible.
|
|
|
|
bool mjit_call_p = false;
|
2018-08-11 12:58:20 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Priority queue of iseqs waiting for JIT compilation.
|
|
|
|
// This variable is a pointer to head unit of the queue.
|
2018-11-01 02:55:22 +03:00
|
|
|
static struct rb_mjit_unit_list unit_queue = { LIST_HEAD_INIT(unit_queue.head) };
|
2019-04-14 08:26:46 +03:00
|
|
|
// List of units which are successfully compiled.
|
2018-11-01 02:55:22 +03:00
|
|
|
static struct rb_mjit_unit_list active_units = { LIST_HEAD_INIT(active_units.head) };
|
2019-04-14 08:26:46 +03:00
|
|
|
// List of compacted so files which will be cleaned up by `free_list()` in `mjit_finish()`.
|
2018-11-01 02:55:22 +03:00
|
|
|
static struct rb_mjit_unit_list compact_units = { LIST_HEAD_INIT(compact_units.head) };
|
2019-04-14 07:52:02 +03:00
|
|
|
// List of units before recompilation and just waiting for dlclose().
|
|
|
|
static struct rb_mjit_unit_list stale_units = { LIST_HEAD_INIT(stale_units.head) };
|
2019-04-14 08:26:46 +03:00
|
|
|
// The number of so far processed ISEQs, used to generate unique id.
|
2018-08-11 12:58:20 +03:00
|
|
|
static int current_unit_num;
|
2019-04-14 08:26:46 +03:00
|
|
|
// A mutex for conitionals and critical sections.
|
2018-08-11 12:58:20 +03:00
|
|
|
static rb_nativethread_lock_t mjit_engine_mutex;
|
2019-04-14 08:26:46 +03:00
|
|
|
// A thread conditional to wake up `mjit_finish` at the end of PCH thread.
|
2018-08-11 12:58:20 +03:00
|
|
|
static rb_nativethread_cond_t mjit_pch_wakeup;
|
2019-04-14 08:26:46 +03:00
|
|
|
// A thread conditional to wake up the client if there is a change in
|
|
|
|
// executed unit status.
|
2018-08-11 12:58:20 +03:00
|
|
|
static rb_nativethread_cond_t mjit_client_wakeup;
|
2019-04-14 08:26:46 +03:00
|
|
|
// A thread conditional to wake up a worker if there we have something
|
|
|
|
// to add or we need to stop MJIT engine.
|
2018-08-11 12:58:20 +03:00
|
|
|
static rb_nativethread_cond_t mjit_worker_wakeup;
|
2019-04-14 08:26:46 +03:00
|
|
|
// A thread conditional to wake up workers if at the end of GC.
|
2018-08-11 12:58:20 +03:00
|
|
|
static rb_nativethread_cond_t mjit_gc_wakeup;
|
2019-01-10 17:31:18 +03:00
|
|
|
// True when GC is working.
|
|
|
|
static bool in_gc;
|
|
|
|
// True when JIT is working.
|
|
|
|
static bool in_jit;
|
2019-01-12 15:39:41 +03:00
|
|
|
// Set to true to stop worker.
|
2019-01-10 17:31:18 +03:00
|
|
|
static bool stop_worker_p;
|
2019-01-12 15:39:41 +03:00
|
|
|
// Set to true if worker is stopped.
|
2019-01-10 17:31:18 +03:00
|
|
|
static bool worker_stopped;
|
2018-08-11 12:58:20 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Path of "/tmp", which can be changed to $TMP in MinGW.
|
2018-08-11 12:58:20 +03:00
|
|
|
static char *tmp_dir;
|
2019-04-14 08:26:46 +03:00
|
|
|
// Hash like { 1 => true, 2 => true, ... } whose keys are valid `class_serial`s.
|
|
|
|
// This is used to invalidate obsoleted CALL_CACHE.
|
2018-08-11 12:58:20 +03:00
|
|
|
static VALUE valid_class_serials;
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Used C compiler path.
|
2018-08-11 13:05:56 +03:00
|
|
|
static const char *cc_path;
|
2019-04-14 08:26:46 +03:00
|
|
|
// Used C compiler flags.
|
2018-12-03 15:32:01 +03:00
|
|
|
static const char **cc_common_args;
|
2019-04-14 08:26:46 +03:00
|
|
|
// Name of the precompiled header file.
|
2018-08-11 13:05:56 +03:00
|
|
|
static char *pch_file;
|
2019-04-14 08:26:46 +03:00
|
|
|
// The process id which should delete the pch_file on mjit_finish.
|
2018-11-18 11:25:48 +03:00
|
|
|
static rb_pid_t pch_owner_pid;
|
2019-04-14 08:26:46 +03:00
|
|
|
// Status of the precompiled header creation. The status is
|
|
|
|
// shared by the workers and the pch thread.
|
2018-08-11 13:05:56 +03:00
|
|
|
static enum {PCH_NOT_READY, PCH_FAILED, PCH_SUCCESS} pch_status;
|
2018-08-11 12:58:20 +03:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2019-04-14 08:26:46 +03:00
|
|
|
// Name of the header file.
|
2018-08-11 13:05:56 +03:00
|
|
|
static char *header_file;
|
2018-08-11 12:58:20 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2019-04-14 08:26:46 +03:00
|
|
|
// Linker option to enable libruby.
|
2018-08-11 13:05:56 +03:00
|
|
|
static char *libruby_pathflag;
|
2018-08-11 12:58:20 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mjit_config.h"
|
|
|
|
|
|
|
|
#if defined(__GNUC__) && \
|
|
|
|
(!defined(__clang__) || \
|
|
|
|
(defined(__clang__) && (defined(__FreeBSD__) || defined(__GLIBC__))))
|
2018-12-03 15:32:01 +03:00
|
|
|
# define GCC_PIC_FLAGS "-Wfatal-errors", "-fPIC", "-shared", "-w", "-pipe",
|
|
|
|
# define MJIT_CFLAGS_PIPE 1
|
2018-08-11 12:58:20 +03:00
|
|
|
#else
|
2018-12-03 15:32:01 +03:00
|
|
|
# define GCC_PIC_FLAGS /* empty */
|
|
|
|
# define MJIT_CFLAGS_PIPE 0
|
2018-08-11 12:58:20 +03:00
|
|
|
#endif
|
|
|
|
|
2019-01-19 12:08:10 +03:00
|
|
|
// Use `-nodefaultlibs -nostdlib` for GCC where possible, which does not work on mingw, cygwin, AIX, and OpenBSD.
|
|
|
|
// This seems to improve MJIT performance on GCC.
|
2019-01-19 10:26:59 +03:00
|
|
|
#if defined __GNUC__ && !defined __clang__ && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(_AIX) && !defined(__OpenBSD__)
|
2019-01-19 12:08:10 +03:00
|
|
|
# define GCC_NOSTDLIB_FLAGS "-nodefaultlibs", "-nostdlib",
|
2019-01-14 10:28:34 +03:00
|
|
|
#else
|
2019-04-14 08:26:46 +03:00
|
|
|
# define GCC_NOSTDLIB_FLAGS // empty
|
2019-01-14 10:28:34 +03:00
|
|
|
#endif
|
|
|
|
|
2018-08-11 12:58:20 +03:00
|
|
|
static const char *const CC_COMMON_ARGS[] = {
|
|
|
|
MJIT_CC_COMMON MJIT_CFLAGS GCC_PIC_FLAGS
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const CC_DEBUG_ARGS[] = {MJIT_DEBUGFLAGS NULL};
|
2018-08-18 04:10:03 +03:00
|
|
|
static const char *const CC_OPTIMIZE_ARGS[] = {MJIT_OPTFLAGS NULL};
|
2018-08-11 12:58:20 +03:00
|
|
|
|
|
|
|
static const char *const CC_LDSHARED_ARGS[] = {MJIT_LDSHARED GCC_PIC_FLAGS NULL};
|
2019-08-30 16:16:32 +03:00
|
|
|
static const char *const CC_DLDFLAGS_ARGS[] = {MJIT_DLDFLAGS NULL};
|
|
|
|
// `CC_LINKER_ARGS` are linker flags which must be passed to `-c` as well.
|
|
|
|
static const char *const CC_LINKER_ARGS[] = {
|
2019-01-19 12:08:10 +03:00
|
|
|
#if defined __GNUC__ && !defined __clang__ && !defined(__OpenBSD__)
|
|
|
|
"-nostartfiles",
|
|
|
|
#endif
|
|
|
|
GCC_NOSTDLIB_FLAGS NULL
|
|
|
|
};
|
2018-08-11 12:58:20 +03:00
|
|
|
|
|
|
|
static const char *const CC_LIBS[] = {
|
2019-01-14 09:18:53 +03:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
|
|
|
MJIT_LIBS // mswin, mingw, cygwin
|
2019-01-14 08:50:13 +03:00
|
|
|
#endif
|
2019-01-14 09:18:53 +03:00
|
|
|
#if defined __GNUC__ && !defined __clang__
|
|
|
|
# if defined(_WIN32)
|
|
|
|
"-lmsvcrt", // mingw
|
|
|
|
# endif
|
2019-01-19 12:08:10 +03:00
|
|
|
"-lgcc", // mingw, cygwin, and GCC platforms using `-nodefaultlibs -nostdlib`
|
2019-10-10 17:22:37 +03:00
|
|
|
#endif
|
|
|
|
#if defined __ANDROID__
|
|
|
|
"-lm", // to avoid 'cannot locate symbol "modf" referenced by .../_ruby_mjit_XXX.so"'
|
2018-08-11 12:58:20 +03:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CC_CODEFLAG_ARGS (mjit_opts.debug ? CC_DEBUG_ARGS : CC_OPTIMIZE_ARGS)
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Print the arguments according to FORMAT to stderr only if MJIT
|
|
|
|
// verbose option value is more or equal to LEVEL.
|
2018-08-11 12:37:14 +03:00
|
|
|
PRINTF_ARGS(static void, 2, 3)
|
|
|
|
verbose(int level, const char *format, ...)
|
|
|
|
{
|
2018-08-11 17:05:34 +03:00
|
|
|
if (mjit_opts.verbose >= level) {
|
|
|
|
va_list args;
|
2018-09-11 17:38:05 +03:00
|
|
|
size_t len = strlen(format);
|
|
|
|
char *full_format = alloca(sizeof(char) * (len + 2));
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Creating `format + '\n'` to atomically print format and '\n'.
|
2018-09-11 17:38:05 +03:00
|
|
|
memcpy(full_format, format, len);
|
|
|
|
full_format[len] = '\n';
|
|
|
|
full_format[len+1] = '\0';
|
2018-08-11 12:37:14 +03:00
|
|
|
|
2018-08-11 17:05:34 +03:00
|
|
|
va_start(args, format);
|
2018-09-11 17:38:05 +03:00
|
|
|
vfprintf(stderr, full_format, args);
|
2018-08-11 17:05:34 +03:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF_ARGS(static void, 1, 2)
|
|
|
|
mjit_warning(const char *format, ...)
|
|
|
|
{
|
|
|
|
if (mjit_opts.warnings || mjit_opts.verbose) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
fprintf(stderr, "MJIT warning: ");
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2018-08-11 12:37:14 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Add unit node to the tail of doubly linked `list`. It should be not in
|
|
|
|
// the list before.
|
2018-08-11 12:37:14 +03:00
|
|
|
static void
|
2018-11-01 02:55:22 +03:00
|
|
|
add_to_list(struct rb_mjit_unit *unit, struct rb_mjit_unit_list *list)
|
2018-08-11 12:37:14 +03:00
|
|
|
{
|
2019-03-30 02:26:31 +03:00
|
|
|
(void)RB_DEBUG_COUNTER_INC_IF(mjit_length_unit_queue, list == &unit_queue);
|
|
|
|
(void)RB_DEBUG_COUNTER_INC_IF(mjit_length_active_units, list == &active_units);
|
|
|
|
(void)RB_DEBUG_COUNTER_INC_IF(mjit_length_compact_units, list == &compact_units);
|
2019-04-14 10:12:44 +03:00
|
|
|
(void)RB_DEBUG_COUNTER_INC_IF(mjit_length_stale_units, list == &stale_units);
|
2019-03-29 17:08:51 +03:00
|
|
|
|
2018-11-01 02:55:22 +03:00
|
|
|
list_add_tail(&list->head, &unit->unode);
|
2018-08-11 12:37:14 +03:00
|
|
|
list->length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-11-01 02:55:22 +03:00
|
|
|
remove_from_list(struct rb_mjit_unit *unit, struct rb_mjit_unit_list *list)
|
2018-08-11 12:37:14 +03:00
|
|
|
{
|
2019-03-29 17:08:51 +03:00
|
|
|
#if USE_DEBUG_COUNTER
|
|
|
|
rb_debug_counter_add(RB_DEBUG_COUNTER_mjit_length_unit_queue, -1, list == &unit_queue);
|
|
|
|
rb_debug_counter_add(RB_DEBUG_COUNTER_mjit_length_active_units, -1, list == &active_units);
|
|
|
|
rb_debug_counter_add(RB_DEBUG_COUNTER_mjit_length_compact_units, -1, list == &compact_units);
|
2019-04-14 10:12:44 +03:00
|
|
|
rb_debug_counter_add(RB_DEBUG_COUNTER_mjit_length_stale_units, -1, list == &stale_units);
|
2019-03-29 17:08:51 +03:00
|
|
|
#endif
|
|
|
|
|
2018-11-01 02:55:22 +03:00
|
|
|
list_del(&unit->unode);
|
2018-08-11 12:37:14 +03:00
|
|
|
list->length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_file(const char *filename)
|
|
|
|
{
|
2018-08-11 17:05:34 +03:00
|
|
|
if (remove(filename)) {
|
|
|
|
mjit_warning("failed to remove \"%s\": %s", filename, strerror(errno));
|
2018-08-11 12:37:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Lazily delete .o and/or .so files.
|
2018-08-11 12:37:14 +03:00
|
|
|
static void
|
|
|
|
clean_object_files(struct rb_mjit_unit *unit)
|
|
|
|
{
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
if (unit->o_file) {
|
|
|
|
char *o_file = unit->o_file;
|
|
|
|
|
|
|
|
unit->o_file = NULL;
|
2019-04-14 08:26:46 +03:00
|
|
|
// For compaction, unit->o_file is always set when compilation succeeds.
|
|
|
|
// So save_temps needs to be checked here.
|
2018-11-18 11:25:48 +03:00
|
|
|
if (!mjit_opts.save_temps && !unit->o_file_inherited_p)
|
2018-08-11 12:37:14 +03:00
|
|
|
remove_file(o_file);
|
|
|
|
free(o_file);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-18 04:10:03 +03:00
|
|
|
#if defined(_WIN32)
|
2018-08-11 12:37:14 +03:00
|
|
|
if (unit->so_file) {
|
|
|
|
char *so_file = unit->so_file;
|
|
|
|
|
|
|
|
unit->so_file = NULL;
|
2019-01-10 17:31:18 +03:00
|
|
|
// unit->so_file is set only when mjit_opts.save_temps is false.
|
2018-08-11 12:37:14 +03:00
|
|
|
remove_file(so_file);
|
|
|
|
free(so_file);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// This is called in the following situations:
|
|
|
|
// 1) On dequeue or `unload_units()`, associated ISeq is already GCed.
|
|
|
|
// 2) The unit is not called often and unloaded by `unload_units()`.
|
|
|
|
// 3) Freeing lists on `mjit_finish()`.
|
|
|
|
//
|
|
|
|
// `jit_func` value does not matter for 1 and 3 since the unit won't be used anymore.
|
|
|
|
// For the situation 2, this sets the ISeq's JIT state to NOT_COMPILED_JIT_ISEQ_FUNC
|
|
|
|
// to prevent the situation that the same methods are continuously compiled.
|
2018-08-11 12:37:14 +03:00
|
|
|
static void
|
|
|
|
free_unit(struct rb_mjit_unit *unit)
|
|
|
|
{
|
2019-04-14 08:26:46 +03:00
|
|
|
if (unit->iseq) { // ISeq is not GCed
|
2018-08-11 12:37:14 +03:00
|
|
|
unit->iseq->body->jit_func = (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
|
|
|
|
unit->iseq->body->jit_unit = NULL;
|
|
|
|
}
|
2019-04-14 08:26:46 +03:00
|
|
|
if (unit->handle && dlclose(unit->handle)) { // handle is NULL if it's in queue
|
2018-10-07 15:28:46 +03:00
|
|
|
mjit_warning("failed to close handle for u%d: %s", unit->id, dlerror());
|
|
|
|
}
|
2018-08-11 12:37:14 +03:00
|
|
|
clean_object_files(unit);
|
2018-08-11 17:27:53 +03:00
|
|
|
free(unit);
|
2018-08-11 12:37:14 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Start a critical section. Use message `msg` to print debug info at `level`.
|
2018-08-11 12:48:07 +03:00
|
|
|
static inline void
|
|
|
|
CRITICAL_SECTION_START(int level, const char *msg)
|
|
|
|
{
|
|
|
|
verbose(level, "Locking %s", msg);
|
|
|
|
rb_native_mutex_lock(&mjit_engine_mutex);
|
|
|
|
verbose(level, "Locked %s", msg);
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Finish the current critical section. Use message `msg` to print
|
|
|
|
// debug info at `level`.
|
2018-08-11 12:48:07 +03:00
|
|
|
static inline void
|
|
|
|
CRITICAL_SECTION_FINISH(int level, const char *msg)
|
|
|
|
{
|
|
|
|
verbose(level, "Unlocked %s", msg);
|
|
|
|
rb_native_mutex_unlock(&mjit_engine_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sprint_uniq_filename(char *str, size_t size, unsigned long id, const char *prefix, const char *suffix)
|
|
|
|
{
|
|
|
|
return snprintf(str, size, "%s/%sp%"PRI_PIDT_PREFIX"uu%lu%s", tmp_dir, prefix, getpid(), id, suffix);
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Return time in milliseconds as a double.
|
2018-08-11 10:57:58 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
double ruby_real_ms_time(void);
|
|
|
|
# define real_ms_time() ruby_real_ms_time()
|
|
|
|
#else
|
|
|
|
static double
|
|
|
|
real_ms_time(void)
|
|
|
|
{
|
|
|
|
# ifdef HAVE_CLOCK_GETTIME
|
|
|
|
struct timespec tv;
|
|
|
|
# ifdef CLOCK_MONOTONIC
|
|
|
|
const clockid_t c = CLOCK_MONOTONIC;
|
|
|
|
# else
|
|
|
|
const clockid_t c = CLOCK_REALTIME;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
clock_gettime(c, &tv);
|
|
|
|
return tv.tv_nsec / 1000000.0 + tv.tv_sec * 1000.0;
|
|
|
|
# else
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_usec / 1000.0 + tv.tv_sec * 1000.0;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
// Return true if class_serial is not obsoleted. This is used by mjit_compile.c.
|
|
|
|
bool
|
2018-08-11 11:34:25 +03:00
|
|
|
mjit_valid_class_serial_p(rb_serial_t class_serial)
|
|
|
|
{
|
|
|
|
CRITICAL_SECTION_START(3, "in valid_class_serial_p");
|
2019-01-10 17:31:18 +03:00
|
|
|
bool found_p = rb_hash_stlike_lookup(valid_class_serials, LONG2FIX(class_serial), NULL);
|
2018-08-11 11:34:25 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in valid_class_serial_p");
|
|
|
|
return found_p;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Return the best unit from list. The best is the first
|
|
|
|
// high priority unit or the unit whose iseq has the biggest number
|
|
|
|
// of calls so far.
|
2018-11-01 02:55:22 +03:00
|
|
|
static struct rb_mjit_unit *
|
2018-08-11 10:57:58 +03:00
|
|
|
get_from_list(struct rb_mjit_unit_list *list)
|
|
|
|
{
|
2018-11-01 02:55:22 +03:00
|
|
|
struct rb_mjit_unit *unit = NULL, *next, *best = NULL;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Find iseq with max total_calls
|
2018-11-01 02:55:22 +03:00
|
|
|
list_for_each_safe(&list->head, unit, next, unode) {
|
2019-04-14 08:26:46 +03:00
|
|
|
if (unit->iseq == NULL) { // ISeq is GCed.
|
2018-11-01 02:55:22 +03:00
|
|
|
remove_from_list(unit, list);
|
|
|
|
free_unit(unit);
|
2018-08-11 10:57:58 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-11-01 02:55:22 +03:00
|
|
|
if (best == NULL || best->iseq->body->total_calls < unit->iseq->body->total_calls) {
|
|
|
|
best = unit;
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 02:55:22 +03:00
|
|
|
if (best) {
|
|
|
|
remove_from_list(best, list);
|
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Return length of NULL-terminated array `args` excluding the NULL marker.
|
2018-08-11 10:57:58 +03:00
|
|
|
static size_t
|
|
|
|
args_len(char *const *args)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; (args[i]) != NULL;i++)
|
|
|
|
;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Concatenate `num` passed NULL-terminated arrays of strings, put the
|
|
|
|
// result (with NULL end marker) into the heap, and return the result.
|
2018-08-11 10:57:58 +03:00
|
|
|
static char **
|
|
|
|
form_args(int num, ...)
|
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
size_t len, n;
|
|
|
|
int i;
|
|
|
|
char **args, **res, **tmp;
|
|
|
|
|
|
|
|
va_start(argp, num);
|
|
|
|
res = NULL;
|
|
|
|
for (i = len = 0; i < num; i++) {
|
|
|
|
args = va_arg(argp, char **);
|
|
|
|
n = args_len(args);
|
|
|
|
if ((tmp = (char **)realloc(res, sizeof(char *) * (len + n + 1))) == NULL) {
|
|
|
|
free(res);
|
2019-07-15 03:35:45 +03:00
|
|
|
res = NULL;
|
|
|
|
break;
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
res = tmp;
|
|
|
|
MEMCPY(res + len, args, char *, n + 1);
|
|
|
|
len += n;
|
|
|
|
}
|
|
|
|
va_end(argp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMPILER_WARNING_PUSH
|
|
|
|
#ifdef __GNUC__
|
|
|
|
COMPILER_WARNING_IGNORED(-Wdeprecated-declarations)
|
|
|
|
#endif
|
2019-04-14 08:26:46 +03:00
|
|
|
// Start an OS process of absolute executable path with arguments `argv`.
|
|
|
|
// Return PID of the process.
|
2018-08-11 10:57:58 +03:00
|
|
|
static pid_t
|
2018-11-07 11:04:39 +03:00
|
|
|
start_process(const char *abspath, char *const *argv)
|
2018-08-11 10:57:58 +03:00
|
|
|
{
|
2019-07-15 04:38:59 +03:00
|
|
|
// Not calling non-async-signal-safe functions between vfork
|
|
|
|
// and execv for safety
|
|
|
|
int dev_null = rb_cloexec_open(ruby_null_device, O_WRONLY, 0);
|
|
|
|
if (dev_null < 0) {
|
|
|
|
verbose(1, "MJIT: Failed to open a null device: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
if (mjit_opts.verbose >= 2) {
|
|
|
|
const char *arg;
|
2018-11-07 11:04:39 +03:00
|
|
|
fprintf(stderr, "Starting process: %s", abspath);
|
2019-07-15 04:29:39 +03:00
|
|
|
for (int i = 0; (arg = argv[i]) != NULL; i++)
|
2018-08-11 10:57:58 +03:00
|
|
|
fprintf(stderr, " %s", arg);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2019-07-15 04:29:39 +03:00
|
|
|
|
|
|
|
pid_t pid;
|
2018-08-11 10:57:58 +03:00
|
|
|
#ifdef _WIN32
|
2019-07-15 04:29:39 +03:00
|
|
|
extern HANDLE rb_w32_start_process(const char *abspath, char *const *argv, int out_fd);
|
|
|
|
int out_fd = 0;
|
|
|
|
if (mjit_opts.verbose <= 1) {
|
|
|
|
// Discard cl.exe's outputs like:
|
|
|
|
// _ruby_mjit_p12u3.c
|
|
|
|
// Creating library C:.../_ruby_mjit_p12u3.lib and object C:.../_ruby_mjit_p12u3.exp
|
|
|
|
out_fd = dev_null;
|
|
|
|
}
|
2018-10-12 18:14:51 +03:00
|
|
|
|
2019-07-15 04:29:39 +03:00
|
|
|
pid = (pid_t)rb_w32_start_process(abspath, argv, out_fd);
|
|
|
|
if (pid == 0) {
|
|
|
|
verbose(1, "MJIT: Failed to create process: %s", dlerror());
|
|
|
|
return -1;
|
2018-10-12 18:14:51 +03:00
|
|
|
}
|
|
|
|
#else
|
2018-11-07 11:04:39 +03:00
|
|
|
if ((pid = vfork()) == 0) { /* TODO: reuse some function in process.c */
|
2018-10-12 18:14:51 +03:00
|
|
|
umask(0077);
|
|
|
|
if (mjit_opts.verbose == 0) {
|
2019-04-14 08:26:46 +03:00
|
|
|
// CC can be started in a thread using a file which has been
|
|
|
|
// already removed while MJIT is finishing. Discard the
|
|
|
|
// messages about missing files.
|
2018-10-12 18:14:51 +03:00
|
|
|
dup2(dev_null, STDERR_FILENO);
|
|
|
|
dup2(dev_null, STDOUT_FILENO);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
(void)close(dev_null);
|
2019-04-14 08:26:46 +03:00
|
|
|
pid = execv(abspath, argv); // Pid will be negative on an error
|
|
|
|
// Even if we successfully found CC to compile PCH we still can
|
|
|
|
// fail with loading the CC in very rare cases for some reasons.
|
|
|
|
// Stop the forked process in this case.
|
2018-10-12 18:14:51 +03:00
|
|
|
verbose(1, "MJIT: Error in execv: %s", abspath);
|
|
|
|
_exit(1);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
#endif
|
2018-10-12 18:14:51 +03:00
|
|
|
(void)close(dev_null);
|
2018-08-11 10:57:58 +03:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
COMPILER_WARNING_POP
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Execute an OS process of executable PATH with arguments ARGV.
|
|
|
|
// Return -1 or -2 if failed to execute, otherwise exit code of the process.
|
|
|
|
// TODO: Use a similar function in process.c
|
2018-08-11 10:57:58 +03:00
|
|
|
static int
|
|
|
|
exec_process(const char *path, char *const argv[])
|
|
|
|
{
|
|
|
|
int stat, exit_code = -2;
|
|
|
|
rb_vm_t *vm = WAITPID_USE_SIGCHLD ? GET_VM() : 0;
|
|
|
|
rb_nativethread_cond_t cond;
|
|
|
|
|
|
|
|
if (vm) {
|
|
|
|
rb_native_cond_initialize(&cond);
|
|
|
|
rb_native_mutex_lock(&vm->waitpid_lock);
|
|
|
|
}
|
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
pid_t pid = start_process(path, argv);
|
2018-08-11 10:57:58 +03:00
|
|
|
for (;pid > 0;) {
|
|
|
|
pid_t r = vm ? ruby_waitpid_locked(vm, pid, &stat, 0, &cond)
|
|
|
|
: waitpid(pid, &stat, 0);
|
|
|
|
if (r == -1) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
fprintf(stderr, "[%"PRI_PIDT_PREFIX"d] waitpid(%lu): %s (SIGCHLD=%d,%u)\n",
|
|
|
|
getpid(), (unsigned long)pid, strerror(errno),
|
|
|
|
RUBY_SIGCHLD, SIGCHLD_LOSSY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (r == pid) {
|
|
|
|
if (WIFEXITED(stat)) {
|
|
|
|
exit_code = WEXITSTATUS(stat);
|
|
|
|
break;
|
2019-01-09 16:58:49 +03:00
|
|
|
}
|
|
|
|
else if (WIFSIGNALED(stat)) {
|
2018-08-11 10:57:58 +03:00
|
|
|
exit_code = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm) {
|
|
|
|
rb_native_mutex_unlock(&vm->waitpid_lock);
|
|
|
|
rb_native_cond_destroy(&cond);
|
|
|
|
}
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
2018-08-12 18:00:03 +03:00
|
|
|
static void
|
|
|
|
remove_so_file(const char *so_file, struct rb_mjit_unit *unit)
|
|
|
|
{
|
2018-08-18 04:10:03 +03:00
|
|
|
#if defined(_WIN32)
|
2019-04-14 08:26:46 +03:00
|
|
|
// Windows can't remove files while it's used.
|
|
|
|
unit->so_file = strdup(so_file); // lazily delete on `clean_object_files()`
|
2018-08-12 18:00:03 +03:00
|
|
|
if (unit->so_file == NULL)
|
|
|
|
mjit_warning("failed to allocate memory to lazily remove '%s': %s", so_file, strerror(errno));
|
|
|
|
#else
|
|
|
|
remove_file(so_file);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-08-11 12:58:20 +03:00
|
|
|
#define append_str2(p, str, len) ((char *)memcpy((p), str, (len))+(len))
|
|
|
|
#define append_str(p, str) append_str2(p, str, sizeof(str)-1)
|
|
|
|
#define append_lit(p, str) append_str2(p, str, rb_strlen_lit(str))
|
|
|
|
|
2018-08-11 10:57:58 +03:00
|
|
|
#ifdef _MSC_VER
|
2019-01-10 17:31:18 +03:00
|
|
|
// Compile C file to so. It returns true if it succeeds. (mswin)
|
|
|
|
static bool
|
2018-08-11 10:57:58 +03:00
|
|
|
compile_c_to_so(const char *c_file, const char *so_file)
|
|
|
|
{
|
2018-10-11 19:28:04 +03:00
|
|
|
const char *files[] = { NULL, NULL, NULL, NULL, NULL, NULL, "-link", libruby_pathflag, NULL };
|
2019-07-14 06:53:39 +03:00
|
|
|
char *p;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[0] = "-Fe*.dll"
|
2018-08-11 17:36:14 +03:00
|
|
|
files[0] = p = alloca(sizeof(char) * (rb_strlen_lit("-Fe") + strlen(so_file) + 1));
|
2018-08-11 10:57:58 +03:00
|
|
|
p = append_lit(p, "-Fe");
|
|
|
|
p = append_str2(p, so_file, strlen(so_file));
|
|
|
|
*p = '\0';
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[1] = "-Fo*.obj"
|
|
|
|
// We don't need .obj file, but it's somehow created to cwd without -Fo and we want to control the output directory.
|
2018-10-07 11:03:36 +03:00
|
|
|
files[1] = p = alloca(sizeof(char) * (rb_strlen_lit("-Fo") + strlen(so_file) - rb_strlen_lit(DLEXT) + rb_strlen_lit(".obj") + 1));
|
2019-07-14 06:53:39 +03:00
|
|
|
char *obj_file = p = append_lit(p, "-Fo");
|
2018-10-07 11:03:36 +03:00
|
|
|
p = append_str2(p, so_file, strlen(so_file) - rb_strlen_lit(DLEXT));
|
|
|
|
p = append_lit(p, ".obj");
|
|
|
|
*p = '\0';
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[2] = "-Yu*.pch"
|
2018-10-07 11:03:36 +03:00
|
|
|
files[2] = p = alloca(sizeof(char) * (rb_strlen_lit("-Yu") + strlen(pch_file) + 1));
|
2018-08-11 10:57:58 +03:00
|
|
|
p = append_lit(p, "-Yu");
|
2018-08-11 12:48:07 +03:00
|
|
|
p = append_str2(p, pch_file, strlen(pch_file));
|
2018-08-11 10:57:58 +03:00
|
|
|
*p = '\0';
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[3] = "C:/.../rb_mjit_header-*.obj"
|
2018-10-07 11:03:36 +03:00
|
|
|
files[3] = p = alloca(sizeof(char) * (strlen(pch_file) + 1));
|
2018-08-11 12:48:07 +03:00
|
|
|
p = append_str2(p, pch_file, strlen(pch_file) - strlen(".pch"));
|
2018-08-11 10:57:58 +03:00
|
|
|
p = append_lit(p, ".obj");
|
|
|
|
*p = '\0';
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[4] = "-Tc*.c"
|
2018-10-07 11:03:36 +03:00
|
|
|
files[4] = p = alloca(sizeof(char) * (rb_strlen_lit("-Tc") + strlen(c_file) + 1));
|
2018-08-11 10:57:58 +03:00
|
|
|
p = append_lit(p, "-Tc");
|
|
|
|
p = append_str2(p, c_file, strlen(c_file));
|
|
|
|
*p = '\0';
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// files[5] = "-Fd*.pdb"
|
2018-10-11 19:28:04 +03:00
|
|
|
files[5] = p = alloca(sizeof(char) * (rb_strlen_lit("-Fd") + strlen(pch_file) + 1));
|
|
|
|
p = append_lit(p, "-Fd");
|
|
|
|
p = append_str2(p, pch_file, strlen(pch_file) - rb_strlen_lit(".pch"));
|
|
|
|
p = append_lit(p, ".pdb");
|
|
|
|
*p = '\0';
|
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
char **args = form_args(5, CC_LDSHARED_ARGS, CC_CODEFLAG_ARGS,
|
|
|
|
files, CC_LIBS, CC_DLDFLAGS_ARGS);
|
2018-08-11 10:57:58 +03:00
|
|
|
if (args == NULL)
|
2019-01-10 17:31:18 +03:00
|
|
|
return false;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
int exit_code = exec_process(cc_path, args);
|
2018-08-11 10:57:58 +03:00
|
|
|
free(args);
|
|
|
|
|
2018-10-07 11:03:36 +03:00
|
|
|
if (exit_code == 0) {
|
2019-04-14 08:26:46 +03:00
|
|
|
// remove never-used files (.obj, .lib, .exp, .pdb). XXX: Is there any way not to generate this?
|
2018-10-07 18:09:58 +03:00
|
|
|
if (!mjit_opts.save_temps) {
|
|
|
|
char *before_dot;
|
|
|
|
remove_file(obj_file);
|
|
|
|
|
|
|
|
before_dot = obj_file + strlen(obj_file) - rb_strlen_lit(".obj");
|
|
|
|
append_lit(before_dot, ".lib"); remove_file(obj_file);
|
|
|
|
append_lit(before_dot, ".exp"); remove_file(obj_file);
|
|
|
|
append_lit(before_dot, ".pdb"); remove_file(obj_file);
|
|
|
|
}
|
2018-10-07 11:03:36 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-08-11 10:57:58 +03:00
|
|
|
verbose(2, "compile_c_to_so: compile error: %d", exit_code);
|
2018-10-07 11:03:36 +03:00
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
return exit_code == 0;
|
|
|
|
}
|
2019-04-14 08:26:46 +03:00
|
|
|
#else // _MSC_VER
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// The function producing the pre-compiled header.
|
2018-08-11 10:57:58 +03:00
|
|
|
static void
|
|
|
|
make_pch(void)
|
|
|
|
{
|
|
|
|
const char *rest_args[] = {
|
|
|
|
# ifdef __clang__
|
|
|
|
"-emit-pch",
|
|
|
|
# endif
|
2019-01-14 10:28:34 +03:00
|
|
|
// -nodefaultlibs is a linker flag, but it may affect cc1 behavior on Gentoo, which should NOT be changed on pch:
|
|
|
|
// https://gitweb.gentoo.org/proj/gcc-patches.git/tree/7.3.0/gentoo/13_all_default-ssp-fix.patch
|
|
|
|
GCC_NOSTDLIB_FLAGS
|
2019-07-14 06:53:39 +03:00
|
|
|
"-o", pch_file, header_file,
|
2018-08-11 10:57:58 +03:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
verbose(2, "Creating precompiled header");
|
2019-07-14 06:53:39 +03:00
|
|
|
char **args = form_args(3, cc_common_args, CC_CODEFLAG_ARGS, rest_args);
|
2018-08-11 10:57:58 +03:00
|
|
|
if (args == NULL) {
|
2018-08-11 17:05:34 +03:00
|
|
|
mjit_warning("making precompiled header failed on forming args");
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in make_pch");
|
2018-08-11 13:05:56 +03:00
|
|
|
pch_status = PCH_FAILED;
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in make_pch");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
int exit_code = exec_process(cc_path, args);
|
2018-08-11 10:57:58 +03:00
|
|
|
free(args);
|
|
|
|
|
|
|
|
CRITICAL_SECTION_START(3, "in make_pch");
|
|
|
|
if (exit_code == 0) {
|
2018-08-11 13:05:56 +03:00
|
|
|
pch_status = PCH_SUCCESS;
|
2018-08-11 17:05:34 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mjit_warning("Making precompiled header failed on compilation. Stopping MJIT worker...");
|
2018-08-11 13:05:56 +03:00
|
|
|
pch_status = PCH_FAILED;
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
/* wakeup `mjit_finish` */
|
|
|
|
rb_native_cond_broadcast(&mjit_pch_wakeup);
|
|
|
|
CRITICAL_SECTION_FINISH(3, "in make_pch");
|
|
|
|
}
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
// Compile .c file to .o file. It returns true if it succeeds. (non-mswin)
|
|
|
|
static bool
|
2018-08-11 10:57:58 +03:00
|
|
|
compile_c_to_o(const char *c_file, const char *o_file)
|
|
|
|
{
|
|
|
|
const char *files[] = {
|
2019-07-14 06:53:39 +03:00
|
|
|
"-o", o_file, c_file,
|
2018-08-11 10:57:58 +03:00
|
|
|
# ifdef __clang__
|
2019-07-14 06:53:39 +03:00
|
|
|
"-include-pch", pch_file,
|
2018-08-11 10:57:58 +03:00
|
|
|
# endif
|
|
|
|
"-c", NULL
|
|
|
|
};
|
|
|
|
|
2019-08-30 16:16:32 +03:00
|
|
|
char **args = form_args(4, cc_common_args, CC_CODEFLAG_ARGS, files, CC_LINKER_ARGS);
|
2018-08-11 10:57:58 +03:00
|
|
|
if (args == NULL)
|
2019-01-10 17:31:18 +03:00
|
|
|
return false;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
int exit_code = exec_process(cc_path, args);
|
2018-08-11 10:57:58 +03:00
|
|
|
free(args);
|
|
|
|
|
|
|
|
if (exit_code != 0)
|
|
|
|
verbose(2, "compile_c_to_o: compile error: %d", exit_code);
|
|
|
|
return exit_code == 0;
|
|
|
|
}
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
// Link .o files to .so file. It returns true if it succeeds. (non-mswin)
|
|
|
|
static bool
|
2018-08-11 10:57:58 +03:00
|
|
|
link_o_to_so(const char **o_files, const char *so_file)
|
|
|
|
{
|
|
|
|
const char *options[] = {
|
2019-07-14 06:53:39 +03:00
|
|
|
"-o", so_file,
|
2018-08-11 10:57:58 +03:00
|
|
|
# ifdef _WIN32
|
2018-08-11 13:05:56 +03:00
|
|
|
libruby_pathflag,
|
2018-08-11 10:57:58 +03:00
|
|
|
# endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2019-08-30 16:16:32 +03:00
|
|
|
char **args = form_args(7, CC_LDSHARED_ARGS, CC_CODEFLAG_ARGS,
|
|
|
|
options, o_files, CC_LIBS, CC_DLDFLAGS_ARGS, CC_LINKER_ARGS);
|
2018-08-11 10:57:58 +03:00
|
|
|
if (args == NULL)
|
2019-01-10 17:31:18 +03:00
|
|
|
return false;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-07-14 06:53:39 +03:00
|
|
|
int exit_code = exec_process(cc_path, args);
|
2018-08-11 10:57:58 +03:00
|
|
|
free(args);
|
|
|
|
|
|
|
|
if (exit_code != 0)
|
|
|
|
verbose(2, "link_o_to_so: link error: %d", exit_code);
|
|
|
|
return exit_code == 0;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Link all cached .o files and build a .so file. Reload all JIT func from it. This
|
|
|
|
// allows to avoid JIT code fragmentation and improve performance to call JIT-ed code.
|
2018-08-11 10:57:58 +03:00
|
|
|
static void
|
|
|
|
compact_all_jit_code(void)
|
|
|
|
{
|
2019-04-14 08:26:46 +03:00
|
|
|
# ifndef _WIN32 // This requires header transformation but we don't transform header on Windows for now
|
2018-11-01 02:55:22 +03:00
|
|
|
struct rb_mjit_unit *unit, *cur = 0;
|
2018-08-11 10:57:58 +03:00
|
|
|
double start_time, end_time;
|
|
|
|
static const char so_ext[] = DLEXT;
|
|
|
|
char so_file[MAXPATHLEN];
|
|
|
|
const char **o_files;
|
2019-01-10 17:31:18 +03:00
|
|
|
int i = 0;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Abnormal use case of rb_mjit_unit that doesn't have ISeq
|
|
|
|
unit = calloc(1, sizeof(struct rb_mjit_unit)); // To prevent GC, don't use ZALLOC
|
2018-08-11 10:57:58 +03:00
|
|
|
if (unit == NULL) return;
|
2018-08-11 12:48:07 +03:00
|
|
|
unit->id = current_unit_num++;
|
2018-08-11 10:57:58 +03:00
|
|
|
sprint_uniq_filename(so_file, (int)sizeof(so_file), unit->id, MJIT_TMP_PREFIX, so_ext);
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// NULL-ending for form_args
|
2018-08-11 17:36:14 +03:00
|
|
|
o_files = alloca(sizeof(char *) * (active_units.length + 1));
|
2018-08-11 12:48:07 +03:00
|
|
|
o_files[active_units.length] = NULL;
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in compact_all_jit_code to keep .o files");
|
2018-11-01 02:55:22 +03:00
|
|
|
list_for_each(&active_units.head, cur, unode) {
|
|
|
|
o_files[i] = cur->o_file;
|
2018-08-11 10:57:58 +03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_time = real_ms_time();
|
2019-01-10 17:31:18 +03:00
|
|
|
bool success = link_o_to_so(o_files, so_file);
|
2018-08-11 10:57:58 +03:00
|
|
|
end_time = real_ms_time();
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// TODO: Shrink this big critical section. For now, this is needed to prevent failure by missing .o files.
|
|
|
|
// This assumes that o -> so link doesn't take long time because the bottleneck, which is compiler optimization,
|
|
|
|
// is already done. But actually it takes about 500ms for 5,000 methods on my Linux machine, so it's better to
|
|
|
|
// finish this critical section before link_o_to_so by disabling unload_units.
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in compact_all_jit_code to keep .o files");
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
void *handle = dlopen(so_file, RTLD_NOW);
|
|
|
|
if (handle == NULL) {
|
2018-08-11 17:05:34 +03:00
|
|
|
mjit_warning("failure in loading code from compacted '%s': %s", so_file, dlerror());
|
2018-08-11 10:57:58 +03:00
|
|
|
free(unit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unit->handle = handle;
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// lazily dlclose handle (and .so file for win32) on `mjit_finish()`.
|
2018-11-01 02:55:22 +03:00
|
|
|
add_to_list(unit, &compact_units);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2018-08-12 18:00:03 +03:00
|
|
|
if (!mjit_opts.save_temps)
|
|
|
|
remove_so_file(so_file, unit);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
|
|
|
CRITICAL_SECTION_START(3, "in compact_all_jit_code to read list");
|
2018-11-01 02:55:22 +03:00
|
|
|
list_for_each(&active_units.head, cur, unode) {
|
2018-08-11 10:57:58 +03:00
|
|
|
void *func;
|
2019-04-14 08:26:46 +03:00
|
|
|
char funcname[35]; // TODO: reconsider `35`
|
2018-11-01 02:55:22 +03:00
|
|
|
sprintf(funcname, "_mjit%d", cur->id);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
|
|
|
if ((func = dlsym(handle, funcname)) == NULL) {
|
2018-08-11 17:05:34 +03:00
|
|
|
mjit_warning("skipping to reload '%s' from '%s': %s", funcname, so_file, dlerror());
|
2018-08-11 10:57:58 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
if (cur->iseq) { // Check whether GCed or not
|
|
|
|
// Usage of jit_code might be not in a critical section.
|
2018-11-01 02:55:22 +03:00
|
|
|
MJIT_ATOMIC_SET(cur->iseq->body->jit_func, (mjit_func_t)func);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CRITICAL_SECTION_FINISH(3, "in compact_all_jit_code to read list");
|
2018-08-11 12:48:07 +03:00
|
|
|
verbose(1, "JIT compaction (%.1fms): Compacted %d methods -> %s", end_time - start_time, active_units.length, so_file);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
free(unit);
|
|
|
|
verbose(1, "JIT compaction failure (%.1fms): Failed to compact methods", end_time - start_time);
|
|
|
|
}
|
2019-04-14 08:26:46 +03:00
|
|
|
# endif // _WIN32
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
#endif // _MSC_VER
|
2018-08-11 10:57:58 +03:00
|
|
|
|
|
|
|
static void *
|
|
|
|
load_func_from_so(const char *so_file, const char *funcname, struct rb_mjit_unit *unit)
|
|
|
|
{
|
|
|
|
void *handle, *func;
|
|
|
|
|
|
|
|
handle = dlopen(so_file, RTLD_NOW);
|
|
|
|
if (handle == NULL) {
|
2018-08-11 17:05:34 +03:00
|
|
|
mjit_warning("failure in loading code from '%s': %s", so_file, dlerror());
|
2018-08-11 10:57:58 +03:00
|
|
|
return (void *)NOT_ADDED_JIT_ISEQ_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
func = dlsym(handle, funcname);
|
|
|
|
unit->handle = handle;
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef __clang__
|
|
|
|
static const char *
|
|
|
|
header_name_end(const char *s)
|
|
|
|
{
|
|
|
|
const char *e = s + strlen(s);
|
2019-04-14 08:26:46 +03:00
|
|
|
# ifdef __GNUC__ // don't chomp .pch for mswin
|
2018-08-11 10:57:58 +03:00
|
|
|
static const char suffix[] = ".gch";
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// chomp .gch suffix
|
2018-08-11 10:57:58 +03:00
|
|
|
if (e > s+sizeof(suffix)-1 && strcmp(e-sizeof(suffix)+1, suffix) == 0) {
|
|
|
|
e -= sizeof(suffix)-1;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Print platform-specific prerequisites in generated code.
|
2018-08-11 10:57:58 +03:00
|
|
|
static void
|
|
|
|
compile_prelude(FILE *f)
|
|
|
|
{
|
2019-04-14 08:26:46 +03:00
|
|
|
#ifndef __clang__ // -include-pch is used for Clang
|
2018-08-11 12:48:07 +03:00
|
|
|
const char *s = pch_file;
|
2018-08-11 10:57:58 +03:00
|
|
|
const char *e = header_name_end(s);
|
|
|
|
|
|
|
|
fprintf(f, "#include \"");
|
2019-04-14 08:26:46 +03:00
|
|
|
// print pch_file except .gch for gcc, but keep .pch for mswin
|
2018-08-11 10:57:58 +03:00
|
|
|
for (; s < e; s++) {
|
|
|
|
switch(*s) {
|
|
|
|
case '\\': case '"':
|
|
|
|
fputc('\\', f);
|
|
|
|
}
|
|
|
|
fputc(*s, f);
|
|
|
|
}
|
|
|
|
fprintf(f, "\"\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
fprintf(f, "void _pei386_runtime_relocator(void){}\n");
|
|
|
|
fprintf(f, "int __stdcall DllMainCRTStartup(void* hinstDLL, unsigned int fdwReason, void* lpvReserved) { return 1; }\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Compile ISeq in UNIT and return function pointer of JIT-ed code.
|
|
|
|
// It may return NOT_COMPILED_JIT_ISEQ_FUNC if something went wrong.
|
2018-08-11 10:57:58 +03:00
|
|
|
static mjit_func_t
|
2019-03-18 19:32:48 +03:00
|
|
|
convert_unit_to_func(struct rb_mjit_unit *unit)
|
2018-08-11 10:57:58 +03:00
|
|
|
{
|
2019-04-14 08:26:46 +03:00
|
|
|
char c_file_buff[MAXPATHLEN], *c_file = c_file_buff, *so_file, funcname[35]; // TODO: reconsider `35`
|
2018-08-11 10:57:58 +03:00
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
void *func;
|
|
|
|
double start_time, end_time;
|
|
|
|
int c_file_len = (int)sizeof(c_file_buff);
|
|
|
|
static const char c_ext[] = ".c";
|
|
|
|
static const char so_ext[] = DLEXT;
|
|
|
|
const int access_mode =
|
|
|
|
#ifdef O_BINARY
|
|
|
|
O_BINARY|
|
|
|
|
#endif
|
|
|
|
O_WRONLY|O_EXCL|O_CREAT;
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
static const char o_ext[] = ".o";
|
|
|
|
char *o_file;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
c_file_len = sprint_uniq_filename(c_file_buff, c_file_len, unit->id, MJIT_TMP_PREFIX, c_ext);
|
|
|
|
if (c_file_len >= (int)sizeof(c_file_buff)) {
|
|
|
|
++c_file_len;
|
|
|
|
c_file = alloca(c_file_len);
|
|
|
|
c_file_len = sprint_uniq_filename(c_file, c_file_len, unit->id, MJIT_TMP_PREFIX, c_ext);
|
|
|
|
}
|
|
|
|
++c_file_len;
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
o_file = alloca(c_file_len - sizeof(c_ext) + sizeof(o_ext));
|
|
|
|
memcpy(o_file, c_file, c_file_len - sizeof(c_ext));
|
|
|
|
memcpy(&o_file[c_file_len - sizeof(c_ext)], o_ext, sizeof(o_ext));
|
|
|
|
#endif
|
|
|
|
so_file = alloca(c_file_len - sizeof(c_ext) + sizeof(so_ext));
|
|
|
|
memcpy(so_file, c_file, c_file_len - sizeof(c_ext));
|
|
|
|
memcpy(&so_file[c_file_len - sizeof(c_ext)], so_ext, sizeof(so_ext));
|
|
|
|
|
|
|
|
sprintf(funcname, "_mjit%d", unit->id);
|
|
|
|
|
|
|
|
fd = rb_cloexec_open(c_file, access_mode, 0600);
|
|
|
|
if (fd < 0 || (f = fdopen(fd, "w")) == NULL) {
|
|
|
|
int e = errno;
|
|
|
|
if (fd >= 0) (void)close(fd);
|
|
|
|
verbose(1, "Failed to fopen '%s', giving up JIT for it (%s)", c_file, strerror(e));
|
|
|
|
return (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// print #include of MJIT header, etc.
|
2018-08-11 10:57:58 +03:00
|
|
|
compile_prelude(f);
|
|
|
|
|
2019-05-16 09:13:36 +03:00
|
|
|
// wait until mjit_gc_exit_hook is called
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "before mjit_compile to wait GC finish");
|
2018-08-11 13:05:56 +03:00
|
|
|
while (in_gc) {
|
2018-08-11 10:57:58 +03:00
|
|
|
verbose(3, "Waiting wakeup from GC");
|
|
|
|
rb_native_cond_wait(&mjit_gc_wakeup, &mjit_engine_mutex);
|
|
|
|
}
|
2018-11-01 02:55:22 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// We need to check again here because we could've waited on GC above
|
2018-11-01 02:55:22 +03:00
|
|
|
if (unit->iseq == NULL) {
|
|
|
|
fclose(f);
|
|
|
|
if (!mjit_opts.save_temps)
|
|
|
|
remove_file(c_file);
|
2019-01-10 17:31:18 +03:00
|
|
|
in_jit = false; // just being explicit for return
|
2018-11-01 02:55:22 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-01-10 17:31:18 +03:00
|
|
|
in_jit = true;
|
2018-11-01 02:55:22 +03:00
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "before mjit_compile to wait GC finish");
|
2018-11-02 14:40:43 +03:00
|
|
|
if (!in_jit) {
|
2018-11-01 02:55:22 +03:00
|
|
|
return (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
|
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-05-29 22:12:10 +03:00
|
|
|
// To make MJIT worker thread-safe against GC.compact, copy ISeq values while `in_jit` is true.
|
|
|
|
long iseq_lineno = 0;
|
2019-05-29 10:47:57 +03:00
|
|
|
if (FIXNUM_P(unit->iseq->body->location.first_lineno))
|
2019-05-29 22:12:10 +03:00
|
|
|
// FIX2INT may fallback to rb_num2long(), which is a method call and dangerous in MJIT worker. So using only FIX2LONG.
|
|
|
|
iseq_lineno = FIX2LONG(unit->iseq->body->location.first_lineno);
|
2019-07-14 15:48:22 +03:00
|
|
|
char *iseq_label = alloca(RSTRING_LEN(unit->iseq->body->location.label) + 1);
|
|
|
|
char *iseq_path = alloca(RSTRING_LEN(rb_iseq_path(unit->iseq)) + 1);
|
2019-05-29 22:12:10 +03:00
|
|
|
strcpy(iseq_label, RSTRING_PTR(unit->iseq->body->location.label));
|
|
|
|
strcpy(iseq_path, RSTRING_PTR(rb_iseq_path(unit->iseq)));
|
|
|
|
|
|
|
|
verbose(2, "start compilation: %s@%s:%ld -> %s", iseq_label, iseq_path, iseq_lineno, c_file);
|
|
|
|
fprintf(f, "/* %s@%s:%ld */\n\n", iseq_label, iseq_path, iseq_lineno);
|
2019-03-18 19:32:48 +03:00
|
|
|
bool success = mjit_compile(f, unit->iseq, funcname);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// release blocking mjit_gc_start_hook
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "after mjit_compile to wakeup client for GC");
|
2019-01-10 17:31:18 +03:00
|
|
|
in_jit = false;
|
2018-08-11 10:57:58 +03:00
|
|
|
verbose(3, "Sending wakeup signal to client in a mjit-worker for GC");
|
|
|
|
rb_native_cond_signal(&mjit_client_wakeup);
|
|
|
|
CRITICAL_SECTION_FINISH(3, "in worker to wakeup client for GC");
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
if (!success) {
|
|
|
|
if (!mjit_opts.save_temps)
|
|
|
|
remove_file(c_file);
|
2019-05-29 22:12:10 +03:00
|
|
|
verbose(1, "JIT failure: %s@%s:%ld -> %s", iseq_label, iseq_path, iseq_lineno, c_file);
|
2018-08-11 10:57:58 +03:00
|
|
|
return (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_time = real_ms_time();
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
success = compile_c_to_so(c_file, so_file);
|
|
|
|
#else
|
2019-04-14 08:26:46 +03:00
|
|
|
// splitting .c -> .o step and .o -> .so step, to cache .o files in the future
|
2019-01-10 17:31:18 +03:00
|
|
|
if ((success = compile_c_to_o(c_file, o_file)) != false) {
|
2019-07-14 16:12:33 +03:00
|
|
|
success = link_o_to_so((const char *[]){ o_file, NULL }, so_file);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Always set o_file for compaction. The value is also used for lazy deletion.
|
2018-08-11 10:57:58 +03:00
|
|
|
unit->o_file = strdup(o_file);
|
2018-08-11 17:18:55 +03:00
|
|
|
if (unit->o_file == NULL) {
|
|
|
|
mjit_warning("failed to allocate memory to remember '%s' (%s), removing it...", o_file, strerror(errno));
|
|
|
|
remove_file(o_file);
|
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
end_time = real_ms_time();
|
|
|
|
|
|
|
|
if (!mjit_opts.save_temps)
|
|
|
|
remove_file(c_file);
|
|
|
|
if (!success) {
|
|
|
|
verbose(2, "Failed to generate so: %s", so_file);
|
|
|
|
return (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
func = load_func_from_so(so_file, funcname, unit);
|
2018-08-12 18:00:03 +03:00
|
|
|
if (!mjit_opts.save_temps)
|
|
|
|
remove_so_file(so_file, unit);
|
2018-08-11 10:57:58 +03:00
|
|
|
|
|
|
|
if ((uintptr_t)func > (uintptr_t)LAST_JIT_ISEQ_FUNC) {
|
2019-05-29 22:12:10 +03:00
|
|
|
verbose(1, "JIT success (%.1fms): %s@%s:%ld -> %s",
|
|
|
|
end_time - start_time, iseq_label, iseq_path, iseq_lineno, c_file);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
return (mjit_func_t)func;
|
|
|
|
}
|
|
|
|
|
2018-11-27 15:00:51 +03:00
|
|
|
typedef struct {
|
2019-03-17 20:12:47 +03:00
|
|
|
const rb_iseq_t *iseq;
|
2018-10-21 17:23:24 +03:00
|
|
|
struct rb_call_cache *cc_entries;
|
2018-10-21 16:21:28 +03:00
|
|
|
union iseq_inline_storage_entry *is_entries;
|
2019-01-10 17:31:18 +03:00
|
|
|
bool finish_p;
|
2018-11-27 15:00:51 +03:00
|
|
|
} mjit_copy_job_t;
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// Singleton MJIT copy job. This is made global since it needs to be durable even when MJIT worker thread is stopped.
|
|
|
|
// (ex: register job -> MJIT pause -> MJIT resume -> dispatch job. Actually this should be just cancelled by finish_p check)
|
2019-03-17 20:12:47 +03:00
|
|
|
static mjit_copy_job_t mjit_copy_job = { .iseq = NULL, .finish_p = true };
|
2018-10-21 16:21:28 +03:00
|
|
|
|
|
|
|
static void mjit_copy_job_handler(void *data);
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// vm_trace.c
|
2018-11-30 06:56:29 +03:00
|
|
|
int rb_workqueue_register(unsigned flags, rb_postponed_job_func_t , void *);
|
|
|
|
|
2019-03-18 20:20:21 +03:00
|
|
|
// Copy inline cache values of `iseq` to `cc_entries` and `is_entries`.
|
|
|
|
// These buffers should be pre-allocated properly prior to calling this function.
|
2019-03-18 19:32:48 +03:00
|
|
|
// Return true if copy succeeds or is not needed.
|
|
|
|
//
|
|
|
|
// We're lazily copying cache values from main thread because these cache values
|
|
|
|
// could be different between ones on enqueue timing and ones on dequeue timing.
|
|
|
|
bool
|
2019-03-18 20:20:21 +03:00
|
|
|
mjit_copy_cache_from_main_thread(const rb_iseq_t *iseq, struct rb_call_cache *cc_entries, union iseq_inline_storage_entry *is_entries)
|
2018-10-21 17:23:24 +03:00
|
|
|
{
|
2019-03-18 19:32:48 +03:00
|
|
|
mjit_copy_job_t *job = &mjit_copy_job; // just a short hand
|
|
|
|
|
|
|
|
CRITICAL_SECTION_START(3, "in mjit_copy_cache_from_main_thread");
|
|
|
|
job->finish_p = true; // disable dispatching this job in mjit_copy_job_handler while it's being modified
|
|
|
|
CRITICAL_SECTION_FINISH(3, "in mjit_copy_cache_from_main_thread");
|
|
|
|
|
2019-03-18 20:20:21 +03:00
|
|
|
job->cc_entries = cc_entries;
|
|
|
|
job->is_entries = is_entries;
|
2019-03-18 19:32:48 +03:00
|
|
|
|
|
|
|
CRITICAL_SECTION_START(3, "in mjit_copy_cache_from_main_thread");
|
|
|
|
job->iseq = iseq; // Prevernt GC of this ISeq from here
|
2019-03-18 20:20:21 +03:00
|
|
|
VM_ASSERT(in_jit);
|
|
|
|
in_jit = false; // To avoid deadlock, allow running GC while waiting for copy job
|
|
|
|
rb_native_cond_signal(&mjit_client_wakeup); // Unblock main thread waiting in `mjit_gc_start_hook`
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
job->finish_p = false; // allow dispatching this job in mjit_copy_job_handler
|
2019-03-18 19:32:48 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in mjit_copy_cache_from_main_thread");
|
2018-10-21 17:23:24 +03:00
|
|
|
|
2018-11-06 10:22:25 +03:00
|
|
|
if (UNLIKELY(mjit_opts.wait)) {
|
|
|
|
mjit_copy_job_handler((void *)job);
|
2019-04-10 15:43:33 +03:00
|
|
|
}
|
|
|
|
else if (rb_workqueue_register(0, mjit_copy_job_handler, (void *)job)) {
|
2019-03-18 19:32:48 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in MJIT copy job wait");
|
|
|
|
// checking `stop_worker_p` too because `RUBY_VM_CHECK_INTS(ec)` may not
|
|
|
|
// lush mjit_copy_job_handler when EC_EXEC_TAG() is not TAG_NONE, and then
|
|
|
|
// `stop_worker()` could dead lock with this function.
|
|
|
|
while (!job->finish_p && !stop_worker_p) {
|
|
|
|
rb_native_cond_wait(&mjit_worker_wakeup, &mjit_engine_mutex);
|
|
|
|
verbose(3, "Getting wakeup from client");
|
|
|
|
}
|
|
|
|
CRITICAL_SECTION_FINISH(3, "in MJIT copy job wait");
|
2019-03-18 04:52:45 +03:00
|
|
|
}
|
2019-03-18 17:01:52 +03:00
|
|
|
|
2019-03-18 19:32:48 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in mjit_copy_cache_from_main_thread");
|
2019-04-14 15:40:44 +03:00
|
|
|
bool success_p = job->finish_p;
|
2019-03-18 19:32:48 +03:00
|
|
|
// Disable dispatching this job in mjit_copy_job_handler while memory allocated by alloca
|
|
|
|
// could be expired after finishing this function.
|
|
|
|
job->finish_p = true;
|
2019-03-18 20:20:21 +03:00
|
|
|
|
|
|
|
in_jit = true; // Prohibit GC during JIT compilation
|
2019-04-14 15:40:44 +03:00
|
|
|
if (job->iseq == NULL) // ISeq GC is notified in mjit_mark_iseq
|
|
|
|
success_p = false;
|
2019-03-18 20:20:21 +03:00
|
|
|
job->iseq = NULL; // Allow future GC of this ISeq from here
|
2019-03-18 19:32:48 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in mjit_copy_cache_from_main_thread");
|
2019-04-14 15:40:44 +03:00
|
|
|
return success_p;
|
2018-10-21 17:23:24 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// The function implementing a worker. It is executed in a separate
|
|
|
|
// thread by rb_thread_create_mjit_thread. It compiles precompiled header
|
|
|
|
// and then compiles requested ISeqs.
|
2018-08-11 10:57:58 +03:00
|
|
|
void
|
|
|
|
mjit_worker(void)
|
|
|
|
{
|
|
|
|
#ifndef _MSC_VER
|
2018-08-11 13:05:56 +03:00
|
|
|
if (pch_status == PCH_NOT_READY) {
|
2018-08-11 10:57:58 +03:00
|
|
|
make_pch();
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-11 13:05:56 +03:00
|
|
|
if (pch_status == PCH_FAILED) {
|
2019-01-10 17:31:18 +03:00
|
|
|
mjit_enabled = false;
|
2018-08-11 13:05:56 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in worker to update worker_stopped");
|
2019-01-10 17:31:18 +03:00
|
|
|
worker_stopped = true;
|
2018-08-11 10:57:58 +03:00
|
|
|
verbose(3, "Sending wakeup signal to client in a mjit-worker");
|
|
|
|
rb_native_cond_signal(&mjit_client_wakeup);
|
2018-08-11 13:05:56 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in worker to update worker_stopped");
|
2019-04-14 08:26:46 +03:00
|
|
|
return; // TODO: do the same thing in the latter half of mjit_finish
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// main worker loop
|
2018-08-11 13:05:56 +03:00
|
|
|
while (!stop_worker_p) {
|
2018-11-01 02:55:22 +03:00
|
|
|
struct rb_mjit_unit *unit;
|
2018-08-11 10:57:58 +03:00
|
|
|
|
2019-04-14 08:26:46 +03:00
|
|
|
// wait until unit is available
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in worker dequeue");
|
2018-11-01 02:55:22 +03:00
|
|
|
while ((list_empty(&unit_queue.head) || active_units.length >= mjit_opts.max_cache_size) && !stop_worker_p) {
|
2018-08-11 10:57:58 +03:00
|
|
|
rb_native_cond_wait(&mjit_worker_wakeup, &mjit_engine_mutex);
|
|
|
|
verbose(3, "Getting wakeup from client");
|
|
|
|
}
|
2018-11-01 02:55:22 +03:00
|
|
|
unit = get_from_list(&unit_queue);
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in worker dequeue");
|
|
|
|
|
2018-11-01 02:55:22 +03:00
|
|
|
if (unit) {
|
2019-03-18 04:52:45 +03:00
|
|
|
// JIT compile
|
2019-03-18 19:32:48 +03:00
|
|
|
mjit_func_t func = convert_unit_to_func(unit);
|
2019-03-30 02:26:31 +03:00
|
|
|
(void)RB_DEBUG_COUNTER_INC_IF(mjit_compile_failures, func == (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC);
|
2019-03-18 19:32:48 +03:00
|
|
|
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_START(3, "in jit func replace");
|
2019-04-14 08:26:46 +03:00
|
|
|
while (in_gc) { // Make sure we're not GC-ing when touching ISeq
|
2018-11-20 14:18:59 +03:00
|
|
|
verbose(3, "Waiting wakeup from GC");
|
|
|
|
rb_native_cond_wait(&mjit_gc_wakeup, &mjit_engine_mutex);
|
|
|
|
}
|
2019-04-14 08:26:46 +03:00
|
|
|
if (unit->iseq) { // Check whether GCed or not
|
2019-10-14 05:29:46 +03:00
|
|
|
if ((uintptr_t)func > (uintptr_t)LAST_JIT_ISEQ_FUNC) {
|
|
|
|
add_to_list(unit, &active_units);
|
|
|
|
}
|
2019-04-14 08:26:46 +03:00
|
|
|
// Usage of jit_code might be not in a critical section.
|
2018-11-01 02:55:22 +03:00
|
|
|
MJIT_ATOMIC_SET(unit->iseq->body->jit_func, func);
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|
2019-10-14 00:11:02 +03:00
|
|
|
else {
|
|
|
|
free_unit(unit);
|
|
|
|
}
|
2018-08-11 10:57:58 +03:00
|
|
|
CRITICAL_SECTION_FINISH(3, "in jit func replace");
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2019-04-14 08:26:46 +03:00
|
|
|
// Combine .o files to one .so and reload all jit_func to improve memory locality
|
2018-08-11 12:48:07 +03:00
|
|
|
if ((!mjit_opts.wait && unit_queue.length == 0 && active_units.length > 1)
|
|
|
|
|| active_units.length == mjit_opts.max_cache_size) {
|
2018-08-11 10:57:58 +03:00
|
|
|
compact_all_jit_code();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 17:31:18 +03:00
|
|
|
// To keep mutex unlocked when it is destroyed by mjit_finish, don't wrap CRITICAL_SECTION here.
|
|
|
|
worker_stopped = true;
|
2018-08-11 10:57:58 +03:00
|
|
|
}
|