4141#include < unordered_map>
4242#include < cerrno>
4343
44-
4544// clang-format off
4645// We want to keep the URL in one line to avoid formatting issues. This will make it easier to
4746// extract the URL for an automatic check.
@@ -57,44 +56,62 @@ const char* processPriorityToString(DWORD priority)
5756{
5857 switch (priority)
5958 {
60- case IDLE_PRIORITY_CLASS : return " IDLE" ;
61- case BELOW_NORMAL_PRIORITY_CLASS : return " BELOW_NORMAL" ;
62- case NORMAL_PRIORITY_CLASS : return " NORMAL" ;
63- case ABOVE_NORMAL_PRIORITY_CLASS : return " ABOVE_NORMAL" ;
64- case HIGH_PRIORITY_CLASS : return " HIGH" ;
65- case REALTIME_PRIORITY_CLASS : return " REALTIME" ;
66- default : return " UNKNOWN" ;
59+ case IDLE_PRIORITY_CLASS :
60+ return " IDLE" ;
61+ case BELOW_NORMAL_PRIORITY_CLASS :
62+ return " BELOW_NORMAL" ;
63+ case NORMAL_PRIORITY_CLASS :
64+ return " NORMAL" ;
65+ case ABOVE_NORMAL_PRIORITY_CLASS :
66+ return " ABOVE_NORMAL" ;
67+ case HIGH_PRIORITY_CLASS :
68+ return " HIGH" ;
69+ case REALTIME_PRIORITY_CLASS :
70+ return " REALTIME" ;
71+ default :
72+ return " UNKNOWN" ;
6773 }
6874}
6975
7076const char * threadPriorityToString (int priority)
7177{
7278 switch (priority)
7379 {
74- case THREAD_PRIORITY_LOWEST : return " LOWEST" ;
75- case THREAD_PRIORITY_BELOW_NORMAL : return " BELOW_NORMAL" ;
76- case THREAD_PRIORITY_NORMAL : return " NORMAL" ;
77- case THREAD_PRIORITY_ABOVE_NORMAL : return " ABOVE_NORMAL" ;
78- case THREAD_PRIORITY_HIGHEST : return " HIGHEST" ;
79- case THREAD_PRIORITY_TIME_CRITICAL : return " TIME_CRITICAL" ;
80- default : return " UNKNOWN" ;
80+ case THREAD_PRIORITY_LOWEST :
81+ return " LOWEST" ;
82+ case THREAD_PRIORITY_BELOW_NORMAL :
83+ return " BELOW_NORMAL" ;
84+ case THREAD_PRIORITY_NORMAL :
85+ return " NORMAL" ;
86+ case THREAD_PRIORITY_ABOVE_NORMAL :
87+ return " ABOVE_NORMAL" ;
88+ case THREAD_PRIORITY_HIGHEST :
89+ return " HIGHEST" ;
90+ case THREAD_PRIORITY_TIME_CRITICAL :
91+ return " TIME_CRITICAL" ;
92+ default :
93+ return " UNKNOWN" ;
8194 }
8295}
8396
84- bool isProcessElevated () {
85- bool isElevated = false ;
86- HANDLE hToken = nullptr ;
87- if (OpenProcessToken (GetCurrentProcess (),TOKEN_QUERY ,&hToken)){
88- TOKEN_ELEVATION elevation;
89- DWORD dwSize = sizeof (elevation);
90- if (GetTokenInformation (hToken, TokenElevation, &elevation, sizeof (elevation), &dwSize)){
91- isElevated = elevation.TokenIsElevated ;
92- }
93- }
94- if (hToken) {
95- CloseHandle (hToken);
97+ bool isProcessElevated ()
98+ {
99+ bool isElevated = false ;
100+ HANDLE hToken = nullptr ;
101+ if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY , &hToken))
102+ {
103+ TOKEN_ELEVATION elevation;
104+ DWORD dwSize = sizeof (elevation);
105+ if (GetTokenInformation (hToken, TokenElevation, &elevation, sizeof (elevation), &dwSize))
106+ {
107+ isElevated = elevation.TokenIsElevated ;
96108 }
97- return isElevated;
109+ }
110+ if (hToken)
111+ {
112+ CloseHandle (hToken);
113+ }
114+ return isElevated;
98115}
99116
100117std::string maskToCpuList (uint64_t mask)
@@ -104,62 +121,60 @@ std::string maskToCpuList(uint64_t mask)
104121 {
105122 if (mask & (1ULL << i))
106123 {
107- if (out.size () > 1 ) out += " ," ;
124+ if (out.size () > 1 )
125+ out += " ," ;
108126 out += std::to_string (i);
109127 }
110128 }
111129 out += " ]" ;
112130 return out;
113-
114131}
115132
116- std::string getLastWindowsErrorMsg (DWORD error_id)
133+ std::string getLastWindowsErrorMsg (DWORD error_id)
117134{
118- LPSTR buffer = nullptr ;
119-
120- DWORD size = FormatMessageA (
121- FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
122- nullptr ,
123- error_id,
124- MAKELANGID (LANG_NEUTRAL , SUBLANG_DEFAULT ),
125- (LPSTR )&buffer,
126- 0 ,
127- nullptr );
128-
129- if (size == 0 || buffer == nullptr )
130- {
131- return " Unknown Windows error" ;
132- }
135+ LPSTR buffer = nullptr ;
133136
134- std::string message (buffer, size);
135- LocalFree (buffer);
137+ DWORD size =
138+ FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
139+ nullptr , error_id, MAKELANGID (LANG_NEUTRAL , SUBLANG_DEFAULT ), (LPSTR )&buffer, 0 , nullptr );
136140
137- return message;
141+ if (size == 0 || buffer == nullptr )
142+ {
143+ return " Unknown Windows error" ;
144+ }
145+
146+ std::string message (buffer, size);
147+ LocalFree (buffer);
148+
149+ return message;
138150};
139151
140152bool setProcessPriority (pprocess_t & process, DWORD priority)
141153{
142154 if (!::SetPriorityClass (process, priority))
143155 {
144156 DWORD err = GetLastError ();
145- URCL_LOG_ERROR (" Unsuccessful in setting the process priority to %s (%llX). Error: %lu (%s)" , processPriorityToString (priority), priority, err, getLastWindowsErrorMsg (err).c_str ());
157+ URCL_LOG_ERROR (" Unsuccessful in setting the process priority to %s (%llX). Error: %lu (%s)" ,
158+ processPriorityToString (priority), priority, err, getLastWindowsErrorMsg (err).c_str ());
146159 return false ;
147160 }
148-
161+
149162 DWORD priority_applied = ::GetPriorityClass (process);
150- if (priority_applied == 0 ){
163+ if (priority_applied == 0 )
164+ {
151165 DWORD err = GetLastError ();
152- URCL_LOG_ERROR (" Unsuccessful in retrieving the process priority for verification. Error: %lu (%s)" , err, getLastWindowsErrorMsg (err).c_str ());
166+ URCL_LOG_ERROR (" Unsuccessful in retrieving the process priority for verification. Error: %lu (%s)" , err,
167+ getLastWindowsErrorMsg (err).c_str ());
153168 return false ;
154169 }
155170
156- URCL_LOG_INFO (" Process priority successfully set to %s (0x%X)" , processPriorityToString (priority_applied), priority_applied);
171+ URCL_LOG_INFO (" Process priority successfully set to %s (0x%X)" , processPriorityToString (priority_applied),
172+ priority_applied);
157173
158174 if (priority_applied != priority)
159175 {
160- URCL_LOG_WARN (" Process priority mismatch. Expected %s (0x%X), got %s (0x%X)" ,
161- processPriorityToString (priority), priority,
162- processPriorityToString (priority_applied), priority_applied);
176+ URCL_LOG_WARN (" Process priority mismatch. Expected %s (0x%X), got %s (0x%X)" , processPriorityToString (priority),
177+ priority, processPriorityToString (priority_applied), priority_applied);
163178 return false ;
164179 }
165180
@@ -172,30 +187,25 @@ bool setProcessAffinity(pprocess_t& process, DWORD_PTR cpu_mask)
172187 {
173188 DWORD err = GetLastError ();
174189 URCL_LOG_ERROR (" Unsuccessful in setting process affinity to CPUs %s (mask=0x%llX). Error: %lu (%s)" ,
175- maskToCpuList (cpu_mask).c_str (),
176- static_cast <uint64_t >(cpu_mask),
177- err,
178- getLastWindowsErrorMsg (err).c_str ());
190+ maskToCpuList (cpu_mask).c_str (), static_cast <uint64_t >(cpu_mask), err,
191+ getLastWindowsErrorMsg (err).c_str ());
179192
180- return false ;
193+ return false ;
181194 }
182195 DWORD_PTR process_mask = 0 ;
183- DWORD_PTR system_mask = 0 ;
196+ DWORD_PTR system_mask = 0 ;
184197 if (!::GetProcessAffinityMask (process, &process_mask, &system_mask))
185198 {
186199 DWORD err = GetLastError ();
187- URCL_LOG_ERROR (" Unsuccessful in setting process affinity to %s. Error: %lu (%s)" ,
188- maskToCpuList (cpu_mask).c_str (),
189- err, getLastWindowsErrorMsg (err).c_str ());
190- return false ;
200+ URCL_LOG_ERROR (" Unsuccessful in setting process affinity to %s. Error: %lu (%s)" , maskToCpuList (cpu_mask).c_str (),
201+ err, getLastWindowsErrorMsg (err).c_str ());
202+ return false ;
191203 }
192- URCL_LOG_INFO (" Process affinity set to CPUs %s (mask=0x%llX)" ,
193- maskToCpuList (process_mask).c_str (),
204+ URCL_LOG_INFO (" Process affinity set to CPUs %s (mask=0x%llX)" , maskToCpuList (process_mask).c_str (),
194205 static_cast <uint64_t >(process_mask));
195206 if (process_mask != cpu_mask)
196207 {
197- URCL_LOG_WARN (" Process affinity mismatch. Expected %s, got %s" ,
198- maskToCpuList (cpu_mask).c_str (),
208+ URCL_LOG_WARN (" Process affinity mismatch. Expected %s, got %s" , maskToCpuList (cpu_mask).c_str (),
199209 maskToCpuList (process_mask).c_str ());
200210 return false ;
201211 }
@@ -209,43 +219,41 @@ bool setThreadAffinity(pthread_t& thread, DWORD_PTR cpu_mask)
209219 if (result == 0 )
210220 {
211221 DWORD err = GetLastError ();
212- URCL_LOG_ERROR (" Unsuccessful in setting thread affinity to %s. Error: %lu (%s)" ,
213- maskToCpuList (cpu_mask).c_str (),
214- err, getLastWindowsErrorMsg (err).c_str ());
222+ URCL_LOG_ERROR (" Unsuccessful in setting thread affinity to %s. Error: %lu (%s)" , maskToCpuList (cpu_mask).c_str (),
223+ err, getLastWindowsErrorMsg (err).c_str ());
215224 return false ;
216225 }
217226
218- URCL_LOG_INFO (" Thread affinity successfully set to CPUs %s (mask=0x%llX)" ,
219- maskToCpuList (cpu_mask).c_str (),
220- static_cast <uint64_t >(cpu_mask));
227+ URCL_LOG_INFO (" Thread affinity successfully set to CPUs %s (mask=0x%llX)" , maskToCpuList (cpu_mask).c_str (),
228+ static_cast <uint64_t >(cpu_mask));
221229 return true ;
222230}
223231
224- bool setThreadPriority (pthread_t & thread, const int priority){
232+ bool setThreadPriority (pthread_t & thread, const int priority)
233+ {
225234 if (!::SetThreadPriority (thread, priority))
226235 {
227236 DWORD err = GetLastError ();
228237 URCL_LOG_ERROR (" Unsuccessful in setting thread priority to %s (%d). Error: %lu (%s)" ,
229- threadPriorityToString (priority), priority,
230- err, getLastWindowsErrorMsg (err).c_str ());
238+ threadPriorityToString (priority), priority, err, getLastWindowsErrorMsg (err).c_str ());
231239
232240 return false ;
233241 }
234242
235243 int applied = ::GetThreadPriority (thread);
236- if (applied == THREAD_PRIORITY_ERROR_RETURN ){
244+ if (applied == THREAD_PRIORITY_ERROR_RETURN )
245+ {
237246 DWORD err = GetLastError ();
238- URCL_LOG_ERROR (" Unsuccessful in retrieving the thread priority for verification. Error: %lu (%s)" , err, getLastWindowsErrorMsg (err).c_str ());
247+ URCL_LOG_ERROR (" Unsuccessful in retrieving the thread priority for verification. Error: %lu (%s)" , err,
248+ getLastWindowsErrorMsg (err).c_str ());
239249 return false ;
240250 }
241251
242- URCL_LOG_INFO (" Thread priority successfully set to %s (%d)" ,
243- threadPriorityToString (applied), applied);
252+ URCL_LOG_INFO (" Thread priority successfully set to %s (%d)" , threadPriorityToString (applied), applied);
244253
245254 if (applied != priority)
246255 {
247- URCL_LOG_WARN (" Thread priority mismatch. Expected %s (%d), got %s (%d)" ,
248- threadPriorityToString (priority), priority,
256+ URCL_LOG_WARN (" Thread priority mismatch. Expected %s (%d), got %s (%d)" , threadPriorityToString (priority), priority,
249257 threadPriorityToString (applied), applied);
250258 return false ;
251259 }
@@ -275,25 +283,20 @@ std::string cpuSetToString(const cpu_set_t& cpuset)
275283 return out;
276284}
277285
278- bool setThreadAffinity (pthread_t thread, const cpu_set_t & cpuset)
286+ bool setThreadAffinity (pthread_t & thread, const cpu_set_t & cpuset)
279287{
280288 int ret = pthread_setaffinity_np (thread, sizeof (cpu_set_t ), &cpuset);
281289
282290 if (ret != 0 )
283291 {
284- URCL_LOG_ERROR (
285- " Unsuccessful in setting thread affinity. Error: %s" ,
286- strerror (ret));
292+ URCL_LOG_ERROR (" Unsuccessful in setting thread affinity. Error: %s" , strerror (ret));
287293 return false ;
288294 }
289295
290296 cpu_set_t applied_set;
291297 CPU_ZERO (&applied_set);
292298
293- if (pthread_getaffinity_np (
294- thread,
295- sizeof (cpu_set_t ),
296- &applied_set) == 0 )
299+ if (pthread_getaffinity_np (thread, sizeof (cpu_set_t ), &applied_set) == 0 )
297300 {
298301 bool match = true ;
299302
@@ -308,10 +311,8 @@ bool setThreadAffinity(pthread_t thread, const cpu_set_t& cpuset)
308311
309312 if (!match)
310313 {
311- URCL_LOG_WARN (
312- " Thread affinity mismatch. Requested %s, got %s" ,
313- cpuSetToString (cpuset).c_str (),
314- cpuSetToString (applied_set).c_str ());
314+ URCL_LOG_WARN (" Thread affinity mismatch. Requested %s, got %s" , cpuSetToString (cpuset).c_str (),
315+ cpuSetToString (applied_set).c_str ());
315316
316317 return false ;
317318 }
@@ -321,9 +322,7 @@ bool setThreadAffinity(pthread_t thread, const cpu_set_t& cpuset)
321322 URCL_LOG_WARN (" Could not retrieve thread affinity" );
322323 }
323324
324- URCL_LOG_INFO (
325- " Thread affinity successfully set to CPUs %s" ,
326- cpuSetToString (applied_set).c_str ());
325+ URCL_LOG_INFO (" Thread affinity successfully set to CPUs %s" , cpuSetToString (applied_set).c_str ());
327326
328327 return true ;
329328}
@@ -332,14 +331,12 @@ bool setThreadAffinity(pthread_t thread, const cpu_set_t& cpuset)
332331
333332bool setFiFoScheduling (pthread_t & thread, int priority)
334333{
335-
336334#ifdef _WIN32
337335
338- if (!isProcessElevated ())
336+ if (!isProcessElevated ())
339337 {
340- URCL_LOG_WARN (
341- " Process is not running with elevated privileges (UAC). "
342- " REALTIME_PRIORITY_CLASS may fail. Try 'Run as Administrator'." );
338+ URCL_LOG_WARN (" Process is not running with elevated privileges (UAC). "
339+ " REALTIME_PRIORITY_CLASS may fail. Try 'Run as Administrator'." );
343340 }
344341 pprocess_t process = ::GetCurrentProcess ();
345342
@@ -351,9 +348,7 @@ bool setFiFoScheduling(pthread_t& thread, int priority)
351348
352349 if (!setThreadPriority (thread, priority))
353350 {
354- URCL_LOG_ERROR (" Unsuccessful in setting thread priority to %s (%d)" ,
355- threadPriorityToString (priority),
356- priority);
351+ URCL_LOG_ERROR (" Unsuccessful in setting thread priority to %s (%d)" , threadPriorityToString (priority), priority);
357352 return false ;
358353 }
359354
0 commit comments