File: futex.c

package info (click to toggle)
linux 6.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,724,576 kB
  • sloc: ansic: 26,558,545; asm: 271,315; sh: 143,998; python: 72,469; makefile: 57,126; perl: 36,821; xml: 19,553; cpp: 5,820; yacc: 4,915; lex: 2,955; awk: 1,667; sed: 28; ruby: 25
file content (74 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
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
// SPDX-License-Identifier: GPL-2.0
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>

#include "futex.h"

#ifndef PR_FUTEX_HASH
#define PR_FUTEX_HASH                   78
# define PR_FUTEX_HASH_SET_SLOTS        1
# define FH_FLAG_IMMUTABLE              (1ULL << 0)
# define PR_FUTEX_HASH_GET_SLOTS        2
# define PR_FUTEX_HASH_GET_IMMUTABLE    3
#endif // PR_FUTEX_HASH

void futex_set_nbuckets_param(struct bench_futex_parameters *params)
{
	unsigned long flags;
	int ret;

	if (params->nbuckets < 0)
		return;

	flags = params->buckets_immutable ? FH_FLAG_IMMUTABLE : 0;
	ret = prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS, params->nbuckets, flags);
	if (ret) {
		printf("Requesting %d hash buckets failed: %d/%m\n",
		       params->nbuckets, ret);
		err(EXIT_FAILURE, "prctl(PR_FUTEX_HASH)");
	}
}

void futex_print_nbuckets(struct bench_futex_parameters *params)
{
	char *futex_hash_mode;
	int ret;

	ret = prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_GET_SLOTS);
	if (params->nbuckets >= 0) {
		if (ret != params->nbuckets) {
			if (ret < 0) {
				printf("Can't query number of buckets: %m\n");
				err(EXIT_FAILURE, "prctl(PR_FUTEX_HASH)");
			}
			printf("Requested number of hash buckets does not currently used.\n");
			printf("Requested: %d in usage: %d\n", params->nbuckets, ret);
			err(EXIT_FAILURE, "prctl(PR_FUTEX_HASH)");
		}
		if (params->nbuckets == 0) {
			ret = asprintf(&futex_hash_mode, "Futex hashing: global hash");
		} else {
			ret = prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_GET_IMMUTABLE);
			if (ret < 0) {
				printf("Can't check if the hash is immutable: %m\n");
				err(EXIT_FAILURE, "prctl(PR_FUTEX_HASH)");
			}
			ret = asprintf(&futex_hash_mode, "Futex hashing: %d hash buckets %s",
				       params->nbuckets,
				       ret == 1 ? "(immutable)" : "");
		}
	} else {
		if (ret <= 0) {
			ret = asprintf(&futex_hash_mode, "Futex hashing: global hash");
		} else {
			ret = asprintf(&futex_hash_mode, "Futex hashing: auto resized to %d buckets",
				       ret);
		}
	}
	if (ret < 0)
		err(EXIT_FAILURE, "ENOMEM, futex_hash_mode");
	printf("%s\n", futex_hash_mode);
	free(futex_hash_mode);
}