Skip to content

Commit 71cb1cd

Browse files
Eugene Ostroukhovaddaleax
Eugene Ostroukhov
authored andcommitted
inspector: implement V8Inspector timer
This timer is currently only used by a profiler. Note that the timer invocation will not interrupt JavaScript code. Fixes: #11401 PR-URL: #14346 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent f8c2302 commit 71cb1cd

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/inspector_agent.cc

+73-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "libplatform/libplatform.h"
1313

1414
#include <string.h>
15+
#include <unordered_map>
1516
#include <vector>
1617

1718
#ifdef __POSIX__
@@ -39,6 +40,7 @@ using v8::Value;
3940
using v8_inspector::StringBuffer;
4041
using v8_inspector::StringView;
4142
using v8_inspector::V8Inspector;
43+
using v8_inspector::V8InspectorClient;
4244

4345
static uv_sem_t start_io_thread_semaphore;
4446
static uv_async_t start_io_thread_async;
@@ -429,9 +431,66 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel {
429431
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
430432
};
431433

434+
class InspectorTimer {
435+
public:
436+
InspectorTimer(uv_loop_t* loop,
437+
double interval_s,
438+
V8InspectorClient::TimerCallback callback,
439+
void* data) : timer_(),
440+
callback_(callback),
441+
data_(data) {
442+
uv_timer_init(loop, &timer_);
443+
int64_t interval_ms = 1000 * interval_s;
444+
uv_timer_start(&timer_, OnTimer, interval_ms, interval_ms);
445+
}
446+
447+
InspectorTimer(const InspectorTimer&) = delete;
448+
449+
void Stop() {
450+
uv_timer_stop(&timer_);
451+
uv_close(reinterpret_cast<uv_handle_t*>(&timer_), TimerClosedCb);
452+
}
453+
454+
private:
455+
static void OnTimer(uv_timer_t* uvtimer) {
456+
InspectorTimer* timer = node::ContainerOf(&InspectorTimer::timer_, uvtimer);
457+
timer->callback_(timer->data_);
458+
}
459+
460+
static void TimerClosedCb(uv_handle_t* uvtimer) {
461+
InspectorTimer* timer =
462+
node::ContainerOf(&InspectorTimer::timer_,
463+
reinterpret_cast<uv_timer_t*>(uvtimer));
464+
delete timer;
465+
}
466+
467+
~InspectorTimer() {}
468+
469+
uv_timer_t timer_;
470+
V8InspectorClient::TimerCallback callback_;
471+
void* data_;
472+
};
473+
474+
class InspectorTimerHandle {
475+
public:
476+
InspectorTimerHandle(uv_loop_t* loop, double interval_s,
477+
V8InspectorClient::TimerCallback callback, void* data) {
478+
timer_ = new InspectorTimer(loop, interval_s, callback, data);
479+
}
480+
481+
InspectorTimerHandle(const InspectorTimerHandle&) = delete;
482+
483+
~InspectorTimerHandle() {
484+
CHECK_NE(timer_, nullptr);
485+
timer_->Stop();
486+
timer_ = nullptr;
487+
}
488+
private:
489+
InspectorTimer* timer_;
490+
};
432491
} // namespace
433492

434-
class NodeInspectorClient : public v8_inspector::V8InspectorClient {
493+
class NodeInspectorClient : public V8InspectorClient {
435494
public:
436495
NodeInspectorClient(node::Environment* env,
437496
v8::Platform* platform) : env_(env),
@@ -527,13 +586,26 @@ class NodeInspectorClient : public v8_inspector::V8InspectorClient {
527586
return channel_.get();
528587
}
529588

589+
void startRepeatingTimer(double interval_s,
590+
TimerCallback callback,
591+
void* data) override {
592+
timers_.emplace(std::piecewise_construct, std::make_tuple(data),
593+
std::make_tuple(env_->event_loop(), interval_s, callback,
594+
data));
595+
}
596+
597+
void cancelTimer(void* data) override {
598+
timers_.erase(data);
599+
}
600+
530601
private:
531602
node::Environment* env_;
532603
v8::Platform* platform_;
533604
bool terminated_;
534605
bool running_nested_loop_;
535606
std::unique_ptr<V8Inspector> client_;
536607
std::unique_ptr<ChannelImpl> channel_;
608+
std::unordered_map<void*, InspectorTimerHandle> timers_;
537609
};
538610

539611
Agent::Agent(Environment* env) : parent_env_(env),

0 commit comments

Comments
 (0)