GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_report.cc Lines: 361 367 98.4 %
Date: 2020-07-19 22:14:24 Branches: 94 130 72.3 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "json_utils.h"
3
#include "node_report.h"
4
#include "debug_utils-inl.h"
5
#include "diagnosticfilename-inl.h"
6
#include "node_internals.h"
7
#include "node_metadata.h"
8
#include "node_mutex.h"
9
#include "node_worker.h"
10
#include "util.h"
11
12
#ifdef _WIN32
13
#include <Windows.h>
14
#else  // !_WIN32
15
#include <sys/resource.h>
16
#include <cxxabi.h>
17
#include <dlfcn.h>
18
#endif
19
20
#include <iostream>
21
#include <cstring>
22
#include <ctime>
23
#include <cwctype>
24
#include <fstream>
25
26
constexpr int NODE_REPORT_VERSION = 2;
27
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
28
constexpr double SEC_PER_MICROS = 1e-6;
29
30
namespace report {
31
using node::arraysize;
32
using node::ConditionVariable;
33
using node::DiagnosticFilename;
34
using node::Environment;
35
using node::JSONWriter;
36
using node::Mutex;
37
using node::NativeSymbolDebuggingContext;
38
using node::TIME_TYPE;
39
using node::worker::Worker;
40
using v8::Array;
41
using v8::Context;
42
using v8::HeapSpaceStatistics;
43
using v8::HeapStatistics;
44
using v8::Isolate;
45
using v8::Local;
46
using v8::Number;
47
using v8::Object;
48
using v8::StackTrace;
49
using v8::String;
50
using v8::TryCatch;
51
using v8::Value;
52
using v8::V8;
53
54
namespace per_process = node::per_process;
55
56
// Internal/static function declarations
57
static void WriteNodeReport(Isolate* isolate,
58
                            Environment* env,
59
                            const char* message,
60
                            const char* trigger,
61
                            const std::string& filename,
62
                            std::ostream& out,
63
                            Local<Object> error,
64
                            bool compact);
65
static void PrintVersionInformation(JSONWriter* writer);
66
static void PrintJavaScriptErrorStack(JSONWriter* writer,
67
                                      Isolate* isolate,
68
                                      Local<Object> error,
69
                                      const char* trigger);
70
static void PrintJavaScriptErrorProperties(JSONWriter* writer,
71
                                           Isolate* isolate,
72
                                           Local<Object> error);
73
static void PrintNativeStack(JSONWriter* writer);
74
static void PrintResourceUsage(JSONWriter* writer);
75
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
76
static void PrintSystemInformation(JSONWriter* writer);
77
static void PrintLoadedLibraries(JSONWriter* writer);
78
static void PrintComponentVersions(JSONWriter* writer);
79
static void PrintRelease(JSONWriter* writer);
80
static void PrintCpuInfo(JSONWriter* writer);
81
static void PrintNetworkInterfaceInfo(JSONWriter* writer);
82
83
// External function to trigger a report, writing to file.
84
13
std::string TriggerNodeReport(Isolate* isolate,
85
                              Environment* env,
86
                              const char* message,
87
                              const char* trigger,
88
                              const std::string& name,
89
                              Local<Object> error) {
90
26
  std::string filename;
91
92
  // Determine the required report filename. In order of priority:
93
  //   1) supplied on API 2) configured on startup 3) default generated
94
13
  if (!name.empty()) {
95
    // Filename was specified as API parameter.
96
4
    filename = name;
97
  } else {
98
18
    std::string report_filename;
99
    {
100
18
      Mutex::ScopedLock lock(per_process::cli_options_mutex);
101
9
      report_filename = per_process::cli_options->report_filename;
102
    }
103
9
    if (report_filename.length() > 0) {
104
      // File name was supplied via start-up option.
105
1
      filename = report_filename;
106
    } else {
107
16
      filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0,
108
8
          "report", "json");
109
    }
110
  }
111
112
  // Open the report file stream for writing. Supports stdout/err,
113
  // user-specified or (default) generated name
114
26
  std::ofstream outfile;
115
  std::ostream* outstream;
116
13
  if (filename == "stdout") {
117
1
    outstream = &std::cout;
118
12
  } else if (filename == "stderr") {
119
1
    outstream = &std::cerr;
120
  } else {
121
21
    std::string report_directory;
122
    {
123
22
      Mutex::ScopedLock lock(per_process::cli_options_mutex);
124
11
      report_directory = per_process::cli_options->report_directory;
125
    }
126
    // Regular file. Append filename to directory path if one was specified
127
11
    if (report_directory.length() > 0) {
128
22
      std::string pathname = report_directory;
129
11
      pathname += node::kPathSeparator;
130
11
      pathname += filename;
131
11
      outfile.open(pathname, std::ios::out | std::ios::binary);
132
    } else {
133
      outfile.open(filename, std::ios::out | std::ios::binary);
134
    }
135
    // Check for errors on the file open
136
11
    if (!outfile.is_open()) {
137
1
      std::cerr << "\nFailed to open Node.js report file: " << filename;
138
139
1
      if (report_directory.length() > 0)
140
1
        std::cerr << " directory: " << report_directory;
141
142
1
      std::cerr << " (errno: " << errno << ")" << std::endl;
143
1
      return "";
144
    }
145
10
    outstream = &outfile;
146
10
    std::cerr << "\nWriting Node.js report to file: " << filename;
147
  }
148
149
  bool compact;
150
  {
151
24
    Mutex::ScopedLock lock(per_process::cli_options_mutex);
152
12
    compact = per_process::cli_options->report_compact;
153
  }
154
12
  WriteNodeReport(isolate, env, message, trigger, filename, *outstream,
155
12
                  error, compact);
156
157
  // Do not close stdout/stderr, only close files we opened.
158
12
  if (outfile.is_open()) {
159
10
    outfile.close();
160
  }
161
162
  // Do not mix JSON and free-form text on stderr.
163
12
  if (filename != "stderr") {
164
11
    std::cerr << "\nNode.js report completed" << std::endl;
165
  }
166
12
  return filename;
167
}
168
169
// External function to trigger a report, writing to a supplied stream.
170
10
void GetNodeReport(Isolate* isolate,
171
                   Environment* env,
172
                   const char* message,
173
                   const char* trigger,
174
                   Local<Object> error,
175
                   std::ostream& out) {
176
10
  WriteNodeReport(isolate, env, message, trigger, "", out, error, false);
177
10
}
178
179
// Internal function to coordinate and write the various
180
// sections of the report to the supplied stream
181
22
static void WriteNodeReport(Isolate* isolate,
182
                            Environment* env,
183
                            const char* message,
184
                            const char* trigger,
185
                            const std::string& filename,
186
                            std::ostream& out,
187
                            Local<Object> error,
188
                            bool compact) {
189
  // Obtain the current time and the pid.
190
  TIME_TYPE tm_struct;
191
22
  DiagnosticFilename::LocalTime(&tm_struct);
192
22
  uv_pid_t pid = uv_os_getpid();
193
194
  // Save formatting for output stream.
195
44
  std::ios old_state(nullptr);
196
22
  old_state.copyfmt(out);
197
198
  // File stream opened OK, now start printing the report content:
199
  // the title and header information (event, filename, timestamp and pid)
200
201
22
  JSONWriter writer(out, compact);
202
22
  writer.json_start();
203
22
  writer.json_objectstart("header");
204
22
  writer.json_keyvalue("reportVersion", NODE_REPORT_VERSION);
205
22
  writer.json_keyvalue("event", message);
206
22
  writer.json_keyvalue("trigger", trigger);
207
22
  if (!filename.empty())
208
12
    writer.json_keyvalue("filename", filename);
209
  else
210
10
    writer.json_keyvalue("filename", JSONWriter::Null{});
211
212
  // Report dump event and module load date/time stamps
213
  char timebuf[64];
214
#ifdef _WIN32
215
  snprintf(timebuf,
216
           sizeof(timebuf),
217
           "%4d-%02d-%02dT%02d:%02d:%02dZ",
218
           tm_struct.wYear,
219
           tm_struct.wMonth,
220
           tm_struct.wDay,
221
           tm_struct.wHour,
222
           tm_struct.wMinute,
223
           tm_struct.wSecond);
224
  writer.json_keyvalue("dumpEventTime", timebuf);
225
#else  // UNIX, OSX
226
66
  snprintf(timebuf,
227
           sizeof(timebuf),
228
           "%4d-%02d-%02dT%02d:%02d:%02dZ",
229
22
           tm_struct.tm_year + 1900,
230
22
           tm_struct.tm_mon + 1,
231
           tm_struct.tm_mday,
232
           tm_struct.tm_hour,
233
           tm_struct.tm_min,
234
22
           tm_struct.tm_sec);
235
22
  writer.json_keyvalue("dumpEventTime", timebuf);
236
#endif
237
238
  uv_timeval64_t ts;
239
22
  if (uv_gettimeofday(&ts) == 0) {
240
    writer.json_keyvalue("dumpEventTimeStamp",
241
22
                         std::to_string(ts.tv_sec * 1000 + ts.tv_usec / 1000));
242
  }
243
244
  // Report native process ID
245
22
  writer.json_keyvalue("processId", pid);
246
22
  if (env != nullptr)
247
22
    writer.json_keyvalue("threadId", env->thread_id());
248
  else
249
    writer.json_keyvalue("threadId", JSONWriter::Null{});
250
251
  {
252
    // Report the process cwd.
253
    char buf[PATH_MAX_BYTES];
254
22
    size_t cwd_size = sizeof(buf);
255
22
    if (uv_cwd(buf, &cwd_size) == 0)
256
22
      writer.json_keyvalue("cwd", buf);
257
  }
258
259
  // Report out the command line.
260
22
  if (!node::per_process::cli_options->cmdline.empty()) {
261
22
    writer.json_arraystart("commandLine");
262
74
    for (const std::string& arg : node::per_process::cli_options->cmdline) {
263
52
      writer.json_element(arg);
264
    }
265
22
    writer.json_arrayend();
266
  }
267
268
  // Report Node.js and OS version information
269
22
  PrintVersionInformation(&writer);
270
22
  writer.json_objectend();
271
272
22
  writer.json_objectstart("javascriptStack");
273
  // Report summary JavaScript error stack backtrace
274
22
  PrintJavaScriptErrorStack(&writer, isolate, error, trigger);
275
276
  // Report summary JavaScript error properties backtrace
277
22
  PrintJavaScriptErrorProperties(&writer, isolate, error);
278
22
  writer.json_objectend();  // the end of 'javascriptStack'
279
280
  // Report native stack backtrace
281
22
  PrintNativeStack(&writer);
282
283
  // Report V8 Heap and Garbage Collector information
284
22
  PrintGCStatistics(&writer, isolate);
285
286
  // Report OS and current thread resource usage
287
22
  PrintResourceUsage(&writer);
288
289
22
  writer.json_arraystart("libuv");
290
22
  if (env != nullptr) {
291
22
    uv_walk(env->event_loop(), WalkHandle, static_cast<void*>(&writer));
292
293
22
    writer.json_start();
294
22
    writer.json_keyvalue("type", "loop");
295
    writer.json_keyvalue("is_active",
296
22
        static_cast<bool>(uv_loop_alive(env->event_loop())));
297
    writer.json_keyvalue("address",
298
22
        ValueToHexString(reinterpret_cast<int64_t>(env->event_loop())));
299
22
    writer.json_end();
300
  }
301
302
22
  writer.json_arrayend();
303
304
22
  writer.json_arraystart("workers");
305
22
  if (env != nullptr) {
306
44
    Mutex workers_mutex;
307
44
    ConditionVariable notify;
308
44
    std::vector<std::string> worker_infos;
309
22
    size_t expected_results = 0;
310
311
46
    env->ForEachWorker([&](Worker* w) {
312
12
      expected_results += w->RequestInterrupt([&](Environment* env) {
313
4
        std::ostringstream os;
314
315
4
        GetNodeReport(env->isolate(),
316
                      env,
317
                      "Worker thread subreport",
318
4
                      trigger,
319
                      Local<Object>(),
320
2
                      os);
321
322
6
        Mutex::ScopedLock lock(workers_mutex);
323
4
        worker_infos.emplace_back(os.str());
324
4
        notify.Signal(lock);
325
4
      });
326
24
    });
327
328
44
    Mutex::ScopedLock lock(workers_mutex);
329
22
    worker_infos.reserve(expected_results);
330
26
    while (worker_infos.size() < expected_results)
331
2
      notify.Wait(lock);
332
24
    for (const std::string& worker_info : worker_infos)
333
2
      writer.json_element(JSONWriter::ForeignJSON { worker_info });
334
  }
335
22
  writer.json_arrayend();
336
337
  // Report operating system information
338
22
  PrintSystemInformation(&writer);
339
340
22
  writer.json_objectend();
341
342
  // Restore output stream formatting.
343
22
  out.copyfmt(old_state);
344
22
}
345
346
// Report Node.js version, OS version and machine information.
347
22
static void PrintVersionInformation(JSONWriter* writer) {
348
44
  std::ostringstream buf;
349
  // Report Node version
350
22
  buf << "v" << NODE_VERSION_STRING;
351
22
  writer->json_keyvalue("nodejsVersion", buf.str());
352
22
  buf.str("");
353
354
#ifndef _WIN32
355
  // Report compiler and runtime glibc versions where possible.
356
  const char* (*libc_version)();
357
44
  *(reinterpret_cast<void**>(&libc_version)) =
358
22
      dlsym(RTLD_DEFAULT, "gnu_get_libc_version");
359
22
  if (libc_version != nullptr)
360
22
    writer->json_keyvalue("glibcVersionRuntime", (*libc_version)());
361
#endif /* _WIN32 */
362
363
#ifdef __GLIBC__
364
22
  buf << __GLIBC__ << "." << __GLIBC_MINOR__;
365
22
  writer->json_keyvalue("glibcVersionCompiler", buf.str());
366
22
  buf.str("");
367
#endif
368
369
  // Report Process word size
370
22
  writer->json_keyvalue("wordSize", sizeof(void*) * 8);
371
22
  writer->json_keyvalue("arch", node::per_process::metadata.arch);
372
22
  writer->json_keyvalue("platform", node::per_process::metadata.platform);
373
374
  // Report deps component versions
375
22
  PrintComponentVersions(writer);
376
377
  // Report release metadata.
378
22
  PrintRelease(writer);
379
380
  // Report operating system and machine information
381
  uv_utsname_t os_info;
382
383
22
  if (uv_os_uname(&os_info) == 0) {
384
22
    writer->json_keyvalue("osName", os_info.sysname);
385
22
    writer->json_keyvalue("osRelease", os_info.release);
386
22
    writer->json_keyvalue("osVersion", os_info.version);
387
22
    writer->json_keyvalue("osMachine", os_info.machine);
388
  }
389
390
22
  PrintCpuInfo(writer);
391
22
  PrintNetworkInterfaceInfo(writer);
392
393
  char host[UV_MAXHOSTNAMESIZE];
394
22
  size_t host_size = sizeof(host);
395
396
22
  if (uv_os_gethostname(host, &host_size) == 0)
397
22
    writer->json_keyvalue("host", host);
398
22
}
399
400
// Report CPU info
401
22
static void PrintCpuInfo(JSONWriter* writer) {
402
  uv_cpu_info_t* cpu_info;
403
  int count;
404
22
  if (uv_cpu_info(&cpu_info, &count) == 0) {
405
22
    writer->json_arraystart("cpus");
406
198
    for (int i = 0; i < count; i++) {
407
176
      writer->json_start();
408
176
      writer->json_keyvalue("model", cpu_info[i].model);
409
176
      writer->json_keyvalue("speed", cpu_info[i].speed);
410
176
      writer->json_keyvalue("user", cpu_info[i].cpu_times.user);
411
176
      writer->json_keyvalue("nice", cpu_info[i].cpu_times.nice);
412
176
      writer->json_keyvalue("sys", cpu_info[i].cpu_times.sys);
413
176
      writer->json_keyvalue("idle", cpu_info[i].cpu_times.idle);
414
176
      writer->json_keyvalue("irq", cpu_info[i].cpu_times.irq);
415
176
      writer->json_end();
416
    }
417
22
    writer->json_arrayend();
418
22
    uv_free_cpu_info(cpu_info, count);
419
  }
420
22
}
421
422
22
static void PrintNetworkInterfaceInfo(JSONWriter* writer) {
423
  uv_interface_address_t* interfaces;
424
  char ip[INET6_ADDRSTRLEN];
425
  char netmask[INET6_ADDRSTRLEN];
426
  char mac[18];
427
  int count;
428
429
22
  if (uv_interface_addresses(&interfaces, &count) == 0) {
430
22
    writer->json_arraystart("networkInterfaces");
431
432
154
    for (int i = 0; i < count; i++) {
433
132
      writer->json_start();
434
132
      writer->json_keyvalue("name", interfaces[i].name);
435
132
      writer->json_keyvalue("internal", !!interfaces[i].is_internal);
436
792
      snprintf(mac,
437
               sizeof(mac),
438
               "%02x:%02x:%02x:%02x:%02x:%02x",
439
132
               static_cast<unsigned char>(interfaces[i].phys_addr[0]),
440
132
               static_cast<unsigned char>(interfaces[i].phys_addr[1]),
441
132
               static_cast<unsigned char>(interfaces[i].phys_addr[2]),
442
132
               static_cast<unsigned char>(interfaces[i].phys_addr[3]),
443
132
               static_cast<unsigned char>(interfaces[i].phys_addr[4]),
444
264
               static_cast<unsigned char>(interfaces[i].phys_addr[5]));
445
132
      writer->json_keyvalue("mac", mac);
446
447
132
      if (interfaces[i].address.address4.sin_family == AF_INET) {
448
66
        uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
449
66
        uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
450
66
        writer->json_keyvalue("address", ip);
451
66
        writer->json_keyvalue("netmask", netmask);
452
66
        writer->json_keyvalue("family", "IPv4");
453
66
      } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
454
66
        uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
455
66
        uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
456
66
        writer->json_keyvalue("address", ip);
457
66
        writer->json_keyvalue("netmask", netmask);
458
66
        writer->json_keyvalue("family", "IPv6");
459
66
        writer->json_keyvalue("scopeid",
460
132
                              interfaces[i].address.address6.sin6_scope_id);
461
      } else {
462
        writer->json_keyvalue("family", "unknown");
463
      }
464
465
132
      writer->json_end();
466
    }
467
468
22
    writer->json_arrayend();
469
22
    uv_free_interface_addresses(interfaces, count);
470
  }
