ClickHouse/dbms/src/DataStreams/IProfilingBlockInputStream.cpp

212 lines
7.0 KiB
C++
Raw Normal View History

2011-08-27 22:43:31 +00:00
#include <iomanip>
2012-02-27 07:54:16 +00:00
/*#include <Poco/Mutex.h>
#include <Poco/Ext/ThreadNumber.h>*/
2011-09-04 21:23:19 +00:00
#include <DB/DataStreams/IProfilingBlockInputStream.h>
2011-08-27 22:43:31 +00:00
namespace DB
{
2012-05-17 19:15:53 +00:00
void BlockStreamProfileInfo::update(Block & block)
2011-08-27 22:43:31 +00:00
{
++blocks;
rows += block.rows();
2012-05-17 19:15:53 +00:00
bytes += block.bytes();
2011-09-05 00:51:25 +00:00
if (column_names.empty())
column_names = block.dumpNames();
2011-08-27 22:43:31 +00:00
}
void BlockStreamProfileInfo::print(std::ostream & ostr) const
{
UInt64 elapsed = work_stopwatch.elapsed();
UInt64 nested_elapsed = 0;
double elapsed_seconds = work_stopwatch.elapsedSeconds();
double nested_elapsed_seconds = 0;
2011-09-26 01:50:32 +00:00
UInt64 nested_rows = 0;
UInt64 nested_blocks = 0;
UInt64 nested_bytes = 0;
if (!nested_infos.empty())
{
for (BlockStreamProfileInfos::const_iterator it = nested_infos.begin(); it != nested_infos.end(); ++it)
{
if ((*it)->work_stopwatch.elapsed() > nested_elapsed)
{
2011-09-26 01:50:32 +00:00
nested_elapsed = (*it)->work_stopwatch.elapsed();
nested_elapsed_seconds = (*it)->work_stopwatch.elapsedSeconds();
}
2011-09-26 01:50:32 +00:00
nested_rows += (*it)->rows;
nested_blocks += (*it)->blocks;
nested_bytes += (*it)->bytes;
}
}
ostr << std::fixed << std::setprecision(2)
<< "Columns: " << column_names << std::endl
<< "Elapsed: " << elapsed_seconds << " sec. "
<< "(" << elapsed * 100.0 / total_stopwatch.elapsed() << "%), " << std::endl;
2011-09-26 01:50:32 +00:00
if (!nested_infos.empty())
2011-09-26 04:26:22 +00:00
{
double self_percents = (elapsed - nested_elapsed) * 100.0 / total_stopwatch.elapsed();
2011-09-26 04:26:22 +00:00
ostr<< "Elapsed (self): " << (elapsed_seconds - nested_elapsed_seconds) << " sec. "
2011-09-26 04:26:22 +00:00
<< "(" << (self_percents >= 50 ? "\033[1;31m" : (self_percents >= 10 ? "\033[1;33m" : "")) /// Раскраска больших значений
<< self_percents << "%"
<< (self_percents >= 10 ? "\033[0m" : "") << "), " << std::endl
<< "Rows (in): " << nested_rows << ", per second: " << nested_rows / elapsed_seconds << ", " << std::endl
<< "Blocks (in): " << nested_blocks << ", per second: " << nested_blocks / elapsed_seconds << ", " << std::endl
2011-09-26 01:50:32 +00:00
<< " " << nested_bytes / 1000000.0 << " MB (memory), "
<< nested_bytes * 1000 / elapsed << " MB/s (memory), " << std::endl;
2011-09-28 06:56:22 +00:00
if (self_percents > 0.1)
ostr << "Rows per second (in, self): " << (nested_rows / (elapsed_seconds - nested_elapsed_seconds))
<< ", " << (elapsed - nested_elapsed) / nested_rows << " ns/row, " << std::endl;
2011-09-26 04:26:22 +00:00
}
2011-09-26 01:50:32 +00:00
ostr << "Rows (out): " << rows << ", per second: " << rows / elapsed_seconds << ", " << std::endl
<< "Blocks (out): " << blocks << ", per second: " << blocks / elapsed_seconds << ", " << std::endl
<< " " << bytes / 1000000.0 << " MB (memory), " << bytes * 1000 / elapsed << " MB/s (memory), " << std::endl
2011-09-26 01:50:32 +00:00
<< "Average block size (out): " << rows / blocks << "." << std::endl;
2011-08-27 22:43:31 +00:00
}
2012-08-23 23:49:28 +00:00
2011-09-04 21:23:19 +00:00
Block IProfilingBlockInputStream::read()
2011-08-27 22:43:31 +00:00
{
if (!info.started)
2011-09-26 01:50:32 +00:00
{
2011-08-27 22:43:31 +00:00
info.total_stopwatch.start();
2011-09-26 01:50:32 +00:00
for (BlockInputStreams::const_iterator it = children.begin(); it != children.end(); ++it)
2012-05-09 08:16:09 +00:00
if (const IProfilingBlockInputStream * child = dynamic_cast<const IProfilingBlockInputStream *>(&**it))
info.nested_infos.push_back(&child->info);
2011-09-26 01:50:32 +00:00
info.started = true;
}
2012-05-09 08:16:09 +00:00
2012-10-20 05:54:35 +00:00
if (is_cancelled)
2012-05-09 08:16:09 +00:00
return Block();
2012-05-31 00:47:05 +00:00
2011-08-27 22:43:31 +00:00
info.work_stopwatch.start();
2011-09-04 21:23:19 +00:00
Block res = readImpl();
2011-08-27 22:43:31 +00:00
info.work_stopwatch.stop();
2011-09-25 05:07:47 +00:00
/* if (res)
2011-09-25 03:37:09 +00:00
{
2012-02-27 07:54:16 +00:00
static Poco::FastMutex mutex;
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
2012-03-05 07:58:34 +00:00
2011-09-25 03:37:09 +00:00
std::cerr << std::endl;
2012-06-24 23:17:06 +00:00
std::cerr << "[ " << Poco::ThreadNumber::get() << " ]\t" << getShortName() << std::endl;
2012-03-05 07:58:34 +00:00
std::cerr << "[ " << Poco::ThreadNumber::get() << " ]\t";
for (size_t i = 0; i < res.columns(); ++i)
{
if (i != 0)
std::cerr << ", ";
std::cerr << res.getByPosition(i).name << " (" << res.getByPosition(i).column->size() << ")";
}
std::cerr << std::endl;
2011-09-25 05:07:47 +00:00
}*/
2012-05-09 15:15:45 +00:00
2012-03-05 07:58:34 +00:00
if (res)
2012-05-17 19:15:53 +00:00
info.update(res);
else
{
/** Если поток закончился, то ещё попросим всех детей прервать выполнение.
* Это имеет смысл при выполнении запроса с LIMIT-ом:
* - бывает ситуация, когда все необходимые данные уже прочитали,
* но источники-дети ещё продолжают работать,
* при чём они могут работать в отдельных потоках или даже удалённо.
*/
cancel();
}
2011-09-25 03:37:09 +00:00
2012-05-17 19:15:53 +00:00
progress(res);
/// Проверка ограничений.
if ((limits.max_rows_to_read && info.rows > limits.max_rows_to_read)
|| (limits.max_bytes_to_read && info.bytes > limits.max_bytes_to_read))
{
if (limits.read_overflow_mode == Limits::THROW)
throw Exception("Limit for rows to read exceeded: read " + Poco::NumberFormatter::format(info.rows)
+ " rows, maximum: " + Poco::NumberFormatter::format(limits.max_rows_to_read),
ErrorCodes::TOO_MUCH_ROWS);
if (limits.read_overflow_mode == Limits::BREAK)
return Block();
throw Exception("Logical error: unkown overflow mode", ErrorCodes::LOGICAL_ERROR);
}
if (limits.max_execution_time != 0
&& info.total_stopwatch.elapsed() > static_cast<UInt64>(limits.max_execution_time.totalMicroseconds()) * 1000)
{
if (limits.timeout_overflow_mode == Limits::THROW)
throw Exception("Timeout exceeded: elapsed " + Poco::NumberFormatter::format(info.total_stopwatch.elapsedSeconds())
+ " seconds, maximum: " + Poco::NumberFormatter::format(limits.max_execution_time.totalMicroseconds() / 1000000.0),
ErrorCodes::TIMEOUT_EXCEEDED);
if (limits.timeout_overflow_mode == Limits::BREAK)
return Block();
throw Exception("Logical error: unkown overflow mode", ErrorCodes::LOGICAL_ERROR);
}
if (limits.min_execution_speed
&& info.total_stopwatch.elapsed() > static_cast<UInt64>(limits.timeout_before_checking_execution_speed.totalMicroseconds()) * 1000
&& info.rows / info.total_stopwatch.elapsedSeconds() < limits.min_execution_speed)
{
throw Exception("Query is executing too slow: " + Poco::NumberFormatter::format(info.rows / info.total_stopwatch.elapsedSeconds())
+ " rows/sec., minimum: " + Poco::NumberFormatter::format(limits.min_execution_speed),
ErrorCodes::TOO_SLOW);
}
2012-05-17 19:15:53 +00:00
2011-08-27 22:43:31 +00:00
return res;
}
2012-05-17 19:15:53 +00:00
void IProfilingBlockInputStream::progress(Block & block)
{
if (children.empty() && progress_callback)
progress_callback(block.rows(), block.bytes());
}
2011-08-27 22:43:31 +00:00
2011-09-04 21:23:19 +00:00
const BlockStreamProfileInfo & IProfilingBlockInputStream::getInfo() const
2011-08-27 22:43:31 +00:00
{
return info;
}
void IProfilingBlockInputStream::cancel()
{
2012-11-10 05:13:46 +00:00
if (!__sync_bool_compare_and_swap(&is_cancelled, false, true))
return;
for (BlockInputStreams::iterator it = children.begin(); it != children.end(); ++it)
if (IProfilingBlockInputStream * child = dynamic_cast<IProfilingBlockInputStream *>(&**it))
child->cancel();
}
2012-05-09 15:15:45 +00:00
void IProfilingBlockInputStream::setProgressCallback(ProgressCallback callback)
{
2012-05-17 19:15:53 +00:00
progress_callback = callback;
for (BlockInputStreams::iterator it = children.begin(); it != children.end(); ++it)
if (IProfilingBlockInputStream * child = dynamic_cast<IProfilingBlockInputStream *>(&**it))
child->setProgressCallback(callback);
2012-05-09 08:16:09 +00:00
}
2011-08-27 22:43:31 +00:00
}