068d518d941b83851a09601b7393138c9bae5779
[platform/upstream/flac.git] / src / utils / flactimer / main.cpp
1 /* flactimer - Runs a command and prints timing information
2  * Copyright (C) 2007,2008,2009  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22
23 #define int64_t __int64
24 #define uint64_t unsigned int64_t
25
26 static inline uint64_t time2nsec(const FILETIME &t)
27 {
28         uint64_t n = t.dwHighDateTime;
29         n <<= 32;
30         n |= (uint64_t)t.dwLowDateTime;
31         return n * 100;
32 }
33
34 static void printtime(FILE *fout, uint64_t nsec, uint64_t total)
35 {
36         unsigned pct = (unsigned)(100.0 * ((double)(int64_t)nsec / (double)(int64_t)total));
37         uint64_t msec = nsec / 1000000; nsec -= msec * 1000000;
38         uint64_t sec = msec / 1000; msec -= sec * 1000;
39         uint64_t min = sec / 60; sec -= min * 60;
40         uint64_t hour = min / 60; min -= hour * 60;
41         fprintf(fout, " %5u.%03u = %02u:%02u:%02u.%03u = %3u%%\n",
42                 (unsigned)((hour*60+min)*60+sec),
43                 (unsigned)msec,
44                 (unsigned)hour,
45                 (unsigned)min,
46                 (unsigned)sec,
47                 (unsigned)msec,
48                 pct
49         );
50 }
51
52 int main(int argc, char *argv[])
53 {
54         const char *usage = "usage: flactimer [-1 | -2 | -o outputfile] command\n";
55         FILE *fout = stderr;
56
57         if(argc == 1 || (argc > 1 && 0 == strcmp(argv[1], "-h"))) {
58                 fprintf(stderr, usage);
59                 return 0;
60         }
61         argv++;
62         argc--;
63         if(0 == strcmp(argv[0], "-1") || 0 == strcmp(argv[0], "/1")) {
64                 fout = stdout;
65                 argv++;
66                 argc--;
67         }
68         else if(0 == strcmp(argv[0], "-2") || 0 == strcmp(argv[0], "/2")) {
69                 fout = stdout;
70                 argv++;
71                 argc--;
72         }
73         else if(0 == strcmp(argv[0], "-o")) {
74                 if(argc < 2) {
75                         fprintf(stderr, usage);
76                         return 1;
77                 }
78                 fout = fopen(argv[1], "w");
79                 if(!fout) {
80                         fprintf(fout, "ERROR opening file %s for writing\n", argv[1]);
81                         return 1;
82                 }
83                 argv += 2;
84                 argc -= 2;
85         }
86         if(argc <= 0) {
87                 fprintf(fout, "ERROR, no command!\n\n");
88                 fprintf(fout, usage);
89                 fclose(fout);
90                 return 1;
91         }
92
93         // improvement: double-quote all args
94         int i, n = 0;
95         for(i = 0; i < argc; i++) {
96                 if(i > 0)
97                         n++;
98                 n += strlen(argv[i]);
99         }
100         char *args = (char*)malloc(n+1);
101         if(!args) {
102                 fprintf(fout, "ERROR, no memory\n");
103                 fclose(fout);
104                 return 1;
105         }
106         args[0] = '\0';
107         for(i = 0; i < argc; i++) {
108                 if(i > 0)
109                         safe_strncat(args, sizeof(args), " ");
110                 safe_strncat(args, sizeof(args), argv[i]);
111         }
112
113         //fprintf(stderr, "@@@ cmd=[%s] args=[%s]\n", argv[0], args);
114
115         STARTUPINFO si;
116         GetStartupInfo(&si);
117
118         DWORD wallclock_msec = GetTickCount();
119
120         PROCESS_INFORMATION pi;
121         BOOL ok = CreateProcess(
122                 argv[0], // lpApplicationName
123                 args, // lpCommandLine
124                 NULL, // lpProcessAttributes
125                 NULL, // lpThreadAttributes
126                 FALSE, // bInheritHandles
127                 0, // dwCreationFlags
128                 NULL, // lpEnvironment
129                 NULL, // lpCurrentDirectory
130                 &si, // lpStartupInfo (inherit from this proc?)
131                 &pi // lpProcessInformation
132         );
133
134         if(!ok) {
135                 fprintf(fout, "ERROR running command\n");
136                 free(args); //@@@ ok to free here or have to wait to wait till process is reaped?
137                 fclose(fout);
138                 return 1;
139         }
140
141         //fprintf(stderr, "@@@ waiting...\n");
142         WaitForSingleObject(pi.hProcess, INFINITE);
143         //fprintf(stderr, "@@@ done\n");
144
145         wallclock_msec = GetTickCount() - wallclock_msec;
146
147         FILETIME creation_time;
148         FILETIME exit_time;
149         FILETIME kernel_time;
150         FILETIME user_time;
151         if(!GetProcessTimes(pi.hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) {
152                 fprintf(fout, "ERROR getting time info\n");
153                 free(args); //@@@ ok to free here or have to wait to wait till process is reaped?
154                 fclose(fout);
155                 return 1;
156         }
157         uint64_t kernel_nsec = time2nsec(kernel_time);
158         uint64_t user_nsec = time2nsec(user_time);
159
160         fprintf(fout, "Kernel Time  = "); printtime(fout, kernel_nsec, (uint64_t)wallclock_msec * 1000000);
161         fprintf(fout, "User Time    = "); printtime(fout, user_nsec, (uint64_t)wallclock_msec * 1000000);
162         fprintf(fout, "Process Time = "); printtime(fout, kernel_nsec+user_nsec, (uint64_t)wallclock_msec * 1000000);
163         fprintf(fout, "Global Time  = "); printtime(fout, (uint64_t)wallclock_msec * 1000000, (uint64_t)wallclock_msec * 1000000);
164
165         CloseHandle(pi.hThread);
166         CloseHandle(pi.hProcess);
167
168         free(args); //@@@ always causes crash, maybe CreateProcess takes ownership?
169         fclose(fout);
170         return 0;
171 }