471
22
}
472
473
22
static void PrintJavaScriptErrorProperties(JSONWriter* writer,
474
                                           Isolate* isolate,
475
                                           Local<Object> error) {
476
22
  writer->json_objectstart("errorProperties");
477
22
  if (!error.IsEmpty()) {
478
38
    TryCatch try_catch(isolate);
479
19
    Local<Context> context = error->GetIsolate()->GetCurrentContext();
480
    Local<Array> keys;
481
38
    if (!error->GetOwnPropertyNames(context).ToLocal(&keys)) {
482
      return writer->json_objectend();  // the end of 'errorProperties'
483
    }
484
19
    uint32_t keys_length = keys->Length();
485

31
    for (uint32_t i = 0; i < keys_length; i++) {
486
      Local<Value> key;
487

48
      if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) {
488
        continue;
489
      }
490
      Local<Value> value;
491
      Local<String> value_string;
492

48
      if (!error->Get(context, key).ToLocal(&value) ||
493
36
          !value->ToString(context).ToLocal(&value_string)) {
494
        continue;
495
      }
496
24
      String::Utf8Value k(isolate, key);
497

12
      if (!strcmp(*k, "stack") || !strcmp(*k, "message")) continue;
498
24
      String::Utf8Value v(isolate, value_string);
499
24
      writer->json_keyvalue(std::string(*k, k.length()),
500
36
                            std::string(*v, v.length()));
501
    }
502
  }
