GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_worker.h Lines: 9 9 100.0 %
Date: 2022-10-14 05:16:24 Branches: 1 2 50.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_WORKER_H_
2
#define SRC_NODE_WORKER_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <optional>
7
#include <unordered_map>
8
#include "node_exit_code.h"
9
#include "node_messaging.h"
10
#include "uv.h"
11
12
namespace node {
13
14
struct SnapshotData;
15
namespace worker {
16
17
class WorkerThreadData;
18
19
enum ResourceLimits {
20
  kMaxYoungGenerationSizeMb,
21
  kMaxOldGenerationSizeMb,
22
  kCodeRangeSizeMb,
23
  kStackSizeMb,
24
  kTotalResourceLimitCount
25
};
26
27
// A worker thread, as represented in its parent thread.
28
class Worker : public AsyncWrap {
29
 public:
30
  Worker(Environment* env,
31
         v8::Local<v8::Object> wrap,
32
         const std::string& url,
33
         std::shared_ptr<PerIsolateOptions> per_isolate_opts,
34
         std::vector<std::string>&& exec_argv,
35
         std::shared_ptr<KVStore> env_vars,
36
         const SnapshotData* snapshot_data);
37
  ~Worker() override;
38
39
  // Run the worker. This is only called from the worker thread.
40
  void Run();
41
42
  // Forcibly exit the thread with a specified exit code. This may be called
43
  // from any thread. `error_code` and `error_message` can be used to create
44
  // a custom `'error'` event before emitting `'exit'`.
45
  void Exit(ExitCode code,
46
            const char* error_code = nullptr,
47
            const char* error_message = nullptr);
48
49
  // Wait for the worker thread to stop (in a blocking manner).
50
  void JoinThread();
51
52
  template <typename Fn>
53
  inline bool RequestInterrupt(Fn&& cb);
54
55
1
  SET_NO_MEMORY_INFO()
56
1
  SET_MEMORY_INFO_NAME(Worker)
57
1
  SET_SELF_SIZE(Worker)
58
  bool IsNotIndicativeOfMemoryLeakAtExit() const override;
59
60
  bool is_stopped() const;
61
1471
  const SnapshotData* snapshot_data() const { return snapshot_data_; }
62
63
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
64
  static void CloneParentEnvVars(
65
      const v8::FunctionCallbackInfo<v8::Value>& args);
66
  static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
67
  static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
68
  static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
69
  static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
70
  static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
71
  static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
72
  static void GetResourceLimits(
73
      const v8::FunctionCallbackInfo<v8::Value>& args);
74
  v8::Local<v8::Float64Array> GetResourceLimits(v8::Isolate* isolate) const;
75
  static void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args);
76
  static void LoopIdleTime(const v8::FunctionCallbackInfo<v8::Value>& args);
77
  static void LoopStartTime(const v8::FunctionCallbackInfo<v8::Value>& args);
78
79
 private:
80
  bool CreateEnvMessagePort(Environment* env);
81
  static size_t NearHeapLimit(void* data, size_t current_heap_limit,
82
                              size_t initial_heap_limit);
83
84
  std::shared_ptr<PerIsolateOptions> per_isolate_opts_;
85
  std::vector<std::string> exec_argv_;
86
  std::vector<std::string> argv_;
87
88
  MultiIsolatePlatform* platform_;
89
  v8::Isolate* isolate_ = nullptr;
90
  std::optional<uv_thread_t> tid_;  // Set while the thread is running
91
92
  std::unique_ptr<InspectorParentHandle> inspector_parent_handle_;
93
94
  // This mutex protects access to all variables listed below it.
95
  mutable Mutex mutex_;
96
97
  const char* custom_error_ = nullptr;
98
  std::string custom_error_str_;
99
  ExitCode exit_code_ = ExitCode::kNoFailure;
100
  ThreadId thread_id_;
101
  uintptr_t stack_base_ = 0;
102
103
  // Custom resource constraints:
104
  double resource_limits_[kTotalResourceLimitCount];
105
  void UpdateResourceConstraints(v8::ResourceConstraints* constraints);
106
107
  // Full size of the thread's stack.
108
  size_t stack_size_ = 4 * 1024 * 1024;
109
  // Stack buffer size that is not available to the JS engine.
110
  static constexpr size_t kStackBufferSize = 192 * 1024;
111
112
  std::unique_ptr<MessagePortData> child_port_data_;
113
  std::shared_ptr<KVStore> env_vars_;
114
115
  // A raw flag that is used by creator and worker threads to
116
  // sync up on pre-mature termination of worker  - while in the
117
  // warmup phase.  Once the worker is fully warmed up, use the
118
  // async handle of the worker's Environment for the same purpose.
119
  bool stopped_ = true;
120
121
  bool has_ref_ = true;
122
  uint64_t environment_flags_ = EnvironmentFlags::kNoFlags;
123
124
  // The real Environment of the worker object. It has a lesser
125
  // lifespan than the worker object itself - comes to life
126
  // when the worker thread creates a new Environment, and gets
127
  // destroyed alongwith the worker thread.
128
  Environment* env_ = nullptr;
129
130
  const SnapshotData* snapshot_data_ = nullptr;
131
  friend class WorkerThreadData;
132
};
133
134
template <typename Fn>
135
3
bool Worker::RequestInterrupt(Fn&& cb) {
136
6
  Mutex::ScopedLock lock(mutex_);
137
3
  if (env_ == nullptr) return false;
138
3
  env_->RequestInterrupt(std::move(cb));
139
3
  return true;
140
}
141
142
}  // namespace worker
143
}  // namespace node
144
145
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
146
147
148
#endif  // SRC_NODE_WORKER_H_