示例#1
0
文件: lib.c 项目: jeffbuttars/timerfd
static PyObject * m_timerfd_gettime(PyObject *self, PyObject *args)
{
    long fd = 0;
    int t_res;
    struct itimerspec g_time;
    PyObject* resp;

    if (!PyArg_ParseTuple(args, "l", &fd)) {
        PyErr_SetString(
            PyExc_TypeError,
            "fd is not a valid type or the argument is missing");
        return NULL;
    }

    t_res = timerfd_gettime(fd, &g_time);
    if (t_res == -1) {
        return PyErr_SetFromErrno(PyExc_IOError);
    }

    resp = PyTuple_New(2);
    if (resp == NULL) {
        /* printf("bad tuple\n"); */
        return NULL;
    }

    PyTuple_SetItem(resp, 0,
                    PyDelta_FromDSU(0, g_time.it_value.tv_sec, NANO2MICRO(g_time.it_value.tv_nsec)));
    PyTuple_SetItem(resp, 1,
                    PyDelta_FromDSU(0, g_time.it_interval.tv_sec, NANO2MICRO(g_time.it_interval.tv_nsec)));

    return resp;
}//m_timerfd_gettime()
示例#2
0
文件: timerfd.c 项目: CarterTsai/rr
int main(void) {
  int fd = timerfd_create(CLOCK_MONOTONIC, 0);
  struct itimerspec spec, old;
  uint64_t num_expirations;

  atomic_printf("created timerfd %d\n", fd);
  test_assert(fd >= 0);

  memset(&spec, 0, sizeof(spec));
  spec.it_value.tv_nsec = 100000000;
  atomic_printf("setting timer to expire in {sec:%ld,nsec:%ld}\n",
                spec.it_value.tv_sec, spec.it_value.tv_nsec);
  timerfd_settime(fd, 0, &spec, &old);

  atomic_printf("  (old expiration was {sec:%ld,nsec:%ld})\n",
                old.it_value.tv_sec, old.it_value.tv_nsec);
  test_assert(0 == old.it_value.tv_sec && 0 == old.it_value.tv_nsec);

  atomic_puts("sleeping 50ms ...");
  usleep(50000);

  timerfd_gettime(fd, &old);
  atomic_printf("  expiration now in {sec:%ld,nsec:%ld})\n",
                old.it_value.tv_sec, old.it_value.tv_nsec);
  test_assert(0 == old.it_value.tv_sec && old.it_value.tv_nsec <= 50000000);

  atomic_puts("waiting for timer to expire ...");
  read(fd, &num_expirations, sizeof(num_expirations));

  atomic_printf("  timer expired %" PRIu64 " times\n", num_expirations);
  test_assert(1 == num_expirations);

  atomic_puts("EXIT-SUCCESS");
  return 0;
}
void CStdMultimediaTimerProc::Set()
{
	struct itimerspec nv, ov;
	timerfd_gettime(fd, &nv);
	nv.it_value.tv_sec = 0;
	nv.it_value.tv_nsec = 1;
	timerfd_settime(fd, 0, &nv, &ov);
}
示例#4
0
static int timerfd_timer_ack(int handle, unsigned int quantity)
{
	uint64_t expirations;
	int read_result = 0;
	int res = 0;
	struct timerfd_timer *our_timer, find_helper = {
		.handle = handle,
	};

	if (handle == -1) {
		ast_log(LOG_ERROR, "Attempting to ack timerfd handle -1");
		return -1;
	}

	if (!(our_timer = ao2_find(timerfd_timers, &find_helper, OBJ_POINTER))) {
		ast_log(LOG_ERROR, "Couldn't find a timer with handle %d\n", handle);
		return -1;
	}

	ao2_lock(our_timer);

	do {
		struct itimerspec timer_status;

		if (timerfd_gettime(handle, &timer_status)) {
			ast_log(LOG_ERROR, "Call to timerfd_gettime() using handle %d error: %s\n", handle, strerror(errno));
			expirations = 0;
			res = -1;
			break;
		}

		if (timer_status.it_value.tv_sec == 0 && timer_status.it_value.tv_nsec == 0) {
			ast_debug(1, "Avoiding read on disarmed timerfd %d\n", handle);
			expirations = 0;
			break;
		}

		read_result = read(handle, &expirations, sizeof(expirations));
		if (read_result == -1) {
			if (errno == EINTR || errno == EAGAIN) {
				continue;
			} else {
				ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
				res = -1;
				break;
			}
		}
	} while (read_result != sizeof(expirations));

	ao2_unlock(our_timer);
	ao2_ref(our_timer, -1);

	if (expirations != quantity) {
		ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
	}
	return res;
}
示例#5
0
int32_t
acrn_timer_gettime(struct acrn_timer *timer, struct itimerspec *cur_value)
{
	if (timer == NULL) {
		return -1;
	}

	return timerfd_gettime(timer->fd, cur_value);
}
示例#6
0
struct itimerspec timerfdObj::gettime()
{
	struct itimerspec its;

