globals.cpp 4.83 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
27
28
29
30
31
32
33
34
35
36
37
38
//*****************************************************************
/*
  JackTrip: A System for High-Quality Audio Network Performance
  over the Internet

  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
  SoundWIRE group at CCRMA, Stanford University.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/
//*****************************************************************

/**
 * \file globals.cpp
 * \author Juan-Pablo Caceres
 * \date August 2008
 */

#include "globals.h"
39
#include "types_jacktrip.h"
40

41
#if defined ( __LINUX__ )
42
#include <sched.h>
43
#endif //__LINUX__
44

45
#if defined ( __MAC_OSX__ )
46
#include <mach/mach.h>
47
#include <mach/thread_policy.h>
48

49
50
//#include <mach/processor.h>

51
52
//#include <mach/clock.h>
//#include <sys/kernel.h>
53
54
//#include <mach/kern/clock.h>

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//#include <Kernel/kern/clock.h>
//#include <kern/kern_types.h>
//m#include <kern/kern_types.h>
//#include <Kernel/kern/clock.h>
//#include <kern/clock.h>


//#include <assert.h>
//#include <CoreServices/CoreServices.h>
//#include <mach/mach.h>
//#include <mach/mach_time.h>
//#include <unistd.h>




//#include <mach/machine.h>
//#include <mach/mach_time.h>
73
//#include <mach/thread_call.h>
74
//#include <mach/processor.h>
75
76
77
78
79
80
81
82
//#include <mach/macro_help.h>

#endif //__MAC_OSX__


#if defined ( __MAC_OSX__ )
//*******************************************************************************
//http://developer.apple.com/DOCUMENTATION/Darwin/Conceptual/KernelProgramming/scheduler/chapter_8_section_4.html
83
//http://lists.apple.com/archives/darwin-dev/2007/Sep/msg00035.html
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
int set_realtime(int period, int computation, int constraint)
{
  //AbsoluteTime time;
  //clock_get_uptime((uint64_t *)&time);

  //uint64_t result;
  //clock_get_uptime(&result);
  //clock_get_system_microtime(&result,&result);

  struct thread_time_constraint_policy ttcpolicy;
  int ret;
  
  ttcpolicy.period=period; // HZ/160
  ttcpolicy.computation=computation; // HZ/3300;
  ttcpolicy.constraint=constraint; // HZ/2200;
  ttcpolicy.preemptible=1;
  
  if ((ret=thread_policy_set(mach_thread_self(),
			     THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&ttcpolicy,
			     THREAD_TIME_CONSTRAINT_POLICY_COUNT)) != KERN_SUCCESS) {
    fprintf(stderr, "set_realtime() failed.\n");
    return 0;
  }
  return 1;
}
#endif //__MAC_OSX__ 

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#if defined ( __LINUX__ )
//*******************************************************************************
int get_fifo_priority (bool half)
{
  int min, max, priority;
  min = sched_get_priority_min (SCHED_FIFO);
  max = sched_get_priority_max (SCHED_FIFO);
  if (half) {
    priority = (max  - (max - min) / 2); }
  else {
    priority = max; }

  //priority=min;
  return priority;
}
127
#endif //__LINUX__
128
129


130
#if defined ( __LINUX__ )
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
//*******************************************************************************
int set_fifo_priority (bool half)
{
  struct sched_param p;
  int priority;
  //  scheduling priority


  if (true) // (!getuid () || !geteuid ())
    {
      priority = get_fifo_priority (half);
      p.sched_priority = priority;

      if (sched_setscheduler (0, SCHED_FIFO, &p) == -1)
	{
	  fprintf (stderr,
		   "\ncould not activate scheduling with priority %d\n",
		   priority);
	  return -1;
	}
      seteuid (getuid ());
      fprintf (stderr,
	       "\nset scheduling priority to %d (SCHED_FIFO)\n",
	       priority);
    }
  else
    {
      fprintf (stderr,
	       "\ninsufficient privileges to set scheduling priority\n");
      priority = 0;
    }
  return priority;
}
164
#endif //__LINUX__
165
166


167
#if defined ( __LINUX__ )
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//*******************************************************************************
int set_realtime_priority (void)
{
  struct sched_param schp;

  memset (&schp, 0, sizeof (schp));
  schp.sched_priority = sched_get_priority_max (SCHED_FIFO);
  if (sched_setscheduler (0, SCHED_FIFO, &schp) != 0)
    {
      perror ("set_scheduler");
      return -1;
    }
  return 0;
}
182
#endif //__LINUX__