Android:找出线程正在运行的核心 [英] Android: Find out which core the thread is running on

查看:775
本文介绍了Android:找出线程正在运行的核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Android编写代码,它会给我一些处理器和运行线程的核心的信息(id?)。

I'm trying to write code for Android which would give me some kind of information (id?) of the processor and the core on which a thread is running on.

我有google'd和grep'ed源的一些灵感,但没有运气。我所知道的是,很可能我需要一些C / C ++调用。

I've google'd and grep'ed the sources for some inspiration but with no luck. All I know is, that most likely I will need some C/C++ calls.

我工作的是以下:

#include <jni.h>

int getCpuId() {
    // missing code
    return 0;
}

int getCoreId() {
    // missing code    
    return 0;
} 

JNIEXPORT int JNICALL Java_com_spendoptima_Utils_getCpuId(JNIEnv * env,
        jobject obj) {
    return getCpuId();
}

JNIEXPORT int JNICALL Java_com_spendoptima_Utils_getCoreId(JNIEnv * env,
        jobject obj) {
    return getCoreId();
}

整个项目编译和运行很好。我可以从Java中调用函数,并得到正确的响应。

The whole project compiles and runs just fine. I'm able to call the functions from within Java and I get the proper responses.

在这里是否可以填充空格的任何人?

Is here anybody who could fill in the blanks?

推荐答案

/bionic/+/kitkat-release/libc/bionic/sched_getcpu.crel =nofollow> sched_getcpu()和 __ getcpu())。坏消息是,它们不是NDK的一部分。

The good news is, the necessary library and system call are defined on Android (sched_getcpu() and __getcpu()). The bad news is, they aren't part of the NDK.

您可以使用这个答案

另一种方法是读取 / proc / self / stat 并解析出处理器条目。 proc(5)手册页说明了格式:

Another approach is to read /proc/self/stat and parse out the processor entry. The proc(5) man page describes the format:


  /proc/[pid]/stat
         Status  information  about  the process.  This is used by ps(1).
         It is defined in /usr/src/linux/fs/proc/array.c.
  ...
         processor %d (since Linux 2.2.8)
                     CPU number last executed on.


这样慢得多,文件如果内核被更新,那么它不是推荐的方法。

This is much slower, and the "file" format may change if the kernel is updated, so it's not the recommended approach.

这篇关于Android:找出线程正在运行的核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