Skip to content

Instantly share code, notes, and snippets.

@ykst
Created November 8, 2013 14:56
Show Gist options
  • Save ykst/7372143 to your computer and use it in GitHub Desktop.
Save ykst/7372143 to your computer and use it in GitHub Desktop.
iOSで動作中のプロセスのメモリ使用量を調べる ref: http://qiita.com/ykst@github/items/a896b70815b6e76f7af0
#include <sys/proc_info.h>
#include <sys/sysctl.h>
#include <libproc.h>
void dump_process_memory_usage()
{
size_t size = 0;
// 全てのプロセスのPIDを取得
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
sysctl(mib, 4, NULL, &size, NULL, 0);
struct kinfo_proc *procs = malloc(size);
sysctl(mib, 4, procs, &size, NULL, 0);
int total = size / sizeof(*procs);
for(int i = 0; i < total; ++i) {
const pid_t pid = procs[i].kp_proc.p_pid;
struct proc_taskallinfo tai;
if(pid > 0) {
proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &tai, sizeof(tai));
char buf[1024];
bzero(buf, 1024);
proc_pidpath(pid, buf, sizeof(buf));
// ついでにsysctlでは取れないフルのプロセス名を取得
NSString *full_process_name = [[[NSString stringWithCString:buf encoding:NSUTF8StringEncoding] lastPathComponent] stringByDeletingPathExtension];
NSLog(@"%@ uses %.2fMB(PID:%d)", full_process_name, tai.ptinfo.pti_resident_size / 1000000.0f);
}
}
}
#ifndef _LIBPROC_WRAPPER_H_
#define _LIBPROC_WRAPPER_H_
#if TARGET_IPHONE_SIMULATOR
#include <sys/proc_info.h>
#include <libproc.h>
#else
extern int proc_listpidspath(uint32_t type,
uint32_t typeinfo,
const char *path,
uint32_t pathflags,
void *buffer,
int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_listallpids(void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
extern int proc_listpgrppids(pid_t pgrpid, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
extern int proc_listchildpids(pid_t ppid, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
extern int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_pidfileportinfo(int pid, uint32_t fileport, int flavor, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
extern int proc_name(int pid, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_kmsgbuf(void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_pidpath(int pid, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
extern int proc_libversion(int *major, int * minor) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
#define PROC_SETPC_NONE 0
#define PROC_SETPC_THROTTLEMEM 1
#define PROC_SETPC_SUSPEND 2
#define PROC_SETPC_TERMINATE 3
#define MAXCOMLEN 16
#define PROC_PIDTASKALLINFO 2
#define PROC_PIDTASKALLINFO_SIZE (sizeof(struct proc_taskallinfo))
extern int proc_setpcontrol(const int control) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
extern int proc_setpcontrol(const int control);
extern int proc_track_dirty(pid_t pid, uint32_t flags);
extern int proc_set_dirty(pid_t pid, bool dirty);
extern int proc_get_dirty(pid_t pid, uint32_t *flags);
extern int proc_terminate(pid_t pid, int *sig);
struct proc_bsdinfo {
uint32_t pbi_flags; /* 64bit; emulated etc */
uint32_t pbi_status;
uint32_t pbi_xstatus;
uint32_t pbi_pid;
uint32_t pbi_ppid;
uid_t pbi_uid;
gid_t pbi_gid;
uid_t pbi_ruid;
gid_t pbi_rgid;
uid_t pbi_svuid;
gid_t pbi_svgid;
uint32_t rfu_1; /* reserved */
char pbi_comm[MAXCOMLEN];
char pbi_name[2*MAXCOMLEN]; /* empty if no name is registered */
uint32_t pbi_nfiles;
uint32_t pbi_pgid;
uint32_t pbi_pjobc;
uint32_t e_tdev; /* controlling tty dev */
uint32_t e_tpgid; /* tty process group id */
int32_t pbi_nice;
uint64_t pbi_start_tvsec;
uint64_t pbi_start_tvusec;
};
struct proc_taskinfo {
uint64_t pti_virtual_size; /* virtual memory size (bytes) */
uint64_t pti_resident_size; /* resident memory size (bytes) */
uint64_t pti_total_user; /* total time */
uint64_t pti_total_system;
uint64_t pti_threads_user; /* existing threads only */
uint64_t pti_threads_system;
int32_t pti_policy; /* default policy for new threads */
int32_t pti_faults; /* number of page faults */
int32_t pti_pageins; /* number of actual pageins */
int32_t pti_cow_faults; /* number of copy-on-write faults */
int32_t pti_messages_sent; /* number of messages sent */
int32_t pti_messages_received; /* number of messages received */
int32_t pti_syscalls_mach; /* number of mach system calls */
int32_t pti_syscalls_unix; /* number of unix system calls */
int32_t pti_csw; /* number of context switches */
int32_t pti_threadnum; /* number of threads in the task */
int32_t pti_numrunning; /* number of running threads */
int32_t pti_priority; /* task priority*/
};
struct proc_taskallinfo {
struct proc_bsdinfo pbsd;
struct proc_taskinfo ptinfo;
};
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment