forked from nodejs/node-v0.x-archive
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add platform files, move getmem to node::OS class.
- Loading branch information
Showing
7 changed files
with
244 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef NODE_PLATFORM_H_ | ||
#define NODE_PLATFORM_H_ | ||
|
||
namespace node { | ||
|
||
class OS { | ||
public: | ||
static int GetMemory(size_t *rss, size_t *vsize); | ||
}; | ||
|
||
|
||
} // namespace node | ||
#endif // NODE_PLATFORM_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "node.h" | ||
#include "platform.h" | ||
|
||
#include <mach/task.h> | ||
#include <mach/mach_init.h> | ||
|
||
namespace node { | ||
|
||
// Researched by Tim Becker and Michael Knight | ||
// http://blog.kuriositaet.de/?p=257 | ||
int OS::GetMemory(size_t *rss, size_t *vsize) { | ||
struct task_basic_info t_info; | ||
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; | ||
|
||
int r = task_info(mach_task_self(), | ||
TASK_BASIC_INFO, | ||
(task_info_t)&t_info, | ||
&t_info_count); | ||
|
||
if (r != KERN_SUCCESS) return -1; | ||
|
||
*rss = t_info.resident_size; | ||
*vsize = t_info.virtual_size; | ||
|
||
return 0; | ||
} | ||
|
||
|
||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "node.h" | ||
#include "platform.h" | ||
|
||
#include <kvm.h> | ||
#include <sys/param.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/user.h> | ||
#include <paths.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
|
||
namespace node { | ||
|
||
|
||
int OS::GetMemory(size_t *rss, size_t *vsize) { | ||
kvm_t *kd = NULL; | ||
struct kinfo_proc *kinfo = NULL; | ||
pid_t pid; | ||
int nprocs; | ||
size_t page_size = getpagesize(); | ||
|
||
pid = getpid(); | ||
|
||
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open"); | ||
if (kd == NULL) goto error; | ||
|
||
kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs); | ||
if (kinfo == NULL) goto error; | ||
|
||
*rss = kinfo->ki_rssize * page_size; | ||
*vsize = kinfo->ki_size; | ||
|
||
kvm_close(kd); | ||
|
||
return 0; | ||
|
||
error: | ||
if (kd) kvm_close(kd); | ||
return -1; | ||
} | ||
|
||
|
||
|
||
} // namespace node |
Oops, something went wrong.