forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpuhog.c
76 lines (69 loc) · 1.68 KB
/
cpuhog.c
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
/**************************************************************************
* Included Files
**************************************************************************/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
long long int count;
void quit(int param)
{
printf("count = %lld\n", count);
exit(1);
}
int main(int argc, char *argv[])
{
int cpuid = 0;
int prio = 0;
int num_processors;
cpu_set_t cmask;
int opt;
struct sched_param param;
signal(SIGINT, &quit);
/*
* get command line options
*/
while ((opt = getopt(argc, argv, "a:n:t:c:i:p:o:f:l:xh")) != -1) {
switch (opt) {
case 'c': /* set CPU affinity */
cpuid = strtol(optarg, NULL, 0);
num_processors = sysconf(_SC_NPROCESSORS_CONF);
CPU_ZERO(&cmask);
CPU_SET(cpuid % num_processors, &cmask);
if (sched_setaffinity(0, num_processors, &cmask) < 0) {
perror("error");
exit(1);
}
fprintf(stderr, "assigned to cpu %d\n", cpuid);
break;
case 'o': /* SCHED_BATCH */
if (!strcmp(optarg, "batch")) {
param.sched_priority = 0;
if(sched_setscheduler(0, SCHED_BATCH, ¶m) == -1) {
perror("sched_setscheduler failed");
exit(1);
}
} else if (!strcmp(optarg, "fifo")) {
param.sched_priority = 1;
if(sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
perror("sched_setscheduler failed");
exit(1);
}
}
break;
}
}
count = 0;
while(1)
count ++;
}