From a441793cba1e5cb7567e08990e82bfb94f6764df Mon Sep 17 00:00:00 2001 From: Jeff Morriss Date: Tue, 25 Jun 2013 22:02:20 +0000 Subject: Move a couple of time-related modules into wsutil. A bunch of files didn't really need to include these header files so remove the include line rather than changing it. svn path=/trunk/; revision=50154 --- wsutil/CMakeLists.txt | 2 + wsutil/Makefile.common | 4 + wsutil/nstime.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++ wsutil/nstime.h | 115 ++++++++++++++++++++++++++++ wsutil/timestats.c | 92 ++++++++++++++++++++++ wsutil/timestats.h | 54 +++++++++++++ 6 files changed, 469 insertions(+) create mode 100644 wsutil/nstime.c create mode 100644 wsutil/nstime.h create mode 100644 wsutil/timestats.c create mode 100644 wsutil/timestats.h (limited to 'wsutil') diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 94a2183082..97da38cbc5 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -47,8 +47,10 @@ set(WSUTIL_FILES crc11.c crcdrm.c mpeg-audio.c + nstime.c privileges.c str_util.c + timestats.c type_util.c ${WSUTIL_PLATFORM_FILES} ) diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common index 570dbe60da..712c6c8448 100644 --- a/wsutil/Makefile.common +++ b/wsutil/Makefile.common @@ -41,8 +41,10 @@ LIBWSUTIL_SRC = \ crc32.c \ crcdrm.c \ mpeg-audio.c \ + nstime.c \ privileges.c \ str_util.c \ + timestats.c \ type_util.c # Header files that are not generated from other files @@ -58,6 +60,8 @@ LIBWSUTIL_INCLUDES = \ crc32.h \ crcdrm.h \ mpeg-audio.h \ + nstime.h \ privileges.h \ str_util.h \ + timestats.h \ type_util.h diff --git a/wsutil/nstime.c b/wsutil/nstime.c new file mode 100644 index 0000000000..e88818856a --- /dev/null +++ b/wsutil/nstime.c @@ -0,0 +1,202 @@ +/* nstime.c + * Routines for manipulating nstime_t structures + * + * Copyright (c) 2005 MX Telecom Ltd. + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include "nstime.h" + +/* this is #defined so that we can clearly see that we have the right number of + zeros, rather than as a guard against the number of nanoseconds in a second + changing ;) */ +#define NS_PER_S 1000000000 + +/* set the given nstime_t to zero */ +void nstime_set_zero(nstime_t *nstime) +{ + nstime->secs = 0; + nstime->nsecs = 0; +} + +/* is the given nstime_t currently zero? */ +gboolean nstime_is_zero(nstime_t *nstime) +{ + if(nstime->secs == 0 && nstime->nsecs == 0) { + return TRUE; + } else { + return FALSE; + } +} + +/* set the given nstime_t to (0,maxint) to mark it as "unset" + * That way we can find the first frame even when a timestamp + * is zero (fix for bug 1056) + */ +void nstime_set_unset(nstime_t *nstime) +{ + nstime->secs = 0; + nstime->nsecs = G_MAXINT; +} + +/* is the given nstime_t currently (0,maxint)? */ +gboolean nstime_is_unset(nstime_t *nstime) +{ + if(nstime->secs == 0 && nstime->nsecs == G_MAXINT) { + return TRUE; + } else { + return FALSE; + } +} + + +/** funcion: nstime_copy + * + * a = b + */ +void nstime_copy(nstime_t *a, const nstime_t *b) +{ + a->secs = b->secs; + a->nsecs = b->nsecs; +} + +/* + * function: nstime_delta + * delta = b - a + */ + +void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a ) +{ + if (b->secs == a->secs) { + /* The seconds part of b is the same as the seconds part of a, so if + the nanoseconds part of the first time is less than the nanoseconds + part of a, b is before a. The nanoseconds part of the delta should + just be the difference between the nanoseconds part of b and the + nanoseconds part of a; don't adjust the seconds part of the delta, + as it's OK if the nanoseconds part is negative, and an overflow + can never result. */ + delta->secs = 0; + delta->nsecs = b->nsecs - a->nsecs; + } else if (b->secs <= a->secs) { + /* The seconds part of b is less than the seconds part of a, so b is + before a. + + Both the "seconds" and "nanoseconds" value of the delta + should have the same sign, so if the difference between the + nanoseconds values would be *positive*, subtract 1,000,000,000 + from it, and add one to the seconds value. */ + delta->secs = b->secs - a->secs; + delta->nsecs = b->nsecs - a->nsecs; + if(delta->nsecs > 0) { + delta->nsecs -= NS_PER_S; + delta->secs ++; + } + } else { + delta->secs = b->secs - a->secs; + delta->nsecs = b->nsecs - a->nsecs; + if(delta->nsecs < 0) { + delta->nsecs += NS_PER_S; + delta->secs --; + } + } +} + +/* + * function: nstime_sum + * sum = a + b + */ + +void nstime_sum(nstime_t *sum, const nstime_t *a, const nstime_t *b) +{ + sum->secs = a->secs + b->secs; + sum->nsecs = a->nsecs + b->nsecs; + if(sum->nsecs>=NS_PER_S || (sum->nsecs>0 && sum->secs<0)){ + sum->nsecs-=NS_PER_S; + sum->secs++; + } else if(sum->nsecs<=-NS_PER_S || (sum->nsecs<0 && sum->secs>0)) { + sum->nsecs+=NS_PER_S; + sum->secs--; + } +} + +/* + * function: nstime_cmp + * + * a > b : > 0 + * a = b : 0 + * a < b : < 0 + */ + +int nstime_cmp (const nstime_t *a, const nstime_t *b ) +{ + if (a->secs == b->secs) { + return a->nsecs - b->nsecs; + } else { + return (int) (a->secs - b->secs); + } +} + +/* + * function: nstime_to_msec + * converts nstime to double, time base is milli seconds + */ + +double nstime_to_msec(const nstime_t *nstime) +{ + return ((double)nstime->secs*1000 + (double)nstime->nsecs/1000000); +} + +/* + * function: nstime_to_sec + * converts nstime to double, time base is seconds + */ + +double nstime_to_sec(const nstime_t *nstime) +{ + return ((double)nstime->secs + (double)nstime->nsecs/1000000000); +} + +/* + * function: wtap_nstime_to_sec + * converts wtap_nstime to double, time base is seconds + */ + +double wtap_nstime_to_sec(const struct wtap_nstime *nstime) +{ + return ((double)nstime->secs + (double)nstime->nsecs/1000000000); +} + +/* + * Editor modelines + * + * Local Variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ + diff --git a/wsutil/nstime.h b/wsutil/nstime.h new file mode 100644 index 0000000000..01fd469b62 --- /dev/null +++ b/wsutil/nstime.h @@ -0,0 +1,115 @@ +/* nstime.h + * Definition of data structure to hold time values with nanosecond resolution + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __NSTIME_H__ +#define __NSTIME_H__ + +#include + +#include +#include "ws_symbol_export.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** @file + * Definition of data structure to hold time values with nanosecond resolution + */ + +/** data structure to hold time values with nanosecond resolution*/ +typedef struct { + time_t secs; + int nsecs; +} nstime_t; + +/* functions */ + +/** set the given nstime_t to zero */ +WS_DLL_PUBLIC void nstime_set_zero(nstime_t *nstime); + +/** is the given nstime_t currently zero? */ +WS_DLL_PUBLIC gboolean nstime_is_zero(nstime_t *nstime); + +/** set the given nstime_t to (0,maxint) to mark it as "unset" + * That way we can find the first frame even when a timestamp + * is zero (fix for bug 1056) + */ +WS_DLL_PUBLIC void nstime_set_unset(nstime_t *nstime); + +/* is the given nstime_t currently (0,maxint)? */ +WS_DLL_PUBLIC gboolean nstime_is_unset(nstime_t *nstime); + +/** duplicate the current time + * + * a = b + */ +WS_DLL_PUBLIC void nstime_copy(nstime_t *a, const nstime_t *b); + +/** calculate the delta between two times (can be negative!) + * + * delta = b-a + * + * Note that it is acceptable for two or more of the arguments to point at the + * same structure. + */ +WS_DLL_PUBLIC void nstime_delta(nstime_t *delta, const nstime_t *b, const nstime_t *a ); + +/** calculate the sum of two times + * + * sum = a+b + * + * Note that it is acceptable for two or more of the arguments to point at the + * same structure. + */ +WS_DLL_PUBLIC void nstime_sum(nstime_t *sum, const nstime_t *b, const nstime_t *a ); + +/** sum += a */ +#define nstime_add(sum, a) nstime_sum(sum, sum, a) + +/** sum -= a */ +#define nstime_subtract(sum, a) nstime_delta(sum, sum, a) + +/** compare two times are return a value similar to memcmp() or strcmp(). + * + * a > b : > 0 + * a = b : 0 + * a < b : < 0 + */ +WS_DLL_PUBLIC int nstime_cmp (const nstime_t *a, const nstime_t *b ); + +/** converts nstime to double, time base is milli seconds */ +WS_DLL_PUBLIC double nstime_to_msec(const nstime_t *nstime); + +/** converts nstime to double, time base is seconds */ +WS_DLL_PUBLIC double nstime_to_sec(const nstime_t *nstime); + +/** converts wtap_nstime to double, time base is seconds */ +WS_DLL_PUBLIC double wtap_nstime_to_sec(const struct wtap_nstime *nstime); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __NSTIME_H__ */ diff --git a/wsutil/timestats.c b/wsutil/timestats.c new file mode 100644 index 0000000000..9803999b91 --- /dev/null +++ b/wsutil/timestats.c @@ -0,0 +1,92 @@ +/* timestats.c + * routines for time statistics + * Copyrigth 2003 Lars Roland + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include "timestats.h" + +/* Initialize a timestat_t struct */ +void +time_stat_init(timestat_t *stats) +{ + stats->num = 0; + stats->min_num = 0; + stats->max_num = 0; + nstime_set_zero(&stats->min); + nstime_set_zero(&stats->max); + nstime_set_zero(&stats->tot); + stats->variance = 0.0; +} + +/* Update a timestat_t struct with a new sample */ +void +time_stat_update(timestat_t *stats, const nstime_t *delta, packet_info *pinfo) +{ + if(stats->num==0){ + stats->max=*delta; + stats->max_num=pinfo->fd->num; + stats->min=*delta; + stats->min_num=pinfo->fd->num; + } + + if( (delta->secsmin.secs) + ||( (delta->secs==stats->min.secs) + &&(delta->nsecsmin.nsecs) ) ){ + stats->min=*delta; + stats->min_num=pinfo->fd->num; + } + + if( (delta->secs>stats->max.secs) + ||( (delta->secs==stats->max.secs) + &&(delta->nsecs>stats->max.nsecs) ) ){ + stats->max=*delta; + stats->max_num=pinfo->fd->num; + } + + nstime_add(&stats->tot, delta); + + stats->num++; +} + +/* + * get_average - function + * + * function to calculate the average + * returns the average as a gdouble , time base is milli seconds + */ + +gdouble get_average(const nstime_t *sum, guint32 num) +{ + gdouble average; + + if(num > 0) { + average = (double)sum->secs*1000 + (double)sum->nsecs/1000000; + average /= num; + } + else { + average = 0; + } + return average; +} diff --git a/wsutil/timestats.h b/wsutil/timestats.h new file mode 100644 index 0000000000..81e6d97042 --- /dev/null +++ b/wsutil/timestats.h @@ -0,0 +1,54 @@ +/* timestats.h + * Routines and definitions for time statistics + * Copyrigth 2003 Lars Roland + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _time_stat +#define _time_stat + +#include +#include "epan/packet_info.h" +#include "nstime.h" + + /* Summary of time statistics*/ +typedef struct _timestat_t { + guint32 num; /* number of samples */ + guint32 min_num; /* frame number of minimum */ + guint32 max_num; /* frame number of maximum */ + nstime_t min; + nstime_t max; + nstime_t tot; + gdouble variance; +} timestat_t; + +/* functions */ + +/* Initialize a timestat_t struct */ +WS_DLL_PUBLIC void time_stat_init(timestat_t *stats); + +/* Update a timestat_t struct with a new sample */ +WS_DLL_PUBLIC void time_stat_update(timestat_t *stats, const nstime_t *delta, packet_info *pinfo); + +WS_DLL_PUBLIC gdouble get_average(const nstime_t *sum, guint32 num); + +#endif -- cgit v1.2.3