mp-linux.cpp revision 5763f13421bf47d142d5165590170a5d991daede
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe/* $Id$ */
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe/** @file
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * IPRT - Multiprocessor, Linux.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe * Copyright (C) 2006-2008 Sun Microsystems, Inc.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * This file is part of VirtualBox Open Source Edition (OSE), as
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * available from http://www.virtualbox.org. This file is free software;
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe * you can redistribute it and/or modify it under the terms of the GNU
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * General Public License (GPL) as published by the Free Software
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Foundation, in version 2 as it comes in the "COPYING" file of the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The contents of this file may alternatively be used under the terms
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * of the Common Development and Distribution License Version 1.0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe * VirtualBox OSE distribution, in which case the provisions of the
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe * CDDL are applicable instead of those of the GPL.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * You may elect to license modified versions of this file under the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * terms and conditions of either the GPL or the CDDL or both.
19d8729755d7f4d9503029a628dacbbdabcd2264wrowe *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Clara, CA 95054 USA or visit http://www.sun.com if you need
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * additional information or have any questions.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe/*******************************************************************************
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding* Header Files *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding*******************************************************************************/
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#define LOG_GROUP RTLOGGROUP_SYSTEM
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe#include <unistd.h>
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe#include <stdio.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <sys/sysctl.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <sys/stat.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <sys/fcntl.h>
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe#include <errno.h>
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <iprt/mp.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding#include <iprt/cpuset.h>
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe#include <iprt/assert.h>
56e85d89d42a6980f31b800266649efbed338da3wrowe#include <iprt/string.h>
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
4439ba95daf7b82794fe338726790e2dab89d119wrowe/** @todo move the rtLinuxSysFs* bits into sysfs.cpp and sysfs.h. */
37ad54b8fd2611b7a4f2b269eec3d27ed784a25dwrowe
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/**
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * @returns true / false, errno is preserved.
5ac28f3fe2417368757f29cf381338357605fd52wrowe * @param pszFormat The name format, without "/sys/".
5ac28f3fe2417368757f29cf381338357605fd52wrowe * @param va The format args.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
4e8732935d428b2a70151b178a95cbba5d3839f5wrowebool rtLinuxSysFsExistsV(const char *pszFormat, va_list va)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding int iSavedErrno = errno;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe /*
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe * Construct the filename and call stat.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding char szFilename[128];
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding static const size_t cchPrefix = sizeof("/sys/") - 1;
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe strcpy(szFilename, "/sys/");
9ec65cbae2f760e485a1c54df5b19853688d5c91wrowe size_t cch = RTStrPrintfV(&szFilename[cchPrefix], sizeof(szFilename) - cchPrefix, pszFormat, va);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding Assert(cch < sizeof(szFilename) - cchPrefix - 1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
5ac28f3fe2417368757f29cf381338357605fd52wrowe struct stat st;
e1ad80c048e29e968221817698529d73098f07a4wrowe bool fRet = stat(szFilename, &st) == 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding errno = iSavedErrno;
4439ba95daf7b82794fe338726790e2dab89d119wrowe return fRet;
37ad54b8fd2611b7a4f2b269eec3d27ed784a25dwrowe}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/**
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
5ac28f3fe2417368757f29cf381338357605fd52wrowe * @returns true / false, errno is preserved.
5ac28f3fe2417368757f29cf381338357605fd52wrowe * @param pszFormat The name format, without "/sys/".
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * @param ... The format args.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingbool rtLinuxSysFsExists(const char *pszFormat, ...)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding va_list va;
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe va_start(va, pszFormat);
4e8732935d428b2a70151b178a95cbba5d3839f5wrowe bool fRet = rtLinuxSysFsExistsV(pszFormat, va);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding va_end(va);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding return fRet;
990e1969a428b8844e07aad088df41340cd009d4wrowe}
4439ba95daf7b82794fe338726790e2dab89d119wrowe
4439ba95daf7b82794fe338726790e2dab89d119wrowe
4439ba95daf7b82794fe338726790e2dab89d119wrowe/**
8c8173f49dd7122e10636b3d20ae841551bd0b43wrowe * Opens a sysfs file.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * @returns The file descriptor. -1 and errno on failure.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * @param pszFormat The name format, without "/sys/".
* @param va The format args.
*/
int rtLinuxSysFsOpenV(const char *pszFormat, va_list va)
{
/*
* Construct the filename and call open.
*/
char szFilename[128];
static const size_t cchPrefix = sizeof("/sys/") - 1;
strcpy(szFilename, "/sys/");
size_t cch = RTStrPrintfV(&szFilename[cchPrefix], sizeof(szFilename) - cchPrefix, pszFormat, va);
Assert(cch < sizeof(szFilename) - cchPrefix - 1);
return open(szFilename, O_RDONLY, 0);
}
/**
* Opens a sysfs file.
*
* @returns The file descriptor. -1 and errno on failure.
* @param pszFormat The name format, without "/sys/".
* @param ... The format args.
*/
int rtLinuxSysFsOpen(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int fd = rtLinuxSysFsOpenV(pszFormat, va);
va_end(va);
return fd;
}
/**
* Closes a file opened with rtLinuxSysFsOpen or rtLinuxSysFsOpenV.
*
* @param fd
*/
void rtLinuxSysFsClose(int fd)
{
int iSavedErrno = errno;
close(fd);
errno = iSavedErrno;
}
/**
* Closes a file opened with rtLinuxSysFsOpen or rtLinuxSysFsOpenV.
*
* @returns The number of bytes read. -1 and errno on failure.
* @param fd The file descriptor returned by rtLinuxSysFsOpen or rtLinuxSysFsOpenV.
* @param pszBuf Where to store the string.
* @param cchBuf The size of the buffer. Must be at least 2 bytes.
*/
ssize_t rtLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf)
{
Assert(cchBuf > 1);
ssize_t cchRead = read(fd, pszBuf, cchBuf - 1);
pszBuf[cchRead >= 0 ? cchRead : 0] = '\0';
return cchRead;
}
/**
* Reads a sysfs file.
*
* @returns 64-bit signed value on success, -1 and errno on failure.
* @param uBase The number base, 0 for autodetect.
* @param pszFormat The filename format, without "/sys/".
* @param va Format args.
*/
int64_t rtLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va)
{
int fd = rtLinuxSysFsOpenV(pszFormat, va);
if (fd == -1)
return -1;
int64_t i64Ret = -1;
char szNum[128];
ssize_t cchNum = rtLinuxSysFsReadStr(fd, szNum, sizeof(szNum));
if (cchNum > 0)
{
int rc = RTStrToInt64Ex(szNum, NULL, uBase, &i64Ret);
if (RT_FAILURE(rc))
{
i64Ret = -1;
errno = -ETXTBSY; /* just something that won't happen at read / open. */
}
}
else if (cchNum == 0)
errno = -ETXTBSY; /* just something that won't happen at read / open. */
rtLinuxSysFsClose(fd);
return i64Ret;
}
/**
* Reads a sysfs file.
*
* @returns 64-bit signed value on success, -1 and errno on failure.
* @param uBase The number base, 0 for autodetect.
* @param pszFormat The filename format, without "/sys/".
* @param ... Format args.
*/
static int64_t rtLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int64_t i64Ret = rtLinuxSysFsReadIntFileV(uBase, pszFormat, va);
va_end(va);
return i64Ret;
}
/**
* Internal worker that determins the max possible CPU count.
*
* @returns Max cpus.
*/
DECLINLINE(RTCPUID) rtMpLinuxMaxCpus(void)
{
int cMax = sysconf(_SC_NPROCESSORS_CONF);
Assert(cMax >= 1);
return cMax;
}
/** @todo RTmpCpuId(). */
RTDECL(int) RTMpCpuIdToSetIndex(RTCPUID idCpu)
{
return idCpu < rtMpLinuxMaxCpus() ? idCpu : -1;
}
RTDECL(RTCPUID) RTMpCpuIdFromSetIndex(int iCpu)
{
return (unsigned)iCpu < rtMpLinuxMaxCpus() ? iCpu : NIL_RTCPUID;
}
RTDECL(RTCPUID) RTMpGetMaxCpuId(void)
{
return rtMpLinuxMaxCpus() - 1;
}
RTDECL(bool) RTMpIsCpuOnline(RTCPUID idCpu)
{
/** @todo check if there is a simpler interface than this... */
int i = rtLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/online", (int)idCpu);
if ( i == -1
&& rtLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu))
{
Assert(!rtLinuxSysFsExists("devices/system/cpu/cpu%d/online", (int)idCpu));
i = 1;
}
Assert(i == 0 || i == -1 || i == 1);
return i != 0 && i != -1;
}
RTDECL(bool) RTMpIsCpuPossible(RTCPUID idCpu)
{
/** @todo check this up with hotplugging! */
return rtLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu);
}
RTDECL(PRTCPUSET) RTMpGetSet(PRTCPUSET pSet)
{
RTCpuSetEmpty(pSet);
RTCPUID cMax = rtMpLinuxMaxCpus();
for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
if (RTMpIsCpuPossible(idCpu))
RTCpuSetAdd(pSet, idCpu);
return pSet;
}
RTDECL(RTCPUID) RTMpGetCount(void)
{
RTCPUSET Set;
RTMpGetSet(&Set);
return RTCpuSetCount(&Set);
}
RTDECL(PRTCPUSET) RTMpGetOnlineSet(PRTCPUSET pSet)
{
RTCpuSetEmpty(pSet);
RTCPUID cMax = rtMpLinuxMaxCpus();
for (RTCPUID idCpu = 0; idCpu < cMax; idCpu++)
if (RTMpIsCpuOnline(idCpu))
RTCpuSetAdd(pSet, idCpu);
return pSet;
}
RTDECL(RTCPUID) RTMpGetOnlineCount(void)
{
RTCPUSET Set;
RTMpGetOnlineSet(&Set);
return RTCpuSetCount(&Set);
}