	if (timerfd_gettime(filedesc, &its) < 0)
		throw SYSEXCEPTION("timerfd_gettime");
	
	return its;
}
static int timerfd_timer_ack(void *data, unsigned int quantity)
{
	struct timerfd_timer *timer = data;
	uint64_t expirations;
	int read_result = 0;
	int res = 0;

	ao2_lock(timer);

	do {
		struct itimerspec timer_status;

		if (timerfd_gettime(timer->fd, &timer_status)) {
			ast_log(LOG_ERROR, "Call to timerfd_gettime() using handle %d error: %s\n", timer->fd, strerror(errno));
			expirations = 0;
			res = -1;
			break;
		}

		if (timer_status.it_value.tv_sec == 0 && timer_status.it_value.tv_nsec == 0) {
			ast_debug(1, "Avoiding read on disarmed timerfd %d\n", timer->fd);
			expirations = 0;
			break;
		}

		read_result = read(timer->fd, &expirations, sizeof(expirations));
		if (read_result == -1) {
			if (errno == EINTR || errno == EAGAIN) {
				continue;
			} else {
				ast_log(LOG_ERROR, "Read error: %s\n", strerror(errno));
				res = -1;
				break;
			}
		}
	} while (read_result != sizeof(expirations));

	ao2_unlock(timer);

	if (expirations != quantity) {
		ast_debug(2, "Expected to acknowledge %u ticks but got %llu instead\n", quantity, (unsigned long long) expirations);
	}

	return res;
}
示例#8
0
static void timerfd_gettime_verify(const struct test_case_t *test)
{
	TEST(timerfd_gettime(*test->fd, test->curr_value));

	if (TEST_RETURN != -1) {
		tst_resm(TFAIL, "timerfd_gettime() succeeded unexpectedly");
		return;
	}

	if (TEST_ERRNO == test->exp_errno) {
		tst_resm(TPASS | TTERRNO,
			 "timerfd_gettime() failed as expected");
	} else {
		tst_resm(TFAIL | TTERRNO,
			 "timerfd_gettime() failed unexpectedly; expected: "
			 "%d - %s", test->exp_errno, strerror(test->exp_errno));
	}
}
示例#9
0
int main(int ac, char **av)
{
  int i, tfd;
  long ticks;
  unsigned long long tnow, ttmr;
  u_int64_t uticks;
  struct itimerspec tmr;
  struct tmr_type clks[] =
  {
#if defined(HAVE_CLOCK_MONOTONIC)
    { CLOCK_MONOTONIC, "CLOCK MONOTONIC" },
#endif
    { CLOCK_REALTIME, "CLOCK REALTIME" },
  };

  for (i = 0; i < sizeof(clks) / sizeof(clks[0]); i++)
  {
    fprintf(stdout, "\n\n---------------------------------------\n");
    fprintf(stdout, "| testing %s\n", clks[i].name);
    fprintf(stdout, "---------------------------------------\n\n");

    fprintf(stdout, "relative timer test (at 500 ms) ...\n");
    set_timespec(&tmr.it_value, 500 * 1000);
    set_timespec(&tmr.it_interval, 0);
    tnow = getustime(clks[i].id);
    if ((tfd = timerfd_create(clks[i].id, 0)) == -1)
    {
      perror("timerfd_create");
      return 1;
    }

    if (timerfd_settime(tfd, 0, &tmr, NULL))
    {
      perror("timerfd_settime");
      return 1;
    }

    fprintf(stdout, "wating timer ...\n");
    ticks = waittmr(tfd, -1);
    ttmr = getustime(clks[i].id);
    if (ticks <= 0)
      fprintf(stdout, "whooops! no timer showed up!\n");
    else
      fprintf(stdout, "got timer ticks (%ld) after %.1f s\n",
              ticks, (ttmr - tnow) * 1e-6);


    fprintf(stdout, "absolute timer test (at 500 ms) ...\n");
    tnow = getustime(clks[i].id);
    set_timespec(&tmr.it_value, tnow + 500 * 1000);
    set_timespec(&tmr.it_interval, 0);
    if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL))
    {
      perror("timerfd_settime");
      return 1;
    }

    fprintf(stdout, "wating timer ...\n");
    ticks = waittmr(tfd, -1);
    ttmr = getustime(clks[i].id);
    if (ticks <= 0)
      fprintf(stdout, "whooops! no timer showed up!\n");
    else
      fprintf(stdout, "got timer ticks (%ld) after %.1f s\n",
              ticks, (ttmr - tnow) * 1e-6);

    fprintf(stdout, "sequential timer test (100 ms clock) ...\n");
    tnow = getustime(clks[i].id);
    set_timespec(&tmr.it_value, tnow + 100 * 1000);
    set_timespec(&tmr.it_interval, 100 * 1000);
    if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL))
    {
      perror("timerfd_settime");
      return 1;
    }

    fprintf(stdout, "sleeping one second ...\n");
    sleep(1);
    if (timerfd_gettime(tfd, &tmr))
    {
      perror("timerfd_gettime");
      return 1;
    }
    fprintf(stdout, "timerfd_gettime returned:\n"
            "\tit_value = %.1f it_interval = %.1f\n",
            tmr.it_value.tv_sec + 1e-9 * tmr.it_value.tv_nsec,
            tmr.it_interval.tv_sec + 1e-9 * tmr.it_interval.tv_nsec);
    fprintf(stdout, "sleeping 1 second ...\n");
    sleep(1);

    fprintf(stdout, "wating timer ...\n");
    ticks = waittmr(tfd, -1);
    ttmr = getustime(clks[i].id);
    if (ticks <= 0)
      fprintf(stdout, "whooops! no timer showed up!\n");
    else
      fprintf(stdout, "got timer ticks (%ld) after %.1f s\n",
              ticks, (ttmr - tnow) * 1e-6);


    fprintf(stdout, "O_NONBLOCK test ...\n");
    tnow = getustime(clks[i].id);
    set_timespec(&tmr.it_value, 100 * 1000);
    set_timespec(&tmr.it_interval, 0);
    if (timerfd_settime(tfd, 0, &tmr, NULL))
    {
      perror("timerfd_settime");
      return 1;
    }
