GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/util.cc Lines: 101 113 89.4 %
Date: 2019-05-05 22:32:45 Branches: 21 38 55.3 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "util.h"
23
24
#include "node_buffer.h"
25
#include "node_errors.h"
26
#include "node_internals.h"
27
#include "string_bytes.h"
28
#include "uv.h"
29
30
#ifdef _WIN32
31
#include <io.h>  // _S_IREAD _S_IWRITE
32
#include <time.h>
33
#ifndef S_IRUSR
34
#define S_IRUSR _S_IREAD
35
#endif  // S_IRUSR
36
#ifndef S_IWUSR
37
#define S_IWUSR _S_IWRITE
38
#endif  // S_IWUSR
39
#else
40
#include <sys/time.h>
41
#include <sys/types.h>
42
#endif
43
44
#include <atomic>
45
#include <cstdio>
46
#include <iomanip>
47
#include <sstream>
48
49
static std::atomic_int seq = {0};  // Sequence number for diagnostic filenames.
50
51
namespace node {
52
53
using v8::ArrayBufferView;
54
using v8::Isolate;
55
using v8::Local;
56
using v8::String;
57
using v8::Value;
58
59
template <typename T>
60
4252856
static void MakeUtf8String(Isolate* isolate,
61
                           Local<Value> value,
62
                           T* target) {
63
  Local<String> string;
64

12758570
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
65
66
  size_t storage;
67

8505713
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
68
4252856
  storage += 1;
69
4252856
  target->AllocateSufficientStorage(storage);
70
  const int flags =
71
4252854
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
72
  const int length =
73
8505707
      string->WriteUtf8(isolate, target->out(), storage, nullptr, flags);
74
4252854
  target->SetLengthAndZeroTerminate(length);
75
}
76
77
4017283
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
78
4017286
  if (value.IsEmpty())
79
4019253
    return;
80
81
4015318
  MakeUtf8String(isolate, value, this);
82
}
83
84
85
547
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
86
547
  if (value.IsEmpty()) {
87
    return;
88
  }
89
90
  Local<String> string;
91
1641
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
92
93
  // Allocate enough space to include the null terminator
94
547
  const size_t storage = string->Length() + 1;
95
547
  AllocateSufficientStorage(storage);
96
97
547
  const int flags = String::NO_NULL_TERMINATION;
98
1094
  const int length = string->Write(isolate, out(), 0, storage, flags);
99
547
  SetLengthAndZeroTerminate(length);
100
}
101
102
250967
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
103
  // Slightly different take on Utf8Value. If value is a String,
104
  // it will return a Utf8 encoded string. If value is a Buffer,
105
  // it will copy the data out of the Buffer as is.
106
250967
  if (value.IsEmpty()) {
107
    // Dereferencing this object will return nullptr.
108
    Invalidate();
109
250967
    return;
110
  }
111
112
501934
  if (value->IsString()) {
113
237541
    MakeUtf8String(isolate, value, this);
114
13426
  } else if (value->IsArrayBufferView()) {
115
26852
    const size_t len = value.As<ArrayBufferView>()->ByteLength();
116
    // Leave place for the terminating '\0' byte.
117
13426
    AllocateSufficientStorage(len + 1);
118
40278
    value.As<ArrayBufferView>()->CopyContents(out(), len);
119
13426
    SetLengthAndZeroTerminate(len);
120
  } else {
121
    Invalidate();
122
  }
