changeset 502:8875ff2e85dc

Profiling support
author Adam Chlipala <adamc@hcoop.net>
date Thu, 20 Nov 2008 12:16:30 -0500
parents 7ef4b2911b09
children d2dfdf90b9b6
files Makefile.in src/c/driver.c src/compiler.sig src/compiler.sml src/demo.sml
diffstat 5 files changed, 45 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile.in	Thu Nov 20 11:34:36 2008 -0500
+++ b/Makefile.in	Thu Nov 20 12:16:30 2008 -0500
@@ -21,10 +21,10 @@
 	rm -rf .cm src/.cm
 
 clib/urweb.o: src/c/urweb.c
-	gcc -O3 -I include -c src/c/urweb.c -o clib/urweb.o
+	gcc -O3 -I include -c src/c/urweb.c -o clib/urweb.o $(CFLAGS)
 
 clib/driver.o: src/c/driver.c
-	gcc -O3 -I include -c src/c/driver.c -o clib/driver.o
+	gcc -O3 -I include -c src/c/driver.c -o clib/driver.o $(CFLAGS)
 
 src/urweb.cm: src/prefix.cm src/sources
 	cat src/prefix.cm src/sources \
--- a/src/c/driver.c	Thu Nov 20 11:34:36 2008 -0500
+++ b/src/c/driver.c	Thu Nov 20 12:16:30 2008 -0500
@@ -1,10 +1,12 @@
 #include <stdio.h>
 
 #include <string.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <unistd.h>
+#include <signal.h>
 
 #include <pthread.h>
 
@@ -297,6 +299,11 @@
   printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
 }
 
+static void sigint(int signum) {
+  printf("Exiting....\n");
+  exit(0);
+}
+
 int main(int argc, char *argv[]) {
   // The skeleton for this function comes from Beej's sockets tutorial.
   int sockfd;  // listen on sock_fd
@@ -304,7 +311,9 @@
   struct sockaddr_in their_addr; // connector's address information
   int sin_size, yes = 1;
   int uw_port = 8080, nthreads = 1, i, *names, opt;
-  
+ 
+  signal(SIGINT, sigint);
+ 
   while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
     switch (opt) {
     case '?':
--- a/src/compiler.sig	Thu Nov 20 11:34:36 2008 -0500
+++ b/src/compiler.sig	Thu Nov 20 12:16:30 2008 -0500
@@ -35,10 +35,11 @@
          sources : string list,
          exe : string,
          sql : string option,
-         debug : bool
+         debug : bool,
+         profile : bool
     }
     val compile : string -> unit
-    val compileC : {cname : string, oname : string, ename : string, libs : string} -> unit
+    val compileC : {cname : string, oname : string, ename : string, libs : string, profile : bool} -> unit
 
     type ('src, 'dst) phase
     type ('src, 'dst) transform
--- a/src/compiler.sml	Thu Nov 20 11:34:36 2008 -0500
+++ b/src/compiler.sml	Thu Nov 20 12:16:30 2008 -0500
@@ -41,7 +41,8 @@
      sources : string list,
      exe : string,
      sql : string option,
-     debug : bool
+     debug : bool,
+     profile : bool
 }
 
 type ('src, 'dst) phase = {
@@ -199,7 +200,7 @@
               handle LrParser.ParseError => [],
      print = SourcePrint.p_file}    
 
-fun p_job {prefix, database, exe, sql, sources, debug} =
+fun p_job {prefix, database, exe, sql, sources, debug, profile} =
     let
         open Print.PD
         open Print
@@ -208,6 +209,10 @@
                  box [string "DEBUG", newline]
              else
                  box [],
+             if profile then
+                 box [string "PROFILE", newline]
+             else
+                 box [],
              case database of
                  NONE => string "No database."
                | SOME db => string ("Database: " ^ db),
@@ -260,19 +265,20 @@
                               readSources acc
                           end
 
