diff options
author | Thomas Haller <thaller@redhat.com> | 2018-10-18 12:24:58 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-10-18 12:24:58 +0200 |
commit | e1b2b88ea8f5ac1d65787c8462fd44fb578e640e (patch) | |
tree | b6ffd4ea4a42ad44476ecc712de5bec560ec4dc4 | |
parent | 2af1dc1d287334dcd02d8282fcfdbe3a8e031363 (diff) | |
parent | eea1f50ea7b66b7c73d6bb3f6053936472e62837 (diff) | |
download | NetworkManager-e1b2b88ea8f5ac1d65787c8462fd44fb578e640e.tar.gz |
all: merge branch 'th/move-to-shared'
https://github.com/NetworkManager/NetworkManager/pull/231
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 36 | ||||
-rwxr-xr-x | contrib/scripts/checkpatch.pl | 1 | ||||
-rw-r--r-- | shared/meson.build | 32 | ||||
-rw-r--r-- | shared/nm-default.h | 1 | ||||
-rw-r--r-- | shared/nm-utils/nm-test-utils.h | 9 | ||||
-rw-r--r-- | shared/nm-utils/nm-time-utils.c | 227 | ||||
-rw-r--r-- | shared/nm-utils/nm-time-utils.h | 45 | ||||
-rw-r--r-- | shared/nm-utils/tests/test-shared-general.c | 76 | ||||
-rw-r--r-- | src/meson.build | 2 | ||||
-rw-r--r-- | src/nm-core-utils.c | 210 | ||||
-rw-r--r-- | src/nm-core-utils.h | 15 | ||||
-rw-r--r-- | src/nm-logging.c | 25 | ||||
-rw-r--r-- | src/nm-logging.h | 2 |
14 files changed, 454 insertions, 228 deletions
diff --git a/.gitignore b/.gitignore index 494ff707ba..bb3f82b514 100644 --- a/.gitignore +++ b/.gitignore @@ -147,6 +147,7 @@ test-*.trs /examples/C/qt/change-ipv4-addresses /shared/nm-version-macros.h +/shared/nm-utils/tests/test-shared-general /introspection/org.freedesktop.NetworkManager*.[ch] diff --git a/Makefile.am b/Makefile.am index 5c1af3a0e2..f99481ab7f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -204,6 +204,39 @@ DISTCLEANFILES += $(polkit_policy_DATA) ############################################################################### +check_programs += shared/nm-utils/tests/test-shared-general + +shared_nm_utils_tests_test_shared_general_CPPFLAGS = \ + $(dflt_cppflags) \ + -I$(srcdir)/shared \ + -DNETWORKMANAGER_COMPILATION_TEST \ + -DNETWORKMANAGER_COMPILATION='(NM_NETWORKMANAGER_COMPILATION_GLIB|NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG)' \ + $(CODE_COVERAGE_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(LIBUDEV_CFLAGS) \ + $(SANITIZER_LIB_CFLAGS) \ + $(NULL) + +shared_nm_utils_tests_test_shared_general_SOURCES = \ + shared/nm-utils/c-list-util.c \ + shared/nm-utils/nm-dedup-multi.c \ + shared/nm-utils/nm-enum-utils.c \ + shared/nm-utils/nm-hash-utils.c \ + shared/nm-utils/nm-io-utils.c \ + shared/nm-utils/nm-random-utils.c \ + shared/nm-utils/nm-secret-utils.c \ + shared/nm-utils/nm-shared-utils.c \ + shared/nm-utils/nm-time-utils.c \ + shared/nm-utils/tests/test-shared-general.c \ + $(NULL) + +shared_nm_utils_tests_test_shared_general_LDADD = \ + $(GLIB_LIBS) \ + shared/libcsiphash.la \ + $(NULL) + +############################################################################### + noinst_LTLIBRARIES += \ introspection/libnmdbus.la @@ -1578,6 +1611,9 @@ src_libNetworkManagerBase_la_CPPFLAGS = $(src_cppflags) src_libNetworkManagerBase_la_SOURCES = \ \ + shared/nm-utils/nm-time-utils.c \ + shared/nm-utils/nm-time-utils.h \ + \ src/nm-core-utils.c \ src/nm-core-utils.h \ src/nm-logging.c \ diff --git a/contrib/scripts/checkpatch.pl b/contrib/scripts/checkpatch.pl index d6b3c4451a..0769273bcb 100755 --- a/contrib/scripts/checkpatch.pl +++ b/contrib/scripts/checkpatch.pl @@ -132,6 +132,7 @@ complain ("Don't use \"$1 $2\" instead of \"$2 $1\"") if $line =~ /\b(char|short complain ("Don't use \"unsigned int\" but just use \"unsigned\"") if $line =~ /\b(unsigned) +(int)\b/; complain ("Please use LGPL2+ for new files") if $is_patch and $line =~ /under the terms of the GNU General Public License/; complain ("Don't use space inside elvis operator ?:") if $line =~ /\?[\t ]+:/; +complain ("Don't add Emacs editor formatting hints to source files") if $line_no == 1 and $line =~ /-\*-.+-\*-/; new_hunk if $_ eq ''; my ($this_indent) = /^(\s*)/; diff --git a/shared/meson.build b/shared/meson.build index 9fb665bcbb..274bc736f9 100644 --- a/shared/meson.build +++ b/shared/meson.build @@ -81,8 +81,9 @@ shared_nm_test_utils_impl_c = files('nm-test-utils-impl.c') shared_nm_utils_nm_vpn_plugin_utils_c = files('nm-utils/nm-vpn-plugin-utils.c') +shared_files_time_utils = files('nm-utils/nm-time-utils.c') + shared_files_libnm_core = files(''' - c-siphash/src/c-siphash.c nm-utils/c-list-util.c nm-utils/nm-dedup-multi.c nm-utils/nm-enum-utils.c @@ -95,7 +96,6 @@ shared_files_libnm_core = files(''' '''.split()) shared_files_clients_common = files(''' - c-siphash/src/c-siphash.c nm-utils/nm-enum-utils.c nm-utils/nm-hash-utils.c nm-utils/nm-random-utils.c @@ -117,3 +117,31 @@ shared_dep = declare_dependency( ], dependencies: glib_dep, ) + +############################################################################### + +test_shared_general = executable( + 'nm-utils/tests/test-shared-general', + [ 'nm-utils/tests/test-shared-general.c', + 'nm-utils/c-list-util.c', + 'nm-utils/nm-dedup-multi.c', + 'nm-utils/nm-enum-utils.c', + 'nm-utils/nm-hash-utils.c', + 'nm-utils/nm-io-utils.c', + 'nm-utils/nm-random-utils.c', + 'nm-utils/nm-secret-utils.c', + 'nm-utils/nm-shared-utils.c', + 'nm-utils/nm-time-utils.c', + ], + c_args: [ + '-DNETWORKMANAGER_COMPILATION_TEST', + '-DNETWORKMANAGER_COMPILATION=(NM_NETWORKMANAGER_COMPILATION_GLIB|NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG)', + ], + dependencies: shared_dep, + link_with: shared_c_siphash, +) +test( + 'shared/nm-utils/test-shared-general', + test_script, + args: test_args + [test_shared_general.full_path()] +) diff --git a/shared/nm-default.h b/shared/nm-default.h index 207acd202f..35c6009ff9 100644 --- a/shared/nm-default.h +++ b/shared/nm-default.h @@ -303,6 +303,7 @@ _nm_g_return_if_fail_warning (const char *log_domain, /*****************************************************************************/ #if (NETWORKMANAGER_COMPILATION) & NM_NETWORKMANAGER_COMPILATION_WITH_DAEMON +#include "nm-core-types.h" #include "nm-types.h" #include "nm-logging.h" #endif diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index c813986210..89bd111992 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -30,6 +30,9 @@ * * Our tests (make check) include this header-only file nm-test-utils.h. * + * You should always include this header *as last*. Reason is, that depending on + * previous includes, functionality will be enabled. + * * Logging: * In tests, nm-logging redirects to glib logging. By default, glib suppresses all debug * messages unless you set G_MESSAGES_DEBUG. To enable debug logging, you can explicitly set @@ -110,7 +113,9 @@ #include <string.h> #include <errno.h> +#ifndef NM_TEST_UTILS_NO_LIBNM #include "nm-utils.h" +#endif /*****************************************************************************/ @@ -1089,6 +1094,8 @@ __define_nmtst_static(02, 1024) __define_nmtst_static(03, 1024) #undef __define_nmtst_static +#if defined (__NM_UTILS_H__) || defined (NM_UTILS_H) + #define NMTST_UUID_INIT(uuid) \ gs_free char *_nmtst_hidden_##uuid = nm_utils_uuid_generate (); \ const char *const uuid = _nmtst_hidden_##uuid @@ -1105,6 +1112,8 @@ nmtst_uuid_generate (void) return u; } +#endif + #define NMTST_SWAP(x,y) \ G_STMT_START { \ char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \ diff --git a/shared/nm-utils/nm-time-utils.c b/shared/nm-utils/nm-time-utils.c new file mode 100644 index 0000000000..26b982246b --- /dev/null +++ b/shared/nm-utils/nm-time-utils.c @@ -0,0 +1,227 @@ +/* NetworkManager -- Network link manager + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * (C) Copyright 2018 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include "nm-time-utils.h" + +/*****************************************************************************/ + +static gint64 monotonic_timestamp_offset_sec; +static int monotonic_timestamp_clock_mode = 0; + +static void +monotonic_timestamp_get (struct timespec *tp) +{ + int clock_mode = 0; + int err = 0; + + switch (monotonic_timestamp_clock_mode) { + case 0: + /* the clock is not yet initialized (first run) */ + err = clock_gettime (CLOCK_BOOTTIME, tp); + if (err == -1 && errno == EINVAL) { + clock_mode = 2; + err = clock_gettime (CLOCK_MONOTONIC, tp); + } else + clock_mode = 1; + break; + case 1: + /* default, return CLOCK_BOOTTIME */ + err = clock_gettime (CLOCK_BOOTTIME, tp); + break; + case 2: + /* fallback, return CLOCK_MONOTONIC. Kernels prior to 2.6.39 + * (released on 18 May, 2011) don't support CLOCK_BOOTTIME. */ + err = clock_gettime (CLOCK_MONOTONIC, tp); + break; + } + + g_assert (err == 0); (void)err; + g_assert (tp->tv_nsec >= 0 && tp->tv_nsec < NM_UTILS_NS_PER_SECOND); + + if (G_LIKELY (clock_mode == 0)) + return; + + /* Calculate an offset for the time stamp. + * + * We always want positive values, because then we can initialize + * a timestamp with 0 and be sure, that it will be less then any + * value nm_utils_get_monotonic_timestamp_*() might return. + * For this to be true also for nm_utils_get_monotonic_timestamp_s() at + * early boot, we have to shift the timestamp to start counting at + * least from 1 second onward. + * + * Another advantage of shifting is, that this way we make use of the whole 31 bit + * range of signed int, before the time stamp for nm_utils_get_monotonic_timestamp_s() + * wraps (~68 years). + **/ + monotonic_timestamp_offset_sec = (- ((gint64) tp->tv_sec)) + 1; + monotonic_timestamp_clock_mode = clock_mode; + + _nm_utils_monotonic_timestamp_initialized (tp, monotonic_timestamp_offset_sec, clock_mode == 1); +} + +/** + * nm_utils_get_monotonic_timestamp_ns: + * + * Returns: a monotonically increasing time stamp in nanoseconds, + * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. + * + * The returned value will start counting at an undefined point + * in the past and will always be positive. + * + * All the nm_utils_get_monotonic_timestamp_*s functions return the same + * timestamp but in different scales (nsec, usec, msec, sec). + **/ +gint64 +nm_utils_get_monotonic_timestamp_ns (void) +{ + struct timespec tp = { 0 }; + + monotonic_timestamp_get (&tp); + + /* Although the result will always be positive, we return a signed + * integer, which makes it easier to calculate time differences (when + * you want to subtract signed values). + **/ + return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * NM_UTILS_NS_PER_SECOND + + tp.tv_nsec; +} + +/** + * nm_utils_get_monotonic_timestamp_us: + * + * Returns: a monotonically increasing time stamp in microseconds, + * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. + * + * The returned value will start counting at an undefined point + * in the past and will always be positive. + * + * All the nm_utils_get_monotonic_timestamp_*s functions return the same + * timestamp but in different scales (nsec, usec, msec, sec). + **/ +gint64 +nm_utils_get_monotonic_timestamp_us (void) +{ + struct timespec tp = { 0 }; + + monotonic_timestamp_get (&tp); + + /* Although the result will always be positive, we return a signed + * integer, which makes it easier to calculate time differences (when + * you want to subtract signed values). + **/ + return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * ((gint64) G_USEC_PER_SEC) + + (tp.tv_nsec / (NM_UTILS_NS_PER_SECOND/G_USEC_PER_SEC)); +} + +/** + * nm_utils_get_monotonic_timestamp_ms: + * + * Returns: a monotonically increasing time stamp in milliseconds, + * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. + * + * The returned value will start counting at an undefined point + * in the past and will always be positive. + * + * All the nm_utils_get_monotonic_timestamp_*s functions return the same + * timestamp but in different scales (nsec, usec, msec, sec). + **/ +gint64 +nm_utils_get_monotonic_timestamp_ms (void) +{ + struct timespec tp = { 0 }; + + monotonic_timestamp_get (&tp); + + /* Although the result will always be positive, we return a signed + * integer, which makes it easier to calculate time differences (when + * you want to subtract signed values). + **/ + return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * ((gint64) 1000) + + (tp.tv_nsec / (NM_UTILS_NS_PER_SECOND/1000)); +} + +/** + * nm_utils_get_monotonic_timestamp_s: + * + * Returns: nm_utils_get_monotonic_timestamp_ms() in seconds (throwing + * away sub second parts). The returned value will always be positive. + * + * This value wraps after roughly 68 years which should be fine for any + * practical purpose. + * + * All the nm_utils_get_monotonic_timestamp_*s functions return the same + * timestamp but in different scales (nsec, usec, msec, sec). + **/ +gint32 +nm_utils_get_monotonic_timestamp_s (void) +{ + struct timespec tp = { 0 }; + + monotonic_timestamp_get (&tp); + return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec); +} + +/** + * nm_utils_monotonic_timestamp_as_boottime: + * @timestamp: the monotonic-timestamp that should be converted into CLOCK_BOOTTIME. + * @timestamp_ns_per_tick: How many nano seconds make one unit of @timestamp? E.g. if + * @timestamp is in unit seconds, pass %NM_UTILS_NS_PER_SECOND; @timestamp in nano + * seconds, pass 1; @timestamp in milli seconds, pass %NM_UTILS_NS_PER_SECOND/1000; etc. + * + * Returns: the monotonic-timestamp as CLOCK_BOOTTIME, as returned by clock_gettime(). + * The unit is the same as the passed in @timestamp basd on @timestamp_ns_per_tick. + * E.g. if you passed @timestamp in as seconds, it will return boottime in seconds. + * If @timestamp is a non-positive, it returns -1. Note that a (valid) monotonic-timestamp + * is always positive. + * + * On older kernels that don't support CLOCK_BOOTTIME, the returned time is instead CLOCK_MONOTONIC. + **/ +gint64 +nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ns_per_tick) +{ + gint64 offset; + + /* only support ns-per-tick being a multiple of 10. */ + g_return_val_if_fail (timestamp_ns_per_tick == 1 + || (timestamp_ns_per_tick > 0 && + timestamp_ns_per_tick <= NM_UTILS_NS_PER_SECOND && + timestamp_ns_per_tick % 10 == 0), + -1); + + /* Check that the timestamp is in a valid range. */ + g_return_val_if_fail (timestamp >= 0, -1); + + /* if the caller didn't yet ever fetch a monotonic-timestamp, he cannot pass any meaningful + * value (because he has no idea what these timestamps would be). That would be a bug. */ + g_return_val_if_fail (monotonic_timestamp_clock_mode != 0, -1); + + /* calculate the offset of monotonic-timestamp to boottime. offset_s is <= 1. */ + offset = monotonic_timestamp_offset_sec * (NM_UTILS_NS_PER_SECOND / timestamp_ns_per_tick); + + /* check for overflow. */ + g_return_val_if_fail (offset > 0 || timestamp < G_MAXINT64 + offset, G_MAXINT64); + + return timestamp - offset; +} + + diff --git a/shared/nm-utils/nm-time-utils.h b/shared/nm-utils/nm-time-utils.h new file mode 100644 index 0000000000..7e4f4f25f7 --- /dev/null +++ b/shared/nm-utils/nm-time-utils.h @@ -0,0 +1,45 @@ +/* NetworkManager -- Network link manager + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * (C) Copyright 2018 Red Hat, Inc. + */ + +#ifndef __NM_TIME_UTILS_H__ +#define __NM_TIME_UTILS_H__ + +gint64 nm_utils_get_monotonic_timestamp_ns (void); +gint64 nm_utils_get_monotonic_timestamp_us (void); +gint64 nm_utils_get_monotonic_timestamp_ms (void); +gint32 nm_utils_get_monotonic_timestamp_s (void); +gint64 nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ticks_per_ns); + +static inline gint64 +nm_utils_get_monotonic_timestamp_ns_cached (gint64 *cache_now) +{ + return (*cache_now) + ?: (*cache_now = nm_utils_get_monotonic_timestamp_ns ()); +} + +struct timespec; + +/* this function must be implemented to handle the notification when + * the first monotonic-timestamp is fetched. */ +extern void _nm_utils_monotonic_timestamp_initialized (const struct timespec *tp, + gint64 offset_sec, + gboolean is_boottime); + +#endif /* __NM_TIME_UTILS_H__ */ diff --git a/shared/nm-utils/tests/test-shared-general.c b/shared/nm-utils/tests/test-shared-general.c new file mode 100644 index 0000000000..f186e423b8 --- /dev/null +++ b/shared/nm-utils/tests/test-shared-general.c @@ -0,0 +1,76 @@ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2018 Red Hat, Inc. + */ + +#define NM_TEST_UTILS_NO_LIBNM 1 + +#include "nm-default.h" + +#include "nm-utils/nm-time-utils.h" +#include "nm-utils/nm-random-utils.h" + +#include "nm-utils/nm-test-utils.h" + +/*****************************************************************************/ + +static int _monotonic_timestamp_initialized; + +void +_nm_utils_monotonic_timestamp_initialized (const struct timespec *tp, + gint64 offset_sec, + gboolean is_boottime) +{ + g_assert (!_monotonic_timestamp_initialized); + _monotonic_timestamp_initialized = 1; +} + +/*****************************************************************************/ + +static void +test_monotonic_timestamp (void) +{ + g_assert (nm_utils_get_monotonic_timestamp_s () > 0); + g_assert (_monotonic_timestamp_initialized); +} + +/*****************************************************************************/ + +static void +test_nmhash (void) +{ + int rnd; + + nm_utils_random_bytes (&rnd, sizeof (rnd)); + + g_assert (nm_hash_val (555, 4) != 0); +} + +/*****************************************************************************/ + +NMTST_DEFINE (); + +int main (int argc, char **argv) +{ + nmtst_init (&argc, &argv, TRUE); + + g_test_add_func ("/general/test_monotonic_timestamp", test_monotonic_timestamp); + g_test_add_func ("/general/test_nmhash", test_nmhash); + + return g_test_run (); +} + diff --git a/src/meson.build b/src/meson.build index 87c576714d..f293f299b5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -45,6 +45,8 @@ sources = files( 'nm-logging.c' ) +sources += shared_files_time_utils + deps = [ libsystemd_dep, libudev_dep, diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 99fa8c9523..50f127c090 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -1879,173 +1879,6 @@ nm_utils_cmp_connection_by_autoconnect_priority (NMConnection *a, NMConnection * /*****************************************************************************/ -static gint64 monotonic_timestamp_offset_sec; -static int monotonic_timestamp_clock_mode = 0; - -static void -monotonic_timestamp_get (struct timespec *tp) -{ - int clock_mode = 0; - int err = 0; - - switch (monotonic_timestamp_clock_mode) { - case 0: - /* the clock is not yet initialized (first run) */ - err = clock_gettime (CLOCK_BOOTTIME, tp); - if (err == -1 && errno == EINVAL) { - clock_mode = 2; - err = clock_gettime (CLOCK_MONOTONIC, tp); - } else - clock_mode = 1; - break; - case 1: - /* default, return CLOCK_BOOTTIME */ - err = clock_gettime (CLOCK_BOOTTIME, tp); - break; - case 2: - /* fallback, return CLOCK_MONOTONIC. Kernels prior to 2.6.39 - * (released on 18 May, 2011) don't support CLOCK_BOOTTIME. */ - err = clock_gettime (CLOCK_MONOTONIC, tp); - break; - } - - g_assert (err == 0); (void)err; - g_assert (tp->tv_nsec >= 0 && tp->tv_nsec < NM_UTILS_NS_PER_SECOND); - - if (G_LIKELY (clock_mode == 0)) - return; - - /* Calculate an offset for the time stamp. - * - * We always want positive values, because then we can initialize - * a timestamp with 0 and be sure, that it will be less then any - * value nm_utils_get_monotonic_timestamp_*() might return. - * For this to be true also for nm_utils_get_monotonic_timestamp_s() at - * early boot, we have to shift the timestamp to start counting at - * least from 1 second onward. - * - * Another advantage of shifting is, that this way we make use of the whole 31 bit - * range of signed int, before the time stamp for nm_utils_get_monotonic_timestamp_s() - * wraps (~68 years). - **/ - monotonic_timestamp_offset_sec = (- ((gint64) tp->tv_sec)) + 1; - monotonic_timestamp_clock_mode = clock_mode; - - if (nm_logging_enabled (LOGL_DEBUG, LOGD_CORE)) { - time_t now = time (NULL); - struct tm tm; - char s[255]; - - strftime (s, sizeof (s), "%Y-%m-%d %H:%M:%S", localtime_r (&now, &tm)); - nm_log_dbg (LOGD_CORE, "monotonic timestamp started counting 1.%09ld seconds ago with " - "an offset of %lld.0 seconds to %s (local time is %s)", - tp->tv_nsec, (long long) -monotonic_timestamp_offset_sec, - clock_mode == 1 ? "CLOCK_BOOTTIME" : "CLOCK_MONOTONIC", s); - } -} - -/** - * nm_utils_get_monotonic_timestamp_ns: - * - * Returns: a monotonically increasing time stamp in nanoseconds, - * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. - * - * The returned value will start counting at an undefined point - * in the past and will always be positive. - * - * All the nm_utils_get_monotonic_timestamp_*s functions return the same - * timestamp but in different scales (nsec, usec, msec, sec). - **/ -gint64 -nm_utils_get_monotonic_timestamp_ns (void) -{ - struct timespec tp = { 0 }; - - monotonic_timestamp_get (&tp); - - /* Although the result will always be positive, we return a signed - * integer, which makes it easier to calculate time differences (when - * you want to subtract signed values). - **/ - return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * NM_UTILS_NS_PER_SECOND + - tp.tv_nsec; -} - -/** - * nm_utils_get_monotonic_timestamp_us: - * - * Returns: a monotonically increasing time stamp in microseconds, - * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. - * - * The returned value will start counting at an undefined point - * in the past and will always be positive. - * - * All the nm_utils_get_monotonic_timestamp_*s functions return the same - * timestamp but in different scales (nsec, usec, msec, sec). - **/ -gint64 -nm_utils_get_monotonic_timestamp_us (void) -{ - struct timespec tp = { 0 }; - - monotonic_timestamp_get (&tp); - - /* Although the result will always be positive, we return a signed - * integer, which makes it easier to calculate time differences (when - * you want to subtract signed values). - **/ - return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * ((gint64) G_USEC_PER_SEC) + - (tp.tv_nsec / (NM_UTILS_NS_PER_SECOND/G_USEC_PER_SEC)); -} - -/** - * nm_utils_get_monotonic_timestamp_ms: - * - * Returns: a monotonically increasing time stamp in milliseconds, - * starting at an unspecified offset. See clock_gettime(), %CLOCK_BOOTTIME. - * - * The returned value will start counting at an undefined point - * in the past and will always be positive. - * - * All the nm_utils_get_monotonic_timestamp_*s functions return the same - * timestamp but in different scales (nsec, usec, msec, sec). - **/ -gint64 -nm_utils_get_monotonic_timestamp_ms (void) -{ - struct timespec tp = { 0 }; - - monotonic_timestamp_get (&tp); - - /* Although the result will always be positive, we return a signed - * integer, which makes it easier to calculate time differences (when - * you want to subtract signed values). - **/ - return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec) * ((gint64) 1000) + - (tp.tv_nsec / (NM_UTILS_NS_PER_SECOND/1000)); -} - -/** - * nm_utils_get_monotonic_timestamp_s: - * - * Returns: nm_utils_get_monotonic_timestamp_ms() in seconds (throwing - * away sub second parts). The returned value will always be positive. - * - * This value wraps after roughly 68 years which should be fine for any - * practical purpose. - * - * All the nm_utils_get_monotonic_timestamp_*s functions return the same - * timestamp but in different scales (nsec, usec, msec, sec). - **/ -gint32 -nm_utils_get_monotonic_timestamp_s (void) -{ - struct timespec tp = { 0 }; - - monotonic_timestamp_get (&tp); - return (((gint64) tp.tv_sec) + monotonic_timestamp_offset_sec); -} - typedef struct { const char *name; @@ -2325,49 +2158,6 @@ out: g_array_free (sorted_hashes, TRUE); } -/** - * nm_utils_monotonic_timestamp_as_boottime: - * @timestamp: the monotonic-timestamp that should be converted into CLOCK_BOOTTIME. - * @timestamp_ns_per_tick: How many nano seconds make one unit of @timestamp? E.g. if - * @timestamp is in unit seconds, pass %NM_UTILS_NS_PER_SECOND; @timestamp in nano - * seconds, pass 1; @timestamp in milli seconds, pass %NM_UTILS_NS_PER_SECOND/1000; etc. - * - * Returns: the monotonic-timestamp as CLOCK_BOOTTIME, as returned by clock_gettime(). - * The unit is the same as the passed in @timestamp basd on @timestamp_ns_per_tick. - * E.g. if you passed @timestamp in as seconds, it will return boottime in seconds. - * If @timestamp is a non-positive, it returns -1. Note that a (valid) monotonic-timestamp - * is always positive. - * - * On older kernels that don't support CLOCK_BOOTTIME, the returned time is instead CLOCK_MONOTONIC. - **/ -gint64 -nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ns_per_tick) -{ - gint64 offset; - - /* only support ns-per-tick being a multiple of 10. */ - g_return_val_if_fail (timestamp_ns_per_tick == 1 - || (timestamp_ns_per_tick > 0 && - timestamp_ns_per_tick <= NM_UTILS_NS_PER_SECOND && - timestamp_ns_per_tick % 10 == 0), - -1); - - /* Check that the timestamp is in a valid range. */ - g_return_val_if_fail (timestamp >= 0, -1); - - /* if the caller didn't yet ever fetch a monotonic-timestamp, he cannot pass any meaningful - * value (because he has no idea what these timestamps would be). That would be a bug. */ - g_return_val_if_fail (monotonic_timestamp_clock_mode != 0, -1); - - /* calculate the offset of monotonic-timestamp to boottime. offset_s is <= 1. */ - offset = monotonic_timestamp_offset_sec * (NM_UTILS_NS_PER_SECOND / timestamp_ns_per_tick); - - /* check for overflow. */ - g_return_val_if_fail (offset > 0 || timestamp < G_MAXINT64 + offset, G_MAXINT64); - - return timestamp - offset; -} - #define IPV6_PROPERTY_DIR "/proc/sys/net/ipv6/conf/" #define IPV4_PROPERTY_DIR "/proc/sys/net/ipv4/conf/" G_STATIC_ASSERT (sizeof (IPV4_PROPERTY_DIR) == sizeof (IPV6_PROPERTY_DIR)); diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 30d1360a1e..2591fd28bb 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -27,6 +27,8 @@ #include "nm-connection.h" +#include "nm-utils/nm-time-utils.h" + /*****************************************************************************/ #define NM_PLATFORM_LIFETIME_PERMANENT G_MAXUINT32 @@ -240,19 +242,6 @@ void nm_utils_log_connection_diff (NMConnection *connection, const char *prefix, const char *dbus_path); -gint64 nm_utils_get_monotonic_timestamp_ns (void); -gint64 nm_utils_get_monotonic_timestamp_us (void); -gint64 nm_utils_get_monotonic_timestamp_ms (void); -gint32 nm_utils_get_monotonic_timestamp_s (void); -gint64 nm_utils_monotonic_timestamp_as_boottime (gint64 timestamp, gint64 timestamp_ticks_per_ns); - -static inline gint64 -nm_utils_get_monotonic_timestamp_ns_cached (gint64 *cache_now) -{ - return (*cache_now) - ?: (*cache_now = nm_utils_get_monotonic_timestamp_ns ()); -} - gboolean nm_utils_is_valid_path_component (const char *name); const char *NM_ASSERT_VALID_PATH_COMPONENT (const char *name); diff --git a/src/nm-logging.c b/src/nm-logging.c index 9e7d389224..328d164ba2 100644 --- a/src/nm-logging.c +++ b/src/nm-logging.c @@ -21,6 +21,8 @@ #include "nm-default.h" +#include "nm-logging.h" + #include <dlfcn.h> #include <syslog.h> #include <stdio.h> @@ -37,8 +39,8 @@ #include <systemd/sd-journal.h> #endif +#include "nm-utils/nm-time-utils.h" #include "nm-errors.h" -#include "nm-core-utils.h" /* often we have some static string where we need to know the maximum length. * _MAX_LEN() returns @max but adds a debugging assertion that @str is indeed @@ -744,6 +746,27 @@ _nm_log_impl (const char *file, /*****************************************************************************/ +void +_nm_utils_monotonic_timestamp_initialized (const struct timespec *tp, + gint64 offset_sec, + gboolean is_boottime) +{ + if (nm_logging_enabled (LOGL_DEBUG, LOGD_CORE)) { + time_t now = time (NULL); + struct tm tm; + char s[255]; + + strftime (s, sizeof (s), "%Y-%m-%d %H:%M:%S", localtime_r (&now, &tm)); + nm_log_dbg (LOGD_CORE, "monotonic timestamp started counting 1.%09ld seconds ago with " + "an offset of %lld.0 seconds to %s (local time is %s)", + tp->tv_nsec, + (long long) -offset_sec, + is_boottime ? "CLOCK_BOOTTIME" : "CLOCK_MONOTONIC", s); + } +} + +/*****************************************************************************/ + static void nm_log_handler (const char *log_domain, GLogLevelFlags level, diff --git a/src/nm-logging.h b/src/nm-logging.h index 0737bbd639..451d51c5e0 100644 --- a/src/nm-logging.h +++ b/src/nm-logging.h @@ -22,8 +22,6 @@ #ifndef __NETWORKMANAGER_LOGGING_H__ #define __NETWORKMANAGER_LOGGING_H__ -#include "nm-core-types.h" - #ifdef __NM_TEST_UTILS_H__ #error nm-test-utils.h must be included as last header #endif |