diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2018-12-07 14:14:23 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2018-12-07 14:14:23 +0000 |
commit | 5ebc5712811b7af0ea6db09b5fb89ce2cf4d17f4 (patch) | |
tree | 5fbd6645d51dba42640c89ad2a3222133ac89966 /staging_vespalib | |
parent | 66b2121207b024b00f9663c4f5ddb2b9fd25951d (diff) |
Use a directory that the vespa user has control over.
Also add vespa version to the assert key file name.
Diffstat (limited to 'staging_vespalib')
-rw-r--r-- | staging_vespalib/src/tests/assert/assert_test.cpp | 19 | ||||
-rw-r--r-- | staging_vespalib/src/tests/assert/asserter.cpp | 9 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/assert.cpp | 63 | ||||
-rw-r--r-- | staging_vespalib/src/vespa/vespalib/util/assert.h | 9 |
4 files changed, 70 insertions, 30 deletions
diff --git a/staging_vespalib/src/tests/assert/assert_test.cpp b/staging_vespalib/src/tests/assert/assert_test.cpp index 4ca962a746c..454c0957974 100644 --- a/staging_vespalib/src/tests/assert/assert_test.cpp +++ b/staging_vespalib/src/tests/assert/assert_test.cpp @@ -2,7 +2,8 @@ #include <vespa/vespalib/testkit/test_kit.h> #include <vespa/vespalib/util/slaveproc.h> #include <vespa/vespalib/util/stringfmt.h> -#include <vespa/fastos/file.h> +#include <vespa/vespalib/util/assert.h> +#include <vespa/vespalib/io/fileutil.h> #include <sys/stat.h> #include <unistd.h> #include <vespa/defaults.h> @@ -10,9 +11,10 @@ using namespace vespalib; TEST("that it borks the first time.") { - vespalib::string assertName = make_string("tmp/myassert.assert.%s", vespa::Defaults::vespaUser()); - FastOS_File::EmptyAndRemoveDirectory("tmp"); - ASSERT_EQUAL(0, mkdir("tmp", 0755)); + std::string assertName; + const char * assertDir = "var/db/vespa/tmp"; + vespalib::rmdir("var", true); + ASSERT_TRUE(vespalib::mkdir(assertDir, true)); { SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000"); proc.wait(); @@ -20,11 +22,18 @@ TEST("that it borks the first time.") { } { SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000"); + proc.readLine(assertName); proc.wait(); ASSERT_EQUAL(proc.getExitCode() & 0x7f, 0); } ASSERT_EQUAL(0, unlink(assertName.c_str())); - ASSERT_EQUAL(0, rmdir("tmp")); + { + SlaveProc proc("ulimit -c 0 && exec env VESPA_HOME=./ ./staging_vespalib_asserter_app myassert 10000"); + proc.wait(); + ASSERT_EQUAL(proc.getExitCode() & 0x7f, 6); + } + ASSERT_EQUAL(0, unlink(assertName.c_str())); + ASSERT_TRUE(vespalib::rmdir("var", true)); } TEST_MAIN_WITH_PROCESS_PROXY() { TEST_RUN_ALL(); } diff --git a/staging_vespalib/src/tests/assert/asserter.cpp b/staging_vespalib/src/tests/assert/asserter.cpp index da9de5eb099..640464889c0 100644 --- a/staging_vespalib/src/tests/assert/asserter.cpp +++ b/staging_vespalib/src/tests/assert/asserter.cpp @@ -2,6 +2,8 @@ #include <vespa/vespalib/util/assert.h> #include <cassert> #include <cstdlib> +#include <fstream> +#include <string> int main(int argc, char *argv[]) { assert(argc == 3); @@ -11,6 +13,13 @@ int main(int argc, char *argv[]) { ASSERT_ONCE_OR_LOG(true, assertKey, 100); ASSERT_ONCE_OR_LOG(false, assertKey, 100); } + std::string filename = vespalib::assert::getAssertLogFileName(assertKey); + std::ifstream is(filename.c_str()); + assert(is); + std::string line; + std::getline(is, line); + printf("%s\n", filename.c_str()); + assert(line.find(assertKey) != std::string::npos); assert(assertCount == vespalib::assert::getNumAsserts(assertKey)); return 0; } diff --git a/staging_vespalib/src/vespa/vespalib/util/assert.cpp b/staging_vespalib/src/vespa/vespalib/util/assert.cpp index fe3f66713b2..0482d873f7f 100644 --- a/staging_vespalib/src/vespa/vespalib/util/assert.cpp +++ b/staging_vespalib/src/vespa/vespalib/util/assert.cpp @@ -3,6 +3,8 @@ #include "assert.h" #include <vespa/defaults.h> #include <vespa/vespalib/util/backtrace.h> +#include <vespa/vespalib/util/stringfmt.h> +#include <vespa/vespalib/component/vtag.h> #include <fstream> #include <map> #include <mutex> @@ -21,41 +23,54 @@ std::map<std::string, size_t> _G_assertMap; } -size_t getNumAsserts(const char *key) +size_t +getNumAsserts(const char *key) { std::lock_guard guard(_G_lock); return _G_assertMap[key]; } -void assertOnceOrLog(const char *expr, const char *key, size_t freq) +vespalib::string +getAssertLogFileName(const char *key) { - std::string relativePath("tmp/"); - relativePath += key; - relativePath += ".assert."; - relativePath += vespa::Defaults::vespaUser(); - std::string rememberAssert = vespa::Defaults::underVespaHome(relativePath.c_str()); - std::ifstream prevAssertFile(rememberAssert.c_str()); - if (prevAssertFile) { - size_t count(0); - { - std::lock_guard guard(_G_lock); - count = _G_assertMap[key]++; - } + vespalib::string relative = make_string("var/db/vespa/tmp/%s.%s.assert", key, Vtag::currentVersion.toString().c_str()); + return vespa::Defaults::underVespaHome(relative.c_str()); +} + +void +assertOnceOrLog(const char *expr, const char *key, size_t freq) +{ + size_t count(0); + { + std::lock_guard guard(_G_lock); + count = _G_assertMap[key]++; + } + if (count) { if ((count % freq) == 0) { LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s", - expr, key, count+1, vespalib::getStackTrace(0).c_str()); + expr, key, count+1, vespalib::getStackTrace(0).c_str()); } } else { - { - LOG(error, "assert(%s) named '%s' failed first time. Stacktrace = %s", - expr, key, vespalib::getStackTrace(0).c_str()); - std::ofstream assertStream(rememberAssert.c_str()); - std::chrono::time_point now = std::chrono::system_clock::now(); - std::time_t now_c = std::chrono::system_clock::to_time_t(now); - assertStream << std::put_time(std::gmtime(&now_c), "%F %T") << " assert(" << expr << ") failed" << std::endl; - assertStream.close(); + std::string rememberAssert = getAssertLogFileName(key); + std::ifstream prevAssertFile(rememberAssert.c_str()); + if (prevAssertFile) { + if ((count % freq) == 0) { + LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s", + expr, key, count + 1, vespalib::getStackTrace(0).c_str()); + } + } else { + { + LOG(error, "assert(%s) named '%s' failed first time. Stacktrace = %s", + expr, key, vespalib::getStackTrace(0).c_str()); + std::ofstream assertStream(rememberAssert.c_str()); + std::chrono::time_point now = std::chrono::system_clock::now(); + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + assertStream << std::put_time(std::gmtime(&now_c), "%F %T") << " assert(" << expr + << ") named " << key << " failed" << std::endl; + assertStream.close(); + } + abort(); } - abort(); } } diff --git a/staging_vespalib/src/vespa/vespalib/util/assert.h b/staging_vespalib/src/vespa/vespalib/util/assert.h index c59d51fb784..698fa6774c1 100644 --- a/staging_vespalib/src/vespa/vespalib/util/assert.h +++ b/staging_vespalib/src/vespa/vespalib/util/assert.h @@ -2,7 +2,7 @@ #pragma once -#include <cstddef> +#include <vespa/vespalib/stllike/string.h> namespace vespalib::assert { @@ -14,6 +14,13 @@ namespace vespalib::assert { size_t getNumAsserts(const char *key); /** + * Get the filename that will be used for remembering asserts. + * @param key + * @return + */ +vespalib::string getAssertLogFileName(const char *key); + +/** * If there is no record on file that this assert has failed, it will be recorded and aborted. * However if there is a record of it, it will merely be logged the first and then every #freq time. * @param expr that failed the assert |