-                  fun finish (prefix, database, exe, sql, debug, sources) =
+                  fun finish (prefix, database, exe, sql, debug, profile, sources) =
                       {prefix = Option.getOpt (prefix, "/"),
                        database = database,
                        exe = Option.getOpt (exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
                                                                       ext = SOME "exe"}),
                        sql = sql,
                        debug = debug,
+                       profile = profile,
                        sources = sources}
 
-                  fun read (prefix, database, exe, sql, debug) =
+                  fun read (prefix, database, exe, sql, debug, profile) =
                       case TextIO.inputLine inf of
-                          NONE => finish (prefix, database, exe, sql, debug, [])
-                        | SOME "\n" => finish (prefix, database, exe, sql, debug, readSources [])
+                          NONE => finish (prefix, database, exe, sql, debug, profile, [])
+                        | SOME "\n" => finish (prefix, database, exe, sql, debug, profile, readSources [])
                         | SOME line =>
                           let
                               val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
@@ -284,28 +290,29 @@
                                   (case prefix of
                                        NONE => ()
                                      | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
-                                   read (SOME arg, database, exe, sql, debug))
+                                   read (SOME arg, database, exe, sql, debug, profile))
                                 | "database" =>
                                   (case database of
                                        NONE => ()
                                      | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
-                                   read (prefix, SOME arg, exe, sql, debug))
+                                   read (prefix, SOME arg, exe, sql, debug, profile))
                                 | "exe" =>
                                   (case exe of
                                        NONE => ()
                                      | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
-                                   read (prefix, database, SOME (relify arg), sql, debug))
+                                   read (prefix, database, SOME (relify arg), sql, debug, profile))
                                 | "sql" =>
                                   (case sql of
                                        NONE => ()
                                      | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
-                                   read (prefix, database, exe, SOME (relify arg), debug))
-                                | "debug" => read (prefix, database, exe, sql, true)
+                                   read (prefix, database, exe, SOME (relify arg), debug, profile))
+                                | "debug" => read (prefix, database, exe, sql, true, profile)
+                                | "profile" => read (prefix, database, exe, sql, debug, true)
                                 | _ => (ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
-                                        read (prefix, database, exe, sql, debug))
+                                        read (prefix, database, exe, sql, debug, profile))
                           end
 
-                  val job = read (NONE, NONE, NONE, NONE, false)
+                  val job = read (NONE, NONE, NONE, NONE, false, false)
               in
                   TextIO.closeIn inf;
                   Monoize.urlPrefix := #prefix job;
@@ -544,13 +551,19 @@
 
 val toSqlify = transform sqlify "sqlify" o toMono_opt2
 
-fun compileC {cname, oname, ename, libs} =
+fun compileC {cname, oname, ename, libs, profile} =
     let
         val urweb_o = clibFile "urweb.o"
         val driver_o = clibFile "driver.o"
 
         val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
         val link = "gcc -Werror -O3 -lm -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
+
+        val (compile, link) =
+            if profile then
+                (compile ^ " -pg", link ^ " -pg")
+            else
+                (compile, link)
     in
         if not (OS.Process.isSuccess (OS.Process.system compile)) then
             print "C compilation failed\n"
@@ -615,7 +628,7 @@
                         TextIO.closeOut outf
                     end;
 
-                compileC {cname = cname, oname = oname, ename = ename, libs = libs};
+                compileC {cname = cname, oname = oname, ename = ename, libs = libs, profile = #profile job};
                 
                 cleanup ()
             end
--- a/src/demo.sml	Thu Nov 20 11:34:36 2008 -0500
+++ b/src/demo.sml	Thu Nov 20 12:16:30 2008 -0500
@@ -92,7 +92,8 @@
                                        file = "demo.exe"},
             sql = SOME (OS.Path.joinDirFile {dir = dirname,
                                              file = "demo.sql"}),
-            debug = false
+            debug = false,
+            profile = false
         }
 
         val parse = Compiler.run (Compiler.transform Compiler.parseUrp "Demo parseUrp")