1 /* flactimer - Runs a command and prints timing information
2 * Copyright (C) 2007,2008,2009 Josh Coalson
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.
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.
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.
23 #define int64_t __int64
24 #define uint64_t unsigned int64_t
26 static inline uint64_t time2nsec(const FILETIME &t)
28 uint64_t n = t.dwHighDateTime;
30 n |= (uint64_t)t.dwLowDateTime;
34 static void printtime(FILE *fout, uint64_t nsec, uint64_t total)
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),
52 int main(int argc, char *argv[])
54 const char *usage = "usage: flactimer [-1 | -2 | -o outputfile] command\n";
57 if(argc == 1 || (argc > 1 && 0 == strcmp(argv[1], "-h"))) {
58 fprintf(stderr, usage);
63 if(0 == strcmp(argv[0], "-1") || 0 == strcmp(argv[0], "/1")) {
68 else if(0 == strcmp(argv[0], "-2") || 0 == strcmp(argv[0], "/2")) {
73 else if(0 == strcmp(argv[0], "-o")) {
75 fprintf(stderr, usage);
78 fout = fopen(argv[1], "w");
80 fprintf(fout, "ERROR opening file %s for writing\n", argv[1]);
87 fprintf(fout, "ERROR, no command!\n\n");
93 // improvement: double-quote all args
95 for(i = 0; i < argc; i++) {
100 char *args = (char*)malloc(n+1);
102 fprintf(fout, "ERROR, no memory\n");
107 for(i = 0; i < argc; i++) {
109 safe_strncat(args, sizeof(args), " ");
110 safe_strncat(args, sizeof(args), argv[i]);
113 //fprintf(stderr, "@@@ cmd=[%s] args=[%s]\n", argv[0], args);
118 DWORD wallclock_msec = GetTickCount();
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
135 fprintf(fout, "ERROR running command\n");
136 free(args); //@@@ ok to free here or have to wait to wait till process is reaped?
141 //fprintf(stderr, "@@@ waiting...\n");
142 WaitForSingleObject(pi.hProcess, INFINITE);
143 //fprintf(stderr, "@@@ done\n");
145 wallclock_msec = GetTickCount() - wallclock_msec;
147 FILETIME creation_time;
149 FILETIME kernel_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?
157 uint64_t kernel_nsec = time2nsec(kernel_time);
158 uint64_t user_nsec = time2nsec(user_time);
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);
165 CloseHandle(pi.hThread);
166 CloseHandle(pi.hProcess);
168 free(args); //@@@ always causes crash, maybe CreateProcess takes ownership?