File: util.c

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (381 lines) | stat: -rw-r--r-- 8,441 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (C) 2010-2022 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Angus Salkeld <asalkeld@redhat.com>
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "os_base.h"

#include "util_int.h"
#include <pthread.h>
#include <sys/stat.h>
#include <qb/qbconfig.h>
#include <qb/qbdefs.h>
#include <qb/qbutil.h>

struct qb_thread_lock_s {
	qb_thread_lock_type_t type;
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	pthread_spinlock_t spinlock;
#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
	pthread_mutex_t mutex;
};

qb_thread_lock_t *
qb_thread_lock_create(qb_thread_lock_type_t type)
{
	struct qb_thread_lock_s *tl = malloc(sizeof(struct qb_thread_lock_s));
	int32_t res;

	if (tl == NULL) {
		return NULL;
	}
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	if (type == QB_THREAD_LOCK_SHORT) {
		tl->type = QB_THREAD_LOCK_SHORT;
		res = pthread_spin_init(&tl->spinlock, 1);
	} else
#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
	{
		tl->type = QB_THREAD_LOCK_LONG;
		res = pthread_mutex_init(&tl->mutex, NULL);
	}
	if (res == 0) {
		return tl;
	} else {
		free(tl);
		return NULL;
	}
}

int32_t
qb_thread_lock(qb_thread_lock_t * tl)
{
	int32_t res;
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	if (tl->type == QB_THREAD_LOCK_SHORT) {
		res = -pthread_spin_lock(&tl->spinlock);
	} else
#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
	{
		res = -pthread_mutex_lock(&tl->mutex);
	}
	return res;
}

int32_t
qb_thread_unlock(qb_thread_lock_t * tl)
{
	int32_t res;
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	if (tl->type == QB_THREAD_LOCK_SHORT) {
		res = -pthread_spin_unlock(&tl->spinlock);
	} else
#endif
	{
		res = -pthread_mutex_unlock(&tl->mutex);
	}
	return res;
}

int32_t
qb_thread_trylock(qb_thread_lock_t * tl)
{
	int32_t res;
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	if (tl->type == QB_THREAD_LOCK_SHORT) {
		res = -pthread_spin_trylock(&tl->spinlock);
	} else
#endif
	{
		res = -pthread_mutex_trylock(&tl->mutex);
	}
	return res;
}

int32_t
qb_thread_lock_destroy(qb_thread_lock_t * tl)
{
	int32_t res;
#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
	if (tl->type == QB_THREAD_LOCK_SHORT) {
		res = -pthread_spin_destroy(&tl->spinlock);
	} else
#endif
	{
		res = -pthread_mutex_destroy(&tl->mutex);
	}
	free(tl);
	return res;
}

/* If the "coarse" monotonic clock is available, it provides the time as of the
 * last clock tick (typically at millisecond resolution), which should be
 * sufficient for expected uses of libqb. This is faster since it avoids a
 * context switch to the kernel.
 */
#ifdef HAVE_MONOTONIC_CLOCK
# ifdef CLOCK_REALTIME_COARSE
#  define QB_CLOCK_REALTIME CLOCK_REALTIME_COARSE
# else
#  define QB_CLOCK_REALTIME CLOCK_REALTIME
# endif
#endif

void
qb_timespec_add_ms(struct timespec *ts, int32_t ms)
{
#ifndef S_SPLINT_S
	ts->tv_sec += ms / 1000;
	ts->tv_nsec += (ms % 1000) * QB_TIME_NS_IN_MSEC;
	if (ts->tv_nsec >= 1000000000L) {
		ts->tv_sec++;
		ts->tv_nsec = ts->tv_nsec - 1000000000L;
	}
#endif /* S_SPLINT_S */
}

uint64_t
qb_util_nano_current_get(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
	struct timespec ts;

	if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
		return (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec;
	}
#endif
	return qb_util_nano_from_epoch_get();
}

uint64_t
qb_util_nano_from_epoch_get(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
	struct timespec ts;

	if (clock_gettime(QB_CLOCK_REALTIME, &ts) == 0) {
		return (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t) ts.tv_nsec;
	}
#endif

#ifdef HAVE_GETTIMEOFDAY
	{
		struct timeval time_from_epoch;

		if (gettimeofday(&time_from_epoch, NULL) == 0) {
			return (time_from_epoch.tv_sec * QB_TIME_NS_IN_SEC) +
				(time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC);
		}
	}
#endif

	return time(NULL) * QB_TIME_NS_IN_SEC;
}

uint64_t
qb_util_nano_monotonic_hz(void)
{
#ifdef HAVE_MONOTONIC_CLOCK
	struct timespec ts;

	if ((clock_getres(CLOCK_MONOTONIC, &ts) == 0)
		|| (clock_getres(CLOCK_REALTIME, &ts) == 0)) {
		return QB_TIME_NS_IN_SEC / (ts.tv_sec * QB_TIME_NS_IN_SEC + ts.tv_nsec);
	}
#endif

	return sysconf(_SC_CLK_TCK);
}