123
}
124
125
void LowMemoryNotification() {
126
  if (per_process::v8_initialized) {
127
    auto isolate = Isolate::GetCurrent();
128
    if (isolate != nullptr) {
129
      isolate->LowMemoryNotification();
130
    }
131
  }
132
}
133
134
4479
std::string GetHumanReadableProcessName() {
135
  char name[1024];
136
4479
  GetHumanReadableProcessName(&name);
137
4479
  return name;
138
}
139
140
4479
void GetHumanReadableProcessName(char (*name)[1024]) {
141
  // Leave room after title for pid, which can be up to 20 digits for 64 bit.
142
4479
  char title[1000] = "Node.js";
143
4479
  uv_get_process_title(title, sizeof(title));
144
4479
  snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
145
4479
}
146
147
70
std::vector<std::string> SplitString(const std::string& in, char delim) {
148
70
  std::vector<std::string> out;
149
70
  if (in.empty())
150
3
    return out;
151
134
  std::istringstream in_stream(in);
152
209
  while (in_stream.good()) {
153
75
    std::string item;
154
75
    std::getline(in_stream, item, delim);
155
75
    if (item.empty()) continue;
156
75
    out.emplace_back(std::move(item));
157
75
  }
158
67
  return out;
159
}
160
161
void ThrowErrStringTooLong(Isolate* isolate) {
162
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
163
}
164
165
8982
double GetCurrentTimeInMicroseconds() {
166
8982
  constexpr double kMicrosecondsPerSecond = 1e6;
167
  uv_timeval64_t tv;
168
8982
  CHECK_EQ(0, uv_gettimeofday(&tv));
169
8982
  return kMicrosecondsPerSecond * tv.tv_sec + tv.tv_usec;
170
}
171
172
4467
int WriteFileSync(const char* path, uv_buf_t buf) {
173
  uv_fs_t req;
174
  int fd = uv_fs_open(nullptr,
175
                      &req,
176
                      path,
177
                      O_WRONLY | O_CREAT | O_TRUNC,
178
                      S_IWUSR | S_IRUSR,
179
4467
                      nullptr);
180
4467
  uv_fs_req_cleanup(&req);
181
4467
  if (fd < 0) {
182
    return fd;
183
  }
184
185
4467
  int err = uv_fs_write(nullptr, &req, fd, &buf, 1, 0, nullptr);
186
4467
  uv_fs_req_cleanup(&req);
187
4467
  if (err < 0) {
188
    return err;
189
  }
190
191
4467
  err = uv_fs_close(nullptr, &req, fd, nullptr);
192
4467
  uv_fs_req_cleanup(&req);
193
4467
  return err;
194
}
195
196
4467
int WriteFileSync(v8::Isolate* isolate,
197
                  const char* path,
198
                  v8::Local<v8::String> string) {
199
4467
  node::Utf8Value utf8(isolate, string);
200
4467
  uv_buf_t buf = uv_buf_init(utf8.out(), utf8.length());
201
4467
  return WriteFileSync(path, buf);
202
}
203
204
29
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
205
#ifdef _WIN32
206
  GetLocalTime(tm_struct);
207
#else  // UNIX, OSX
208
  struct timeval time_val;
209
29
  gettimeofday(&time_val, nullptr);
210
29
  localtime_r(&time_val.tv_sec, tm_struct);
211
#endif
212
29
}
213
214
// Defined in node_internals.h
215
16
std::string DiagnosticFilename::MakeFilename(
216
    uint64_t thread_id,
217
    const char* prefix,
218
    const char* ext) {
219
16
  std::ostringstream oss;
220
  TIME_TYPE tm_struct;
221
16
  LocalTime(&tm_struct);
222
16
  oss << prefix;
223
#ifdef _WIN32
224
  oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear;
225
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth;
226
  oss << std::setfill('0') << std::setw(2) << tm_struct.wDay;
227
  oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour;
228
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute;
229
  oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond;
230
#else  // UNIX, OSX
231
16
  oss << "."
232
32
            << std::setfill('0')
233
32
            << std::setw(4)
234
32
            << tm_struct.tm_year + 1900;
235
16
  oss << std::setfill('0')
236
32
            << std::setw(2)
237
32
            << tm_struct.tm_mon + 1;
238
16
  oss << std::setfill('0')
239
32
            << std::setw(2)
240
32
            << tm_struct.tm_mday;
241
16
  oss << "."
242
32
            << std::setfill('0')
243
32
            << std::setw(2)
244
32
            << tm_struct.tm_hour;
245
16
  oss << std::setfill('0')
246
32
            << std::setw(2)
247
32
            << tm_struct.tm_min;
248
16
  oss << std::setfill('0')
249
32
            << std::setw(2)
250
32
            << tm_struct.tm_sec;
251
#endif
252
16
  oss << "." << uv_os_getpid();
253
16
  oss << "." << thread_id;
254
16
  oss << "." << std::setfill('0') << std::setw(3) << ++seq;
255
16
  oss << "." << ext;
256
16
  return oss.str();
257
}
258
259
}  // namespace node