NaCl runtime fixes
[mono.git] / mono / monograph / monograph.c
index c19dc71fe6f7d65d361885409ec78bf3e0c23a56..27f1a5f3cf9cf801d857e750382395fcb80cccff 100644 (file)
@@ -1,5 +1,7 @@
 #include <glib.h>
 #include <string.h>
+#include <math.h>
+#include "mono/metadata/metadata-internals.h"
 #include "mono/metadata/class-internals.h"
 #include "mono/metadata/assembly.h"
 #include "mono/metadata/tokentype.h"
@@ -16,6 +18,11 @@ static int max_depth = 6;
 static int verbose = 0;
 static const char *graph_properties = "\tnode [fontsize=8.0]\n\tedge [len=2,color=red]\n";
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+volatile int __nacl_thread_suspension_needed = 0;
+void __nacl_suspend_thread_if_needed() {}
+#endif
+
 static void
 output_type_edge (MonoClass *first, MonoClass *second) {
        if (include_namespace)
@@ -169,6 +176,7 @@ static int cast_sealed = 0;
 static int cast_iface = 0;
 static int total_cast = 0;
 static int nonvirt_callvirt = 0;
+static int iface_callvirt = 0;
 static int total_callvirt = 0;
 
 static void
@@ -177,7 +185,7 @@ method_stats (MonoMethod *method) {
        MonoMethodHeader *header;
        MonoMethodSignature *sig;
        const unsigned char *ip, *il_code_end;
-       int i, n;
+       guint32 i, n;
        int local_branch = 0, local_condbranch = 0, local_throw = 0, local_calls = 0;
        gint64 l;
 
@@ -285,16 +293,16 @@ method_stats (MonoMethod *method) {
                                        case MONO_CEE_LDLOC:
                                        case MONO_CEE_STLOC:
                                                var_waste += 3;
-                                               g_print ("%s %d\n", mono_opcode_name (i), n);
+                                               /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                                break;
                                        default:
                                                var_waste += 2;
-                                               g_print ("%s %d\n", mono_opcode_name (i), n);
+                                               /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                                break;
                                        }
                                } else {
                                        var_waste += 2;
-                                       g_print ("%s %d\n", mono_opcode_name (i), n);
+                                       /*g_print ("%s %d\n", mono_opcode_name (i), n);*/
                                }
                        }
                        ip += 3;
@@ -306,7 +314,7 @@ method_stats (MonoMethod *method) {
                                case MONO_CEE_LDLOC_S:
                                case MONO_CEE_STLOC_S:
                                        var_waste++;
-                                       g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);
+                                       /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
                                        break;
                                default:
                                        break;
@@ -316,7 +324,7 @@ method_stats (MonoMethod *method) {
                        break;
                case MonoShortInlineI:
                        if ((signed char)ip [1] <= 8 && (signed char)ip [1] >= -1) {
-                               g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);
+                               /*g_print ("%s %d\n", mono_opcode_name (i), (signed char)ip [1]);*/
                                int_waste ++;
                        }
                        ip += 2;
