Skip to content

Commit aaefb40

Browse files
Merge branch 'tbeu-Fix-process-priority'
2 parents f526952 + 9ea3e4c commit aaefb40

File tree

6 files changed

+134
-19
lines changed

6 files changed

+134
-19
lines changed

Blocks/OperatingSystem.mo

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,29 @@ package OperatingSystem
55
block SynchronizeRealtime "A pseudo realtime synchronization"
66
extends Modelica_DeviceDrivers.Utilities.Icons.BaseIcon;
77
parameter Integer resolution(min = 1) = 1 "resolution of the timer";
8+
parameter Boolean setPriority = true "true, if process priority is to be set, otherwise false";
89
parameter
910
Modelica_DeviceDrivers.Blocks.OperatingSystem.Types.ProcessPriority
10-
priority = "Normal" "Priority of the simulation process";
11+
priority = "Normal" "Priority of the simulation process" annotation(Dialog(enable=setPriority));
1112
output Real calculationTime "Time needed for calculation";
1213
output Real availableTime
1314
"Time available for calculation (integrator step size)";
15+
Modelica_DeviceDrivers.OperatingSystem.ProcessPriority procPrio = Modelica_DeviceDrivers.OperatingSystem.ProcessPriority(
16+
if
17+
(priority == "Idle") then -2 else
18+
if
19+
(priority == "Below normal") then -1 else
20+
if
21+
(priority == "Normal") then 0 else
22+
if
23+
(priority == "High priority") then 1 else
24+
if
25+
(priority == "Realtime") then 2 else
26+
0) if setPriority;
1427
protected
15-
Real dummyState
28+
Real dummyState(start = 0)
1629
"dummy state to be integrated, to force synchronization in every integration step";
1730
equation
18-
when (initial()) then
19-
Modelica_DeviceDrivers.OperatingSystem.setProcessPriority(
20-
if
21-
(priority == "Idle") then -2 else
22-
if
23-
(priority == "Below normal") then -1 else
24-
if
25-
(priority == "Normal") then 0 else
26-
if
27-
(priority == "High priority") then 1 else
28-
if
29-
(priority == "Realtime") then 2 else
30-
0);
31-
end when;
32-
3331
(calculationTime,availableTime) = Modelica_DeviceDrivers.OperatingSystem.realtimeSynchronize(time,resolution);
3432
der(dummyState) =calculationTime;
3533
annotation (preferredView="info",
@@ -39,7 +37,7 @@ package OperatingSystem
3937
"modelica://Modelica_DeviceDrivers/Resources/Images/Icons/clock.png"),
4038
Text(
4139
extent={{-100,-100},{100,-140}},
42-
textString="%priority"), Text(extent={{-150,142},{150,102}},
40+
textString=DynamicSelect("", if setPriority then "%priority" else "")), Text(extent={{-150,142},{150,102}},
4341
textString="%name")}),
4442
Documentation(info="<html>
4543
<p>Synchronizes the simulation time of the simulation process with the operating system real-time clock. Different priority levels are supported:</p>

OperatingSystem/ProcessPriority.mo

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
within Modelica_DeviceDrivers.OperatingSystem;
2+
class ProcessPriority "An object for process priority."
3+
extends ExternalObject;
4+
function constructor "Creates a ProcessPriority instance with a given process priority."
5+
input Integer priority "Simulation process priority (-2(lowest)..2(realtime))";
6+
output ProcessPriority procPrio;
7+
external "C" procPrio = MDD_ProcessPriorityConstructor(priority)
8+
annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
9+
Include = "#include \"MDDRealtimeSynchronize.h\" ",
10+
Library = "rt",
11+
__iti_dll = "ITI_MDD.dll");
12+
end constructor;
13+
14+
function destructor
15+
input ProcessPriority procPrio;
16+
external "C" MDD_ProcessPriorityDestructor(procPrio)
17+
annotation(IncludeDirectory="modelica://Modelica_DeviceDrivers/Resources/Include",
18+
Include = "#include \"MDDRealtimeSynchronize.h\" ",
19+
Library = "rt",
20+
__iti_dll = "ITI_MDD.dll");
21+
end destructor;
22+
end ProcessPriority;

OperatingSystem/package.order

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
beep
22
clock
3+
ProcessPriority
34
randomReal
45
realtimeSynchronize
56
setProcessPriority

