Debian Codegraph @

runtime/dyninst/timer.c

raw
1 /* -*- linux-c -*-
2 * Dyninst 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 _STAPDYN_TIMER_C_
12 #define _STAPDYN_TIMER_C_
13
14 #include <signal.h>
15 #include <time.h>
16
17 #ifndef NSEC_PER_SEC
18 #define NSEC_PER_SEC 1000000000L
19 #endif
20
21 struct stap_hrtimer_probe {
22 struct sigevent sigev;
23 timer_t timer_id;
24 struct itimerspec its;
25 const struct stap_probe * const probe;
26 int64_t intrv;
27 int64_t rnd;
28 };
29
30
31 static void _stp_hrtimer_init(void)
32 {
33 return;
34 }
35
36
37 static int
38 _stp_hrtimer_start(struct stap_hrtimer_probe *shp)
39 {
40 /* Specify a timer with the correct initial value (possibly
41 * randomized a bit).
42 *
43 * If this isn't a randomized timer probe, go ahead and set
44 * up the repeating interval values.
45 *
46 * The probe's interval is in nanoseconds,
47 * but in a int64_t. So, break it down into seconds and
48 * (leftover) nanoseconds so it will fit in a 'struct
49 * timespec'.
50 */
51
52 if (shp->rnd == 0) {
53 shp->its.it_value.tv_sec = (shp->its.it_interval.tv_sec \
54 = shp->intrv / NSEC_PER_SEC);
55 shp->its.it_value.tv_nsec = (shp->its.it_interval.tv_nsec \
56 = shp->intrv % NSEC_PER_SEC);
57 }
58 else {
59 int64_t i = shp->intrv + _stp_random_u(shp->rnd);
60 shp->its.it_value.tv_sec = i / NSEC_PER_SEC;
61 shp->its.it_value.tv_nsec = i % NSEC_PER_SEC;
62 }
63 return timer_settime(shp->timer_id, 0, &shp->its, NULL);
64 }
65
66
67 static int
68 _stp_hrtimer_create(struct stap_hrtimer_probe *shp, void (*function)(sigval_t))
69 {
70 int rc;
71
72 /* Create the timer. */
73 shp->sigev.sigev_notify = SIGEV_THREAD;
74 shp->sigev.sigev_value.sival_ptr = shp;
75 shp->sigev.sigev_notify_function = function;
76 shp->sigev.sigev_notify_attributes = NULL;
77 return timer_create(CLOCK_MONOTONIC, &shp->sigev, &shp->timer_id);
78 }
79
80
81 static void _stp_hrtimer_update(struct stap_hrtimer_probe *shp)
82 {
83 int64_t i = shp->intrv;
84
85 /* The timer only needs updating if this is a randomized timer
86 * probe */
87 if (shp->rnd == 0)
88 return;
89
90 /* The probe's interval is in nanoseconds, but in a
91 * int64_t. So, break it down into seconds and (leftover)
92 * nanoseconds.
93 */
94 i += _stp_random_u(shp->rnd);
95 shp->its.it_value.tv_sec = i / NSEC_PER_SEC;
96 shp->its.it_value.tv_nsec = i % NSEC_PER_SEC;
97 timer_settime(shp->timer_id, 0, &shp->its, NULL);
98 }
99
100
101 static void _stp_hrtimer_cancel(struct stap_hrtimer_probe *shp)
102 {
103 shp->its.it_value.tv_sec = 0;
104 shp->its.it_value.tv_nsec = 0;
105 timer_settime(shp->timer_id, 0, &shp->its, NULL);
106 }
107
108 static void _stp_hrtimer_delete(struct stap_hrtimer_probe *shp)
109 {
110 (void) timer_delete(shp->timer_id);
111 }
112
113 #endif /* _STAPDYN_TIMER_C_ */