1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/**
* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2013. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/
#include "test.h"
#include <ucs/config/parser.h>
#include <ucs/config/global_opts.h>
#include <ucs/stats/stats.h>
#include <ucs/sys/sys.h>
#include <memory>
namespace ucs {
pthread_mutex_t test_base::m_logger_mutex = PTHREAD_MUTEX_INITIALIZER;
unsigned test_base::m_total_warnings = 0;
unsigned test_base::m_total_errors = 0;
std::vector<std::string> test_base::m_errors;
std::vector<std::string> test_base::m_warnings;
std::vector<std::string> test_base::m_first_warns_and_errors;
test_base::test_base() :
m_state(NEW),
m_initialized(false),
m_num_threads(1),
m_num_valgrind_errors_before(0),
m_num_errors_before(0),
m_num_warnings_before(0),
m_num_log_handlers_before(0)
{
push_config();
}
test_base::~test_base() {
while (!m_config_stack.empty()) {
pop_config();
}
ucs_assertv_always(m_state == FINISHED ||
m_state == SKIPPED ||
m_state == NEW || /* can be skipped from a class constructor */
m_state == ABORTED,
"state=%d", m_state);
}
void test_base::set_num_threads(unsigned num_threads) {
if (m_state != NEW) {
GTEST_FAIL() << "Cannot modify number of threads after test is started, "
<< "it must be done in the constructor.";
}
m_num_threads = num_threads;
}
unsigned test_base::num_threads() const {
return m_num_threads;
}
void test_base::set_config(const std::string& config_str)
{
std::string::size_type pos = config_str.find("=");
modify_config_mode_t mode;
std::string name, value;
if (config_str == "") {
return;
}
if (pos == std::string::npos) {
name = config_str;
value = "";
} else {
name = config_str.substr(0, pos);
value = config_str.substr(pos + 1);
}
mode = FAIL_IF_NOT_EXIST;
/*
* What happens if NAME is not a valid configuration key?
* - "NAME=VALUE" : fail the test
* - "NAME?=VALUE" : set UCX_NAME environment variable
* - "NAME~=VALUE" : skip the test
*/
if (name.length() > 0) {
char modifier = name.at(name.length() - 1);
bool valid_modifier;
switch (modifier) {
case '?':
mode = SETENV_IF_NOT_EXIST;
valid_modifier = true;
break;
case '~':
mode = SKIP_IF_NOT_EXIST;
valid_modifier = true;
break;
default:
valid_modifier = false;
break;
}
if (valid_modifier) {
name = name.substr(0, name.length() - 1);
}
}
modify_config(name, value, mode);
}
void test_base::modify_config(const std::string& name,
const std::string& value,
modify_config_mode_t mode)
{
ucs_status_t status = ucs_global_opts_set_value(name.c_str(),
value.c_str());
if (status == UCS_ERR_NO_ELEM) {
switch (mode) {
case FAIL_IF_NOT_EXIST:
UCS_TEST_ABORT("Invalid UCS configuration for " << name << " : "
<< value << ", error message: "
<< ucs_status_string(status) << "(" << status << ")");
case SETENV_IF_NOT_EXIST:
m_env_stack.push_back(new scoped_setenv(("UCX_" + name).c_str(),
value.c_str()));
break;
case SKIP_IF_NOT_EXIST:
UCS_TEST_SKIP_R(name + " is not a valid configuration");
case IGNORE_IF_NOT_EXIST:
break;
}
} else {
ASSERT_UCS_OK(status);
}
}
void test_base::push_config()
{
ucs_global_opts_t new_opts;
/* save current options to the vector
* it is important to keep the first original global options at the first
* vector element to release it at the end. Otherwise, memtrack will not work
*/
m_config_stack.push_back(ucs_global_opts);
ucs_global_opts_clone(&new_opts);
ucs_global_opts = new_opts;
}
void test_base::pop_config()
{
ucs_global_opts_release();
ucs_global_opts = m_config_stack.back();
m_config_stack.pop_back();
}
void test_base::stats_activate()
{
#ifdef ENABLE_STATS
ucs_stats_cleanup();
push_config();
modify_config("STATS_DEST", "file:/dev/null");
modify_config("STATS_TRIGGER", "exit");
ucs_stats_init();
ASSERT_TRUE(ucs_stats_is_active());
#endif
}
void test_base::stats_restore()
{
#ifdef ENABLE_STATS
ucs_stats_cleanup();
pop_config();
ucs_stats_init();
#endif
}
ucs_log_func_rc_t
test_base::count_warns_logger(const char *file, unsigned line, const char *function,
ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
pthread_mutex_lock(&m_logger_mutex);
if (level == UCS_LOG_LEVEL_ERROR) {
++m_total_errors;
} else if (level == UCS_LOG_LEVEL_WARN) {
++m_total_warnings;
}
if ((level <= UCS_LOG_LEVEL_WARN) &&
(m_first_warns_and_errors.size() < 5)) {
/* Save the first few errors/warnings which cause the test to fail */
va_list ap2;
va_copy(ap2, ap);
std::stringstream ss;
ss << file << ":" << line << " " << format_message(message, ap2);
va_end(ap2);
m_first_warns_and_errors.push_back(ss.str());
}
pthread_mutex_unlock(&m_logger_mutex);
return UCS_LOG_FUNC_RC_CONTINUE;
}
std::string test_base::format_message(const char *message, va_list ap)
{
const size_t buffer_size = ucs_log_get_buffer_size();
std::string buf(buffer_size, '\0');
vsnprintf(&buf[0], buffer_size, message, ap);
buf.resize(strlen(buf.c_str()));
return buf;
}
void test_base::push_debug_message_with_limit(std::vector<std::string>& vec,
const std::string& message,
const size_t limit) {
if (vec.size() >= limit) {
UCS_TEST_ABORT("aborting after " + ucs::to_string(vec.size()) +
" error messages (" + message + ")");
}
vec.push_back(message);
}
ucs_log_func_rc_t
test_base::common_logger(ucs_log_level_t log_level_to_handle, bool print,
std::vector<std::string> &messages_vec, size_t limit,
const char *file, unsigned line, const char *function,
ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
if (level != log_level_to_handle) {
return UCS_LOG_FUNC_RC_CONTINUE;
}
// dump the formatted message to a stringstream
va_list ap2;
va_copy(ap2, ap);
std::istringstream iss(format_message(message, ap2));
va_end(ap2);
// save each line of the message to messages_vec, and print it if reqeusted
pthread_mutex_lock(&m_logger_mutex);
std::string message_line;
while (getline(iss, message_line, '\n')) {
push_debug_message_with_limit(messages_vec, message_line, limit);
if (print) {
UCS_TEST_MESSAGE << "< " << message_line << " >";
}
}
pthread_mutex_unlock(&m_logger_mutex);
// if the message was not printed, pass it to default handler in debug level
if (!print) {
ucs_log_default_handler(file, line, function, UCS_LOG_LEVEL_DEBUG,
comp_conf, message, ap);
}
return UCS_LOG_FUNC_RC_STOP;
}
ucs_log_func_rc_t
test_base::hide_errors_logger(const char *file, unsigned line,
const char *function, ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
return common_logger(UCS_LOG_LEVEL_ERROR, false, m_errors,
std::numeric_limits<size_t>::max(), file, line,
function, level, comp_conf, message, ap);
}
ucs_log_func_rc_t
test_base::hide_warns_logger(const char *file, unsigned line,
const char *function, ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
return common_logger(UCS_LOG_LEVEL_WARN, false, m_warnings,
std::numeric_limits<size_t>::max(), file, line,
function, level, comp_conf, message, ap);
}
ucs_log_func_rc_t
test_base::wrap_errors_logger(const char *file, unsigned line,
const char *function, ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
return common_logger(UCS_LOG_LEVEL_ERROR, true, m_errors, 1000, file, line,
function, level, comp_conf, message, ap);
}
ucs_log_func_rc_t
test_base::wrap_warns_logger(const char *file, unsigned line,
const char *function, ucs_log_level_t level,
const ucs_log_component_config_t *comp_conf,
const char *message, va_list ap)
{
return common_logger(UCS_LOG_LEVEL_WARN, true, m_warnings, 1000, file, line,
function, level, comp_conf, message, ap);
}
unsigned test_base::num_errors()
{
return m_total_errors - m_num_errors_before;
}
unsigned test_base::num_warnings()
{
return m_total_warnings - m_num_warnings_before;
}
void test_base::SetUpProxy() {
ucs_assert(m_state == NEW);
m_num_valgrind_errors_before = VALGRIND_COUNT_ERRORS;
m_num_warnings_before = m_total_warnings;
m_num_errors_before = m_total_errors;
m_errors.clear();
m_warnings.clear();
m_first_warns_and_errors.clear();
m_num_log_handlers_before = ucs_log_num_handlers();
ucs_log_push_handler(count_warns_logger);
try {
check_skip_test();
init();
m_initialized = true;
m_state = RUNNING;
} catch (test_skip_exception& e) {
skipped(e);
} catch (test_abort_exception&) {
m_state = ABORTED;
}
}
void test_base::TearDownProxy() {
ucs_assertv_always(m_state == FINISHED ||
m_state == SKIPPED ||
m_state == ABORTED,
"state=%d", m_state);
watchdog_signal();
if (m_initialized) {
cleanup();
}
m_errors.clear();
ucs_assert(ucs_log_get_current_indent() == 0);
if (ucs_log_num_handlers() > m_num_log_handlers_before) {
ucs_log_pop_handler();
}
unsigned num_not_removed = ucs_log_num_handlers() - m_num_log_handlers_before;
if (num_not_removed != 0) {
ADD_FAILURE() << num_not_removed << " log handlers were not removed";
}
int num_valgrind_errors = VALGRIND_COUNT_ERRORS - m_num_valgrind_errors_before;
if (num_valgrind_errors > 0) {
ADD_FAILURE() << "Got " << num_valgrind_errors << " valgrind errors during the test";
}
if ((num_errors() > 0) || (num_warnings() > 0)) {
ADD_FAILURE() << "Got " << num_errors() << " errors "
<< "and " << num_warnings() << " warnings "
<< "during the test";
for (size_t i = 0; i < m_first_warns_and_errors.size(); ++i) {
UCS_TEST_MESSAGE << "< " << m_first_warns_and_errors[i] << " >";
}
}
}
void test_base::run()
{
if (num_threads() == 1) {
test_body();
} else {
pthread_t threads[num_threads()];
pthread_barrier_init(&m_barrier, NULL, num_threads());
for (unsigned i = 0; i < num_threads(); ++i) {
pthread_create(&threads[i], NULL, thread_func, reinterpret_cast<void*>(this));
}
for (unsigned i = 0; i < num_threads(); ++i) {
void *retval;
pthread_join(threads[i], &retval);
}
pthread_barrier_destroy(&m_barrier);
}
}
void *test_base::thread_func(void *arg)
{
test_base *self = reinterpret_cast<test_base*>(arg);
self->barrier(); /* Let all threads start in the same time */
self->test_body();
return NULL;
}
void test_base::TestBodyProxy() {
if (m_state == RUNNING) {
try {
run();
m_state = FINISHED;
} catch (test_skip_exception& e) {
skipped(e);
} catch (test_abort_exception&) {
m_state = ABORTED;
} catch (exit_exception& e) {
if (RUNNING_ON_VALGRIND) {
/* When running with valgrind, exec true/false instead of just
* exiting, to avoid warnings about memory leaks of objects
* allocated inside gtest run loop.
*/
const char *program = e.failed() ? "false" : "true";
execlp(program, program, NULL);
}
/* If not running on valgrind / execp failed, use exit() */
exit(e.failed() ? 1 : 0);
} catch (...) {
m_state = ABORTED;
throw;
}
}
}
void test_base::skipped(const test_skip_exception& e) {
std::string reason = e.what();
if (reason.empty()) {
detail::message_stream("SKIP");
} else {
detail::message_stream("SKIP") << "(" << reason << ")";
}
m_state = SKIPPED;
skipped_tests.insert(::testing::UnitTest::
GetInstance()->current_test_info());
}
void test_base::init() {
}
void test_base::cleanup() {
}
bool test_base::barrier() {
if (num_threads() <= 1) {
return true;
}
int ret = pthread_barrier_wait(&m_barrier);
if (ret == 0) {
return false;
} else if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
return true;
} else {
UCS_TEST_ABORT("pthread_barrier_wait() failed");
}
}
static void clear_dontcopy_regions_vma_cb(ucs_sys_vma_info_t *info, void *ctx) {
int ret;
if (info->flags & UCS_SYS_VMA_FLAG_DONTCOPY) {
ret = madvise((void*)info->start, info->end - info->start, MADV_DOFORK);
if (ret != 0) {
UCS_TEST_MESSAGE << "madvise(DOFORK) failed, errno: " << errno
<< std::hex << " 0x" << info->start
<< "-0x" << info->end;
}
}
}
clear_dontcopy_regions::clear_dontcopy_regions() {
ucs_sys_iterate_vm(NULL, SIZE_MAX, clear_dontcopy_regions_vma_cb, NULL);
}
}
|