3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include "time_util.h"
31 #include <sys/resource.h>
35 #include "ws_printf.h" /* ws_g_warning */
37 /* converts a broken down date representation, relative to UTC,
38 * to a timestamp; it uses timegm() if it's available.
39 * Copied from Glib source gtimer.c
42 mktime_utc(struct tm *tm)
47 static const int days_before[] =
49 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
54 if (tm->tm_mon < 0 || tm->tm_mon > 11)
57 retval = (tm->tm_year - 70) * 365;
59 /* count number of leap years */
60 yr = tm->tm_year + 1900;
61 if (tm->tm_mon + 1 < 3 && (yr % 4) == 0 && ((yr % 100) != 0 || (yr % 400) == 0))
63 retval += (((yr / 4) - (yr / 100) + (yr / 400)) - 477); /* 477 = ((1970 / 4) - (1970 / 100) + (1970 / 400)) */
65 retval += days_before[tm->tm_mon] + tm->tm_mday - 1;
67 retval = ((((retval * 24) + tm->tm_hour) * 60) + tm->tm_min) * 60 + tm->tm_sec;
72 #endif /* !HAVE_TIMEGM */
75 void get_resource_usage(double *user_time, double *sys_time) {
79 getrusage(RUSAGE_SELF, &ru);
81 *user_time = ru.ru_utime.tv_sec + (ru.ru_utime.tv_usec / 1000000.0);
82 *sys_time = ru.ru_stime.tv_sec + (ru.ru_stime.tv_usec / 1000000.0);
84 HANDLE h_proc = GetCurrentProcess();
85 FILETIME cft, eft, kft, uft;
86 ULARGE_INTEGER uli_time;
88 GetProcessTimes(h_proc, &cft, &eft, &kft, &uft);
90 uli_time.LowPart = uft.dwLowDateTime;
91 uli_time.HighPart = uft.dwHighDateTime;
92 *user_time = uli_time.QuadPart / 10000000.0;
93 uli_time.LowPart = kft.dwLowDateTime;
94 uli_time.HighPart = kft.dwHighDateTime;
95 *sys_time = uli_time.QuadPart / 1000000000.0;
99 static double last_user_time = 0.0;
100 static double last_sys_time = 0.0;
102 void log_resource_usage(gboolean reset_delta, const char *format, ...) {
104 GString *log_str = g_string_new("");
108 get_resource_usage(&user_time, &sys_time);
110 if (reset_delta || last_user_time == 0.0) {
111 last_user_time = user_time;
112 last_sys_time = sys_time;
115 g_string_append_printf(log_str, "user %.3f +%.3f sys %.3f +%.3f ",
116 user_time, user_time - last_user_time,
117 sys_time, sys_time - last_sys_time);
119 va_start(ap, format);
120 g_string_append_vprintf(log_str, format, ap);
123 ws_g_warning("%s", log_str->str);
124 g_string_free(log_str, TRUE);
129 * Editor modelines - http://www.wireshark.org/tools/modelines.html
134 * indent-tabs-mode: t
137 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
138 * :indentSize=8:tabSize=8:noTabs=false: