[前][次][番号順一覧][スレッド一覧]

ruby-changes:52078

From: k0kubun <ko1@a...>
Date: Sat, 11 Aug 2018 16:58:05 +0900 (JST)
Subject: [ruby-changes:52078] k0kubun:r64285 (trunk): mjit_worker.c: carve out worker-related code

k0kubun	2018-08-11 16:57:58 +0900 (Sat, 11 Aug 2018)

  New Revision: 64285

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64285

  Log:
    mjit_worker.c: carve out worker-related code
    
    The motivation of this change is to make sure rb_funcall or GC-related
    functions are not called on worker-related code. Currently such
    functions are used in some places and I believe it's partly because it's
    hard to identify which part is called on MJIT worker thread.
    
    Now, mjit.c is safe to use them but we know we need to safely deal with
    mjit_compile.c, mjit_worker.c and mjit_internal.h.
    
    mjit_compile.c: update the comment about it

  Added files:
    trunk/mjit_internal.h
    trunk/mjit_worker.c
  Modified files:
    trunk/common.mk
    trunk/mjit.c
    trunk/mjit.h
    trunk/mjit_compile.c
Index: mjit_internal.h
===================================================================
--- mjit_internal.h	(nonexistent)
+++ mjit_internal.h	(revision 64285)
@@ -0,0 +1,264 @@ https://github.com/ruby/ruby/blob/trunk/mjit_internal.h#L1
+/**********************************************************************
+
+  mjit_internal.h - Utility functions shared by mjit*.c
+
+  Copyright (C) 2018 Takashi Kokubun <takashikkbn@g...>.
+
+**********************************************************************/
+
+/* NOTE: All functions in this file can be executed on MJIT worker. So don't
+   call Ruby methods (C functions that may call rb_funcall) or trigger
+   GC (using xmalloc, ZALLOC, etc.) in this file. */
+
+#ifndef RUBY_MJIT_INTERNAL_H
+#define RUBY_MJIT_INTERNAL_H 1
+
+#include "mjit.h"
+
+#ifndef MAXPATHLEN
+#  define MAXPATHLEN 1024
+#endif
+
+#define RB_CONDATTR_CLOCK_MONOTONIC 1
+
+#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)))
+#define dlclose(handle) (FreeLibrary(handle))
+#define RTLD_NOW  -1
+
+#define waitpid(pid,stat_loc,options) (WaitForSingleObject((HANDLE)(pid), INFINITE), GetExitCodeProcess((HANDLE)(pid), (LPDWORD)(stat_loc)), (pid))
+#define WIFEXITED(S) ((S) != STILL_ACTIVE)
+#define WEXITSTATUS(S) (S)
+#define WIFSIGNALED(S) (0)
+typedef intptr_t pid_t;
+#endif
+
+#define MJIT_TMP_PREFIX "_ruby_mjit_"
+
+/* The unit structure that holds metadata of ISeq for MJIT.  */
+struct rb_mjit_unit {
+    /* Unique order number of unit.  */
+    int id;
+    /* Dlopen handle of the loaded object file.  */
+    void *handle;
+    const rb_iseq_t *iseq;
+#ifndef _MSC_VER
+    /* This value is always set for `compact_all_jit_code`. Also used for lazy deletion. */
+    char *o_file;
+#endif
+#ifdef _WIN32
+    /* DLL cannot be removed while loaded on Windows. If this is set, it'll be lazily deleted. */
+    char *so_file;
+#endif
+    /* Only used by unload_units. Flag to check this unit is currently on stack or not. */
+    char used_code_p;
+};
+
+/* Node of linked list in struct rb_mjit_unit_list.
+   TODO: use ccan/list for this */
+struct rb_mjit_unit_node {
+    struct rb_mjit_unit *unit;
+    struct rb_mjit_unit_node *next, *prev;
+};
+
+/* Linked list of struct rb_mjit_unit.  */
+struct rb_mjit_unit_list {
+    struct rb_mjit_unit_node *head;
+    int length; /* the list length */
+};
+
+enum pch_status_t {PCH_NOT_READY, PCH_FAILED, PCH_SUCCESS};
+
+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);
+
+extern char *mjit_tmp_dir;
+
+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", mjit_tmp_dir, prefix, getpid(), id, suffix);
+}
+
+/* Print the arguments according to FORMAT to stderr only if MJIT
+   verbose option value is more or equal to LEVEL.  */
+PRINTF_ARGS(static void, 2, 3)
+verbose(int level, const char *format, ...)
+{
+    va_list args;
+
+    va_start(args, format);
+    if (mjit_opts.verbose >= level)
+        vfprintf(stderr, format, args);
+    va_end(args);
+    if (mjit_opts.verbose >= level)
+        fprintf(stderr, "\n");
+}
+
+extern rb_nativethread_lock_t mjit_engine_mutex;
+
+/* Start a critical section.  Use message MSG to print debug info at
+   LEVEL.  */
+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);
+}
+
+/* Finish the current critical section.  Use message MSG to print
+   debug info at LEVEL. */
+static inline void
+CRITICAL_SECTION_FINISH(int level, const char *msg)
+{
+    verbose(level, "Unlocked %s", msg);
+    rb_native_mutex_unlock(&mjit_engine_mutex);
+}
+
+/* Allocate struct rb_mjit_unit_node and return it. This MUST NOT be
+   called inside critical section because that causes deadlock. ZALLOC
+   may fire GC and GC hooks mjit_gc_start_hook that starts critical section. */
+static struct rb_mjit_unit_node *
+create_list_node(struct rb_mjit_unit *unit)
+{
+    struct rb_mjit_unit_node *node = ZALLOC(struct rb_mjit_unit_node);
+    node->unit = unit;
+    return node;
+}
+
+/* Add unit node to the tail of doubly linked LIST.  It should be not in
+   the list before.  */
+static void
+add_to_list(struct rb_mjit_unit_node *node, struct rb_mjit_unit_list *list)
+{
+    /* Append iseq to list */
+    if (list->head == NULL) {
+        list->head = node;
+    }
+    else {
+        struct rb_mjit_unit_node *tail = list->head;
+        while (tail->next != NULL) {
+            tail = tail->next;
+        }
+        tail->next = node;
+        node->prev = tail;
+    }
+    list->length++;
+}
+
+static void
+remove_from_list(struct rb_mjit_unit_node *node, struct rb_mjit_unit_list *list)
+{
+    if (node->prev && node->next) {
+        node->prev->next = node->next;
+        node->next->prev = node->prev;
+    }
+    else if (node->prev == NULL && node->next) {
+        list->head = node->next;
+        node->next->prev = NULL;
+    }
+    else if (node->prev && node->next == NULL) {
+        node->prev->next = NULL;
+    }
+    else {
+        list->head = NULL;
+    }
+    list->length--;
+    xfree(node);
+}
+
+static void
+remove_file(const char *filename)
+{
+    if (remove(filename) && (mjit_opts.warnings || mjit_opts.verbose)) {
+        fprintf(stderr, "MJIT warning: failed to remove \"%s\": %s\n",
+                filename, strerror(errno));
+    }
+}
+
+/* Lazily delete .o and/or .so files. */
+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;
+        /* For compaction, unit->o_file is always set when compilation succeeds.
+           So save_temps needs to be checked here. */
+        if (!mjit_opts.save_temps)
+            remove_file(o_file);
+        free(o_file);
+    }
+#endif
+
+#ifdef _WIN32
+    if (unit->so_file) {
+        char *so_file = unit->so_file;
+
+        unit->so_file = NULL;
+        /* unit->so_file is set only when mjit_opts.save_temps is FALSE. */
+        remove_file(so_file);
+        free(so_file);
+    }
+#endif
+}
+
+/* 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 continously compiled.  */
+static void
+free_unit(struct rb_mjit_unit *unit)
+{
+    if (unit->iseq) { /* ISeq is not GCed */
+        unit->iseq->body->jit_func = (mjit_func_t)NOT_COMPILED_JIT_ISEQ_FUNC;
+        unit->iseq->body->jit_unit = NULL;
+    }
+    if (unit->handle) /* handle is NULL if it's in queue */
+        dlclose(unit->handle);
+    clean_object_files(unit);
+    xfree(unit);
+}
+
+#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))
+
+#include "mjit_config.h"
+
+#if defined(__GNUC__) && \
+     (!defined(__clang__) || \
+      (defined(__clang__) && (defined(__FreeBSD__) || defined(__GLIBC__))))
+#define GCC_PIC_FLAGS "-Wfatal-errors", "-fPIC", "-shared", "-w", \
+    "-pipe",
+#else
+#define GCC_PIC_FLAGS /* empty */
+#endif
+
+static const char *const CC_COMMON_ARGS[] = {
+    MJIT_CC_COMMON MJIT_CFLAGS GCC_PIC_FLAGS
+    NULL
+};
+
+/* GCC and CLANG executable paths.  TODO: The paths should absolute
+   ones to prevent changing C compiler for security reasons.  */
+#define CC_PATH CC_COMMON_ARGS[0]
+
+#endif /* RUBY_MJIT_INTERNAL_H */
Index: mjit_worker.c
===================================================================
--- mjit_worker.c	(nonexistent)
+++ mjit_worker.c	(revision 64285)
@@ -0,0 +1,896 @@ https://github.com/ruby/ruby/blob/trunk/mjit_worker.c#L1
+/**********************************************************************
+
+  mjit_worker.c - Worker for MRI method JIT compiler
+
+  Copyright (C) 2017 Vladimir Makarov <vmakarov@r...>.
+
+**********************************************************************/
+
+/* 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 xmalloc, ZALLOC, etc.) in this file. */
+
+/* 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 "internal.h"
+#include "vm_core.h"
+#include "mjit.h"
+#include "gc.h"
+#include "constant.h"
+#include "id_table.h"
+#include "ruby_assert.h"
+#include "ruby/thread.h"
+#include "ruby/util.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"
+#include "mjit_internal.h"
+
+/* process.c */
+rb_pid_t ruby_waitpid_locked(rb_vm_t *, rb_pid_t, int *status, int options,
+                          rb_nativethread_cond_t *cond);
+
+/* Atomically set function pointer if possible. */
+#define MJIT_ATOMIC_SET(var, val) (void)ATOMIC_PTR_EXCHANGE(var, val)
+
+extern struct mjit_options mjit_opts;
+extern int mjit_enabled;
+
+extern struct rb_mjit_unit_list mjit_unit_queue;
+extern struct rb_mjit_unit_list mjit_active_units;
+extern struct rb_mjit_unit_list mjit_compact_units;
+extern int mjit_current_unit_num;
+extern rb_nativethread_cond_t mjit_pch_wakeup;
+extern rb_nativethread_cond_t mjit_client_wakeup;
+extern rb_nativethread_cond_t mjit_worker_wakeup;
+extern rb_nativethread_cond_t mjit_gc_wakeup;
+
+extern int mjit_in_gc;
+extern int mjit_in_jit;
+
+/* --- Defined in the client thread before starting MJIT threads: ---  */
+/* Used C compiler path.  */
+const char *mjit_cc_path;
+/* Name of the precompiled header file.  */
+char *mjit_pch_file;
+
+#ifndef _MSC_VER
+/* Name of the header file.  */
+char *mjit_header_file;
+#endif
+
+#ifdef _WIN32
+/* Linker option to enable libruby. */
+char *mjit_libruby_pathflag;
+#endif
+
+/* Return time in milliseconds as a double.  */
+#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
+
+static const char *const CC_DEBUG_ARGS[] = {MJIT_DEBUGFLAGS NULL};
+static const char *const CC_OPTIMIZE_ARGS[] = {MJIT_OPTFLAGS NULL};
+
+static const char *const CC_LDSHARED_ARGS[] = {MJIT_LDSHARED GCC_PIC_FLAGS NULL};
+static const char *const CC_DLDFLAGS_ARGS[] = {
+    MJIT_DLDFLAGS
+#if defined __GNUC__ && !defined __clang__
+    "-nostartfiles",
+# if !defined(_WIN32) && !defined(__CYGWIN__)
+    "-nodefaultlibs", "-nostdlib",
+# endif
+#endif
+    NULL
+};
+
+static const char *const CC_LIBS[] = {
+#if defined(_WIN32) || defined(__CYGWIN__)
+    MJIT_LIBS
+# if defined __GNUC__ && !defined __clang__
+#  if defined(_WIN32)
+    "-lmsvcrt",
+#  endif
+    "-lgcc",
+# endif
+#endif
+    NULL
+};
+
+#define CC_CODEFLAG_ARGS (mjit_opts.debug ? CC_DEBUG_ARGS : CC_OPTIMIZE_ARGS)
+
+/* Status of the precompiled header creation.  The status is
+   shared by the workers and the pch thread.  */
+enum pch_status_t pch_status;
+
+/* 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.  */
+static struct rb_mjit_unit_node *
+get_from_list(struct rb_mjit_unit_list *list)
+{
+    struct rb_mjit_unit_node *node, *best = NULL;
+
+    if (list->head == NULL)
+        return NULL;
+
+    /* Find iseq with max total_calls */
+    for (node = list->head; node != NULL; node = node ? node->next : NULL) {
+        if (node->unit->iseq == NULL) { /* ISeq is GCed. */
+            free_unit(node->unit);
+            remove_from_list(node, list);
+            continue;
+        }
+
+        if (best == NULL || best->unit->iseq->body->total_calls < node->unit->iseq->body->total_calls) {
+            best = node;
+        }
+    }
+
+    return best;
+}
+
+/* Return length of NULL-terminated array ARGS excluding the NULL
+   marker.  */
+static size_t
+args_len(char *const *args)
+{
+    size_t i;
+
+    for (i = 0; (args[i]) != NULL;i++)
+        ;
+    return i;
+}
+
+/* Concatenate NUM passed NULL-terminated arrays of strings, put the
+   result (with NULL end marker) into the heap, and return the
+   result.  */
+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);
+            return NULL;
+        }
+        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
+/* Start an OS process of executable PATH with arguments ARGV.  Return
+   PID of the process.
+   TODO: Use the same function in process.c */
+static pid_t
+start_process(const char *path, char *const *argv)
+{
+    pid_t pid;
+
+    if (mjit_opts.verbose >= 2) {
+        int i;
+        const char *arg;
+
+        fprintf(stderr, "Starting process: %s", path);
+        for (i = 0; (arg = argv[i]) != NULL; i++)
+            fprintf(stderr, " %s", arg);
+        fprintf(stderr, "\n");
+    }
+#ifdef _WIN32
+    pid = spawnvp(_P_NOWAIT, path, argv);
+#else
+    {
+        /*
+         * Not calling non-async-signal-safe functions between vfork
+         * and execv for safety
+         */
+        char fbuf[MAXPATHLEN];
+        const char *abspath = dln_find_exe_r(path, 0, fbuf, sizeof(fbuf));
+        int dev_null;
+
+        if (!abspath) {
+            verbose(1, "MJIT: failed to find `%s' in PATH\n", path);
+            return -1;
+        }
+        dev_null = rb_cloexec_open(ruby_null_device, O_WRONLY, 0);
+
+        if ((pid = vfork()) == 0) {
+            umask(0077);
+            if (mjit_opts.verbose == 0) {
+                /* 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.  */
+                dup2(dev_null, STDERR_FILENO);
+                dup2(dev_null, STDOUT_FILENO);
+            }
+            (void)close(dev_null);
+            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.  */
+            verbose(1, "MJIT: Error in execv: %s\n", abspath);
+            _exit(1);
+        }
+        (void)close(dev_null);
+    }
+#endif
+    return pid;
+}
+COMPILER_WARNING_POP
+
+/* 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 */
+static int
+exec_process(const char *path, char *const argv[])
+{
+    int stat, exit_code = -2;
+    pid_t pid;
+    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);
+    }
+
+    pid = start_process(path, argv);
+    for (;pid > 0;) {
+        pid_t r = vm ? ruby_waitpid_locked(vm, pid, &stat, 0, &cond)
+                     : waitpid(pid, &stat, 0);
+      (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]