GithubHelp home page GithubHelp logo

avz / node-posix-clock Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 2.0 244 KB

POSIX clock_gettime(2), clock_getres(2) and clock_nanosleep(2) for NodeJS

License: MIT License

Python 3.57% JavaScript 45.17% C++ 51.26%

node-posix-clock's Introduction

POSIX clock_*() for NodeJS Build Status

Installation

npm install posix-clock

Examples

clock_getres()

var clock = require('posix-clock');

var clockResolution = clock.getres(clock.MONOTONIC);
console.log(
	'Resolution of CLOCK_MONOTONIC: '
		+ clockResolution.sec + ' sec and '
		+ clockResolution.nsec + ' nanosec.'
	, clockResolution
);
Resolution of CLOCK_MONOTONIC: 0 sec and 1 nanosec. { sec: 0, nsec: 1 }

clock_gettime()

var clock = require('posix-clock');

var clockTime = clock.gettime(clock.MONOTONIC);
console.log(
	'Time from CLOCK_MONOTONIC: '
		+ clockTime.sec + ' sec and '
		+ clockTime.nsec + ' nanosec.'
	, clockTime
);
Time from CLOCK_MONOTONIC: 15224 sec and 557776233 nanosec. { sec: 15224, nsec: 557776233 }

clock_nanosleep()

var clock = require('posix-clock');

// sleep until 13 Feb 2009 23:31:30 UTC (Unix Timestamp = 1234567890)
clock.nanosleep(
	clock.REALTIME,
	clock.TIMER_ABSTIME,
	{
		sec: 1234567890,
		nsec: 0
	}
);

// sleep at least 10 seconds and 123 nanoseconds
clock.nanosleep(
	clock.REALTIME,
	0,
	{
		sec: 10,
		nsec: 123
	}
);

API

Methods

  • gettime(clockId) - the function retrieve the time from the specified clock clockId. See man 2 clock_gettime for more details.
  • getres(clockId) - the function return the resolution (precision) of the specified clock clockId. The resolution of clocks depends on the implementation and cannot be configured by a particular process. See man 2 clock_getres for more details.
  • nanosleep(clockId, flags, sleepTime) - high resolution sleep with specifiable clock. If the flag TIMER_ABSTIME is not set in the flags argument, the nanosleep() function shall cause the current thread to be suspended from execution until either the time interval specified by the sleepTime argument has elapsed, or a signal is delivered to the calling thread and its action is to invoke a signal-catching function, or the process is terminated. The clock used to measure the time shall be the clock specified by clockId. See man 2 clock_nanosleep for more details. On non-linux OS only nanosleep(REALTIME, 0, {...}) is supported.

Clocks

  • REALTIME - system-wide clock that measures real (i.e., wall-clock) time. This clock is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.

  • MONOTONIC - clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.

Linux-specific

  • PROCESS_CPUTIME_ID - since Linux 2.6.12. High-resolution per-process timer from the CPU.

  • THREAD_CPUTIME_ID - since Linux 2.6.12. Thread-specific CPU-time clock.

  • REALTIME_COARSE - since Linux 2.6.32; Linux-specific. A faster but less precise version of REALTIME. Use when you need very fast, but not fine-grained timestamps.

  • MONOTONIC_COARSE - since Linux 2.6.32; Linux-specific. A faster but less precise version of MONOTONIC. Use when you need very fast, but not fine-grained timestamps.

  • MONOTONIC_RAW - since Linux 2.6.28; Linux-specific. Similar to MONOTONIC, but provides access to a raw hardware-based time that is not subject to NTP adjustments or the incremental adjustments performed by adjtime(3).

  • BOOTTIME - since Linux 2.6.39; Linux-specific Identical to MONOTONIC, except it also includes any time that the system is suspended. This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of REALTIME, which may have discontinuities if the time is changed using settimeofday(2).

