summaryrefslogtreecommitdiff
path: root/src/util.cc
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2019-03-23 07:39:52 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2019-04-06 12:01:45 +0800
commit864860e9f3d4eed0b0b81af55197d7e525ea6306 (patch)
treeed0c9cbc1cea99323cd4fa4c5385a11b262be8ee /src/util.cc
parentbaa54a5ae78ff04a3e8d8ac97c052304a6f6c18c (diff)
downloadandroid-node-v8-864860e9f3d4eed0b0b81af55197d7e525ea6306.tar.gz
android-node-v8-864860e9f3d4eed0b0b81af55197d7e525ea6306.tar.bz2
android-node-v8-864860e9f3d4eed0b0b81af55197d7e525ea6306.zip
src: port coverage serialization to C++
This patch moves the serialization of coverage profiles into C++. With this we no longer need to patch `process.reallyExit` and hook into the exit events, but instead hook into relevant places in C++ which are safe from user manipulation. This also makes the code easier to reuse for other types of profiles. PR-URL: https://github.com/nodejs/node/pull/26874 Reviewed-By: Ben Coe <bencoe@gmail.com>
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index 099911cf25..49c8a0f46a 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -28,7 +28,14 @@
#include "uv.h"
#ifdef _WIN32
+#include <io.h> // _S_IREAD _S_IWRITE
#include <time.h>
+#ifndef S_IRUSR
+#define S_IRUSR _S_IREAD
+#endif // S_IRUSR
+#ifndef S_IWUSR
+#define S_IWUSR _S_IWRITE
+#endif // S_IWUSR
#else
#include <sys/time.h>
#include <sys/types.h>
@@ -40,6 +47,9 @@
namespace node {
+// Microseconds in a second, as a float.
+#define MICROS_PER_SEC 1e6
+
using v8::ArrayBufferView;
using v8::Isolate;
using v8::Local;
@@ -152,6 +162,55 @@ void ThrowErrStringTooLong(Isolate* isolate) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
}
+double GetCurrentTimeInMicroseconds() {
+#ifdef _WIN32
+// The difference between the Unix Epoch and the Windows Epoch in 100-ns ticks.
+#define TICKS_TO_UNIX_EPOCH 116444736000000000LL
+ FILETIME ft;
+ GetSystemTimeAsFileTime(&ft);
+ uint64_t filetime_int =
+ static_cast<uint64_t>(ft.dwHighDateTime) << 32 | ft.dwLowDateTime;
+ // FILETIME is measured in terms of 100 ns. Convert that to 1 us (1000 ns).
+ return (filetime_int - TICKS_TO_UNIX_EPOCH) / 10.;
+#else
+ struct timeval tp;
+ gettimeofday(&tp, nullptr);
+ return MICROS_PER_SEC * tp.tv_sec + tp.tv_usec;
+#endif
+}
+
+int WriteFileSync(const char* path, uv_buf_t buf) {
+ uv_fs_t req;
+ int fd = uv_fs_open(nullptr,
+ &req,
+ path,
+ O_WRONLY | O_CREAT | O_TRUNC,
+ S_IWUSR | S_IRUSR,
+ nullptr);
+ uv_fs_req_cleanup(&req);
+ if (fd < 0) {
+ return fd;
+ }
+
+ int err = uv_fs_write(nullptr, &req, fd, &buf, 1, 0, nullptr);
+ uv_fs_req_cleanup(&req);
+ if (err < 0) {
+ return err;
+ }
+
+ err = uv_fs_close(nullptr, &req, fd, nullptr);
+ uv_fs_req_cleanup(&req);
+ return err;
+}
+
+int WriteFileSync(v8::Isolate* isolate,
+ const char* path,
+ v8::Local<v8::String> string) {
+ node::Utf8Value utf8(isolate, string);
+ uv_buf_t buf = uv_buf_init(utf8.out(), utf8.length());
+ return WriteFileSync(path, buf);
+}
+
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
#ifdef _WIN32
GetLocalTime(tm_struct);