#if 0
    fprintf(stdout, "timerfd = %d\n", tfd);
#endif

    fprintf(stdout, "wating timer (flush the single tick) ...\n");
    ticks = waittmr(tfd, -1);
    ttmr = getustime(clks[i].id);
    if (ticks <= 0)
      fprintf(stdout, "whooops! no timer showed up!\n");
    else
      fprintf(stdout, "got timer ticks (%ld) after %.1f s\n",
              ticks, (ttmr - tnow) * 1e-6);

    fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) | O_NONBLOCK);

    if (read(tfd, &uticks, sizeof(uticks)) > 0)
      fprintf(stdout, "whooops! timer ticks not zero when should have been\n");
    else if (errno != EAGAIN)
      fprintf(stdout, "whooops! bad errno value (%d = '%s')!\n",
              errno, strerror(errno));
    else
      fprintf(stdout, "success\n");

    fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) & ~O_NONBLOCK);

    close(tfd);
  }

  return 0;
}
示例#10
0
int main()
{
#ifdef __NR_timerfd_create
    int fd;
    struct itimerspec val, oval;

    fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
    //staptest// timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK) = NNNN

    timerfd_gettime(fd, &val);
    //staptest// timerfd_gettime (NNNN, XXXX) = 0

    val.it_value.tv_sec = 0;
    val.it_value.tv_nsec = 0;
    val.it_interval.tv_sec = 0;
    val.it_interval.tv_nsec = 0;
    timerfd_settime(fd, TFD_TIMER_ABSTIME, &val, &oval);
    //staptest// timerfd_settime (NNNN, TFD_TIMER_ABSTIME, \[0.000000,0.000000\], XXXX) = 0

    close(fd);
    //staptest// close (NNNN) = NNNN
    
    /* Limit testing. */

    fd = timerfd_create(-1, 0);
    //staptest// timerfd_create (0xffffffff, 0x0) = NNNN

    close(fd);
    //staptest// close (NNNN) = NNNN

    fd = timerfd_create(CLOCK_REALTIME, -1);
    //staptest// timerfd_create (CLOCK_REALTIME, TFD_[^ ]+|XXXX) = NNNN

    close(fd);
    //staptest// close (NNNN) = NNNN

    timerfd_gettime(-1, &val);
    //staptest// timerfd_gettime (-1, XXXX) = NNNN

    timerfd_gettime(-1, (struct itimerspec *)-1);
#ifdef __s390__
    //staptest// timerfd_gettime (-1, 0x[7]?[f]+) = NNNN
#else
    //staptest// timerfd_gettime (-1, 0x[f]+) = NNNN
#endif

    val.it_value.tv_sec = 0;
    val.it_value.tv_nsec = 0;
    val.it_interval.tv_sec = 0;
    val.it_interval.tv_nsec = 0;
    timerfd_settime(-1, TFD_TIMER_ABSTIME, &val, &oval);
    //staptest// timerfd_settime (-1, TFD_TIMER_ABSTIME, \[0.000000,0.000000\], XXXX) = NNNN

    timerfd_settime(-1, -1, &val, &oval);
    //staptest// timerfd_settime (-1, TFD_TIMER_[^ ]+|XXXX, \[0.000000,0.000000\], XXXX) = NNNN

    timerfd_settime(-1, TFD_TIMER_ABSTIME, (struct itimerspec *)-1, &oval);
#ifdef __s390__
    //staptest// timerfd_settime (-1, TFD_TIMER_ABSTIME, 0x[7]?[f]+, XXXX) = NNNN
#else
    //staptest// timerfd_settime (-1, TFD_TIMER_ABSTIME, 0x[f]+, XXXX) = NNNN
#endif

    timerfd_settime(-1, TFD_TIMER_ABSTIME, &val, (struct itimerspec *)-1);
#ifdef __s390__
    //staptest// timerfd_settime (-1, TFD_TIMER_ABSTIME, \[0.000000,0.000000\], 0x[7]?[f]+) = NNNN
#else
    //staptest// timerfd_settime (-1, TFD_TIMER_ABSTIME, \[0.000000,0.000000\], 0x[f]+) = NNNN
#endif
#endif
    return 0;
}
示例#11
0
int main(int ac, char **av)
{
	int i, tfd;
	long ticks;
	unsigned long long tnow, ttmr;
	u_int64_t uticks;
	struct itimerspec tmr;
	struct tmr_type clks[] = {
		{CLOCK_MONOTONIC, "CLOCK MONOTONIC"},
		{CLOCK_REALTIME, "CLOCK REALTIME"},
	};

	if ((tst_kvercmp(2, 6, 25)) < 0) {
		tst_resm(TCONF, "This test can only run on kernels that are ");
		tst_resm(TCONF, "2.6.25 and higher");
		exit(0);
	}

	for (i = 0; i < sizeof(clks) / sizeof(clks[0]); i++) {
		fprintf(stdout,
			"\n\n---------------------------------------\n");
		fprintf(stdout, "| testing %s\n", clks[i].name);
		fprintf(stdout, "---------------------------------------\n\n");

		fprintf(stdout, "relative timer test (at 500 ms) ...\n");
		set_timespec(&tmr.it_value, 500 * 1000);
		set_timespec(&tmr.it_interval, 0);
		tnow = getustime(clks[i].id);
		if ((tfd = timerfd_create(clks[i].id, 0)) == -1) {
			perror("timerfd");
			return 1;
		}
		fprintf(stdout, "timerfd = %d\n", tfd);

		if (timerfd_settime(tfd, 0, &tmr, NULL)) {
			perror("timerfd_settime");
			return 1;
		}

		fprintf(stdout, "wating timer ...\n");
		ticks = waittmr(tfd, -1);
		ttmr = getustime(clks[i].id);
		if (ticks <= 0)
			fprintf(stdout, "whooops! no timer showed up!\n");
		else
			fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
				ticks, (ttmr - tnow) / 1000);

		fprintf(stdout, "absolute timer test (at 500 ms) ...\n");
		tnow = getustime(clks[i].id);
		set_timespec(&tmr.it_value, tnow + 500 * 1000);
		set_timespec(&tmr.it_interval, 0);
		if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL)) {
			perror("timerfd_settime");
			return 1;
		}

		fprintf(stdout, "wating timer ...\n");
		ticks = waittmr(tfd, -1);
		ttmr = getustime(clks[i].id);
		if (ticks <= 0)
			fprintf(stdout, "whooops! no timer showed up!\n");
		else
			fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
				ticks, (ttmr - tnow) / 1000);

		fprintf(stdout, "sequential timer test (100 ms clock) ...\n");
		tnow = getustime(clks[i].id);
		set_timespec(&tmr.it_value, tnow + 100 * 1000);
		set_timespec(&tmr.it_interval, 100 * 1000);
		if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL)) {
			perror("timerfd_settime");
			return 1;
		}

		fprintf(stdout, "sleeping 1 second ...\n");
		sleep(1);
		if (timerfd_gettime(tfd, &tmr)) {
			perror("timerfd_gettime");
			return 1;
		}
		fprintf(stdout, "timerfd_gettime returned:\n"
			"\tit_value = { %ld, %ld } it_interval = { %ld, %ld }\n",
			(long)tmr.it_value.tv_sec, (long)tmr.it_value.tv_nsec,
			(long)tmr.it_interval.tv_sec,
			(long)tmr.it_interval.tv_nsec);
		fprintf(stdout, "sleeping 1 second ...\n");
		sleep(1);

		fprintf(stdout, "wating timer ...\n");
		ticks = waittmr(tfd, -1);
		ttmr = getustime(clks[i].id);
		if (ticks <= 0)
			fprintf(stdout, "whooops! no timer showed up!\n");
		else
			fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
				ticks, (ttmr - tnow) / 1000);

		fprintf(stdout, "O_NONBLOCK test ...\n");
		tnow = getustime(clks[i].id);
		set_timespec(&tmr.it_value, 100 * 1000);
		set_timespec(&tmr.it_interval, 0);
		if (timerfd_settime(tfd, 0, &tmr, NULL)) {
			perror("timerfd_settime");
			return 1;
		}
		fprintf(stdout, "timerfd = %d\n", tfd);

		fprintf(stdout, "wating timer (flush the single tick) ...\n");
		ticks = waittmr(tfd, -1);
		ttmr = getustime(clks[i].id);
		if (ticks <= 0)
			fprintf(stdout, "whooops! no timer showed up!\n");
		else
			fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
				ticks, (ttmr - tnow) / 1000);

		fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) | O_NONBLOCK);

		if (read(tfd, &uticks, sizeof(uticks)) > 0)
			fprintf(stdout,
				"whooops! timer ticks not zero when should have been\n");
		else if (errno != EAGAIN)
			fprintf(stdout,
				"whooops! bad errno value (%d = '%s')!\n",
				errno, strerror(errno));
		else
			fprintf(stdout, "success\n");

		fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) & ~O_NONBLOCK);

		close(tfd);
	}

	tst_exit();
}
示例#12
0
/*
 *  stress_timerfd
 *	stress timerfd
 */
