From 4ed4c358d909c1ce660ebaf79d01093af7857aec Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 21 Nov 2019 14:37:07 +0000 Subject: Reduce the number of different ways to get the time. --- .../src/tests/util/statebuf/statebuf_test.cpp | 28 ++------ searchlib/src/vespa/searchlib/util/statebuf.cpp | 62 +++-------------- searchlib/src/vespa/searchlib/util/statebuf.h | 78 ++++++---------------- 3 files changed, 36 insertions(+), 132 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/tests/util/statebuf/statebuf_test.cpp b/searchlib/src/tests/util/statebuf/statebuf_test.cpp index 40abf942fa1..0b651a8c13c 100644 --- a/searchlib/src/tests/util/statebuf/statebuf_test.cpp +++ b/searchlib/src/tests/util/statebuf/statebuf_test.cpp @@ -1,19 +1,13 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include -LOG_SETUP("statebuf_test"); + #include #include #include -#include - -namespace search -{ - -namespace -{ +#include +LOG_SETUP("statebuf_test"); -} +namespace search { class Fixture : public StateBuf { @@ -39,12 +33,6 @@ TEST_F("strings can be appended to stream", Fixture) EXPECT_EQUAL("Hello world", f.str()); } -TEST_F("quoted strings can be appended to stream", Fixture) -{ - f.appendQuoted("This is a quoting test, \\ \" \n oops"); - EXPECT_EQUAL("\"This is a quoting test, \\\\ \\\" \\n oops\"", f.str()); -} - TEST_F("keys can be appended to stream", Fixture) { (f.appendKey("foo") << "fooval").appendKey("bar") << "barval"; @@ -66,18 +54,14 @@ TEST_F("negative integers can be appended to stream", Fixture) TEST_F("struct timespec can be appended to stream", Fixture) { - struct timespec ts; - ts.tv_sec = 15; - ts.tv_nsec = 256; + std::chrono::nanoseconds ts(15*1000000000l + 256); f << ts; EXPECT_EQUAL("15.000000256", f.str()); } TEST_F("timestamp can be appended to stream", Fixture) { - struct timespec ts; - ts.tv_sec = 16; - ts.tv_nsec = 257; + std::chrono::nanoseconds ts(16*1000000000l + 257); f.appendTimestamp(ts); EXPECT_EQUAL("ts=16.000000257", f.str()); } diff --git a/searchlib/src/vespa/searchlib/util/statebuf.cpp b/searchlib/src/vespa/searchlib/util/statebuf.cpp index af1c7dda30d..d97dccebaf0 100644 --- a/searchlib/src/vespa/searchlib/util/statebuf.cpp +++ b/searchlib/src/vespa/searchlib/util/statebuf.cpp @@ -3,6 +3,7 @@ #include "statebuf.h" #include + LOG_SETUP(".searchlib.util.statebuf"); @@ -36,30 +37,6 @@ StateBuf::operator<<(const char *s) noexcept } -StateBuf & -StateBuf::appendQuoted(const char *s) noexcept -{ - *this << '"'; - for (const char *p = s; *p != '\0'; ++p) { - switch (*p) { - case '\\': - *this << '\\' << '\\'; - break; - case '\n': - *this << '\\' << 'n'; - break; - case '"': - *this << '\\' << '"'; - break; - default: - *this << *p; - } - } - *this << '"'; - return *this; -} - - StateBuf & StateBuf::appendKey(const char *s) noexcept { @@ -168,18 +145,20 @@ StateBuf::appendHex(unsigned long val) noexcept StateBuf & -StateBuf::operator<<(const struct timespec &ts) noexcept +StateBuf::operator<<(std::chrono::nanoseconds ns) noexcept { - (*this << static_cast(ts.tv_sec) << '.'). - appendDecFraction(static_cast(ts.tv_nsec), 9); + std::chrono::seconds sec = std::chrono::duration_cast(ns); + std::chrono::nanoseconds remainder = (ns - sec); + (*this << static_cast(sec.count()) << '.'). + appendDecFraction(static_cast(remainder.count()), 9); return *this; } StateBuf & -StateBuf::appendTimestamp(const struct timespec &ts) noexcept +StateBuf::appendTimestamp(std::chrono::nanoseconds ns) noexcept { - appendKey("ts") << ts; + appendKey("ts") << ns; return *this; } @@ -187,16 +166,7 @@ StateBuf::appendTimestamp(const struct timespec &ts) noexcept StateBuf & StateBuf::appendTimestamp() noexcept { - struct timespec ts; - /* - * clock_gettime() is supposed to be async signal safe. - * gettimeofday() is not documented to be async signal safe. - */ - int gtres = clock_gettime(CLOCK_REALTIME, &ts); - if (gtres != 0) { - LOG_ABORT("should not be reached"); - } - appendTimestamp(ts); + appendTimestamp(std::chrono::system_clock::now().time_since_epoch()); return *this; } @@ -210,20 +180,6 @@ StateBuf::appendAddr(void *addr) noexcept } -size_t -StateBuf::size() const noexcept -{ - return _cur - _start; -}; - - -const char * -StateBuf::base() const noexcept -{ - return _start; -} - - std::string StateBuf::str() const { diff --git a/searchlib/src/vespa/searchlib/util/statebuf.h b/searchlib/src/vespa/searchlib/util/statebuf.h index 3dbf8beaf16..b16783cee64 100644 --- a/searchlib/src/vespa/searchlib/util/statebuf.h +++ b/searchlib/src/vespa/searchlib/util/statebuf.h @@ -3,9 +3,9 @@ #pragma once #include +#include -namespace search -{ +namespace search { /** * Class used to serialize application state in a mostly safe manner. @@ -20,13 +20,12 @@ class StateBuf char *_cur; char *_end; - void - overflow() noexcept __attribute__((__noinline__, __noreturn__)); + static void overflow() noexcept __attribute__((__noinline__, __noreturn__)); public: StateBuf(void *buf, size_t bufLen) noexcept; - inline StateBuf & + StateBuf & operator<<(char c) noexcept __attribute__((__always_inline__)) { if (__builtin_expect(_cur != _end, true)) { @@ -37,62 +36,27 @@ public: } - StateBuf & - operator<<(const char *s) noexcept; - - StateBuf & - appendQuoted(const char *s) noexcept; - - StateBuf & - appendKey(const char *s) noexcept; - - StateBuf & - operator<<(const struct timespec &ts) noexcept; - - StateBuf & - appendTimestamp(const struct timespec &ts) noexcept; - - StateBuf & - appendTimestamp() noexcept; - - StateBuf & - appendAddr(void *addr) noexcept; - - StateBuf & - operator<<(unsigned long long val) noexcept; - - StateBuf & - operator<<(long long val) noexcept; - - StateBuf & - operator<<(unsigned long val) noexcept; - - StateBuf & - operator<<(long val) noexcept; - - StateBuf & - operator<<(unsigned int val) noexcept; - - StateBuf & - operator<<(int val) noexcept; - - StateBuf & - appendDecFraction(unsigned long val, unsigned int width) noexcept; - - StateBuf & - appendHex(unsigned long val) noexcept; - - size_t - size() const noexcept; - - const char * - base() const noexcept; + StateBuf & operator<<(const char *s) noexcept; + StateBuf & appendKey(const char *s) noexcept; + StateBuf & operator<<(std::chrono::nanoseconds ns) noexcept; + StateBuf & appendTimestamp(std::chrono::nanoseconds ns) noexcept; + StateBuf & appendTimestamp() noexcept; + StateBuf & appendAddr(void *addr) noexcept; + StateBuf & operator<<(unsigned long long val) noexcept; + StateBuf & operator<<(long long val) noexcept; + StateBuf & operator<<(unsigned long val) noexcept; + StateBuf & operator<<(long val) noexcept; + StateBuf & operator<<(unsigned int val) noexcept; + StateBuf & operator<<(int val) noexcept; + StateBuf & appendDecFraction(unsigned long val, unsigned int width) noexcept; + StateBuf & appendHex(unsigned long val) noexcept; + size_t size() const noexcept { return _cur - _start; } + const char * base() const noexcept { return _start; } /* * Unit test helper methods. */ - std::string - str() const; + std::string str() const; }; } -- cgit v1.2.3