503
22
  writer->json_objectend();  // the end of 'errorProperties'
504
}
505
506
// Report the JavaScript stack.
507
22
static void PrintJavaScriptErrorStack(JSONWriter* writer,
508
                                 Isolate* isolate,
509
                                 Local<Object> error,
510
                                 const char* trigger) {
511
  Local<Value> stackstr;
512
44
  std::string ss = "";
513
44
  TryCatch try_catch(isolate);
514

44
  if ((!strcmp(trigger, "FatalError")) ||
515
22
      (!strcmp(trigger, "Signal"))) {
516
1
    ss = "No stack.\nUnavailable.\n";
517

61
  } else if (!error.IsEmpty() &&
518
             error
519
40
                 ->Get(isolate->GetCurrentContext(),
520
                       node::FIXED_ONE_BYTE_STRING(isolate,
521
78
                                                   "stack"))
522
19
                 .ToLocal(&stackstr)) {
523
38
    String::Utf8Value sv(isolate, stackstr);
524
19
    ss = std::string(*sv, sv.length());
525
  }
526
22
  int line = ss.find('\n');
527
22
  if (line == -1) {
528
4
    writer->json_keyvalue("message", ss);
529
  } else {
530
36
    std::string l = ss.substr(0, line);
531
18
    writer->json_keyvalue("message", l);
532
18
    writer->json_arraystart("stack");
533
18
    ss = ss.substr(line + 1);
534
18
    line = ss.find('\n');
535
244
    while (line != -1) {
536
113
      l = ss.substr(0, line);
537
561
      l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) {
538
561
                return !std::iswspace(ch);
539
674
              }));
