Skip to content

Commit e2a40d8

Browse files
committed
vmprof_memory.c
1 parent a8674b2 commit e2a40d8

File tree

12 files changed

+137
-105
lines changed

12 files changed

+137
-105
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def run(self):
7575
'src/vmprof_unix.c',
7676
'src/vmprof_common.c',
7777
'src/vmprof_mt.c',
78+
'src/vmprof_memory.c',
7879
] + extra_source_files,
7980
depends=[
8081
'src/vmprof_unix.h',
@@ -84,6 +85,7 @@ def run(self):
8485
'src/symboltable.h',
8586
'src/machine.h',
8687
'src/vmprof.h',
88+
'src/vmprof_memory.h',
8789
],
8890
extra_compile_args=extra_compile_args,
8991
libraries=libraries)]

src/rss_darwin.h

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/rss_unix.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/vmprof.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#define _GNU_SOURCE 1
4+
35
#ifndef RPYTHON_VMPROF
46
#include <Python.h>
57
#endif

src/vmprof_common.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "vmprof_common.h"
22

3+
#include <assert.h>
4+
#include <errno.h>
5+
36
static volatile int is_enabled = 0;
47
static long prepare_interval_usec = 0;
58
static long profile_interval_usec = 0;
@@ -149,7 +152,7 @@ void *volatile _PyThreadState_Current;
149152

150153
#ifdef RPYTHON_VMPROF
151154
#ifndef RPYTHON_LL2CTYPES
152-
static PY_STACK_FRAME_T *get_vmprof_stack(void)
155+
PY_STACK_FRAME_T *get_vmprof_stack(void)
153156
{
154157
struct pypy_threadlocal_s *tl;
155158
_OP_THREADLOCALREF_ADDR_SIGHANDLER(tl);
@@ -159,7 +162,7 @@ static PY_STACK_FRAME_T *get_vmprof_stack(void)
159162
return tl->vmprof_tl_stack;
160163
}
161164
#else
162-
static PY_STACK_FRAME_T *get_vmprof_stack(void)
165+
PY_STACK_FRAME_T *get_vmprof_stack(void)
163166
{
164167
return 0;
165168
}

src/vmprof_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <pthread.h>
1616
#endif
1717

18+
#include "vmprof_getpc.h"
19+
1820
#ifdef VMPROF_LINUX
1921
#include <syscall.h>
2022
#endif

src/vmprof_getpc.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,23 @@ struct CallUnrollInfo {
131131
// typedef int ucontext_t;
132132
// #endif
133133

134-
static intptr_t GetPC(ucontext_t *signal_ucontext) {
135-
// RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
136-
fprintf(stderr, "GetPC is not yet implemented on Windows\n");
137-
return NULL;
138-
}
134+
#define GetPC(__SIGNAL_UCONTEXT) (NULL)
135+
//inline intptr_t GetPC(ucontext_t *signal_ucontext) {
136+
// // RAW_LOG(ERROR, "GetPC is not yet implemented on Windows\n");
137+
// fprintf(stderr, "GetPC is not yet implemented on Windows\n");
138+
// return NULL;
139+
//}
139140

140141
// Normal cases. If this doesn't compile, it's probably because
141142
// PC_FROM_UCONTEXT is the empty string. You need to figure out
142143
// the right value for your system, and add it to the list in
143144
// vmrpof_config.h
144145
#else
145-
static intptr_t GetPC(ucontext_t *signal_ucontext) {
146-
return signal_ucontext->PC_FROM_UCONTEXT; // defined in config.h
147-
}
146+
147+
#define GetPC(__SIGNAL_UCONTEXT) (__SIGNAL_UCONTEXT->PC_FROM_UCONTEXT)
148+
//inline intptr_t GetPC(ucontext_t *signal_ucontext) {
149+
// return signal_ucontext->PC_FROM_UCONTEXT; // defined in config.h
150+
//}
148151

149152
#endif
150153

src/vmprof_memory.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "vmprof_memory.h"
2+
3+
#ifdef VMPROF_APPLE
4+
/* On OS X we can get RSS using the Mach API. */
5+
#include <mach/mach.h>
6+
#include <mach/message.h>
7+
#include <mach/kern_return.h>
8+
#include <mach/task_info.h>
9+
10+
static mach_port_t mach_task;
11+
#else
12+
#include <stdlib.h>
13+
#include <sys/types.h>
14+
#include <sys/stat.h>
15+
#include <fcntl.h>
16+
/* On '''normal''' Unices we can get RSS from '/proc/<pid>/status'. */
17+
static int proc_file = -1;
18+
#endif
19+
20+
int setup_rss(void)
21+
{
22+
#ifdef VMPROF_LINUX
23+
char buf[128];
24+
25+
sprintf(buf, "/proc/%d/status", getpid());
26+
proc_file = open(buf, O_RDONLY);
27+
return proc_file;
28+
#elif defined(VMPROF_APPLE)
29+
mach_task = mach_task_self();
30+
return 0;
31+
#else
32+
return 0;
33+
#endif
34+
}
35+
36+
int teardown_rss(void)
37+
{
38+
#ifdef VMPROF_LINUX
39+
close(proc_file);
40+
proc_file = -1;
41+
return 0;
42+
#else
43+
return 0;
44+
#endif
45+
}
46+
47+
long get_current_proc_rss(void)
48+
{
49+
#ifdef VMPROF_LINUX
50+
char buf[1024];
51+
int i = 0;
52+
53+
if (lseek(proc_file, 0, SEEK_SET) == -1)
54+
return -1;
55+
if (read(proc_file, buf, 1024) == -1)
56+
return -1;
57+
while (i < 1020) {
58+
if (strncmp(buf + i, "VmRSS:\t", 7) == 0) {
59+
i += 7;
60+
return atoi(buf + i);
61+
}
62+
i++;
63+
}
64+
return -1;
65+
#elif defined(VMPROF_APPLE)
66+
mach_msg_type_number_t out_count = MACH_TASK_BASIC_INFO_COUNT;
67+
mach_task_basic_info_data_t taskinfo = { .resident_size = 0 };
68+
69+
kern_return_t error = task_info(mach_task, MACH_TASK_BASIC_INFO, (task_info_t)&taskinfo, &out_count);
70+
if (error == KERN_SUCCESS) {
71+
return (long)(taskinfo.resident_size / 1024);
72+
} else {
73+
return -1;
74+
}
75+
#else
76+
return -1; // not implemented
77+
#endif
78+
}

src/vmprof_memory.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
int setup_rss(void);
4+
int teardown_rss(void);
5+
long get_current_proc_rss(void);

src/vmprof_mt.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "vmprof_mt.h"
22
/* Support for multithreaded write() operations (implementation) */
33

4+
#include <assert.h>
5+
46
#if defined(__i386__) || defined(__amd64__)
57
static inline void write_fence(void) { asm("" : : : "memory"); }
68
#else

0 commit comments

Comments
 (0)