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
|
|
|
|
|
{
|
2011-09-26 01:50:32 +00:00
|
|
|
|
Poco::Timestamp::TimeDiff nested_elapsed = 0;
|
|
|
|
|
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)
|
|
|
|
|
nested_elapsed = (*it)->work_stopwatch.elapsed();
|
|
|
|
|
nested_rows += (*it)->rows;
|
|
|
|
|
nested_blocks += (*it)->blocks;
|
|
|
|
|
nested_bytes += (*it)->bytes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ostr << std::fixed << std::setprecision(2)
|
|
|
|
|
<< "Columns: " << column_names << std::endl
|
|
|
|
|
<< "Elapsed: " << work_stopwatch.elapsed() / 1000000.0 << " sec. "
|
|
|
|
|
<< "(" << work_stopwatch.elapsed() * 100.0 / total_stopwatch.elapsed() << "%), " << std::endl;
|
|
|
|
|
|
|
|
|
|
if (!nested_infos.empty())
|
2011-09-26 04:26:22 +00:00
|
|
|
|
{
|
|
|
|
|
double self_percents = (work_stopwatch.elapsed() - nested_elapsed) * 100.0 / total_stopwatch.elapsed();
|
|
|
|
|
|
2011-09-26 01:50:32 +00:00
|
|
|
|
ostr<< "Elapsed (self): " << (work_stopwatch.elapsed() - nested_elapsed) / 1000000.0 << " 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
|
2011-09-26 01:50:32 +00:00
|
|
|
|
<< "Rows (in): " << nested_rows << ", per second: " << nested_rows * 1000000 / work_stopwatch.elapsed() << ", " << std::endl
|
|
|
|
|
<< "Blocks (in): " << nested_blocks << ", per second: " << nested_blocks * 1000000.0 / work_stopwatch.elapsed() << ", " << std::endl
|
|
|
|
|
<< " " << nested_bytes / 1000000.0 << " MB (memory), "
|
|
|
|
|
<< nested_bytes / work_stopwatch.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 * 1000000 / (work_stopwatch.elapsed() - nested_elapsed))
|
|
|
|
|
<< ", " << (work_stopwatch.elapsed() - nested_elapsed) * 1000 / 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 * 1000000 / work_stopwatch.elapsed() << ", " << std::endl
|
|
|
|
|
<< "Blocks (out): " << blocks << ", per second: " << blocks * 1000000.0 / work_stopwatch.elapsed() << ", " << std::endl
|
|
|
|
|
<< " " << bytes / 1000000.0 << " MB (memory), " << bytes / work_stopwatch.elapsed() << " MB/s (memory), " << std::endl
|
|
|
|
|
<< "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);
|
2012-10-17 19:59:27 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/** Если поток закончился, то ещё попросим всех детей прервать выполнение.
|
|
|
|
|
* Это имеет смысл при выполнении запроса с LIMIT-ом:
|
|
|
|
|
* - бывает ситуация, когда все необходимые данные уже прочитали,
|
|
|
|
|
* но источники-дети ещё продолжают работать,
|
|
|
|
|
* при чём они могут работать в отдельных потоках или даже удалённо.
|
|
|
|
|
*/
|
|
|
|
|
cancel();
|
|
|
|
|
}
|
2011-09-25 03:37:09 +00:00
|
|
|
|
|
2012-05-17 19:15:53 +00:00
|
|
|
|
progress(res);
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-17 19:55:56 +00:00
|
|
|
|
void IProfilingBlockInputStream::cancel()
|
|
|
|
|
{
|
|
|
|
|
is_cancelled = true;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|