2007-08-20 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / mini / driver.c
index 09ff7c97973d7282afbe64db40aa76e406c8455b..a9a3d441a562d107e30dbcd031a9c2d283521311 100644 (file)
 
 #include <config.h>
 #include <signal.h>
+#if HAVE_SCHED_SETAFFINITY
+#include <sched.h>
+#endif
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/loader.h>
@@ -37,6 +42,7 @@
 #include <mono/metadata/verify.h>
 #include <mono/metadata/mono-debug.h>
 #include <mono/metadata/security-manager.h>
+#include <mono/metadata/security-core-clr.h>
 #include <mono/os/gc_wrapper.h>
 #include "mono/utils/mono-counters.h"
 
 #include <ctype.h>
 #include "inssel.h"
 #include <locale.h>
+#include "version.h"
 
 static FILE *mini_stats_fd = NULL;
 
 static void mini_usage (void);
 
-extern int mini_wapi_hps (int argc, char **argv);
-extern int mini_wapi_semdel (int argc, char **argv);
-extern int mini_wapi_seminfo (int argc, char **argv);
-
 /* This turns off command line globbing under win32 */
 #ifdef PLATFORM_WIN32
 int _CRT_glob = 0;
@@ -326,7 +329,6 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
        *total_run = 0;
        for (opt = 0; opt < G_N_ELEMENTS (opt_sets); ++opt) {
                double elapsed, comp_time, start_time;
-               MonoJitInfo *jinfo;
 
                opt_flags = opt_sets [opt];
                mono_set_defaults (verbose, opt_flags);
@@ -339,8 +341,8 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                /* fixme: ugly hack - delete all previously compiled methods */
                g_hash_table_destroy (mono_domain_get ()->jit_trampoline_hash);
                mono_domain_get ()->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-               g_hash_table_destroy (mono_domain_get ()->jit_code_hash);
-               mono_domain_get ()->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+               mono_internal_hash_table_destroy (&(mono_domain_get ()->jit_code_hash));
+               mono_jit_code_hash_init (&(mono_domain_get ()->jit_code_hash));
 
                g_timer_start (timer);
                if (mini_stats_fd)
@@ -358,8 +360,8 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                                        if (verbose >= 2)
                                                g_print ("Running '%s' ...\n", method->name);
 #ifdef MONO_USE_AOT_COMPILER
-                                       if ((jinfo = mono_aot_get_method (mono_get_root_domain (), method)))
-                                               func = jinfo->code_start;
+                                       if ((func = mono_aot_get_method (mono_get_root_domain (), method)))
+                                               ;
                                        else
 #endif
                                                func = (TestMethod)(gpointer)cfg->native_code;
@@ -386,8 +388,13 @@ mini_regression (MonoImage *image, int verbose, int *total_run) {
                        fprintf (mini_stats_fd, "],\n");
                g_timer_stop (timer);
                elapsed = g_timer_elapsed (timer, NULL);
-               g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n", 
-                       run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
+               if (failed > 0 || cfailed > 0){
+                       g_print ("Results: total tests: %d, failed: %d, cfailed: %d (pass: %.2f%%)\n", 
+                                run, failed, cfailed, 100.0*(run-failed-cfailed)/run);
+               } else {
+                       g_print ("Results: total tests: %d, all pass \n",  run);
+               }
+               
                g_print ("Elapsed time: %f secs (%f, %f), Code size: %d\n\n", elapsed, 
                         elapsed - comp_time, comp_time, code_size);
                total += failed + cfailed;
@@ -419,11 +426,325 @@ mini_regression_list (int verbose, int count, char *images [])
                total += mini_regression (mono_assembly_get_image (ass), verbose, &run);
                total_run += run;
        }
-       g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n", 
-               total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
+       if (total > 0){
+               g_print ("Overall results: tests: %d, failed: %d, opt combinations: %d (pass: %.2f%%)\n", 
+                        total_run, total, (int)G_N_ELEMENTS (opt_sets), 100.0*(total_run-total)/total_run);
+       } else {
+               g_print ("Overall results: tests: %d, 100%% pass, opt combinations: %d\n", 
+                        total_run, (int)G_N_ELEMENTS (opt_sets));
+       }
+       
        return total;
 }
 