FreeBSD-specific

  • REALTIME_FAST - analog of REALTIME but do not perform a full time counter query, so their accuracy is one timer tick
  • REALTIME_PRECISE - analog of REALTIME but get the most exact value as possible, at the expense of execution time
  • MONOTONIC_FAST - analog of MONOTONIC but do not perform a full time counter query, so their accuracy is one timer tick
  • MONOTONIC_PRECISE - analog of MONOTONIC but get the most exact value as possible, at the expense of execution time
  • UPTIME - which starts at zero when the kernel boots and increments monotonically in SI seconds while the machine is running
  • UPTIME_FAST - analog of UPTIME but do not perform a full time counter query, so their accuracy is one timer tick
  • UPTIME_PRECISE - analog of UPTIME but get the most exact value as possible, at the expense of execution time
  • SECOND - returns the current second without performing a full time counter query, using in-kernel cached value of current second.
  • PROF - for time that increments when the CPU is running in user or kernel mode

node-posix-clock's People

Contributors

avz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

oliwer tufosa

node-posix-clock's Issues

Node 12 support

Hi,

Thanks for this project!

After upgrading to Node 12, I noticed node-posix-clock is incompatible. Here are the errors I'm getting:

> node-gyp rebuild

  CXX(target) Release/obj.target/posix-clock/src/posix-clock.o
../src/posix-clock.cpp:32:53: error: too few arguments to function call, single argument
      'context' was not specified
        clockid_t clockId = (clockid_t)info[0]->Int32Value();
                                       ~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2569:3: note: 'Int32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:47:2: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        AVZ_FILL_TIMESPEC(obj, ts.tv_sec, ts.tv_nsec);
        ^
../src/posix-clock.cpp:15:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("sec").ToLocalChecked(), Nan::New<Number>(sec)); \
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:47:2: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        AVZ_FILL_TIMESPEC(obj, ts.tv_sec, ts.tv_nsec);
        ^
../src/posix-clock.cpp:16:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("nsec").ToLocalChecked(), Nan::New<Number>(nsec));
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:60:53: error: too few arguments to function call, single argument
      'context' was not specified
        clockid_t clockId = (clockid_t)info[0]->Int32Value();
                                       ~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2569:3: note: 'Int32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:75:2: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        AVZ_FILL_TIMESPEC(obj, ts.tv_sec, ts.tv_nsec);
        ^
../src/posix-clock.cpp:15:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("sec").ToLocalChecked(), Nan::New<Number>(sec)); \
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:75:2: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        AVZ_FILL_TIMESPEC(obj, ts.tv_sec, ts.tv_nsec);
        ^
../src/posix-clock.cpp:16:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("nsec").ToLocalChecked(), Nan::New<Number>(nsec));
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:89:53: error: too few arguments to function call, single argument
      'context' was not specified
        clockid_t clockId = (clockid_t)info[0]->Int32Value();
                                       ~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2569:3: note: 'Int32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:96:34: error: too few arguments to function call, single argument
      'context' was not specified
        int flags = info[1]->Int32Value();
                    ~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2569:3: note: 'Int32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<int32_t> Int32Value(Local<Context> context) const;
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:107:36: error: no matching member function for call to 'ToObject'
        Local<Object> objSleep = info[2]->ToObject();
                                 ~~~~~~~~~^~~~~~~~
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2532:44: note: candidate function not
      viable: requires single argument 'context', but no arguments were provided
  V8_WARN_UNUSED_RESULT MaybeLocal<Object> ToObject(
                                           ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2546:35: note: candidate function not
      viable: requires single argument 'isolate', but no arguments were provided
                    Local<Object> ToObject(Isolate* isolate) const);
                                  ^