Resources/Include/MDDRealtimeSynchronize.h

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,83 @@ DllExport void MDD_setPriority(int priority) {
6565
break;
6666
}
6767

68-
ModelicaFormatMessage("LastError: %d\n",GetLastError());
68+
{
69+
DWORD dw = GetLastError();
70+
if (dw) {
71+
ModelicaFormatMessage("LastError: %d\n", dw);
72+
}
73+
}
74+
}
75+
76+
typedef struct {
77+
DWORD prio;
78+
} ProcPrio;
79+
80+
DllExport void* MDD_ProcessPriorityConstructor(int priority) {
81+
ProcPrio* prio = (ProcPrio*) malloc(sizeof(ProcPrio));
82+
if (prio) {
83+
prio->prio = GetPriorityClass(GetCurrentProcess());
84+
}
85+
MDD_setPriority(priority);
86+
return (void*) prio;
87+
}
88+
89+
DllExport void MDD_ProcessPriorityDestructor(void* prioObj) {
90+
ProcPrio* prio = (ProcPrio*) prioObj;
91+
if (prio) {
92+
switch (prio->prio) {
93+
case IDLE_PRIORITY_CLASS:
94+
ModelicaFormatMessage("setting...\n");
95+
if (SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS)) {
96+
ModelicaFormatMessage("ProcessPriority set to idle.\n");
97+
}
98+
break;
99+
100+
case BELOW_NORMAL_PRIORITY_CLASS:
101+
ModelicaFormatMessage("setting...\n");
102+
if (SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS)) {
103+
ModelicaFormatMessage("ProcessPriority set to below normal.\n");
104+
}
105+
break;
106+
107+
case NORMAL_PRIORITY_CLASS:
108+
ModelicaFormatMessage("setting...\n");
109+
if (SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)) {
110+
ModelicaFormatMessage("ProcessPriority set to normal.\n");
111+
}
112+
break;
113+
114+
case HIGH_PRIORITY_CLASS:
115+
ModelicaFormatMessage("setting...\n");
116+
if (SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS)) {
117+
ModelicaFormatMessage("ProcessPriority set to high.\n");
118+
}
119+
break;
120+
121+
case REALTIME_PRIORITY_CLASS:
122+
ModelicaFormatMessage("setting...\n");
123+
if (SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
124+
ModelicaFormatMessage("ProcessPriority set to realtime.\n");
125+
}
126+
break;
127+
128+
default:
129+
ModelicaFormatMessage("setting...\n");
130+
if (SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS)) {
131+
ModelicaFormatMessage("ProcessPriority set to normal.\n");
132+
}
133+
break;
134+
}
135+
136+
{
137+
DWORD dw = GetLastError();
138+
if (dw) {
139+
ModelicaFormatMessage("LastError: %d\n", dw);
140+
}
141+
}
142+
143+
free(prio);
144+
}
69145
}
70146

71147
/** Request time from a monotonic increasing real-time clock.
@@ -214,6 +290,7 @@ DllExport double MDD_realtimeSynchronize(double simTime, int resolution, double
214290
#include <stdlib.h>
215291
#include <unistd.h>
216292
#include <errno.h>
293+
#include <string.h>
217294
#include <sys/mman.h>
218295
#include "../src/include/CompatibilityDefs.h"
219296
#define MY_RT_PRIORITY (49) /**< we use 49 since PRREMPT_RT use 50
@@ -307,6 +384,23 @@ void MDD_setPriority(int priority) {
307384

308385
}
309386

387+
typedef struct {
388+
int prio; /* dummy */
389+
} ProcPrio;
390+
391+
DllExport void* MDD_ProcessPriorityConstructor(int priority) {
392+
ProcPrio* prio = (ProcPrio*) malloc(sizeof(ProcPrio));
393+
MDD_setPriority(priority);
394+
return (void*) prio;
395+
}
396+
397+
DllExport void MDD_ProcessPriorityDestructor(void* prioObj) {
398+
ProcPrio* prio = (ProcPrio*) prioObj;
399+
if (prio) {
400+
free(prio);
401+
}
402+
}
403+
310404
/** Slow down task so that simulation time == real-time.
311405
*
312406
* @param[in] simTime (s) simulation time

Resources/Library/win32/ITI_MDD.dll

1 KB
Binary file not shown.

Resources/Library/win64/ITI_MDD.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)