
获取 CPU 核数
Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。
Android 的 CPU 设备文件位于/sys/devices/system/cpu/目录,文件名的的格式为cpu\d+。
root@generic_x86_64:/sys/devices/system/cpu # ls cpu0 cpufreq
cpuidle
kernel_max
modalias
offline
online
possible
power
present
uevent
统计一下文件个数便可以获得 CPU 核数。
public static int getNumberOfCPUCores() {
if (BuildVERSIONSDK_INT <= BuildVERSION_CODESGINGERBREAD_MR1) {
// Gingerbread doesn't support giving a single application access to both cores, but a
// handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
// chipset and Gingerbread; that can let an app in the background run without impacting
// the foreground application But for our purposes, it makes them single core
return 1;
}
int cores;
try {
cores = new File(“/sys/devices/system/cpu/”)。listFiles(CPU_FILTER)。length;
} catch (SecurityException e) {
cores = DEVICEINFO_UNKNOWN;
} catch (NullPointerException e) {
cores = DEVICEINFO_UNKNOWN;
}
return cores;
}
private static final FileFilter CPU_FILTER = new FileFilter() {
@Override
public boolean accept(File pathname) {
String path = pathnamegetName();
//regex is slow, so checking char by char
if (pathstartsWith(“cpu”)) {
for (int i = 3; i < pathlength(); i++) {
if (pathcharAt(i) < '0' pathcharAt(i) > '9‘) {
return false;
}
}
return true;
}
return false;
}
};
获取时钟频率
获取时钟频率需要读取系统文件 -/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq或者/proc/cpuinfo。
Android 模拟器中并没有cpuinfo_max_freq文件,因此只能读取/proc/cpuinfo。
/proc/cpuinfo包含了很多 cpu 数据。
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 70
model name : Intel(R) Core(TM) i7-4770HQ CPU @ 220GHz
stepping : 1
cpu MHz : 0000
cache size : 1024 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 4
wp : yes
代码如下:
public static int getCPUMaxFreqKHz() {
int maxFreq = DEVICEINFO_UNKNOWN;
try {
for (int i = 0; i < getNumberOfCPUCores(); i++) {
String filename =
“/sys/devices/system/cpu/cpu” + i + “/cpufreq/cpuinfo_max_freq”;
File cpuInfoMaxFreqFile = new File(filename);
if (cpuInfoMaxFreqFileexists()) {
byte[] buffer = new byte[128];
FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
try {
streamread(buffer);
int endIndex = 0;
//Trim the first number out of the byte buffer
while (buffer[endIndex] >= '0' && buffer[endIndex] <= '9'
&& endIndex < bufferlength) endIndex++;
String str = new String(buffer, 0, endIndex);
Integer freqBound = IntegerparseInt(str);
if (freqBound > maxFreq) maxFreq = freqBound;
} catch (NumberFormatException e) {
//Fall through and use /proc/cpuinfo
} finally {
streamclose();
}
}
}
if (maxFreq == DEVICEINFO_UNKNOWN) {
FileInputStream stream = new FileInputStream(“/proc/cpuinfo”);
try {
int freqBound = parseFileForValue(“cpu MHz”, stream);
freqBound = 1000; //MHz -> kHz
if (freqBound > maxFreq) maxFreq = freqBound;
} finally {
streamclose();
}
}
} catch (IOException e) {
maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown
}
return maxFreq;
}
获取内存大小
如果 SDK 版本大于等于JELLY_BEAN,可以通过ActivityManager来获取内从大小。
ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();
ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);
amgetMemoryInfo(memInfo);
如果版本低于JELLY_BEAN,则只能读取系统文件了。
FileInputStream stream = new FileInputStream(“/proc/meminfo”);
totalMem = parseFileForValue(“MemTotal”, stream);
完整代码如下:
@TargetApi(BuildVERSION_CODESJELLY_BEAN)
public static long getTotalMemory(Context c) {
// memInfototalMem not supported in pre-Jelly Bean APIs
if (BuildVERSIONSDK_INT >= BuildVERSION_CODESJELLY_BEAN) {
ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();
ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);
amgetMemoryInfo(memInfo);
if (memInfo != null) {
return memInfototalMem;
} else {
return DEVICEINFO_UNKNOWN;
}
} else {
long totalMem = DEVICEINFO_UNKNOWN;
try {
FileInputStream stream = new FileInputStream(“/proc/meminfo”);
try {
totalMem = parseFileForValue(“MemTotal”, stream);
totalMem = 1024;
} finally {
streamclose();
}
} catch (IOException e) {
}
return totalMem;
}
}
1 mount -t vfat /dev/block/mmcblk0p1 /mnt/sdcard; 不好用!
2 [Framework 层]主要分析 system/vold/maincpp, DirectVolumecpp, Volumecpp[void Volume::setState(int state); int Volume::mountVol()]
3 [Framework 层]system/vold/maincpp->main函数-->初始化VolumeManager,NetlinkManager,CommandListener;
4 [Framework 层]system/vold/commandlistenercpp-->FrameworkListener("vold") 和registerCmd(new VolumeCmd())[VolumeCmd 的过滤关键字为volume];
5 [Framework 层]system/core/libsysutils/src/frameworklistenercpp; FrameworkListener->dispatchCommand
6 [Framework 层]system/core/libsysutils/src/socketlistenercpp; startListener-->android_get_control_socket(mSocketName)其中mSocketName="vold",该socket已经通过initrc 中的socket vold stream 0660 root mount 开始就创建了,绑定后就开始listen(mSock, 4)
7 [Framework 层]system/vold/netlinkmanagercpp;在NetlinkManager::start() 中会新创建一个socket; 最后设置了 mHandler = new NetlinkHandler(mSock);
8 [Framework 层]system/vold/netlinkhandlercpp;中onEvent函数会处理收到的消息;而NetlinkHandler继承于NetlinkListener又继承于SocketListener(socket, false),由false可知该socket不是监听socket;在SocketListener::startListener()中会增加 mClients->push_back(new SocketClient(mSock));
9 [Framework 层]system/vold/directvolumecpp;中handleBlockEvent-->mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeDiskInserted,msg,false);会发送VolumeDiskInserted给CommandListener[mVm],然后逐个对其mClients中的SocketClient发送消息;
10 [Framework 层]谁触发的 DirectVolume::handleBlockEvent? 由NetlinkHandler::onEvent;--> vm->handleBlockEvent(evt);-->VolumeManager::handleBlockEvent;-->mVolumes中逐个volume->handleBlockEvent;
11 [Framework 层]而VolumeManager中的mVolumes 中元素,是在maincpp中process_config函数分析手机上的文件/etc/voldfstab而得到的;
12 [Framework 层]又是谁触发NetlinkHandler::onEvent的? 又回到了NetlinkManager::start() 创建了nladdrnl_pid = getpid();的socket。也就是必须有其他socket主动连接到该 NetlinkManager的socket(NetlinkManager::mSock);然后由SocketListener::runListener()--> NetlinkListener::onDataAvailable(c)-->NetlinkHandler::onEvent;
13 [Framework 层]现在必须查明 谁给NetlinkManager::mSock请求连接并发送消息subsys=“block”应该是kernel发出的,/drivers/base/corec-->device_add-->kobject_uevent(&dev->kobj, KOBJ_ADD);
14 [Framework 层]在SocketListener::runListener()中如果是可以监听的socket[mListen=true],会增加 mClients->push_back(new SocketClient(c)); 如果需要收到VolumeDiskInserted的消息,你就首先需要申请和SocketListener[名称为vold]连接。比如某些应用需要获取该消息
1 [Kernel 层]主要分析 kernel/drivers/mmc/core/busc, mmc的driver注册到/sys/bus/mmc/devices下mmc3:aaaa, 实际对应的路径又为/sys/devices/sdi3/mmc_host/mmc3/mmc3:aaaa,这正好与前面dev_mount xxxx 中的设备点一致
2 [Kernel 层]kernel/drivers/mmc/core/busc [int mmc_add_card(struct mmc_card card)];
kernel/drivers/mmc/core/sdc [mmc_attach_sd]
kernel/drivers/mmc/core/corec [mmc_rescan_try_freq <---mmc_rescan]
kernel/drivers/mmc/core/hostc [mmc_alloc_host: INIT_DELAYED_WORK(&host->detect, mmc_rescan);]
EXPORT_SYMBOL(mmc_alloc_host) : 内核"导出"的符号表,这个表在insmod 时候会用到,可通过cat /proc/kallsyms查看
kernel/drivers/mmc/core/corec [mmc_detect_change]
kernel/drivers/mmc/core/sdc [mmc_sd_detect]
kernel/drivers/mmc/host/mmcic [mmci_cd_irq] 根据查kernel/arch/arm/configs/semc_lotus_defconfig中关于mmc 的config 和Makefile选择
kernel/drivers/mmc/host/mmcic [mmci_probe] 注册中断监听函数mmci_cd_irq,还有mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);初始化mmc_host;
kernel/drivers/mmc/core/corec [mmc_detect_change]-->mmc_schedule_delayed_work(&host->detect, delay);
host->detect = mmc_rescan;在kernel/drivers/mmc/core/hostc中mmc_alloc_host函数有INIT_DELAYED_WORK(&host->detect, mmc_rescan);其中mmc_rescan在kernel/drivers/mmc/core/corec中
似乎不太重要mmc_rescan-->host->bus_ops->detect(host)[该函数在/kernel/driver/mmc/core/mmcc中赋值为mmc_detect]-->mmc_claim_host(host);-->__mmc_claim_host-->add_wait_queue(&host->wq, &wait);
3 [Kernel 层]kernel的sd卡插入和拔出的detect event 如何传递到 user space的vold->NetlinkManager::mSock的?????
4 [Kernel 层]kernel/lib/kobject_ueventc中uevent_net_init函数会创建netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT,), 而kobject_uevent_env->netlink_broadcast_filtered-->do_one_broadcast[单一广播]会发送uevent到user space
5 [Kernel 层]/drivers/base/corec;中device_add函数有kobject_uevent(&dev->kobj, KOBJ_ADD) 该处是所找的向user space发送消息
6 [Kernel 层]kernel/lib/kobject_ueventc中kobject_uevent->kobject_uevent_env(kobj, action, NULL);
7 [Kernel 层]/drivers/mmc/core/busc有mmc_add_card->device_add函数
8 [Kernel 层]/drivers/mmc/core/sdc有mmc_attach_sd函数->mmc_add_card(host->card);
9 [Kernel 层]/drivers/mmc/core/corec有mmc_rescan_try_freq函数->mmc_attach_sd; 注意其attch顺序为sdio,sd,mmc; 而mmc_rescan会调用mmc_rescan_try_freq
以上就是关于如何获取 Android 设备的CPU核数,时钟频率以及内存大小全部的内容,包括:如何获取 Android 设备的CPU核数,时钟频率以及内存大小、如何手动mount sdcard和检测到sd card、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)