../src/posix-clock.cpp:108:36: warning: 'Get' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        Local<Value> secValue = objSleep->Get(Nan::New("sec").ToLocalChecked());
                                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3412:3: note: 'Get' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:109:37: warning: 'Get' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
        Local<Value> nsecValue = objSleep->Get(Nan::New("nsec").ToLocalChecked());
                                           ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3412:3: note: 'Get' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version", Local<Value> Get(Local<Value> key));
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:123:53: error: too few arguments to function call, single argument
      'context' was not specified
        sleepTimeTs.tv_sec = (time_t)secValue->Uint32Value();
                                     ~~~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2567:3: note: 'Uint32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:124:53: error: too few arguments to function call, single argument
      'context' was not specified
        sleepTimeTs.tv_nsec = (long)nsecValue->Uint32Value();
                                    ~~~~~~~~~~~~~~~~~~~~~~ ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:2567:3: note: 'Uint32Value' declared here
  V8_WARN_UNUSED_RESULT Maybe<uint32_t> Uint32Value(
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:347:31: note: expanded from macro
      'V8_WARN_UNUSED_RESULT'
#define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
                              ^
../src/posix-clock.cpp:177:4: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
                        AVZ_FILL_TIMESPEC(obj, remainingTimeTs.tv_sec, remainingTimeTs.tv_nsec);
                        ^
../src/posix-clock.cpp:15:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("sec").ToLocalChecked(), Nan::New<Number>(sec)); \
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:177:4: warning: 'Set' is deprecated: Use maybe version
      [-Wdeprecated-declarations]
                        AVZ_FILL_TIMESPEC(obj, remainingTimeTs.tv_sec, remainingTimeTs.tv_nsec);
                        ^
../src/posix-clock.cpp:16:13: note: expanded from macro 'AVZ_FILL_TIMESPEC'
                (target)->Set(Nan::New("nsec").ToLocalChecked(), Nan::New<Number>(nsec));
                          ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3358:3: note: 'Set' has been explicitly
      marked deprecated here
  V8_DEPRECATE_SOON("Use maybe version",
  ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8config.h:322:29: note: expanded from macro
      'V8_DEPRECATE_SOON'
  declarator __attribute__((deprecated(message)))
                            ^
../src/posix-clock.cpp:192:6: error: variable has incomplete type 'void'
void init(Handle<Object> exports) {
     ^
../src/posix-clock.cpp:192:11: error: use of undeclared identifier 'Handle'
void init(Handle<Object> exports) {
          ^
../src/posix-clock.cpp:192:18: error: 'Object' does not refer to a value
void init(Handle<Object> exports) {
                 ^
/Users/alexcastillo/.node-gyp/12.2.0/include/node/v8.h:3356:17: note: declared here
class V8_EXPORT Object : public Value {
                ^
../src/posix-clock.cpp:192:26: error: use of undeclared identifier 'exports'
void init(Handle<Object> exports) {
                         ^
../src/posix-clock.cpp:192:34: error: expected ';' after top level declarator
void init(Handle<Object> exports) {
                                 ^
                                 ;
8 warnings and 12 errors generated.
make: *** [Release/obj.target/posix-clock/src/posix-clock.o] Error 1

Any advice?

Node v16+ support

Hi,

Thanks again for this project! We've been using it for many years.

We are trying to upgrade to Node v16-19+, I noticed node-posix-clock is incompatible. Here are the errors I'm getting:

> [email protected] install /Users/**/*/node_modules/posix-clock
> node-gyp rebuild

  CC(target) Release/obj.target/nothing/../node-addon-api/src/nothing.o
  LIBTOOL-STATIC Release/nothing.a
env: python: No such file or directory
make: *** [Release/nothing.a] Error 127
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:314:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
gyp ERR! System Darwin 21.6.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/**/*/node_modules/posix-clock
gyp ERR! node -v v12.22.7
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 
npm WARN notsup Unsupported engine for @fastify/[email protected]: wanted: {"node":">=14"} (current: {"node":"12.22.7","npm":"6.14.15"})
npm WARN notsup Not compatible with your version of node/npm: @fastify/[email protected]

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/***/.npm/_logs/2023-01-28T00_00_18_341Z-debug.log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.