int stress_timerfd(
	uint64_t *const counter,
	const uint32_t instance,
	const uint64_t max_ops,
	const char *name)
{
	struct itimerspec timer;

	(void)instance;

	if (!set_timerfd_freq) {
		if (opt_flags & OPT_FLAGS_MAXIMIZE)
			opt_timerfd_freq = MAX_TIMERFD_FREQ;
		if (opt_flags & OPT_FLAGS_MINIMIZE)
			opt_timerfd_freq = MIN_TIMERFD_FREQ;
	}
	rate_ns = opt_timerfd_freq ? 1000000000 / opt_timerfd_freq : 1000000000;

	timerfd = timerfd_create(CLOCK_REALTIME, 0);
	if (timerfd < 0) {
		pr_failed_err(name, "timerfd_create");
		(void)close(timerfd);
		return EXIT_FAILURE;
	}
	stress_timerfd_set(&timer);
	if (timerfd_settime(timerfd, 0, &timer, NULL) < 0) {
		pr_failed_err(name, "timer_settime");
		(void)close(timerfd);
		return EXIT_FAILURE;
	}

	do {
		int ret;
		uint64_t exp;
		struct itimerspec value;
		struct timeval timeout;
		fd_set rdfs;

		FD_ZERO(&rdfs);
		FD_SET(timerfd, &rdfs);
		timeout.tv_sec = 0;
		timeout.tv_usec = 500000;

		if (!opt_do_run)
			break;
		ret = select(timerfd + 1, &rdfs, NULL, NULL, &timeout);
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			pr_failed_err(name, "select");
			break;
		}
		if (ret < 1)
			continue; /* Timeout */

		ret = read(timerfd, &exp, sizeof exp);
		if (ret < 0) {
			pr_failed_err(name, "timerfd read");
			break;
		}
		if (timerfd_gettime(timerfd, &value) < 0) {
			pr_failed_err(name, "timerfd_gettime");
			break;
		}
		if (opt_flags & OPT_FLAGS_TIMERFD_RAND) {
			stress_timerfd_set(&timer);
			if (timerfd_settime(timerfd, 0, &timer, NULL) < 0) {
				pr_failed_err(name, "timer_settime");
				break;
			}
		}
		(*counter)++;
	} while (opt_do_run && (!max_ops || timerfd_counter < max_ops));

	(void)close(timerfd);

	return EXIT_SUCCESS;
}