void
qb_util_timespec_from_epoch_get(struct timespec *ts)
{
#ifdef HAVE_MONOTONIC_CLOCK
	if (clock_gettime(QB_CLOCK_REALTIME, ts) == 0) {
		return;
	}
#endif

#ifdef HAVE_GETTIMEOFDAY
	{
		struct timeval time_from_epoch;

		if (gettimeofday(&time_from_epoch, NULL) == 0) {
#ifndef S_SPLINT_S
			ts->tv_sec = time_from_epoch.tv_sec;
			ts->tv_nsec = time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC;
#endif /* S_SPLINT_S */
			return;
		}
	}
#endif

	ts->tv_sec = time(NULL);
	ts->tv_nsec = 0;
}

struct qb_util_stopwatch {
	uint64_t started;
	uint64_t stopped;
	uint32_t split_options;
	uint32_t split_size;
	uint32_t split_entries;
	uint64_t *split_entry_list;
};

qb_util_stopwatch_t *
qb_util_stopwatch_create(void)
{
	struct qb_util_stopwatch *sw;
	sw = (struct qb_util_stopwatch *)calloc(1, sizeof(struct qb_util_stopwatch));
	return sw;
}

void
qb_util_stopwatch_free(qb_util_stopwatch_t * sw)
{
	free(sw->split_entry_list);
	free(sw);
}

void
qb_util_stopwatch_start(qb_util_stopwatch_t * sw)
{
	sw->started = qb_util_nano_current_get();
	sw->stopped = 0;
	sw->split_entries = 0;
}

void
qb_util_stopwatch_stop(qb_util_stopwatch_t * sw)
{
	sw->stopped = qb_util_nano_current_get();
}

uint64_t
qb_util_stopwatch_us_elapsed_get(qb_util_stopwatch_t * sw)
{
	if (sw->stopped == 0 || sw->started == 0) {
		return 0;
	}
	return ((sw->stopped - sw->started) / QB_TIME_NS_IN_USEC);
}

float
qb_util_stopwatch_sec_elapsed_get(qb_util_stopwatch_t * sw)
{
	uint64_t e6;
	if (sw->stopped == 0 || sw->started == 0) {
		return 0;
	}
	e6 = qb_util_stopwatch_us_elapsed_get(sw);
	return ((float)e6 / (float)QB_TIME_US_IN_SEC);
}

int32_t
qb_util_stopwatch_split_ctl(qb_util_stopwatch_t *sw,
        uint32_t max_splits, uint32_t options)
{
	sw->split_size = max_splits;
	sw->split_options = options;
	sw->split_entry_list = (uint64_t *)calloc(1, sizeof (uint64_t) * max_splits);
	if (sw->split_entry_list == NULL) {
		return -errno;
	}
	return 0;
}

uint64_t
qb_util_stopwatch_split(qb_util_stopwatch_t *sw)
{
	uint32_t new_entry_pos;
	uint64_t time_start;
	uint64_t time_end;

	if (sw->split_size == 0) {
		return 0;
	}
	if (!(sw->split_options & QB_UTIL_SW_OVERWRITE) &&
	    sw->split_entries == sw->split_size) {
		return 0;
	}
	if (sw->started == 0) {
		qb_util_stopwatch_start(sw);
	}
	new_entry_pos = sw->split_entries % (sw->split_size);
	sw->split_entry_list[new_entry_pos] = qb_util_nano_current_get();
	sw->split_entries++;

	time_start = sw->split_entry_list[new_entry_pos];
	if (sw->split_entries == 1) {
		/* first entry */
		time_end = sw->started;
	} else if (new_entry_pos == 0) {
		/* wrap around */
		time_end = sw->split_entry_list[sw->split_size - 1];
	} else {
		time_end = sw->split_entry_list[(new_entry_pos - 1) % sw->split_size];
	}
	return (time_start - time_end) / QB_TIME_NS_IN_USEC;
}

uint32_t
qb_util_stopwatch_split_last(qb_util_stopwatch_t *sw)
{
	if (sw->split_entries) {
		return sw->split_entries - 1;
	}
	return sw->split_entries;
}

uint64_t
qb_util_stopwatch_time_split_get(qb_util_stopwatch_t *sw,
				 uint32_t receint, uint32_t older)
{
	uint64_t time_start;
	uint64_t time_end;

	if (sw->started == 0 ||
	    receint >= sw->split_entries ||
	    older >= sw->split_entries ||
	    receint < older) {
		return 0;
	}
	if (sw->split_options & QB_UTIL_SW_OVERWRITE &&
	    (receint < (sw->split_entries - sw->split_size) ||
	     older < (sw->split_entries - sw->split_size))) {
		return 0;
	}

	time_start = sw->split_entry_list[receint % (sw->split_size)];
	if (older == receint) {
		time_end = sw->started;
	} else {
		time_end = sw->split_entry_list[older % (sw->split_size)];
	}
	return (time_start - time_end) / QB_TIME_NS_IN_USEC;
}

const struct qb_version qb_ver = {
	.major = QB_VER_MAJOR,
	.minor = QB_VER_MINOR,
	.micro = QB_VER_MICRO,
	.rest = QB_VER_REST,
};

const char *const qb_ver_str = QB_VER_STR;