aboutsummaryrefslogtreecommitdiff
path: root/cpuidle/cpuidle_killer.c
blob: 4297c18fb475c15091ea2f322b02bfaedbcc3640 (plain)
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/timex.h>
#ifdef ANDROID
/* 
* As of 5.0.0, Bionic provides timex, but not the
* adjtimex interface.
* However, the kernel does.
*/
#include <linux/timex.h> /* for struct timex */
#include <asm/unistd.h> /* for __NR_adjtimex */
static int adjtimex(struct timex *buf)
{
	return syscall(__NR_adjtimex, buf);
}
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sched.h>
#include <signal.h>

#define DURATION  120 /* seconds */
#define SEC_TO_USEC(d) ((d) * 1000 * 1000)

static int sigon;

void sighandler(int sig)
{
	sigon = 1;
}

void timeout(int sig)
{
	printf("Err : Test duration exceeded\n");
	exit(1);
}

int tick_me(int nrsleeps, int delay, int cpu)
{
	int i;
	cpu_set_t mask;
	unsigned long counter = 0;
	struct itimerval t = { };

	CPU_ZERO(&mask);
	CPU_SET(cpu, &mask);

	/* stick on the specified cpu */
	if (sched_setaffinity(getpid(), sizeof(mask), &mask)) {
		fprintf(stderr, "sched_setaffinity (%d): %m", cpu);
		return -1;
	}

	signal(SIGALRM, sighandler);

	for (i = 0; i < nrsleeps / 2; i++) {
		usleep(delay);

		sigon = 0;
		t.it_value.tv_sec = 0;
		t.it_value.tv_usec = delay;

		if (setitimer(ITIMER_REAL, &t, NULL)) {
			perror("setitimer");
			return 1;
		}

		while (!sigon)
			counter++;
	}

	fprintf(stderr, "CPU%d counter value %lu\n", cpu, counter);

	return 0;
}

int isonline(int cpu)
{
        FILE *f;
        char path[MAXPATHLEN];
        int online;

        if (!cpu)
                return 1;

        sprintf(path, "/sys/devices/system/cpu/cpu%d/online", cpu);

        f = fopen(path, "r");
        if (!f) {
                perror("fopen");
                return -1;
        }

        fscanf(f, "%d", &online);

        fclose(f);

        return online;
}

int main(int argc, char *argv[])
{
	int ret, i, nrcpus;
	int nrsleeps, delay;
	pid_t *pids;
	struct timex timex = { 0 };

	if (adjtimex(&timex) < 0) {
		perror("adjtimex");
		return 1;
	}

	fprintf(stderr, "jiffies are : %ld usecs\n", timex.tick);

	nrcpus = sysconf(_SC_NPROCESSORS_CONF);
	if (nrcpus < 0) {
		perror("sysconf");
		return 1;
	}

	fprintf(stderr, "found %d cpu(s)\n", nrcpus);
	pids = (pid_t *) calloc(nrcpus, sizeof(pid_t));
	if (pids == NULL) {
		fprintf(stderr, "error: calloc failed\n");
		return 1;
	}

	for (i = 0; i < nrcpus; i++) {

		ret = isonline(i);
		if (!ret) {
			fprintf(stderr, "cpu%d is offline, ignoring\n", i);
			continue;
		}

		pids[i] = fork();
		if (pids[i] < 0) {
			perror("fork");
			return 1;
		}

		if (!pids[i]) {

			struct timeval before, after;
			long duration;
			float deviation;

			nrsleeps = SEC_TO_USEC(DURATION) / (timex.tick * 10);
			delay    = SEC_TO_USEC(DURATION) / nrsleeps;

			fprintf(stderr, "CPU%d duration: %d secs, #sleep: %d,"
			       " delay: %d us\n", i, DURATION, nrsleeps, delay);

			gettimeofday(&before, NULL);
			if (tick_me(nrsleeps, delay, i))
				return 1;
			gettimeofday(&after, NULL);

			duration = SEC_TO_USEC(after.tv_sec - before.tv_sec) +
				(after.tv_usec - before.tv_usec);

			fprintf(stderr, "CPU%d test duration: %f secs\n", i,
				(float)(duration) / (float)SEC_TO_USEC(1));

			deviation = ((float)duration - (float)SEC_TO_USEC(DURATION)) /
				(float)SEC_TO_USEC(DURATION);

			fprintf(stderr, "CPU%d deviation %f\n", i, deviation);
			if (deviation > 0.5) {
				fprintf(stderr, "expected test duration too long\n");
				return 1;
			}

			return 0;
		}
	}

	ret = 0;

	signal(SIGALRM, timeout);

	alarm(DURATION + 20);

	for (i = 0; i < nrcpus; i++) {
		int status;

		/* skip for offline cpus */
		if (!pids[i]) {
			fprintf(stderr, "no_wait_for_process on cpu %d\n", i);
			continue;
		}

		waitpid(pids[i], &status, 0);
		if (status != 0) {
			fprintf(stderr, "test for cpu %d has failed\n", i);
			ret = 1;
		}
	}

	return ret;
}