@@ -356,6 +364,8 @@ method_stats (MonoMethod *method) {
                                MonoMethod *cm = mono_get_method (method->klass->image, read32 (ip + 1), NULL);
                                if (cm && !(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
                                        nonvirt_callvirt++;
+                               if (cm && (cm->klass->flags & TYPE_ATTRIBUTE_INTERFACE))
+                                       iface_callvirt++;
                                total_callvirt++;
                        }
                        ip += 5;
@@ -411,6 +421,9 @@ static int num_pdepth = 0;
 static int max_pdepth = 0;
 static int num_pdepth_ovf = 0;
 static int num_ifaces = 0;
+static int *pdepth_array = NULL;
+static int pdepth_array_size = 0;
+static int pdepth_array_next = 0;
 
 static void
 type_stats (MonoClass *klass) {
@@ -426,6 +439,13 @@ type_stats (MonoClass *klass) {
                depth++;
                parent = parent->parent;
        }
+       if (pdepth_array_next >= pdepth_array_size) {
+               pdepth_array_size *= 2;
+               if (!pdepth_array_size)
+                       pdepth_array_size = 128;
+               pdepth_array = g_realloc (pdepth_array, pdepth_array_size * sizeof (int));
+       }
+       pdepth_array [pdepth_array_next++] = depth;
        num_pdepth += depth;
        if (max_pdepth < depth)
                max_pdepth = depth;
@@ -471,12 +491,58 @@ stats (MonoImage *image, const char *name) {
        g_print ("sealed type cast: %d/%d\n", cast_sealed, total_cast);
        g_print ("interface type cast: %d/%d\n", cast_iface, total_cast);
        g_print ("non virtual callvirt: %d/%d\n", nonvirt_callvirt, total_callvirt);
+       g_print ("interface callvirt: %d/%d\n", iface_callvirt, total_callvirt);
        
        g_print ("\nType stats:\n");
        g_print ("interface types: %d/%d\n", num_ifaces, num_types);
-       g_print ("parent depth: max: %d, mean: %f, overflowing: %d\n", max_pdepth, (double)num_pdepth/(num_types - num_ifaces), num_pdepth_ovf);
+       {
+               double mean = 0;
+               double stddev = 0;
+               if (pdepth_array_next) {
+                       int i;
+                       mean = (double)num_pdepth/pdepth_array_next;
+                       for (i = 0; i < pdepth_array_next; ++i) {
+                               stddev += (pdepth_array [i] - mean) * (pdepth_array [i] - mean);
+                       }
+                       stddev = sqrt (stddev/pdepth_array_next);
+               }
+               g_print ("parent depth: max: %d, mean: %f, sttdev: %f, overflowing: %d\n", max_pdepth, mean, stddev, num_pdepth_ovf);
+       }
+}
+
+static void
+type_size_stats (MonoClass *klass)
+{
+       int code_size = 0;
+       MonoMethod *method;
+       MonoMethodHeader *header;
+       gpointer iter;
+
+       iter = NULL;
+       while ((method = mono_class_get_methods (klass, &iter))) {
+               guint32 size, maxs;
+               header = mono_method_get_header (method);
+               if (!header)
+                       continue;
+               mono_method_header_get_code (header, &size, &maxs);
+               code_size += size;
+       }
+       g_print ("%s.%s: code: %d\n", klass->name_space, klass->name, code_size);
 }
 
+static void
+size_stats (MonoImage *image, const char *name) {
+       int i, num_types;
+       MonoClass *klass;
+       
+       num_types = mono_image_get_table_rows (image, MONO_TABLE_TYPEDEF);
+       for (i = 0; i < num_types; ++i) {
+               klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | (i + 1));
+               type_size_stats (klass);
+       }
+}
+
+
 static char *
 get_signature (MonoMethod *method) {
        GString *res;
@@ -490,9 +556,9 @@ get_signature (MonoMethod *method) {
 
        res = g_string_new ("");
        if (include_namespace && *(method->klass->name_space))
-               g_string_sprintfa (res, "%s.", method->klass->name_space);
+               g_string_append_printf (res, "%s.", method->klass->name_space);
        result = mono_signature_get_desc (mono_method_signature (method), include_namespace);
-       g_string_sprintfa (res, "%s:%s(%s)", method->klass->name, method->name, result);
+       g_string_append_printf (res, "%s:%s(%s)", method->klass->name, method->name, result);
        g_free (result);
        g_hash_table_insert (hash, method, res->str);
 
@@ -526,7 +592,7 @@ print_method (MonoMethod *method, int depth) {
        GHashTable *hash;
        static GHashTable *visited = NULL;
        const unsigned char *ip, *il_code_end;
-       int i;
+       guint32 i;
 
        if (depth++ > max_depth)
                return;
@@ -685,7 +751,7 @@ mono_method_find_bblocks (MonoMethodHeader *header)
 {
        const unsigned char *ip, *end, *start;
        const MonoOpcode *opcode;
-       int i, block_end = 0;
+       guint32 i, block_end = 0;
        GPtrArray *result = g_ptr_array_new ();
        GHashTable *table = g_hash_table_new (g_direct_hash, g_direct_equal);
        MonoBasicBlock *entry_bb, *end_bb, *bb, *target;
@@ -810,7 +876,7 @@ mono_method_find_bblocks (MonoMethodHeader *header)
                                ip += 4;
                                if (!(target = g_hash_table_lookup (table, itarget))) {
                                        target = g_new0 (MonoBasicBlock, 1);
-                                       target->cil_code = itarget;
+                                       target->cil_code = (const guchar*)itarget;
                                        g_ptr_array_add (result, target);
                                        g_hash_table_insert (table, (gpointer) itarget, target);
                                }
@@ -874,9 +940,9 @@ df_visit (MonoBasicBlock *bb, int *dfn, const unsigned char* code)
                next = tmp->data;
                if (!next->dfn) {
                        if (!bb->cil_code)
-                               fprintf (output, "\t\"DF entry\" -> \"IL_%04x (%d)\"\n", next->cil_code - code, *dfn + 1);
+                               fprintf (output, "\t\"DF entry\" -> \"IL_%04x (%d)\"\n", (unsigned int)(next->cil_code - code), *dfn + 1);
                        else
-                               fprintf (output, "\t\"IL_%04x (%d)\" -> \"IL_%04x (%d)\"\n", bb->cil_code - code, bb->dfn, next->cil_code - code, *dfn + 1);
+                               fprintf (output, "\t\"IL_%04x (%d)\" -> \"IL_%04x (%d)\"\n", (unsigned int)(bb->cil_code - code), bb->dfn, (unsigned int)(next->cil_code - code), *dfn + 1);
                        df_visit (next, dfn, code);
                }
        }
@@ -903,7 +969,7 @@ print_method_cfg (MonoMethod *method) {
                        fprintf (output, "\tB%p [shape=record,label=\"end\"]\n", bb);
                else {
                        code = mono_disasm_code (&graph_dh, method, bb->cil_code, bb->cil_code + bb->cil_length);
-                       fprintf (output, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb, bb->cil_code - il_code, code);
+                       fprintf (output, "\tB%p [shape=record,label=\"IL_%04x\\n%s\"]\n", bb, (unsigned int)(bb->cil_code - il_code), code);
                        g_free (code);
                }
        }
@@ -973,6 +1039,7 @@ usage (void) {
        printf ("\t-c|--call             output call graph instead of type hierarchy\n");
        printf ("\t-C|--control-flow     output control flow of methodname\n");
        printf ("\t--stats               output some statistics about the assembly\n");
+       printf ("\t--size                output some size statistics about the assembly\n");
        printf ("\t-d|--depth num        max depth recursion (default: 6)\n");
        printf ("\t-o|--output filename  write graph to file filename (default: stdout)\n");
        printf ("\t-f|--fullname         include namespace in type and method names\n");
@@ -998,6 +1065,7 @@ enum {
        GRAPH_CALL,
        GRAPH_INTERFACE,
        GRAPH_CONTROL_FLOW,
+       GRAPH_SIZE_STATS,
        GRAPH_STATS
 };
 
@@ -1026,7 +1094,6 @@ main (int argc, char *argv[]) {
        int callneato = 0;
        int i;
        
-       mono_init (argv [0]);
        output = stdout;
 
        for (i = 1; i < argc; ++i) {
@@ -1040,6 +1107,8 @@ main (int argc, char *argv[]) {
                        graphtype = GRAPH_INTERFACE;
                } else if (strcmp (argv [i], "--stats") == 0) {
                        graphtype = GRAPH_STATS;
+               } else if (strcmp (argv [i], "--size") == 0) {
+                       graphtype = GRAPH_SIZE_STATS;
                } else if (strcmp (argv [i], "--fullname") == 0 || strcmp (argv [i], "-f") == 0) {
                        include_namespace = 1;
                } else if (strcmp (argv [i], "--neato") == 0 || strcmp (argv [i], "-n") == 0) {
@@ -1064,8 +1133,10 @@ main (int argc, char *argv[]) {
        if (argc > i + 1)
                cname = argv [i + 1];
        if (aname) {
+               mono_init_from_assembly (argv [0], aname);
                assembly = mono_assembly_open (aname, NULL);
        } else {
+               mono_init (argv [0]);
                assembly = mono_image_get_assembly (mono_get_corlib ());
        }
        if (!cname && (graphtype == GRAPH_TYPES))
@@ -1082,10 +1153,10 @@ main (int argc, char *argv[]) {
 
                if (outputfile) {
                        type = strrchr (outputfile, '.');
-                       g_string_sprintfa (command, " -o %s", outputfile);
+                       g_string_append_printf (command, " -o %s", outputfile);
                }
                if (type)
-                       g_string_sprintfa (command, " -T%s", type + 1);
+                       g_string_append_printf (command, " -T%s", type + 1);
                output = popen (command->str, "w");
                if (!output) {
                        g_print ("Cannot run neato: you may need to install the graphviz package.\n");
@@ -1119,6 +1190,9 @@ main (int argc, char *argv[]) {
        case GRAPH_STATS:
                stats (image, cname);
                break;
+       case GRAPH_SIZE_STATS:
+               size_stats (image, cname);
+               break;
        default:
                g_error ("wrong graph type");
        }
@@ -1131,5 +1205,3 @@ main (int argc, char *argv[]) {
                fclose (output);
        return 0;
 }
-
-