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: 338 341 99.1 %
Date: 2020-06-24 22:13:30 Branches: 73 96 76.0 %

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

40
  if ((!strcmp(trigger, "FatalError")) ||
467
20
      (!strcmp(trigger, "Signal"))) {
468
1
    ss = "No stack.\nUnavailable.\n";
469
  } else {
470
38
    String::Utf8Value sv(isolate, stackstr);
471
19
    ss = std::string(*sv, sv.length());
472
  }
473
20
  int line = ss.find('\n');
474
20
  if (line == -1) {
475
4
    writer->json_keyvalue("message", ss);
476
  } else {
477
32
    std::string l = ss.substr(0, line);
478
16
    writer->json_keyvalue("message", l);
479
16
    writer->json_arraystart("stack");
480
16
    ss = ss.substr(line + 1);
481
16
    line = ss.find('\n');
482
218
    while (line != -1) {
483
101
      l = ss.substr(0, line);
484
501
      l.erase(l.begin(), std::find_if(l.begin(), l.end(), [](int ch) {
485
501
                return !std::iswspace(ch);
486
602
              }));
487
101
      writer->json_element(l);
488
101
      ss = ss.substr(line + 1);
489
101
      line = ss.find('\n');
490
    }
491
16
    writer->json_arrayend();
492
  }
493
20
  writer->json_objectend();
494
20
}
495
496
// Report a native stack backtrace
497
20
static void PrintNativeStack(JSONWriter* writer) {
498
40
  auto sym_ctx = NativeSymbolDebuggingContext::New();
499
  void* frames[256];
500
20
  const int size = sym_ctx->GetStackTrace(frames, arraysize(frames));
501
20
  writer->json_arraystart("nativeStack");
502
  int i;
503
175
  for (i = 1; i < size; i++) {
504
155
    void* frame = frames[i];
505
155
    writer->json_start();
506
    writer->json_keyvalue("pc",
507
155
                          ValueToHexString(reinterpret_cast<uintptr_t>(frame)));
508
155
    writer->json_keyvalue("symbol", sym_ctx->LookupSymbol(frame).Display());
509
155
    writer->json_end();
510
  }
511
20
  writer->json_arrayend();
512
20
}
513
514
// Report V8 JavaScript heap information.
515
// This uses the existing V8 HeapStatistics and HeapSpaceStatistics APIs.
516
// The isolate->GetGCStatistics(&heap_stats) internal V8 API could potentially
517
// provide some more useful information - the GC history and the handle counts
518
20
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
519
20
  HeapStatistics v8_heap_stats;
520
20
  isolate->GetHeapStatistics(&v8_heap_stats);
521
20
  HeapSpaceStatistics v8_heap_space_stats;
522
523
20
  writer->json_objectstart("javascriptHeap");
524
20
  writer->json_keyvalue("totalMemory", v8_heap_stats.total_heap_size());
525
  writer->json_keyvalue("totalCommittedMemory",
526
20
                        v8_heap_stats.total_physical_size());
527
20
  writer->json_keyvalue("usedMemory", v8_heap_stats.used_heap_size());
528
  writer->json_keyvalue("availableMemory",
529
20
                        v8_heap_stats.total_available_size());
530
20
  writer->json_keyvalue("memoryLimit", v8_heap_stats.heap_size_limit());
531
532
20
  writer->json_objectstart("heapSpaces");
533
  // Loop through heap spaces
534
180
  for (size_t i = 0; i < isolate->NumberOfHeapSpaces(); i++) {
535
160
    isolate->GetHeapSpaceStatistics(&v8_heap_space_stats, i);
536
160
    writer->json_objectstart(v8_heap_space_stats.space_name());
537
160
    writer->json_keyvalue("memorySize", v8_heap_space_stats.space_size());
538
    writer->json_keyvalue(
539
        "committedMemory",
540
160
        v8_heap_space_stats.physical_space_size());
541
    writer->json_keyvalue(
542
        "capacity",
543
480
        v8_heap_space_stats.space_used_size() +
544
320
            v8_heap_space_stats.space_available_size());
545
160
    writer->json_keyvalue("used", v8_heap_space_stats.space_used_size());
546
    writer->json_keyvalue(
547
160
        "available", v8_heap_space_stats.space_available_size());
548
160
    writer->json_objectend();
549
  }
