stat_timing.c 2.65 KB
Newer Older
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
/*
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief   OS abstraction from time measurement
 * @author  Sebastian Hack, Michael Beck, Matthias Braun
 * @version $Id$
 */
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
27

28
#include "stat_timing.h"
Sebastian Hack's avatar
Sebastian Hack committed
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>

#ifdef __linux__

#include <unistd.h>
#include <time.h>
#include <sys/time.h>

/* define GNU macro for processor affinity stuff if on linux */
#if defined __linux__ && !defined __USE_GNU
#define __USE_GNU
#endif
#include <sched.h>

Sebastian Hack's avatar
Sebastian Hack committed
44
/* we can only use the scheduling stuff, if that macro is defined in unistd.h */
Michael Beck's avatar
Michael Beck committed
45
#if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1
Sebastian Hack's avatar
Sebastian Hack committed
46

47
48
49
50
51
52
53
54
#define HAVE_IMPL

static int                 in_max_prio = 0;
static cpu_set_t           affinity;
static int                 scheduler;
static struct sched_param  sched_params;

void timing_enter_max_prio(void)
Sebastian Hack's avatar
Sebastian Hack committed
55
{
56
57
58
59
	int                 res;
	int                 new_scheduler = SCHED_FIFO;
	struct sched_param  new_sched_params;
	cpu_set_t           new_affinity;
Sebastian Hack's avatar
Sebastian Hack committed
60

61
62
	if (in_max_prio)
		return;
Sebastian Hack's avatar
Sebastian Hack committed
63

64
65
66
67
68
69
70
71
72
73
	/* remember old scheduler settings */
	res = sched_getaffinity(0, sizeof(affinity), &affinity);
	if (res < 0)
		return;
	scheduler = sched_getscheduler(0);
	if (scheduler < 0)
		return;
	res = sched_getparam(0, &sched_params);
	if (res < 0)
		return;
Sebastian Hack's avatar
Sebastian Hack committed
74

75
76
77
78
	/* set high prio */
	CPU_ZERO(&new_affinity);
	CPU_SET(0, &new_affinity);
	res = sched_setaffinity(0, sizeof(new_affinity), &new_affinity);
Sebastian Hack's avatar
Sebastian Hack committed
79
	if (res < 0)
80
81
82
83
84
85
		return;
	new_scheduler = SCHED_FIFO;
	sched_params.sched_priority = sched_get_priority_max(new_scheduler);
	sched_setscheduler(0, new_scheduler, &new_sched_params);
	if (res < 0)
		return;
Sebastian Hack's avatar
Sebastian Hack committed
86

87
	in_max_prio = 1;
Sebastian Hack's avatar
Sebastian Hack committed
88
89
}

90
void timing_leave_max_prio(void)
Sebastian Hack's avatar
Sebastian Hack committed
91
92
93
{
	int res;

94
95
96
97
98
	if (!in_max_prio)
		return;

	/* restore old settings */
	res = sched_setaffinity(0, sizeof(affinity), &affinity);
Sebastian Hack's avatar
Sebastian Hack committed
99
	if (res < 0)
100
		return;
Sebastian Hack's avatar
Sebastian Hack committed
101

102
	sched_setscheduler(0, scheduler, &sched_params);
Sebastian Hack's avatar
Sebastian Hack committed
103
	if (res < 0)
104
		return;
Sebastian Hack's avatar
Sebastian Hack committed
105

106
	in_max_prio = 0;
Sebastian Hack's avatar
Sebastian Hack committed
107
108
109
}

#endif
110
#endif
Sebastian Hack's avatar
Sebastian Hack committed
111
112


113
114
115
#ifndef HAVE_IMPL

/* dummy implementation */
Sebastian Hack's avatar
Sebastian Hack committed
116

117
void timing_enter_max_prio(void)
Sebastian Hack's avatar
Sebastian Hack committed
118
119
120
{
}

121
void timing_leave_max_prio(void)
Sebastian Hack's avatar
Sebastian Hack committed
122
123
124
{
}

125
#endif