+#ifdef MONO_JIT_INFO_TABLE_TEST
+typedef struct _JitInfoData
+{
+       guint start;
+       guint length;
+       MonoJitInfo *ji;
+       struct _JitInfoData *next;
+} JitInfoData;
+
+typedef struct
+{
+       guint start;
+       guint length;
+       int num_datas;
+       JitInfoData *data;
+} Region;
+
+typedef struct
+{
+       int num_datas;
+       int num_regions;
+       Region *regions;
+       int num_frees;
+       JitInfoData *frees;
+} ThreadData;
+
+static int num_threads;
+static ThreadData *thread_datas;
+static MonoDomain *test_domain;
+
+static JitInfoData*
+alloc_random_data (Region *region)
+{
+       JitInfoData **data;
+       JitInfoData *prev;
+       guint prev_end;
+       guint next_start;
+       guint max_len;
+       JitInfoData *d;
+       int num_retries = 0;
+       int pos, i;
+
+ restart:
+       prev = NULL;
+       data = &region->data;
+       pos = random () % (region->num_datas + 1);
+       i = 0;
+       while (*data != NULL) {
+               if (i++ == pos)
+                       break;
+               prev = *data;
+               data = &(*data)->next;
+       }
+
+       if (prev == NULL)
+               g_assert (*data == region->data);
+       else
+               g_assert (prev->next == *data);
+
+       if (prev == NULL)
+               prev_end = region->start;
+       else
+               prev_end = prev->start + prev->length;
+
+       if (*data == NULL)
+               next_start = region->start + region->length;
+       else
+               next_start = (*data)->start;
+
+       g_assert (prev_end <= next_start);
+
+       max_len = next_start - prev_end;
+       if (max_len < 128) {
+               if (++num_retries >= 10)
+                       return NULL;
+               goto restart;
+       }
+       if (max_len > 1024)
+               max_len = 1024;
+
+       d = g_new0 (JitInfoData, 1);
+       d->start = prev_end + random () % (max_len / 2);
+       d->length = random () % MIN (max_len, next_start - d->start) + 1;
+
+       g_assert (d->start >= prev_end && d->start + d->length <= next_start);
+
+       d->ji = g_new0 (MonoJitInfo, 1);
+       d->ji->method = (MonoMethod*) 0xABadBabe;
+       d->ji->code_start = (gpointer)(gulong) d->start;
+       d->ji->code_size = d->length;
+       d->ji->cas_inited = 1;  /* marks an allocated jit info */
+
+       d->next = *data;
+       *data = d;
+
+       ++region->num_datas;
+
+       return d;
+}
+
+static JitInfoData**
+choose_random_data (Region *region)
+{
+       int n;
+       int i;
+       JitInfoData **d;
+
+       g_assert (region->num_datas > 0);
+
+       n = random () % region->num_datas;
+
+       for (d = &region->data, i = 0;
+            i < n;
+            d = &(*d)->next, ++i)
+               ;
+
+       return d;
+}
+
+static Region*
+choose_random_region (ThreadData *td)
+{
+       return &td->regions [random () % td->num_regions];
+}
+
+static ThreadData*
+choose_random_thread (void)
+{
+       return &thread_datas [random () % num_threads];
+}
+
+static void
+free_jit_info_data (ThreadData *td, JitInfoData *free)
+{
+       free->next = td->frees;
+       td->frees = free;
+
+       if (++td->num_frees >= 1000) {
+               int i;
+
+               for (i = 0; i < 500; ++i)
+                       free = free->next;
+
+               while (free->next != NULL) {
+                       JitInfoData *next = free->next->next;
+
+                       g_free (free->next->ji);
+                       g_free (free->next);
+                       free->next = next;
+
+                       --td->num_frees;
+               }
+       }
+}
+
+#define NUM_THREADS            8
+#define REGIONS_PER_THREAD     10
+#define REGION_SIZE            0x10000
+
+#define MAX_ADDR               (REGION_SIZE * REGIONS_PER_THREAD * NUM_THREADS)
+
+#define MODE_ALLOC     1
+#define MODE_FREE      2
+
+static void
+test_thread_func (ThreadData *td)
+{
+       int mode = MODE_ALLOC;
+       int i = 0;
+       gulong lookup_successes = 0, lookup_failures = 0;
+       MonoDomain *domain = test_domain;
+       int thread_num = (int)(td - thread_datas);
+       gboolean modify_thread = thread_num < NUM_THREADS / 2; /* only half of the threads modify the table */
+
+       for (;;) {
+               int alloc;
+               int lookup = 1;
+
+               if (td->num_datas == 0) {
+                       lookup = 0;
+                       alloc = 1;
+               } else if (modify_thread && random () % 1000 < 5) {
+                       lookup = 0;
+                       if (mode == MODE_ALLOC)
+                               alloc = (random () % 100) < 70;
+                       else if (mode == MODE_FREE)
+                               alloc = (random () % 100) < 30;
+               }
+
+               if (lookup) {
+                       /* modify threads sometimes look up their own jit infos */
+                       if (modify_thread && random () % 10 < 5) {
+                               Region *region = choose_random_region (td);
+
+                               if (region->num_datas > 0) {
+                                       JitInfoData **data = choose_random_data (region);
+                                       guint pos = (*data)->start + random () % (*data)->length;
+                                       MonoJitInfo *ji = mono_jit_info_table_find (domain, (char*)(gulong) pos);
+
+                                       g_assert ((*data)->ji == ji);
+                                       g_assert (ji->cas_inited);
+                               }
+                       } else {
+                               int pos = random () % MAX_ADDR;
+                               char *addr = (char*)(gulong) pos;
+                               MonoJitInfo *ji = mono_jit_info_table_find (domain, addr);
+
+                               if (ji != NULL) {
+                                       g_assert (addr >= (char*)ji->code_start && addr < (char*)ji->code_start + ji->code_size);
+                                       ++lookup_successes;
+                               } else
+                                       ++lookup_failures;
+                       }
+               } else if (alloc) {
+                       JitInfoData *data = alloc_random_data (choose_random_region (td));
+
+                       if (data != NULL) {
+                               mono_jit_info_table_add (domain, data->ji);
+
+                               ++td->num_datas;
+                       }
+               } else {
+                       Region *region = choose_random_region (td);
+
+                       if (region->num_datas > 0) {
+                               JitInfoData **data = choose_random_data (region);
+                               JitInfoData *free;
+
+                               mono_jit_info_table_remove (domain, (*data)->ji);
+
+                               (*data)->ji->cas_inited = 0; /* marks a free jit info */
+
+                               free = *data;
+                               *data = (*data)->next;
+
+                               free_jit_info_data (td, free);
+
+                               --region->num_datas;
+                               --td->num_datas;
+                       }
+               }
+
+               if (++i % 100000 == 0) {
+                       int j;
+                       g_print ("num datas %d (%ld - %ld): %d", (int)(td - thread_datas),
+                                lookup_successes, lookup_failures, td->num_datas);
+                       for (j = 0; j < td->num_regions; ++j)
+                               g_print ("  %d", td->regions [j].num_datas);
+                       g_print ("\n");
+               }
+
+               if (td->num_datas < 100)
+                       mode = MODE_ALLOC;
+               else if (td->num_datas > 2000)
+                       mode = MODE_FREE;
+       }
+}
+
+/*
+static void
+small_id_thread_func (gpointer arg)
+{
+       MonoThread *thread = mono_thread_current ();
+       MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
+
+       g_print ("my small id is %d\n", (int)thread->small_id);
+       mono_hazard_pointer_clear (hp, 1);
+       sleep (3);
+       g_print ("done %d\n", (int)thread->small_id);
+}
+*/
+
+static void
+jit_info_table_test (MonoDomain *domain)
+{
+       int i;
+
+       g_print ("testing jit_info_table\n");
+
+       num_threads = NUM_THREADS;
+       thread_datas = g_new0 (ThreadData, num_threads);
+
+       for (i = 0; i < num_threads; ++i) {
+               int j;
+
+               thread_datas [i].num_regions = REGIONS_PER_THREAD;
+               thread_datas [i].regions = g_new0 (Region, REGIONS_PER_THREAD);
+
+               for (j = 0; j < REGIONS_PER_THREAD; ++j) {
+                       thread_datas [i].regions [j].start = (num_threads * j + i) * REGION_SIZE;
+                       thread_datas [i].regions [j].length = REGION_SIZE;
+               }
+       }
+
+       test_domain = domain;
+
+       /*
+       for (i = 0; i < 72; ++i)
+               mono_thread_create (domain, small_id_thread_func, NULL);
+
+       sleep (2);
+       */
+
+       for (i = 0; i < num_threads; ++i)
+               mono_thread_create (domain, test_thread_func, &thread_datas [i]);
+}
+#endif
+
 enum {
        DO_BENCH,
        DO_REGRESSION,
@@ -620,9 +941,11 @@ mini_usage (void)
                "    --verbose, -v          Increases the verbosity level\n"
                "    --help, -h             Show usage information\n"
                "    --version, -V          Show version information\n"
+               "    --runtime=VERSION      Use the VERSION runtime, instead of autodetecting\n"
                "    --optimize=OPT         Turns on or off a specific optimization\n"
                "                           Use --list-opt to get a list of optimizations\n"
-               "    --security             Turns on the security manager (unsupported, default is off)\n");
+               "    --security[=mode]      Turns on the unsupported security manager (off by default)\n"
+               "                           mode is one of cas or core-clr\n");
 }
 
 static void