550
551
20
  writer->json_objectend();
552
20
  writer->json_objectend();
553
20
}
554
555
20
static void PrintResourceUsage(JSONWriter* writer) {
556
  // Get process uptime in seconds
557
  uint64_t uptime =
558
20
      (uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
559
20
  if (uptime == 0) uptime = 1;  // avoid division by zero.
560
561
  // Process and current thread usage statistics
562
  uv_rusage_t rusage;
563
20
  writer->json_objectstart("resourceUsage");
564
20
  if (uv_getrusage(&rusage) == 0) {
565
    double user_cpu =
566
20
        rusage.ru_utime.tv_sec + SEC_PER_MICROS * rusage.ru_utime.tv_usec;
567
    double kernel_cpu =
568
20
        rusage.ru_stime.tv_sec + SEC_PER_MICROS * rusage.ru_stime.tv_usec;
569
20
    writer->json_keyvalue("userCpuSeconds", user_cpu);
570
20
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
571
20
    double cpu_abs = user_cpu + kernel_cpu;
572
20
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
573
20
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
574
20
    writer->json_keyvalue("maxRss", rusage.ru_maxrss * 1024);
575
20
    writer->json_objectstart("pageFaults");
576
20
    writer->json_keyvalue("IORequired", rusage.ru_majflt);
577
20
    writer->json_keyvalue("IONotRequired", rusage.ru_minflt);
578
20
    writer->json_objectend();
579
20
    writer->json_objectstart("fsActivity");
580
20
    writer->json_keyvalue("reads", rusage.ru_inblock);
581
20
    writer->json_keyvalue("writes", rusage.ru_oublock);
582
20
    writer->json_objectend();
583
  }
584
20
  writer->json_objectend();
585
#ifdef RUSAGE_THREAD
586
  struct rusage stats;
587
20
  if (getrusage(RUSAGE_THREAD, &stats) == 0) {
588
20
    writer->json_objectstart("uvthreadResourceUsage");
589
    double user_cpu =
590
20
        stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
591
    double kernel_cpu =
592
20
        stats.ru_stime.tv_sec + SEC_PER_MICROS * stats.ru_stime.tv_usec;
593
20
    writer->json_keyvalue("userCpuSeconds", user_cpu);
594
20
    writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
595
20
    double cpu_abs = user_cpu + kernel_cpu;
596
20
    double cpu_percentage = (cpu_abs / uptime) * 100.0;
597
20
    writer->json_keyvalue("cpuConsumptionPercent", cpu_percentage);
598
20
    writer->json_objectstart("fsActivity");
599
20
    writer->json_keyvalue("reads", stats.ru_inblock);
600
20
    writer->json_keyvalue("writes", stats.ru_oublock);
601
20
    writer->json_objectend();
602
20
    writer->json_objectend();
603
  }
604
#endif
605
20
}
606
607
// Report operating system information.
608
20
static void PrintSystemInformation(JSONWriter* writer) {
609
  uv_env_item_t* envitems;
610
  int envcount;
611
  int r;
612
613
20
  writer->json_objectstart("environmentVariables");
614
615
  {
616
40
    Mutex::ScopedLock lock(node::per_process::env_var_mutex);
617
20
    r = uv_os_environ(&envitems, &envcount);
618
  }
619
620
20
  if (r == 0) {
621
1400
    for (int i = 0; i < envcount; i++)
622
1380
      writer->json_keyvalue(envitems[i].name, envitems[i].value);
623
624
20
    uv_os_free_environ(envitems, envcount);
625
  }
626
627
20
  writer->json_objectend();
628
629
#ifndef _WIN32
630
  static struct {
631
    const char* description;
632
    int id;
633
  } rlimit_strings[] = {
634
    {"core_file_size_blocks", RLIMIT_CORE},
635
    {"data_seg_size_kbytes", RLIMIT_DATA},
636
    {"file_size_blocks", RLIMIT_FSIZE},
637
#if !(defined(_AIX) || defined(__sun))
638
    {"max_locked_memory_bytes", RLIMIT_MEMLOCK},
639
#endif
640
#ifndef __sun
641
    {"max_memory_size_kbytes", RLIMIT_RSS},
642
#endif
643
    {"open_files", RLIMIT_NOFILE},
644
    {"stack_size_bytes", RLIMIT_STACK},
645
    {"cpu_time_seconds", RLIMIT_CPU},
646
#ifndef __sun
647
    {"max_user_processes", RLIMIT_NPROC},
648
#endif
649
#ifndef __OpenBSD__
650
    {"virtual_memory_kbytes", RLIMIT_AS}
651
#endif
652
  };
653
654
20
  writer->json_objectstart("userLimits");
655
  struct rlimit limit;
656
40
  std::string soft, hard;
657
658
220
  for (size_t i = 0; i < arraysize(rlimit_strings); i++) {
659
200
    if (getrlimit(rlimit_strings[i].id, &limit) == 0) {
660
200
      writer->json_objectstart(rlimit_strings[i].description);
661
662
200
      if (limit.rlim_cur == RLIM_INFINITY)
663
120
        writer->json_keyvalue("soft", "unlimited");
664
      else
665
80
        writer->json_keyvalue("soft", limit.rlim_cur);
666
667
200
      if (limit.rlim_max == RLIM_INFINITY)
668
140
        writer->json_keyvalue("hard", "unlimited");
669
      else
670
60
        writer->json_keyvalue("hard", limit.rlim_max);
671
672
200
      writer->json_objectend();
673
    }
674
  }
675
20
  writer->json_objectend();
676
#endif  // _WIN32
677
678
20
  PrintLoadedLibraries(writer);
679
20
}
680
681
// Report a list of loaded native libraries.
682
20
static void PrintLoadedLibraries(JSONWriter* writer) {
683
20
  writer->json_arraystart("sharedObjects");
684
  std::vector<std::string> modules =
685
40
      NativeSymbolDebuggingContext::GetLoadedLibraries();
686
20
  for (auto const& module_name : modules) writer->json_element(module_name);
687
20
  writer->json_arrayend();
688
20
}
689
690
// Obtain and report the node and subcomponent version strings.
691
20
static void PrintComponentVersions(JSONWriter* writer) {
692
40
  std::stringstream buf;
693
694
20
  writer->json_objectstart("componentVersions");
695
696
#define V(key)                                                                 \
697
  writer->json_keyvalue(#key, node::per_process::metadata.versions.key);
698
20
  NODE_VERSIONS_KEYS(V)
699
#undef V
700
701
20
  writer->json_objectend();
702
20
}
703
704
// Report runtime release information.
705
20
static void PrintRelease(JSONWriter* writer) {
706
20
  writer->json_objectstart("release");
707
20
  writer->json_keyvalue("name", node::per_process::metadata.release.name);
708
#if NODE_VERSION_IS_LTS
709
  writer->json_keyvalue("lts", node::per_process::metadata.release.lts);
710
#endif
711
712
#ifdef NODE_HAS_RELEASE_URLS
713
  writer->json_keyvalue("headersUrl",
714
                        node::per_process::metadata.release.headers_url);
715
  writer->json_keyvalue("sourceUrl",
716
                        node::per_process::metadata.release.source_url);
717
#ifdef _WIN32
718
  writer->json_keyvalue("libUrl", node::per_process::metadata.release.lib_url);
719
#endif  // _WIN32
720
#endif  // NODE_HAS_RELEASE_URLS
721
722
20
  writer->json_objectend();
723
20
}
724
725

13197
}  // namespace report