3
A
回答
8
下面的C函数返回的CPU时间和过程PID的常驻内存。要获得其他进程的资源,您需要root权限。您也可以尝试getrusage(),但我从来没有正确使用内存。通过getrusage()获取CPU时间对我来说总是有效。
该函数根据ps和top命令的源代码进行调整。它是我程序的一部分,用于监视其他进程的内存。
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/mach_port.h>
#include <mach/mach_traps.h>
#include <mach/task_info.h>
#include <mach/thread_info.h>
#include <mach/thread_act.h>
#include <mach/vm_region.h>
#include <mach/vm_map.h>
#include <mach/task.h>
#include <mach/shared_memory_server.h>
typedef struct vmtotal vmtotal_t;
typedef struct { /* dynamic process information */
size_t rss, vsize;
double utime, stime;
} RunProcDyn;
/* On Mac OS X, the only way to get enough information is to become root. Pretty frustrating!*/
int run_get_dynamic_proc_info(pid_t pid, RunProcDyn *rpd)
{
task_t task;
kern_return_t error;
mach_msg_type_number_t count;
thread_array_t thread_table;
thread_basic_info_t thi;
thread_basic_info_data_t thi_data;
unsigned table_size;
struct task_basic_info ti;
error = task_for_pid(mach_task_self(), pid, &task);
if (error != KERN_SUCCESS) {
/* fprintf(stderr, "++ Probably you have to set suid or become root.\n"); */
rpd->rss = rpd->vsize = 0;
rpd->utime = rpd->stime = 0;
return 0;
}
count = TASK_BASIC_INFO_COUNT;
error = task_info(task, TASK_BASIC_INFO, (task_info_t)&ti, &count);
assert(error == KERN_SUCCESS);
{ /* adapted from ps/tasks.c */
vm_region_basic_info_data_64_t b_info;
vm_address_t address = GLOBAL_SHARED_TEXT_SEGMENT;
vm_size_t size;
mach_port_t object_name;
count = VM_REGION_BASIC_INFO_COUNT_64;
error = vm_region_64(task, &address, &size, VM_REGION_BASIC_INFO,
(vm_region_info_t)&b_info, &count, &object_name);
if (error == KERN_SUCCESS) {
if (b_info.reserved && size == (SHARED_TEXT_REGION_SIZE) &&
ti.virtual_size > (SHARED_TEXT_REGION_SIZE + SHARED_DATA_REGION_SIZE))
{
ti.virtual_size -= (SHARED_TEXT_REGION_SIZE + SHARED_DATA_REGION_SIZE);
}
}
rpd->rss = ti.resident_size;
rpd->vsize = ti.virtual_size;
}
{ /* calculate CPU times, adapted from top/libtop.c */
unsigned i;
rpd->utime = ti.user_time.seconds + ti.user_time.microseconds * 1e-6;
rpd->stime = ti.system_time.seconds + ti.system_time.microseconds * 1e-6;
error = task_threads(task, &thread_table, &table_size);
assert(error == KERN_SUCCESS);
thi = &thi_data;
for (i = 0; i != table_size; ++i) {
count = THREAD_BASIC_INFO_COUNT;
error = thread_info(thread_table[i], THREAD_BASIC_INFO, (thread_info_t)thi, &count);
assert(error == KERN_SUCCESS);
if ((thi->flags & TH_FLAGS_IDLE) == 0) {
rpd->utime += thi->user_time.seconds + thi->user_time.microseconds * 1e-6;
rpd->stime += thi->system_time.seconds + thi->system_time.microseconds * 1e-6;
}
if (task != mach_task_self()) {
error = mach_port_deallocate(mach_task_self(), thread_table[i]);
assert(error == KERN_SUCCESS);
}
}
error = vm_deallocate(mach_task_self(), (vm_offset_t)thread_table, table_size * sizeof(thread_array_t));
assert(error == KERN_SUCCESS);
}
mach_port_deallocate(mach_task_self(), task);
return 0;
}
#endif /* __APPLE__ */
1
2
用仪器启动您的应用程序。把它通过的步伐,并评估结果......
+0
仪器肯定是一个非常有用的程序,谢谢,但我并不总是有权访问应用程序 –
0
继@ user172818的意见,我想getrusage
和它的工作对我来说:
#include <sys/time.h>
#include <sys/resource.h>
long getMemoryUsage()
{
struct rusage usage;
if(0 == getrusage(RUSAGE_SELF, &usage))
return usage.ru_maxrss; // bytes
else
return 0;
}
我使用Mac OS X 10.9.4
,与编译器Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
。
相关问题
- 1. 我怎样才能总是让我的应用程序响应
- 2. 我怎样才能在Mac上从Python应用程序
- 3. Android:我怎样才能让我的应用程序多语言?
- 4. 我怎样才能在我的应用程序散列密码
- 5. 怎样才能找到我的android 2.3应用程序正在使用多少内存的堆?
- 6. 我怎样才能在短信应用我的消息在iphone我的应用程序内发出看?
- 7. 我怎样才能使用我的流星应用程序?
- 8. 我怎样才能改变我的应用程序的语言
- 9. 我怎样才能使全部我的VB 6.0应用程序
- 10. 我怎样才能重新启动我的iPhone应用程序
- 11. 我怎样才能让在Lua的GUI应用程序
- 12. 我怎样才能在Android上调用Facebook应用程序?
- 13. 我应该在我的Java程序中使用多少内存?
- 14. 我怎样才能得到我的android应用程序的内部版本号?
- 15. 我怎样才能找出什么线程在我的Android应用程序中使用CPU
- 16. 我怎样才能从我的iPhone应用程序中的目标c
- 17. 我怎样才能打开我的Android应用程序的多个实例
- 18. 我怎样才能在Java程序
- 19. 我怎样才能实现可在我的应用程序中的longclickable
- 20. 我怎样才能找出我的kivy应用程序的高度和宽度应该是给定设备?
- 21. 我怎样才能关闭我的应用程序,当我切换到另一个应用程序
- 22. 我怎样才能整合iPhone应用程序中的代码
- 23. 我怎样才能从我的应用程序从Android远程URL打开PDF
- 24. 我怎样才能使一个应用程序的许可证在c#开发
- 25. 我怎样才能在Linux下编译C++应用程序的ipod/iphone(debian)
- 26. 我怎么看我的程序在吃多少内存?
- 27. 我怎么能看到有多少人是在应用程序,我
- 28. 使用InAppSettingsKit,我怎样才能让它不出现在设置应用程序?
- 29. 我怎样才能从DLL通信到调用应用程序在c + +
- 30. 我怎么能在WPF应用程序
这看起来非常方便,谢谢 –
非常有用的一段代码。一个注释:在发布的代码中,语句mach_port_deallocate(mach_task_self(),thread_table [i])受到条件(task!= mach_task_self())和mach_port_deallocate(mach_task_self(),任务)的保护)在所有情况下运行。我认为应该在任务== mach_task_self()调用mach_port_deallocate(mach_task_self(),task)后遇到麻烦(应该有条件地执行后面的语句)。这由文档支持,mach_task_self()返回一个缓存值。 – tcovo
这非常有用。不幸的是,虽然在当前的SDK –