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: 330 333 99.1 %
Date: 2020-02-27 22:14:15 Branches: 77 106 72.6 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_report.h"
3
#include "debug_utils-inl.h"
4
#include "diagnosticfilename-inl.h"
5
#include "node_internals.h"
6
#include "node_metadata.h"
7
#include "node_mutex.h"
8
#include "node_worker.h"
9
#include "util.h"
10
11
#ifdef _WIN32
12
#include <Windows.h>
13
#else  // !_WIN32
14
#include <sys/resource.h>
15
#include <cxxabi.h>
16
#include <dlfcn.h>
17
#endif
18
19
#include <cstring>
20
#include <ctime>
21
#include <cwctype>
22
#include <fstream>
23
24
constexpr int NODE_REPORT_VERSION = 2;
25
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
26
constexpr double SEC_PER_MICROS = 1e-6;
27
28
namespace report {
29
using node::arraysize;
30
using node::ConditionVariable;
31
using node::DiagnosticFilename;
32
using node::Environment;
33
using node::Mutex;
34
using node::NativeSymbolDebuggingContext;
35
using node::PerIsolateOptions;
36
using node::TIME_TYPE;
37
using node::worker::Worker;
38
using v8::HeapSpaceStatistics;
39
using v8::HeapStatistics;
40
using v8::Isolate;
41
using v8::Local;
42
using v8::Number;
43
using v8::StackTrace;
44
using v8::String;
45
using v8::V8;
46
using v8::Value;
47
48
// Internal/static function declarations
49
static void WriteNodeReport(Isolate* isolate,
50
                            Environment* env,
51
                            const char* message,
52
                            const char* trigger,
53
                            const std::string& filename,
54
                            std::ostream& out,
55
                            Local<String> stackstr);
56
static void PrintVersionInformation(JSONWriter* writer);
57
static void PrintJavaScriptStack(JSONWriter* writer,
58
                                 Isolate* isolate,
59
                                 Local<String> stackstr,
60
                                 const char* trigger);
61
static void PrintNativeStack(JSONWriter* writer);
62
static void PrintResourceUsage(JSONWriter* writer);
63
static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate);
64
static void PrintSystemInformation(JSONWriter* writer);
65
static void PrintLoadedLibraries(JSONWriter* writer);
66
static void PrintComponentVersions(JSONWriter* writer);
67
static void PrintRelease(JSONWriter* writer);
68
static void PrintCpuInfo(JSONWriter* writer);
69
static void PrintNetworkInterfaceInfo(JSONWriter* writer);
70
71
// External function to trigger a report, writing to file.
72
// The 'name' parameter is in/out: an input filename is used
73
// if supplied, and the actual filename is returned.
74
11
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
22
  std::string filename;
81
22
  std::shared_ptr<PerIsolateOptions> options;
82
11
  if (env != nullptr) options = env->isolate_data()->options();
83
84
  // Determine the required report filename. In order of priority:
85
  //   1) supplied on API 2) configured on startup 3) default generated
86
11
  if (!name.empty()) {
87
    // Filename was specified as API parameter.
88
4
    filename = name;
89

7
  } else if (env != nullptr && options->report_filename.length() > 0) {
90
    // File name was supplied via start-up option.
91
1
    filename = options->report_filename;
92
  } else {
93
12
    filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0,
94
6
                                   "report", "json");
95
  }
96
97
  // Open the report file stream for writing. Supports stdout/err,
98
  // user-specified or (default) generated name
99
22
  std::ofstream outfile;
100
  std::ostream* outstream;
101
11
  if (filename == "stdout") {
102
1
    outstream = &std::cout;
103
10
  } else if (filename == "stderr") {
104
1
    outstream = &std::cerr;
105
  } else {
106
    // Regular file. Append filename to directory path if one was specified
107

9
    if (env != nullptr && options->report_directory.length() > 0) {
108
18
      std::string pathname = options->report_directory;
109
9
      pathname += node::kPathSeparator;
110
9
      pathname += filename;
111
9
      outfile.open(pathname, std::ios::out | std::ios::binary);
112
    } else {
113
      outfile.open(filename, std::ios::out | std::ios::binary);
114
    }
115
    // Check for errors on the file open
116
9
    if (!outfile.is_open()) {
117
1
      std::cerr << "\nFailed to open Node.js report file: " << filename;
118
119

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

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

12606
}  // namespace report