Skip to content

Commit

Permalink
added invocation time to the threaded block output
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.opensource.yandex.net/xscript/trunk@710 b01ef89b-65f2-463d-9415-e8412542ae63
  • Loading branch information
golubtsov committed Jan 21, 2009
1 parent 3210ffe commit 123b618
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/xscript/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Block : public Object {
virtual XmlDocHelper processResponse(Context *ctx, XmlDocHelper doc, boost::any &a);
virtual void property(const char *name, const char *value);
virtual void postCall(Context *ctx, const XmlDocHelper &doc, const boost::any &a);
virtual void postInvoke(Context *ctx, const XmlDocHelper &doc);
virtual void callInternal(boost::shared_ptr<Context> ctx, unsigned int slot);
virtual void callInternalThreaded(boost::shared_ptr<Context> ctx, unsigned int slot);

Expand Down
2 changes: 2 additions & 0 deletions include/xscript/threaded_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class ThreadedBlock : public virtual Block {

protected:
virtual void property(const char *name, const char *value);
virtual void postInvoke(Context *ctx, const XmlDocHelper &doc);

private:
bool threaded_;
int timeout_;
bool check_elapsed_time_;
};

} // namespace xscript
Expand Down
1 change: 1 addition & 0 deletions include/xscript/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class TimeoutCounter {
bool unlimited() const;
bool expired() const;
int remained() const;
int elapsed() const;
void timeout(int timeout);
int timeout() const;

Expand Down
10 changes: 9 additions & 1 deletion library/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ Block::invoke(Context *ctx) {
}

try {
return invokeInternal(ctx);
InvokeResult result = invokeInternal(ctx);
if (result.success) {
postInvoke(ctx, result.doc);
}
return result;
}
catch (const CriticalInvokeError &e) {
std::string full_error;
Expand Down Expand Up @@ -481,6 +485,10 @@ void
Block::postCall(Context *, const XmlDocHelper &, const boost::any &) {
}

void
Block::postInvoke(Context *, const XmlDocHelper &) {
}

void
Block::callInternal(boost::shared_ptr<Context> ctx, unsigned int slot) {
ctx->result(slot, invoke(ctx.get()));
Expand Down
18 changes: 17 additions & 1 deletion library/threaded_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace xscript {

ThreadedBlock::ThreadedBlock(const Extension *ext, Xml *owner, xmlNodePtr node) :
Block(ext, owner, node), threaded_(false), timeout_(0) {
Block(ext, owner, node), threaded_(false), timeout_(0), check_elapsed_time_(0) {
}

ThreadedBlock::~ThreadedBlock() {
Expand Down Expand Up @@ -48,11 +48,27 @@ ThreadedBlock::property(const char *name, const char *value) {
else if (strncasecmp(name, "timeout", sizeof("timeout")) == 0) {
timeout_ = boost::lexical_cast<int>(value);
}
else if (strncasecmp(name, "elapsed-time", sizeof("elapsed-time")) == 0) {
check_elapsed_time_ = (strncasecmp(value, "yes", sizeof("yes")) == 0);
}
else {
Block::property(name, value);
}
}

void
ThreadedBlock::postInvoke(Context *ctx, const XmlDocHelper &doc) {
if (!check_elapsed_time_ || tagged()) {
return;
}
xmlNodePtr node = xmlDocGetRootElement(doc.get());
if (NULL == node) {
return;
}
std::string elapsed = boost::lexical_cast<std::string>(0.001*ctx->blockTimer(this).elapsed());
xmlNewProp(node, (const xmlChar*)"elapsed-time", (const xmlChar*)elapsed.c_str());
}

void
ThreadedBlock::startTimer(Context *ctx) {
if (runByMainThread(ctx)) {
Expand Down
9 changes: 5 additions & 4 deletions library/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,17 @@ TimeoutCounter::reset(int timeout) {

int
TimeoutCounter::remained() const {

if (unlimited()) {
return UNLIMITED_TIME;
}
return timeout_ - elapsed();
}

int
TimeoutCounter::elapsed() const {
struct timeval current;
gettimeofday(&current, 0);

return timeout_ - (current.tv_sec - init_time_.tv_sec) * 1000 -
return (current.tv_sec - init_time_.tv_sec) * 1000 +
(current.tv_usec - init_time_.tv_usec) / 1000;
}

Expand All @@ -435,7 +437,6 @@ TimeoutCounter::expired() const {
if (unlimited()) {
return false;
}

return remained() <= 0;
}

Expand Down

0 comments on commit 123b618

Please sign in to comment.