540
113
      writer->json_element(l);
541
113
      ss = ss.substr(line + 1);
542
113
      line = ss.find('\n');
543
    }
544
18
    writer->json_arrayend();
545
  }
546
22
}
547
548
// Report a native stack backtrace
549
22
static void PrintNativeStack(JSONWriter* writer) {
550
44
  auto sym_ctx = NativeSymbolDebuggingContext::New();
551
  void* frames[256];
552
22
  const int size = sym_ctx->GetStackTrace(frames, arraysize(frames));
553
22
  writer->json_arraystart("nativeStack");
554
  int i;
555
191
  for (i = 1; i < size; i++) {
556
169
    void* frame = frames[i];
557
169
    writer->json_start();
558
    writer->json_keyvalue("pc",
559
169
                          ValueToHexString(reinterpret_cast<uintptr_t>(frame)));
560
169
    writer->json_keyvalue("symbol", sym_ctx->LookupSymbol(frame).Display());
561
169
    writer->json_end();
562
  }
563
22
  writer->json_arrayend();
564
22
}
565
566
// Report V8 JavaScript heap information.
567
// This uses the existing V8 HeapStatistics and HeapSpaceStatistics APIs.
568
// The isolate->GetGCStatistics(&heap_stats) internal V8 API could potentially
569
// provide some more useful information - the GC history and the handle counts
570
22
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
571
22
  HeapStatistics v8_heap_stats;
