1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2004 Hewlett-Packard Co
3 	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 
28 #include <sys/time.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 
32 int
main(int argc,char ** argv,char ** envp)33 main (int argc, char **argv, char **envp)
34 {
35   char *program, **child_argv;
36   struct timeval start, stop;
37   double secs;
38   int status, i;
39   long count;
40   pid_t pid;
41 
42   count = atol (argv[1]);
43   program = argv[2];
44 
45   child_argv = alloca ((argc - 1) * sizeof (char *));
46   for (i = 0; i < argc - 2; ++i)
47     child_argv[i] = argv[2 + i];
48   child_argv[i] = NULL;
49 
50   gettimeofday (&start, NULL);
51   for (i = 0; i < count; ++i)
52     {
53       pid = fork ();
54       if (pid == 0)
55         {
56           execve (program, child_argv, envp);
57           _exit (-1);
58         }
59       else
60         {
61           waitpid (pid, &status, 0);
62           if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
63             {
64               fprintf (stderr, "%s: child failed\n", argv[0]);
65               exit (-1);
66             }
67         }
68     }
69   gettimeofday (&stop, NULL);
70 
71   secs = ((stop.tv_sec + 1e-6 * stop.tv_usec)
72 	  - (start.tv_sec + 1e-6 * start.tv_usec));
73   printf ("%lu nsec/execution\n",
74 	  (unsigned long) (1e9 * secs / (double) count));
75   return 0;
76 }
77