2 * IPRT - Multiprocessor.
6 * Copyright (C) 2008-2016 Oracle Corporation
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
29 #include <iprt/cdefs.h>
30 #include <iprt/types.h>
35 /** @defgroup grp_rt_mp RTMp - Multiprocessor
41 * Gets the identifier of the CPU executing the call.
43 * When called from a system mode where scheduling is active, like ring-3 or
44 * kernel mode with interrupts enabled on some systems, no assumptions should
45 * be made about the current CPU when the call returns.
49 RTDECL(RTCPUID
) RTMpCpuId(void);
52 * Get the CPU set index of the CPU executing the call.
54 * Same scheduling warnings as for RTMpCpuId().
56 * @returns CPU set index.
58 RTDECL(int) RTMpCurSetIndex(void);
61 * Get the CPU set index and identifier of the CPU executing the call.
63 * Same scheduling warnings as for RTMpCpuId().
65 * @returns CPU set index.
66 * @param pidCpu Where to return the CPU identifier. (not optional)
68 RTDECL(int) RTMpCurSetIndexAndId(PRTCPUID pidCpu
);
71 * Converts a CPU identifier to a CPU set index.
73 * This may or may not validate the presence of the CPU.
75 * @returns The CPU set index on success, -1 on failure.
76 * @param idCpu The identifier of the CPU.
78 RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu
);
81 * Converts a CPU set index to a a CPU identifier.
83 * This may or may not validate the presence of the CPU, so, use
84 * RTMpIsCpuPossible for that.
86 * @returns The corresponding CPU identifier, NIL_RTCPUID on failure.
87 * @param iCpu The CPU set index.
89 RTDECL(RTCPUID
) RTMpCpuIdFromSetIndex(int iCpu
);
92 * Translates an NT process group member to a CPU set index.
94 * @returns CPU set index, -1 if not valid.
95 * @param idxGroup The CPU group.
96 * @param idxMember The CPU group member number.
98 * @remarks Only available on Windows.
100 RTDECL(int) RTMpSetIndexFromCpuGroupMember(uint32_t idxGroup
, uint32_t idxMember
);
103 * Gets the member numbers for a CPU group.
105 * @returns Maximum number of group members.
106 * @param idxGroup The CPU group.
107 * @param pcActive Where to return the number of active members.
109 * @remarks Only available on Windows.
111 RTDECL(uint32_t) RTMpGetCpuGroupCounts(uint32_t idxGroup
, uint32_t *pcActive
);
114 * Get the maximum number of CPU groups.
116 * @returns Maximum number of CPU groups.
118 * @remarks Only available on Windows.
120 RTDECL(uint32_t) RTMpGetMaxCpuGroupCount(void);
123 * Gets the max CPU identifier (inclusive).
125 * Intended for brute force enumerations, but use with
126 * care as it may be expensive.
128 * @returns The current higest CPU identifier value.
130 RTDECL(RTCPUID
) RTMpGetMaxCpuId(void);
133 * Gets the size of a CPU array that is indexed by CPU set index.
135 * This takes both online, offline and hot-plugged cpus into account.
137 * @returns Number of elements.
139 * @remarks Use RTMpCpuIdToSetIndex to convert a RTCPUID into an array index.
141 RTDECL(uint32_t) RTMpGetArraySize(void);
144 * Checks if a CPU exists in the system or may possibly be hotplugged later.
146 * @returns true/false accordingly.
147 * @param idCpu The identifier of the CPU.
149 RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu
);
152 * Gets set of the CPUs present in the system plus any that may
153 * possibly be hotplugged later.
156 * @param pSet Where to put the set.
158 RTDECL(PRTCPUSET
) RTMpGetSet(PRTCPUSET pSet
);
161 * Get the count of CPUs present in the system plus any that may
162 * possibly be hotplugged later.
164 * @returns The count.
165 * @remarks Don't use this for CPU array sizing, use RTMpGetArraySize instead.
167 RTDECL(RTCPUID
) RTMpGetCount(void);
170 * Get the count of physical CPU cores present in the system plus any that may
171 * possibly be hotplugged later.
173 * @returns The number of cores.
175 RTDECL(RTCPUID
) RTMpGetCoreCount(void);
178 * Gets set of the CPUs present that are currently online.
181 * @param pSet Where to put the set.
183 RTDECL(PRTCPUSET
) RTMpGetOnlineSet(PRTCPUSET pSet
);
186 * Get the count of CPUs that are currently online.
190 RTDECL(RTCPUID
) RTMpGetOnlineCount(void);
193 * Get the count of physical CPU cores in the system with one or more online
196 * @returns The number of online cores.
198 RTDECL(RTCPUID
) RTMpGetOnlineCoreCount(void);
201 * Checks if a CPU is online or not.
203 * @returns true/false accordingly.
204 * @param idCpu The identifier of the CPU.
206 RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu
);
210 * Gets set of the CPUs present in the system.
213 * @param pSet Where to put the set.
215 RTDECL(PRTCPUSET
) RTMpGetPresentSet(PRTCPUSET pSet
);
218 * Get the count of CPUs that are present in the system.
222 RTDECL(RTCPUID
) RTMpGetPresentCount(void);
225 * Get the count of physical CPU cores present in the system.
227 * @returns The number of cores.
229 RTDECL(RTCPUID
) RTMpGetPresentCoreCount(void);
232 * Checks if a CPU is present in the system.
234 * @returns true/false accordingly.
235 * @param idCpu The identifier of the CPU.
237 RTDECL(bool) RTMpIsCpuPresent(RTCPUID idCpu
);
241 * Get the current frequency of a CPU.
243 * The CPU must be online.
245 * @returns The frequency as MHz. 0 if the CPU is offline
246 * or the information is not available.
247 * @param idCpu The identifier of the CPU.
249 RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu
);
252 * Get the maximum frequency of a CPU.
254 * The CPU must be online.
256 * @returns The frequency as MHz. 0 if the CPU is offline
257 * or the information is not available.
258 * @param idCpu The identifier of the CPU.
260 RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu
);
263 * Get the CPU description string.
265 * The CPU must be online.
267 * @returns IPRT status code.
268 * @param idCpu The identifier of the CPU. NIL_RTCPUID can be used to
269 * indicate the current CPU.
270 * @param pszBuf The output buffer.
271 * @param cbBuf The size of the output buffer.
273 RTDECL(int) RTMpGetDescription(RTCPUID idCpu
, char *pszBuf
, size_t cbBuf
);
279 * Check if there's work (DPCs on Windows) pending on the current CPU.
281 * @return true if there's pending work on the current CPU, false otherwise.
283 RTDECL(bool) RTMpIsCpuWorkPending(void);
287 * Worker function passed to RTMpOnAll, RTMpOnOthers and RTMpOnSpecific that
288 * is to be called on the target cpus.
290 * @param idCpu The identifier for the CPU the function is called on.
291 * @param pvUser1 The 1st user argument.
292 * @param pvUser2 The 2nd user argument.
294 typedef DECLCALLBACK(void) FNRTMPWORKER(RTCPUID idCpu
, void *pvUser1
, void *pvUser2
);
295 /** Pointer to a FNRTMPWORKER. */
296 typedef FNRTMPWORKER
*PFNRTMPWORKER
;
298 /** @name RTMPON_F_XXX - RTMpOn flags.
300 /** Caller doesn't care if pfnWorker is executed at the same time on the
301 * specified CPUs or not, as long as it gets executed. */
302 #define RTMPON_F_WHATEVER_EXEC 0
303 /** The caller insists on pfnWorker being executed more or less concurrently
304 * on the specified CPUs. */
305 #define RTMPON_F_CONCURRENT_EXEC RT_BIT_32(1)
306 /** Mask of valid bits. */
307 #define RTMPON_F_VALID_MASK UINT32_C(0x00000001)
311 * Checks if the RTMpOnAll() is safe with regards to all threads executing
314 * If for instance, the RTMpOnAll() is implemented in a way where the threads
315 * might cause a classic deadlock, it is considered -not- concurrent safe.
316 * Windows currently is one such platform where it isn't safe.
318 * @returns true if RTMpOnAll() is concurrent safe, false otherwise.
320 RTDECL(bool) RTMpOnAllIsConcurrentSafe(void);
323 * Executes a function on each (online) CPU in the system.
325 * @returns IPRT status code.
326 * @retval VINF_SUCCESS on success.
327 * @retval VERR_NOT_SUPPORTED if this kind of operation isn't supported by the system.
329 * @param pfnWorker The worker function.
330 * @param pvUser1 The first user argument for the worker.
331 * @param pvUser2 The second user argument for the worker.
333 * @remarks The execution isn't in any way guaranteed to be simultaneous,
334 * it might even be serial (cpu by cpu).
336 RTDECL(int) RTMpOnAll(PFNRTMPWORKER pfnWorker
, void *pvUser1
, void *pvUser2
);
339 * Executes a function on a all other (online) CPUs in the system.
341 * The caller must disable preemption prior to calling this API if the outcome
342 * is to make any sense. But do *not* disable interrupts.
344 * @returns IPRT status code.
345 * @retval VINF_SUCCESS on success.
346 * @retval VERR_NOT_SUPPORTED if this kind of operation isn't supported by the system.
348 * @param pfnWorker The worker function.
349 * @param pvUser1 The first user argument for the worker.
350 * @param pvUser2 The second user argument for the worker.
352 * @remarks The execution isn't in any way guaranteed to be simultaneous,
353 * it might even be serial (cpu by cpu).
355 RTDECL(int) RTMpOnOthers(PFNRTMPWORKER pfnWorker
, void *pvUser1
, void *pvUser2
);
358 * Executes a function on a specific CPU in the system.
360 * @returns IPRT status code.
361 * @retval VINF_SUCCESS on success.
362 * @retval VERR_NOT_SUPPORTED if this kind of operation isn't supported by the system.
363 * @retval VERR_CPU_OFFLINE if the CPU is offline.
364 * @retval VERR_CPU_NOT_FOUND if the CPU wasn't found.
366 * @param idCpu The id of the CPU.
367 * @param pfnWorker The worker function.
368 * @param pvUser1 The first user argument for the worker.
369 * @param pvUser2 The second user argument for the worker.
371 RTDECL(int) RTMpOnSpecific(RTCPUID idCpu
, PFNRTMPWORKER pfnWorker
, void *pvUser1
, void *pvUser2
);
374 * Executes a function on two specific CPUs in the system.
376 * @returns IPRT status code.
377 * @retval VINF_SUCCESS on success.
378 * @retval VERR_NOT_SUPPORTED if this kind of operation isn't supported by the
379 * system or if the specified modifier flag isn't supported.
380 * @retval VERR_CPU_OFFLINE if one or more of the CPUs are offline (see
382 * @retval VERR_CPU_NOT_FOUND if on or both of the CPUs weren't found.
383 * @retval VERR_NOT_ALL_CPUS_SHOWED if one of the CPUs didn't show.
385 * @param idCpu1 The id of the first CPU.
386 * @param idCpu2 The id of the second CPU.
387 * @param fFlags Combination of RTMPON_F_XXX flags.
388 * @param pfnWorker The worker function.
389 * @param pvUser1 The first user argument for the worker.
390 * @param pvUser2 The second user argument for the worker.
392 * @remarks There is a possible race between one (or both) of the CPUs going
393 * offline while setting up the call. The worker function must take
396 RTDECL(int) RTMpOnPair(RTCPUID idCpu1
, RTCPUID idCpu2
, uint32_t fFlags
, PFNRTMPWORKER pfnWorker
, void *pvUser1
, void *pvUser2
);
399 * Indicates whether RTMpOnPair supports running the pfnWorker concurrently on
400 * both CPUs using RTMPON_F_CONCURRENT_EXEC.
402 * @returns true if supported, false if not.
404 RTDECL(bool) RTMpOnPairIsConcurrentExecSupported(void);
408 * Pokes the specified CPU.
410 * This should cause the execution on the CPU to be interrupted and forcing it
411 * to enter kernel context. It is optimized version of a RTMpOnSpecific call
412 * with a worker which returns immediately.
414 * @returns IPRT status code.
415 * @retval VERR_NOT_SUPPORTED if this kind of operation isn't supported by the
416 * system. The caller must not automatically assume that this API works
417 * when any of the RTMpOn* APIs works. This is because not all systems
418 * supports unicast MP events and this API will not be implemented as a
420 * @retval VERR_CPU_OFFLINE if the CPU is offline.
421 * @retval VERR_CPU_NOT_FOUND if the CPU wasn't found.
423 * @param idCpu The id of the CPU to poke.
425 RTDECL(int) RTMpPokeCpu(RTCPUID idCpu
);
429 * MP event, see FNRTMPNOTIFICATION.
431 typedef enum RTMPEVENT
433 /** The CPU goes online. */
434 RTMPEVENT_ONLINE
= 1,
435 /** The CPU goes offline. */
440 * Notification callback.
442 * The context this is called in differs a bit from platform to platform, so be
443 * careful while in here.
445 * On Windows we're running with IRQL=PASSIVE_LEVEL (reschedulable) according to
446 * the KeRegisterProcessorChangeCallback documentation - unrestricted API
447 * access. Probably not being called on the onlined/offlined CPU...
449 * On Solaris we're holding the cpu_lock, IPL/SPL/PIL is not yet known, however
450 * we will most likely -not- be firing on the CPU going offline/online.
452 * On Linux it looks like we're called with preemption enabled on any CPU and
453 * not necessarily on the CPU going offline/online.
455 * There is no callbacks for darwin at the moment, due to lack of suitable KPI.
457 * @param idCpu The CPU this applies to.
458 * @param enmEvent The event.
459 * @param pvUser The user argument.
461 typedef DECLCALLBACK(void) FNRTMPNOTIFICATION(RTMPEVENT enmEvent
, RTCPUID idCpu
, void *pvUser
);
462 /** Pointer to a FNRTMPNOTIFICATION(). */
463 typedef FNRTMPNOTIFICATION
*PFNRTMPNOTIFICATION
;
466 * Registers a notification callback for cpu events.
468 * On platforms which doesn't do cpu offline/online events this API
469 * will just be a no-op that pretends to work.
471 * @todo We'll be adding a flag to this soon to indicate whether the callback should be called on all
472 * CPUs that are currently online while it's being registered. This is to help avoid some race
473 * conditions (we'll hopefully be able to implement this on linux, solaris/win is no issue).
475 * @returns IPRT status code.
476 * @retval VINF_SUCCESS on success.
477 * @retval VERR_NO_MEMORY if a registration record cannot be allocated.
478 * @retval VERR_ALREADY_EXISTS if the pfnCallback and pvUser already exist
479 * in the callback list.
481 * @param pfnCallback The callback.
482 * @param pvUser The user argument to the callback function.
484 RTDECL(int) RTMpNotificationRegister(PFNRTMPNOTIFICATION pfnCallback
, void *pvUser
);
487 * This deregisters a notification callback registered via RTMpNotificationRegister().
489 * The pfnCallback and pvUser arguments must be identical to the registration call
490 * of we won't find the right entry.
492 * @returns IPRT status code.
493 * @retval VINF_SUCCESS on success.
494 * @retval VERR_NOT_FOUND if no matching entry was found.
496 * @param pfnCallback The callback.
497 * @param pvUser The user argument to the callback function.
499 RTDECL(int) RTMpNotificationDeregister(PFNRTMPNOTIFICATION pfnCallback
, void *pvUser
);
501 #endif /* IN_RING0 */