File: ncpu.c

package info (click to toggle)
haproxy 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 24,584 kB
  • sloc: ansic: 275,085; sh: 3,607; xml: 1,756; python: 1,345; makefile: 1,162; perl: 168; cpp: 21
file content (136 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download | duplicates (2)
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
#define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// gcc -fPIC -shared -O2 -o ncpu{.so,.c}
// NCPU=16 LD_PRELOAD=$PWD/ncpu.so command args...

static char prog_full_path[PATH_MAX];

long sysconf(int name)
{
	if (name == _SC_NPROCESSORS_ONLN ||
	    name == _SC_NPROCESSORS_CONF) {
		const char *ncpu = getenv("NCPU");
		int n;

		n = ncpu ? atoi(ncpu) : CPU_SETSIZE;
		if (n < 0 || n > CPU_SETSIZE)
			n = CPU_SETSIZE;
		return n;
	}
	errno = EINVAL;
	return -1;
}

/* return a cpu_set having the first $NCPU set */
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask)
{
	const char *ncpu;
	int i, n;

	CPU_ZERO_S(cpusetsize, mask);

	ncpu = getenv("NCPU");
	n = ncpu ? atoi(ncpu) : CPU_SETSIZE;
	if (n < 0 || n > CPU_SETSIZE)
		n = CPU_SETSIZE;

	for (i = 0; i < n; i++)
		CPU_SET_S(i, cpusetsize, mask);

	return 0;
}

/* silently ignore the operation */
int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask)
{
	return 0;
}

void usage(const char *argv0)
{
	fprintf(stderr,
		"Usage: %s [-n ncpu] [cmd [args...]]\n"
		"       Will install itself in LD_PRELOAD before calling <cmd> with args.\n"
		"       The number of CPUs may also come from variable NCPU or default to %d.\n"
		"\n"
		"",
		argv0, CPU_SETSIZE);
	exit(1);
}

/* Called in wrapper mode, no longer supported on recent glibc */
int main(int argc, char **argv)
{
	const char *argv0 = argv[0];
	char *preload;
	int plen;

	prog_full_path[0] = 0;
	plen = readlink("/proc/self/exe", prog_full_path, sizeof(prog_full_path) - 1);
	if (plen != -1)
		prog_full_path[plen] = 0;
	else
		plen = snprintf(prog_full_path, sizeof(prog_full_path), "%s", argv[0]);

	while (1) {
		argc--;
		argv++;

		if (argc < 1)
			usage(argv0);

		if (strcmp(argv[0], "--") == 0) {
			argc--;
			argv++;
			break;
		}
		else if (strcmp(argv[0], "-n") == 0) {
			if (argc < 2)
				usage(argv0);

			if (setenv("NCPU", argv[1], 1) != 0)
				usage(argv0);
			argc--;
			argv++;
		}
		else {
			/* unknown arg, that's the command */
			break;
		}
	}

	/* here the only args left start with the cmd name */

	/* now we'll concatenate ourselves at the end of the LD_PRELOAD variable */
	preload = getenv("LD_PRELOAD");
	if (preload) {
		int olen = strlen(preload);
		preload = realloc(preload, olen + 1 + plen + 1);
		if (!preload) {
			perror("realloc");
			exit(2);
		}
		preload[olen] = ' ';
		memcpy(preload + olen + 1, prog_full_path, plen);
		preload[olen + 1 + plen] = 0;
	}
	else {
		preload = prog_full_path;
	}

	if (setenv("LD_PRELOAD", preload, 1) < 0) {
		perror("setenv");
		exit(2);
	}

	execvp(*argv, argv);
	perror("execve");
	exit(2);
}