npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

posix-clock

v3.0.0

Published

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

Downloads

87

Readme

POSIX clock_*() for NodeJS Build Status

Installation

npm install posix-clock

WARNING: if you want to use a module with NodeJS before v12, then use module version 2:

npm install posix-clock@2

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