Tests: Fix sleep interruption in health stall test
[lttng-tools.git] / src / bin / lttng-sessiond / health.c
CommitLineData
44a5e5eb
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#define _GNU_SOURCE
19#include <assert.h>
20#include <inttypes.h>
21#include <stdio.h>
22#include <stdlib.h>
8809eec0 23#include <time.h>
44a5e5eb 24
8809eec0 25#include <common/defaults.h>
44a5e5eb
DG
26#include <common/error.h>
27
28#include "health.h"
29
8809eec0
MD
30static const struct timespec time_delta = {
31 .tv_sec = DEFAULT_HEALTH_CHECK_DELTA_S,
32 .tv_nsec = DEFAULT_HEALTH_CHECK_DELTA_NS,
33};
34
35/*
36 * Set time difference in res from time_a and time_b.
37 */
38static void time_diff(const struct timespec *time_a,
39 const struct timespec *time_b, struct timespec *res)
40{
41 if (time_a->tv_nsec - time_b->tv_nsec < 0) {
42 res->tv_sec = time_a->tv_sec - time_b->tv_sec - 1;
43 res->tv_nsec = 1000000000L + time_a->tv_sec - time_b->tv_sec;
44 } else {
45 res->tv_sec = time_a->tv_sec - time_b->tv_sec;
931a97e5 46 res->tv_nsec = time_a->tv_nsec - time_b->tv_nsec;
8809eec0
MD
47 }
48}
49
50/*
51 * Return true if time_a - time_b > diff, else false.
52 */
53static int time_diff_gt(const struct timespec *time_a,
54 const struct timespec *time_b, const struct timespec *diff)
55{
56 struct timespec res;
57
58 time_diff(time_a, time_b, &res);
59 time_diff(&res, diff, &res);
60
61 if (res.tv_sec > 0) {
62 return 1;
63 } else if (res.tv_sec == 0 && res.tv_nsec > 0) {
64 return 1;
65 }
66
67 return 0;
68}
69
44a5e5eb
DG
70/*
71 * Check health of a specific health state counter.
72 *
73 * Return 0 if health is bad or else 1.
74 */
75int health_check_state(struct health_state *state)
76{
8809eec0 77 int retval = 1, ret;
139ac872 78 unsigned long current, last;
8809eec0 79 struct timespec current_time;
44a5e5eb
DG
80
81 assert(state);
82
139ac872 83 last = state->last;
44a5e5eb 84 current = uatomic_read(&state->current);
44a5e5eb 85
8809eec0 86 ret = clock_gettime(CLOCK_MONOTONIC, &current_time);
931a97e5 87 if (ret < 0) {
8809eec0 88 PERROR("Error reading time\n");
139ac872 89 /* error */
8809eec0
MD
90 retval = 0;
91 goto end;
44a5e5eb
DG
92 }
93
8809eec0
MD
94 /*
95 * Thread is in bad health if flag HEALTH_ERROR is set. It is also in bad
96 * health if, after the delta delay has passed, its the progress counter
97 * has not moved and it has NOT been waiting for a poll() call.
98 */
99 if (uatomic_read(&state->flags) & HEALTH_ERROR) {
100 retval = 0;
101 goto end;
102 }
44a5e5eb 103
139ac872 104 /*
8809eec0
MD
105 * Initial condition need to update the last counter and sample time, but
106 * should not check health in this initial case, because we don't know how
107 * much time has passed.
139ac872 108 */
8809eec0
MD
109 if (state->last_time.tv_sec == 0 && state->last_time.tv_nsec == 0) {
110 /* update last counter and last sample time */
111 state->last = current;
112 memcpy(&state->last_time, &current_time, sizeof(current_time));
113 } else {
114 if (time_diff_gt(&current_time, &state->last_time, &time_delta)) {
115 if (current == last && !HEALTH_IS_IN_POLL(current)) {
116 /* error */
117 retval = 0;
118 }
119 /* update last counter and last sample time */
120 state->last = current;
121 memcpy(&state->last_time, &current_time, sizeof(current_time));
122 }
123 }
124
125end:
77c7c900 126 DBG("Health state current %lu, last %lu, ret %d",
8809eec0 127 current, last, ret);
139ac872 128
8809eec0 129 return retval;
44a5e5eb 130}
This page took 0.030701 seconds and 4 git commands to generate.