@@ -657,9 +980,21 @@ static const char info[] =
 #else
     "\tSIGSEGV:       normal\n"
 #endif
+#ifdef HAVE_EPOLL
+    "\tNotifications: epoll\n"
+#else
+    "\tNotification:  Thread + polling\n"
+#endif
+        "\tArchitecture:  " ARCHITECTURE "\n"
        "\tDisabled:      " DISABLED_FEATURES "\n"
        "";
 
+#ifndef MONO_ARCH_AOT_SUPPORTED
+#define error_if_aot_unsupported() do {fprintf (stderr, "AOT compilation is not supported on this platform.\n"); exit (1);} while (0)
+#else
+#define error_if_aot_unsupported()
+#endif
+
 int
 mono_main (int argc, char* argv[])
 {
@@ -680,9 +1015,22 @@ mono_main (int argc, char* argv[])
        char *trace_options = NULL;
        char *profile_options = NULL;
        char *aot_options = NULL;
+       char *forced_version = NULL;
+#ifdef MONO_JIT_INFO_TABLE_TEST
+       int test_jit_info_table = FALSE;
+#endif
 
        setlocale (LC_ALL, "");
 
+#if HAVE_SCHED_SETAFFINITY
+       if (getenv ("MONO_NO_SMP")) {
+               unsigned long proc_mask = 1;
+               sched_setaffinity (getpid(), sizeof (unsigned long), &proc_mask);
+       }
+#endif
+       if (!g_thread_supported ())
+               g_thread_init (NULL);
+
        if (mono_running_on_valgrind () && getenv ("MONO_VALGRIND_LEAK_CHECK")) {
                GMemVTable mem_vtable;
 
@@ -712,7 +1060,7 @@ mono_main (int argc, char* argv[])
                } else if (strcmp (argv [i], "--verbose") == 0 || strcmp (argv [i], "-v") == 0) {
                        mini_verbose++;
                } else if (strcmp (argv [i], "--version") == 0 || strcmp (argv [i], "-V") == 0) {
-                       g_print ("Mono JIT compiler version %s, (C) 2002-2006 Novell, Inc and Contributors. www.mono-project.com\n", VERSION);
+                       g_print ("Mono JIT compiler version %s (%s)\nCopyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com\n", VERSION, FULL_VERSION);
                        g_print (info);
                        if (mini_verbose) {
                                const char *cerror;
@@ -785,13 +1133,17 @@ mono_main (int argc, char* argv[])
                        mono_jit_stats.enabled = TRUE;
 #ifndef DISABLE_AOT
                } else if (strcmp (argv [i], "--aot") == 0) {
+                       error_if_aot_unsupported ();
                        mono_compile_aot = TRUE;
                } else if (strncmp (argv [i], "--aot=", 6) == 0) {
+                       error_if_aot_unsupported ();
                        mono_compile_aot = TRUE;
                        aot_options = &argv [i][6];
 #endif
                } else if (strcmp (argv [i], "--compile-all") == 0) {
                        action = DO_COMPILE;
+               } else if (strncmp (argv [i], "--runtime=", 10) == 0) {
+                       forced_version = &argv [i][10];
                } else if (strcmp (argv [i], "--profile") == 0) {
                        enable_profile = TRUE;
                        profile_options = NULL;
@@ -827,8 +1179,23 @@ mono_main (int argc, char* argv[])
                } else if (strcmp (argv [i], "--debug") == 0) {
                        enable_debugging = TRUE;
                } else if (strcmp (argv [i], "--security") == 0) {
-                       mono_use_security_manager = TRUE;
+                       mono_security_set_mode (MONO_SECURITY_MODE_CAS);
                        mono_activate_security_manager ();
+               } else if (strncmp (argv [i], "--security=", 11) == 0) {
+                       if (strcmp (argv [i] + 11, "temporary-smcs-hack") == 0) {
+                               mono_security_set_mode (MONO_SECURITY_MODE_SMCS_HACK);
+                       } else if (strcmp (argv [i] + 11, "core-clr") == 0) {
+                               mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
+                       } else if (strcmp (argv [i] + 11, "core-clr-test") == 0) {
+                               mono_security_set_mode (MONO_SECURITY_MODE_CORE_CLR);
+                               mono_security_core_clr_test = TRUE;
+                       } else if (strcmp (argv [i] + 11, "cas") == 0){
+                               mono_security_set_mode (MONO_SECURITY_MODE_CAS);
+                               mono_activate_security_manager ();
+                       } else {
+                               fprintf (stderr, "error: --security= option has invalid argument (cas or core-clr)\n");
+                               return 1;
+                       }
                } else if (strcmp (argv [i], "--desktop") == 0) {
 #if defined (HAVE_BOEHM_GC)
                        GC_dont_expand = 1;
@@ -849,6 +1216,10 @@ mono_main (int argc, char* argv[])
                                fprintf (stderr, "Invalid --wapi suboption: '%s'\n", argv [i]);
                                return 1;
                        }
+#ifdef MONO_JIT_INFO_TABLE_TEST
+               } else if (strcmp (argv [i], "--test-jit-info-table") == 0) {
+                       test_jit_info_table = TRUE;
+#endif
                } else {
                        fprintf (stderr, "Unknown command line option: '%s'\n", argv [i]);
                        return 1;
@@ -884,7 +1255,7 @@ mono_main (int argc, char* argv[])
        }
 
        if (action == DO_DEBUGGER) {
-               opt |= MONO_OPT_SHARED;
+               // opt |= MONO_OPT_SHARED;
                opt &= ~MONO_OPT_INLINE;
                opt &= ~MONO_OPT_COPYPROP;
                opt &= ~MONO_OPT_CONSPROP;
@@ -900,7 +1271,7 @@ mono_main (int argc, char* argv[])
        }
 
        mono_set_defaults (mini_verbose, opt);
-       domain = mini_init (argv [i]);
+       domain = mini_init (argv [i], forced_version);
        
        switch (action) {
        case DO_REGRESSION:
@@ -957,9 +1328,14 @@ mono_main (int argc, char* argv[])
                mono_config_parse (config_file);
        }
 
+#ifdef MONO_JIT_INFO_TABLE_TEST
+       if (test_jit_info_table)
+               jit_info_table_test (domain);
+#endif
+
        assembly = mono_assembly_open (aname, NULL);
        if (!assembly) {
-               fprintf (stderr, "cannot open assembly %s\n", aname);
+               fprintf (stderr, "Cannot open assembly %s.\n", aname);
                mini_cleanup (domain);
                return 2;
        }
@@ -978,13 +1354,15 @@ mono_main (int argc, char* argv[])
                error = mono_check_corlib_version ();
                if (error) {
                        fprintf (stderr, "Corlib not in sync with this runtime: %s\n", error);
+                       fprintf (stderr, "Loaded from: %s\n",
+                               mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown");
                        fprintf (stderr, "Download a newer corlib or a newer runtime at http://www.go-mono.com/daily.\n");
                        exit (1);
                }
 
 #ifdef PLATFORM_WIN32
                /* Detach console when executing IMAGE_SUBSYSTEM_WINDOWS_GUI on win32 */
-               if (!mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
+               if (!enable_debugging && !mono_compile_aot && ((MonoCLIImageInfo*)(mono_assembly_get_image (assembly)->image_info))->cli_header.nt.pe_subsys_required == IMAGE_SUBSYSTEM_WINDOWS_GUI)
                        FreeConsole ();
 #endif
 
@@ -1139,7 +1517,32 @@ mono_main (int argc, char* argv[])
 MonoDomain * 
 mono_jit_init (const char *file)
 {
-       return mini_init (file);
+       return mini_init (file, NULL);
+}
+
+/**
+ * mono_jit_init_version:
+ * @file: the initial assembly to load
+ * @runtime_version: the version of the runtime to load
+ *
+ * Use this version when you want to force a particular runtime
+ * version to be used.  By default Mono will pick the runtime that is
+ * referenced by the initial assembly (specified in @file), this
+ * routine allows programmers to specify the actual runtime to be used
+ * as the initial runtime is inherited by all future assemblies loaded
+ * (since Mono does not support having more than one mscorlib runtime
+ * loaded at once).
+ *
+ * The @runtime_version can be one of these strings: "v1.1.4322" for
+ * the 1.1 runtime or "v2.0.50727"  for the 2.0 runtime. 
+ *
+ * Returns: the MonoDomain representing the domain where the assembly
+ * was loaded.
+ */
+MonoDomain * 
+mono_jit_init_version (const char *file, const char *runtime_version)
+{
+       return mini_init (file, runtime_version);
 }
 
 void