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