572
22
  isolate->GetHeapStatistics(&v8_heap_stats);
573
22
  HeapSpaceStatistics v8_heap_space_stats;
574
575
22
  writer->json_objectstart("javascriptHeap");
576
22
  writer->json_keyvalue("totalMemory", v8_heap_stats.total_heap_size());
577
  writer->json_keyvalue("totalCommittedMemory",
578
22
                        v8_heap_stats.total_physical_size());
579
22
  writer->json_keyvalue("usedMemory", v8_heap_stats.used_heap_size());
580
  writer->json_keyvalue("availableMemory",
581
22
                        v8_heap_stats.total_available_size());
582
22
  writer->json_keyvalue("memoryLimit", v8_heap_stats.heap_size_limit());
583
584
22
  writer->json_objectstart("heapSpaces");
585
  // Loop through heap spaces
586
198
  for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
587
176
    isolate->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
588
176
    writer->json_objectstart(v8_heap_space_stats.space_name());
589
176
    writer->json_keyvalue("memorySize", v8_heap_space_stats.space_size());
590
    writer->json_keyvalue(
591
        "committedMemory",
592
176
        v8_heap_space_stats.physical_space_size());
593
    writer->json_keyvalue(
594
        "capacity",
595
528
        v8_heap_space_stats.space_used_size() +
596
352
            v8_heap_space_stats.space_available_size());
