| 1 |
/* -*- linux-c -*- |
| 2 |
* Kernel Timer Functions |
| 3 |
* Copyright (C) 2012 Red Hat Inc. |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _LINUX_TIMER_C_ |
| 12 |
#define _LINUX_TIMER_C_ |
| 13 |
|
| 14 |
#include "timer.h" |
| 15 |
|
| 16 |
static void _stp_hrtimer_init(void) |
| 17 |
{ |
| 18 |
#if defined(STAPCONF_HRTIMER_GET_RES) |
| 19 |
struct timespec res; |
| 20 |
hrtimer_get_res (CLOCK_MONOTONIC, &res); |
| 21 |
stap_hrtimer_resolution = timespec_to_ns(&res); |
| 22 |
#else |
| 23 |
stap_hrtimer_resolution = hrtimer_resolution; |
| 24 |
#endif |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
static inline ktime_t _stp_hrtimer_get_interval(struct stap_hrtimer_probe *stp) |
| 29 |
{ |
| 30 |
unsigned long nsecs; |
| 31 |
uint64_t i = stp->intrv; |
| 32 |
|
| 33 |
if (stp->rnd != 0) { |
| 34 |
#if 1 |
| 35 |
// XXX: why not use stp_random_pm instead of this? |
| 36 |
int64_t r; |
| 37 |
get_random_bytes(&r, sizeof(r)); |
| 38 |
|
| 39 |
// ensure that r is positive |
| 40 |
r &= ((uint64_t)1 << (8*sizeof(r) - 1)) - 1; |
| 41 |
r = _stp_mod64(NULL, r, (2*stp->rnd+1)); |
| 42 |
r -= stp->rnd; |
| 43 |
i += r; |
| 44 |
#else |
| 45 |
i += _stp_random_pm(stp->rnd); |
| 46 |
#endif |
| 47 |
} |
| 48 |
if (unlikely(i < stap_hrtimer_resolution)) |
| 49 |
i = stap_hrtimer_resolution; |
| 50 |
nsecs = do_div(i, NSEC_PER_SEC); |
| 51 |
return ktime_set(i, nsecs); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static inline void _stp_hrtimer_update(struct stap_hrtimer_probe *stp) |
| 56 |
{ |
| 57 |
ktime_t time; |
| 58 |
|
| 59 |
time = ktime_add(hrtimer_get_expires(&stp->hrtimer), |
| 60 |
_stp_hrtimer_get_interval(stp)); |
| 61 |
hrtimer_set_expires(&stp->hrtimer, time); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int |
| 66 |
_stp_hrtimer_start(struct stap_hrtimer_probe *stp) |
| 67 |
{ |
| 68 |
(void)hrtimer_start(&stp->hrtimer, _stp_hrtimer_get_interval(stp), |
| 69 |
HRTIMER_MODE_REL); |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
static int |
| 74 |
_stp_hrtimer_create(struct stap_hrtimer_probe *stp, |
| 75 |
hrtimer_return_t (*function)(struct hrtimer *)) |
| 76 |
{ |
| 77 |
STP_HRTIMER_SETUP(&stp->hrtimer, function, |
| 78 |
CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
// For kernel-mode, there is no difference between cancel/delete. |
| 83 |
|
| 84 |
static void |
| 85 |
_stp_hrtimer_cancel(struct stap_hrtimer_probe *stp) |
| 86 |
{ |
| 87 |
hrtimer_cancel(&stp->hrtimer); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
_stp_hrtimer_delete(struct stap_hrtimer_probe *stp) |
| 92 |
{ |
| 93 |
_stp_hrtimer_cancel(stp); |
| 94 |
} |
| 95 |
|
| 96 |
#endif /* _LINUX_TIMER_C_ */ |