597
176
    writer->json_keyvalue("used", v8_heap_space_stats.space_used_size());
598
    writer->json_keyvalue(
599
176
        "available", v8_heap_space_stats.space_available_size());
600
176
    writer->json_objectend();
601
  }
602
603
22
  writer->json_objectend();
604
22
  writer->json_objectend();
605
22
}
606
607
22
static void PrintResourceUsage(JSONWriter* writer) {
608
  // Get process uptime in seconds
609
  uint64_t uptime =
610
22
      (uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
611
22
  if (uptime == 0) uptime = 1;  // avoid division by zero.
612
613
  // Process and current thread usage statistics
614
  uv_rusage_t rusage;
615
22
  writer->json_objectstart("resourceUsage");
616
22
  if (uv_getrusage(&rusage) == 0) {
617
    double user_cpu =
618
22
        rusage.ru_utime.tv_sec + SEC_PER_MICROS * rusage.ru_utime.tv_usec;
619
    double kernel_cpu =
620
22
        rusage.ru_stime.tv_sec + SEC_PER_MICROS * rusage.ru_stime.tv_usec;
621
22
    writer->json_keyvalue("userCpuSeconds", user_cpu);
622
22
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
623
22
    double cpu_abs = user_cpu + kernel_cpu;
624
22
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
625
22
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
626
22
    writer->json_keyvalue("maxRss", rusage.ru_maxrss * 1024);
627
22
    writer->json_objectstart("pageFaults");
628
22
    writer->json_keyvalue("IORequired", rusage.ru_majflt);
629
22
    writer->json_keyvalue("IONotRequired", rusage.ru_minflt);
630
22
    writer->json_objectend();
631
22
    writer->json_objectstart("fsActivity");
632
22
    writer->json_keyvalue("reads", rusage.ru_inblock);
633
22
    writer->json_keyvalue("writes", rusage.ru_oublock);
634
22
    writer->json_objectend();
635
  }
636
22
  writer->json_objectend();
637
#ifdef RUSAGE_THREAD
638
  struct rusage stats;
639
22
  if (getrusage(RUSAGE_THREAD, &stats) == 0) {
640
22
    writer->json_objectstart("uvthreadResourceUsage");
641
    double user_cpu =
642
22
        stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
643
    double kernel_cpu =
644
22
        stats.ru_stime.tv_sec + SEC_PER_MICROS * stats.ru_stime.tv_usec;
645
22
    writer->json_keyvalue("userCpuSeconds", user_cpu);
646
22
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
647
22
    double cpu_abs = user_cpu + kernel_cpu;
648
22
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
649
22
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
650
22
    writer->json_objectstart("fsActivity");
651
22
    writer->json_keyvalue("reads", stats.ru_inblock);
652
22
    writer->json_keyvalue("writes", stats.ru_oublock);
653
22
    writer->json_objectend();
654
22
    writer->json_objectend();
655
  }
656
#endif
657
22
}
658
659
// Report operating system information.
660
22
static void PrintSystemInformation(JSONWriter* writer) {
661
  uv_env_item_t* envitems;
662
  int envcount;
663
  int r;
664
665
22
  writer->json_objectstart("environmentVariables");
666
667
  {
668
44
    Mutex::ScopedLock lock(node::per_process::env_var_mutex);
669
22
    r = uv_os_environ(&envitems, &envcount);
670
  }
671
672
22
  if (r == 0) {
673
1540
    for (int i = 0; i < envcount; i++)
674
1518
      writer->json_keyvalue(envitems[i].name, envitems[i].value);
675
676
22
    uv_os_free_environ(envitems, envcount);
677
  }
678
679
22
  writer->json_objectend();
680
681
#ifndef _WIN32
682
  static struct {
683
    const char* description;
684
    int id;
685
  } rlimit_strings[] = {
686
    {"core_file_size_blocks", RLIMIT_CORE},
687
    {"data_seg_size_kbytes", RLIMIT_DATA},
688
    {"file_size_blocks", RLIMIT_FSIZE},
689
#if !(defined(_AIX) || defined(__sun))
690
    {"max_locked_memory_bytes", RLIMIT_MEMLOCK},
691
#endif
692
#ifndef __sun
693
    {"max_memory_size_kbytes", RLIMIT_RSS},
694
#endif
695
    {"open_files", RLIMIT_NOFILE},
696
    {"stack_size_bytes", RLIMIT_STACK},
697
    {"cpu_time_seconds", RLIMIT_CPU},
698
#ifndef __sun
699
    {"max_user_processes", RLIMIT_NPROC},
700
#endif
701
#ifndef __OpenBSD__
702
    {"virtual_memory_kbytes", RLIMIT_AS}
703
#endif
704
  };
705
706
22
  writer->json_objectstart("userLimits");
707
  struct rlimit limit;
708
44
  std::string soft, hard;
709
710
242
  for (size_t i = 0; i < arraysize(rlimit_strings); i++) {
711
220
    if (getrlimit(rlimit_strings[i].id, &limit) == 0) {
712
220
      writer->json_objectstart(rlimit_strings[i].description);
713
714
220
      if (limit.rlim_cur == RLIM_INFINITY)
715
132
        writer->json_keyvalue("soft", "unlimited");
716
      else
717
88
        writer->json_keyvalue("soft", limit.rlim_cur);
718
719
220
      if (limit.rlim_max == RLIM_INFINITY)
720
154
        writer->json_keyvalue("hard", "unlimited");
721
      else
722
66
        writer->json_keyvalue("hard", limit.rlim_max);
723
724
220
      writer->json_objectend();
725
    }
726
  }
727
22
  writer->json_objectend();
728
#endif  // _WIN32
729
730
22
  PrintLoadedLibraries(writer);
731
22
}
732
733
// Report a list of loaded native libraries.
734
22
static void PrintLoadedLibraries(JSONWriter* writer) {
735
22
  writer->json_arraystart("sharedObjects");
736
  std::vector<std::string> modules =
737
44
      NativeSymbolDebuggingContext::GetLoadedLibraries();
738
22
  for (auto const& module_name : modules) writer->json_element(module_name);
739
22
  writer->json_arrayend();
740
22
}
741
742
// Obtain and report the node and subcomponent version strings.
743
22
static void PrintComponentVersions(JSONWriter* writer) {
744
44
  std::stringstream buf;
745
746
22
  writer->json_objectstart("componentVersions");
747
748
#define V(key)                                                                 \
749
  writer->json_keyvalue(#key, node::per_process::metadata.versions.key);
750
22
  NODE_VERSIONS_KEYS(V)
751
#undef V
752
753
22
  writer->json_objectend();
754
22
}
755
756
// Report runtime release information.
757
22
static void PrintRelease(JSONWriter* writer) {
758
22
  writer->json_objectstart("release");
759
22
  writer->json_keyvalue("name", node::per_process::metadata.release.name);
760
#if NODE_VERSION_IS_LTS
761
  writer->json_keyvalue("lts", node::per_process::metadata.release.lts);
762
#endif
763
764
#ifdef NODE_HAS_RELEASE_URLS
765
  writer->json_keyvalue("headersUrl",
766
                        node::per_process::metadata.release.headers_url);
767
  writer->json_keyvalue("sourceUrl",
768
                        node::per_process::metadata.release.source_url);
769
#ifdef _WIN32
770
  writer->json_keyvalue("libUrl", node::per_process::metadata.release.lib_url);
771
#endif  // _WIN32
772
#endif  // NODE_HAS_RELEASE_URLS
773
774
22
  writer->json_objectend();
775
22
}
776
777

14970
}  // namespace report